From 54591b3b7e65c731f8dfec22efa2ac7e297eb168 Mon Sep 17 00:00:00 2001 From: Ferdian Sulaiman Date: Wed, 18 Jul 2018 11:20:41 +0700 Subject: [PATCH] add camera error callback on camera.open --- .../google/android/cameraview/Camera1.java | 51 ++++++++++--------- .../android/cameraview/CameraViewImpl.java | 2 + .../google/android/cameraview/CameraView.java | 15 ++++++ 3 files changed, 45 insertions(+), 23 deletions(-) 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 02493604..fbea700d 100644 --- a/library/src/main/api14/com/google/android/cameraview/Camera1.java +++ b/library/src/main/api14/com/google/android/cameraview/Camera1.java @@ -305,30 +305,35 @@ private void openCamera() { handler.postDelayed(new Runnable() { @Override public void run() { - 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)); - } - // AspectRatio - if (mAspectRatio == null) { - mAspectRatio = Constants.DEFAULT_ASPECT_RATIO; - } - adjustCameraParameters(); - mCamera.setDisplayOrientation(calcDisplayOrientation(mDisplayOrientation)); - mCallback.onCameraOpened(); - if (mPreview.isReady()) { - setUpPreview(); + 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)); + } + // AspectRatio + if (mAspectRatio == null) { + mAspectRatio = Constants.DEFAULT_ASPECT_RATIO; + } + adjustCameraParameters(); + mCamera.setDisplayOrientation(calcDisplayOrientation(mDisplayOrientation)); + mCallback.onCameraOpened(); + if (mPreview.isReady()) { + setUpPreview(); + } + mShowingPreview = true; + mCamera.startPreview(); + } catch (Exception e) { + Log.e("Camera1", "on open camera error", e); + mCallback.onCameraError(e); } - mShowingPreview = true; - mCamera.startPreview(); } }, delay); diff --git a/library/src/main/base/com/google/android/cameraview/CameraViewImpl.java b/library/src/main/base/com/google/android/cameraview/CameraViewImpl.java index 31dab0c4..7151e7ba 100644 --- a/library/src/main/base/com/google/android/cameraview/CameraViewImpl.java +++ b/library/src/main/base/com/google/android/cameraview/CameraViewImpl.java @@ -77,6 +77,8 @@ interface Callback { void onPictureTaken(byte[] data); + void onCameraError(Exception e); + } } 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 43e3ca67..d3e31d2a 100644 --- a/library/src/main/java/com/google/android/cameraview/CameraView.java +++ b/library/src/main/java/com/google/android/cameraview/CameraView.java @@ -654,6 +654,13 @@ public void onPictureTaken(byte[] data) { } } + @Override + public void onCameraError(Exception e) { + for (Callback callback : mCallbacks) { + callback.onCameraError(CameraView.this, e); + } + } + public void reserveRequestLayoutOnOpen() { mRequestLayoutOnOpen = true; } @@ -732,6 +739,14 @@ public void onCameraOpened(CameraView cameraView) { public void onCameraClosed(CameraView cameraView) { } + /** + * Called when camera failed to open. + * + * @param cameraView The associated {@link CameraView}. + */ + public void onCameraError(CameraView cameraView, Exception e) { + } + /** * Called when a picture is taken. *