Skip to content

Commit

Permalink
Cancel scan request when rear-facing camera is not present or when ca…
Browse files Browse the repository at this point in the history
…mera instance is null. dm77#7
  • Loading branch information
Dushyanth Maguluru committed Mar 11, 2013
1 parent d8eb87b commit f159520
Showing 1 changed file with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Handler;
Expand Down Expand Up @@ -31,6 +32,12 @@ public class ZBarScannerActivity extends Activity implements Camera.PreviewCallb
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if(!isCameraAvailable()) {
// Cancel request if there is no rear-facing camera.
cancelRequest();
return;
}

// Hide the window title.
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
Expand Down Expand Up @@ -66,6 +73,11 @@ protected void onResume() {

// Open the default i.e. the first rear facing camera.
mCamera = Camera.open();
if(mCamera == null) {
// Cancel request if mCamera is null.
cancelRequest();
return;
}
mPreviewing = true;
mPreview.setCamera(mCamera);
}
Expand All @@ -87,6 +99,16 @@ protected void onPause() {
}
}

public boolean isCameraAvailable() {
PackageManager pm = getPackageManager();
return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);
}

public void cancelRequest() {
setResult(Activity.RESULT_CANCELED);
finish();
}

public void onPreviewFrame(byte[] data, Camera camera) {
Camera.Parameters parameters = camera.getParameters();
Camera.Size size = parameters.getPreviewSize();
Expand Down

0 comments on commit f159520

Please sign in to comment.