Skip to content

Commit

Permalink
fix(android): android crash fixes. Make sure no unsupported aspect ra…
Browse files Browse the repository at this point in the history
…tio is used, and do not crash when there are no cameras available. (react-native-camera#2662)
  • Loading branch information
cristianoccazinsp authored and sibelius committed Dec 31, 2019
1 parent d84127f commit db7b9e4
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion android/src/main/java/com/google/android/cameraview/Camera1.java
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ boolean setAspectRatio(final AspectRatio ratio) {
final Set<Size> sizes = mPreviewSizes.sizes(ratio);
if (sizes == null) {
// do nothing, ratio remains unchanged. Consistent with Camera2 and initial mount behaviour
Log.w("CAMERA_1::", "setAspectRatio received an unsupported value and will be ignored.");
} else {
mAspectRatio = ratio;
mBgHandler.post(new Runnable() {
Expand Down Expand Up @@ -870,7 +871,10 @@ private void chooseCamera() {
if(_mCameraId == null){
int count = Camera.getNumberOfCameras();
if(count == 0){
throw new RuntimeException("No camera available.");
//throw new RuntimeException("No camera available.");
mCameraId = INVALID_CAMERA_ID;
Log.w("CAMERA_1::", "getNumberOfCameras returned 0. No camera available.");
return;
}

for (int i = 0; i < count; i++) {
Expand Down Expand Up @@ -899,19 +903,38 @@ private boolean openCamera() {
if (mCamera != null) {
releaseCamera();
}

// in case we got an invalid camera ID
// due to no cameras or invalid ID provided,
// return false so we can raise a mount error
if(mCameraId == INVALID_CAMERA_ID){
return false;
}

try {
mCamera = Camera.open(mCameraId);
mCameraParameters = mCamera.getParameters();

// Supported preview sizes
mPreviewSizes.clear();
for (Camera.Size size : mCameraParameters.getSupportedPreviewSizes()) {
mPreviewSizes.add(new Size(size.width, size.height));
}

// Supported picture sizes;
mPictureSizes.clear();
for (Camera.Size size : mCameraParameters.getSupportedPictureSizes()) {
mPictureSizes.add(new Size(size.width, size.height));
}

// to be consistent with Camera2, and to prevent crashes on some devices
// do not allow preview sizes that are not also in the picture sizes set
for (AspectRatio aspectRatio : mPreviewSizes.ratios()) {
if (mPictureSizes.sizes(aspectRatio) == null) {
mPreviewSizes.remove(aspectRatio);
}
}

// AspectRatio
if (mAspectRatio == null) {
mAspectRatio = Constants.DEFAULT_ASPECT_RATIO;
Expand Down Expand Up @@ -939,6 +962,7 @@ private AspectRatio chooseAspectRatio() {
void adjustCameraParameters() {
SortedSet<Size> sizes = mPreviewSizes.sizes(mAspectRatio);
if (sizes == null) { // Not supported
Log.w("CAMERA_1::", "adjustCameraParameters received an unsupported aspect ratio value and will be ignored.");
mAspectRatio = chooseAspectRatio();
sizes = mPreviewSizes.sizes(mAspectRatio);
}
Expand Down

0 comments on commit db7b9e4

Please sign in to comment.