diff --git a/library/src/main/api14/com/google/android/cameraview/Camera1.java b/library/src/main/api14/com/google/android/cameraview/Camera1.java index 256326e9..8c571fe7 100644 --- a/library/src/main/api14/com/google/android/cameraview/Camera1.java +++ b/library/src/main/api14/com/google/android/cameraview/Camera1.java @@ -358,9 +358,13 @@ void adjustCameraParameters() { } Size size = chooseOptimalSize(sizes); - // Always re-apply camera parameters - // Largest picture size in this ratio - final Size pictureSize = mPictureSizes.sizes(mAspectRatio).last(); + final Size pictureSize; + if (mPictureSizes.sizes(mAspectRatio) == null) { + pictureSize = size; + } else { + // Largest picture size in this ratio + pictureSize = mPictureSizes.sizes(mAspectRatio).last(); + } if (mShowingPreview) { mCamera.stopPreview(); } diff --git a/library/src/main/api21/com/google/android/cameraview/Camera2.java b/library/src/main/api21/com/google/android/cameraview/Camera2.java index 554a369b..58667ae2 100644 --- a/library/src/main/api21/com/google/android/cameraview/Camera2.java +++ b/library/src/main/api21/com/google/android/cameraview/Camera2.java @@ -213,9 +213,14 @@ boolean start() { if (!chooseCameraIdByFacing()) { return false; } - collectCameraInfo(); - prepareImageReader(); - startOpeningCamera(); + try { + collectCameraInfo(); + prepareImageReader(); + startOpeningCamera(); + } catch(Exception e) { + Log.e(TAG, "Failed to start camera2", e); + mCallback.onCameraError(e); + } return true; } @@ -345,7 +350,10 @@ void takePicture() { try { lockFocus(); } catch (Exception e) { - Log.e(TAG, "Failed to lockFocus(). captureStillPicture instead and turn off mAutoFocus", e); + Log.e(TAG, + "Failed to lockFocus(). captureStillPicture instead and turn off " + + "mAutoFocus", + e); captureStillPicture(); mAutoFocus = false; } @@ -460,12 +468,36 @@ private void prepareImageReader() { if (mImageReader != null) { mImageReader.close(); } - Size largest = mPictureSizes.sizes(mAspectRatio).last(); - mImageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(), + final Size pictureSize; + SortedSet sizes = mPictureSizes.sizes(mAspectRatio); + if (sizes == null) { // Not supported + mAspectRatio = chooseAspectRatio(); + if (mPictureSizes.sizes(mAspectRatio) == null) { + pictureSize = chooseOptimalSize(); + } else { + // Largest picture size in this ratio + pictureSize = mPictureSizes.sizes(mAspectRatio).last(); + } + } else { + pictureSize = sizes.last(); + } + + mImageReader = ImageReader.newInstance(pictureSize.getWidth(), pictureSize.getHeight(), ImageFormat.JPEG, /* maxImages */ 2); mImageReader.setOnImageAvailableListener(mOnImageAvailableListener, null); } + private AspectRatio chooseAspectRatio() { + AspectRatio r = null; + for (AspectRatio ratio : mPreviewSizes.ratios()) { + r = ratio; + if (ratio.equals(Constants.DEFAULT_ASPECT_RATIO)) { + return ratio; + } + } + return r; + } + /** *

Starts opening a camera device.

*

The result will be processed in {@link #mCameraDeviceCallback}.

diff --git a/library/src/main/java/com/google/android/cameraview/CameraView.java b/library/src/main/java/com/google/android/cameraview/CameraView.java index d3e31d2a..ed0fd666 100644 --- a/library/src/main/java/com/google/android/cameraview/CameraView.java +++ b/library/src/main/java/com/google/android/cameraview/CameraView.java @@ -357,7 +357,11 @@ public boolean getAdjustViewBounds() { * {@link #FACING_FRONT}. */ public void setFacing(@Facing int facing) { - mImpl.setFacing(facing); + try { + mImpl.setFacing(facing); + } catch (Exception e) { + mCallbacks.onCameraError(e); + } } /** @@ -426,7 +430,11 @@ public boolean getAutoFocus() { * @param flash The desired flash mode. */ public void setFlash(@Flash int flash) { - mImpl.setFlash(flash); + try { + mImpl.setFlash(flash); + } catch (Exception e) { + mCallbacks.onCameraError(e); + } } /** @@ -505,7 +513,8 @@ protected void createWindowFrame() { R.drawable.frame_face_mask); int height = face.getHeight(); int width = face.getWidth(); - canvas.drawText(title, centerX, centerY - (height / 2) - dpToPx(TITLE_MARGIN_BOTTOM), textPaint); + canvas.drawText(title, centerX, + centerY - (height / 2) - dpToPx(TITLE_MARGIN_BOTTOM), textPaint); canvas.drawBitmap(faceMask, centerX - (width / 2), centerY - (height / 2), paint); canvas.drawBitmap(face, centerX - (width / 2), centerY - (height / 2), null); break; @@ -517,7 +526,8 @@ protected void createWindowFrame() { R.drawable.frame_face_id_mask); int height = face.getHeight(); int width = face.getWidth(); - canvas.drawText(title, centerX, centerY - (height / 2) - dpToPx(TITLE_MARGIN_BOTTOM), textPaint); + canvas.drawText(title, centerX, + centerY - (height / 2) - dpToPx(TITLE_MARGIN_BOTTOM), textPaint); canvas.drawBitmap(faceMask, centerX - (width / 2), centerY - (height / 2), paint); canvas.drawBitmap(face, centerX - (width / 2), centerY - (height / 2), null); break; @@ -533,7 +543,8 @@ protected void createWindowFrame() { break; } case PASSPORT: { - Bitmap picture = BitmapFactory.decodeResource(getResources(), R.drawable.frame_passport); + Bitmap picture = BitmapFactory.decodeResource(getResources(), + R.drawable.frame_passport); drawRoundedRectBitmap(picture, centerX, centerY, canvas, radius, paint, textPaint); break; } @@ -549,7 +560,8 @@ private void drawRoundedRectBitmap(Bitmap picture, int centerX, int centerY, Can int radius, Paint paint, Paint textPaint) { int height = picture.getHeight(); int width = picture.getWidth(); - canvas.drawText(title, centerX, centerY - (height / 2) - dpToPx(TITLE_MARGIN_BOTTOM), textPaint); + canvas.drawText(title, centerX, centerY - (height / 2) - dpToPx(TITLE_MARGIN_BOTTOM), + textPaint); // draw masking canvas.drawPath( roundedRect(centerX - (width / 2), centerY - (height / 2),