Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for camera availability before switching #203

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,33 +111,33 @@ public void onCreate(Bundle icicle) {

String buttonText = "";
try {
buttonText = (String) getIntent().getStringExtra("cancelButtonText");
} catch (Exception e) {
buttonText = "Cancel";
Log.e("BCActivity:onCreate()", "onCreate: " + e.getLocalizedMessage());
}
buttonText = (String) getIntent().getStringExtra("cancelButtonText");
} catch (Exception e) {
buttonText = "Cancel";
Log.e("BCActivity:onCreate()", "onCreate: " + e.getLocalizedMessage());
}

Button btnBarcodeCaptureCancel = findViewById(R.id.btnBarcodeCaptureCancel);
btnBarcodeCaptureCancel.setText(buttonText);
btnBarcodeCaptureCancel.setOnClickListener(this);
Button btnBarcodeCaptureCancel = findViewById(R.id.btnBarcodeCaptureCancel);
btnBarcodeCaptureCancel.setText(buttonText);
btnBarcodeCaptureCancel.setOnClickListener(this);

imgViewBarcodeCaptureUseFlash = findViewById(R.id.imgViewBarcodeCaptureUseFlash);
imgViewBarcodeCaptureUseFlash.setOnClickListener(this);
imgViewBarcodeCaptureUseFlash.setVisibility(FlutterBarcodeScannerPlugin.isShowFlashIcon ? View.VISIBLE : View.GONE);
imgViewBarcodeCaptureUseFlash = findViewById(R.id.imgViewBarcodeCaptureUseFlash);
imgViewBarcodeCaptureUseFlash.setOnClickListener(this);
imgViewBarcodeCaptureUseFlash.setVisibility(FlutterBarcodeScannerPlugin.isShowFlashIcon ? View.VISIBLE : View.GONE);

imgViewSwitchCamera = findViewById(R.id.imgViewSwitchCamera);
imgViewSwitchCamera.setOnClickListener(this);
imgViewSwitchCamera = findViewById(R.id.imgViewSwitchCamera);
imgViewSwitchCamera.setOnClickListener(this);

mPreview = findViewById(R.id.preview);
mGraphicOverlay = findViewById(R.id.graphicOverlay);
mPreview = findViewById(R.id.preview);
mGraphicOverlay = findViewById(R.id.graphicOverlay);

// read parameters from the intent used to launch the activity.
boolean autoFocus = true;
boolean useFlash = false;
// read parameters from the intent used to launch the activity.
boolean autoFocus = true;
boolean useFlash = false;

// Check for the camera permission before accessing the camera. If the
// permission is not granted yet, request permission.
int rc = ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
// Check for the camera permission before accessing the camera. If the
// permission is not granted yet, request permission.
int rc = ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
if (rc == PackageManager.PERMISSION_GRANTED) {
createCameraSource(autoFocus, useFlash, CameraSource.CAMERA_FACING_BACK);
} else {
Expand Down Expand Up @@ -419,13 +419,30 @@ public void onClick(View v) {
finish();
} else if (i == R.id.imgViewSwitchCamera) {
int currentFacing = mCameraSource.getCameraFacing();
boolean autoFocus = mCameraSource.getFocusMode() != null;
boolean useFlash = flashStatus == USE_FLASH.ON.ordinal();
createCameraSource(autoFocus, useFlash, getInverseCameraFacing(currentFacing));
startCameraSource();
int inverseFacing = getInverseCameraFacing(currentFacing);
if (hasCamera(inverseFacing)) {
boolean autoFocus = mCameraSource.getFocusMode() != null;
boolean useFlash = flashStatus == USE_FLASH.ON.ordinal();
createCameraSource(autoFocus, useFlash, inverseFacing);
startCameraSource();
}
}
}

private boolean hasCamera(int cameraFacing) {
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
int numberOfCameras = Camera.getNumberOfCameras();

for (int i = 0; i < numberOfCameras; i++) {
Camera.getCameraInfo(i, cameraInfo);
if (cameraInfo.facing == cameraFacing) {
return true;
}
}

return false;
}

private int getInverseCameraFacing(int cameraFacing) {
if (cameraFacing == CameraSource.CAMERA_FACING_FRONT) {
return CameraSource.CAMERA_FACING_BACK;
Expand Down
13 changes: 0 additions & 13 deletions example/ios/Flutter/flutter_export_environment.sh

This file was deleted.