From 66c37373bd28afd6ce313fd45908de0602e3c67d Mon Sep 17 00:00:00 2001 From: Alexander Date: Sat, 11 Jul 2020 23:06:30 +0300 Subject: [PATCH] Wrapped some crashes which happened on start camera and which connect with params. (#897) Co-authored-by: Mattia Iavarone --- .../cameraview/engine/Camera1Engine.java | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/engine/Camera1Engine.java b/cameraview/src/main/java/com/otaliastudios/cameraview/engine/Camera1Engine.java index 7cb41d1a..b5161c78 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/engine/Camera1Engine.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/engine/Camera1Engine.java @@ -165,6 +165,10 @@ public class Camera1Engine extends CameraBaseEngine implements LOG.e("onStartEngine:", "Failed to connect. Maybe in use by another app?"); throw new CameraException(e, CameraException.REASON_FAILED_TO_CONNECT); } + if (mCamera == null) { + LOG.e("onStartEngine:", "Failed to connect. Camera is null, maybe in use by another app or already released?"); + throw new CameraException(CameraException.REASON_FAILED_TO_CONNECT); + } mCamera.setErrorCallback(this); // Set parameters that might have been set before the camera was opened. @@ -179,8 +183,13 @@ public class Camera1Engine extends CameraBaseEngine implements LOG.e("onStartEngine:", "Failed to connect. Problem with camera params"); throw new CameraException(e, CameraException.REASON_FAILED_TO_CONNECT); } - mCamera.setDisplayOrientation(getAngles().offset(Reference.SENSOR, Reference.VIEW, - Axis.ABSOLUTE)); // <- not allowed during preview + try { + mCamera.setDisplayOrientation(getAngles().offset(Reference.SENSOR, Reference.VIEW, + Axis.ABSOLUTE)); // <- not allowed during preview + } catch (Exception e) { + LOG.e("onStartEngine:", "Failed to connect. Can't set display orientation, maybe preview already exists?"); + throw new CameraException(CameraException.REASON_FAILED_TO_CONNECT); + } LOG.i("onStartEngine:", "Ended"); return Tasks.forResult(mCameraOptions); } @@ -222,7 +231,13 @@ public class Camera1Engine extends CameraBaseEngine implements mPreview.setStreamSize(previewSize.getWidth(), previewSize.getHeight()); mPreview.setDrawRotation(0); - Camera.Parameters params = mCamera.getParameters(); + Camera.Parameters params; + try { + params = mCamera.getParameters(); + } catch (Exception e) { + LOG.e("onStartPreview:", "Failed to get params from camera. Maybe low level problem with camera or camera has already released?"); + throw new CameraException(e, CameraException.REASON_FAILED_TO_START_PREVIEW); + } // NV21 should be the default, but let's make sure, since YuvImage will only support this // and a few others params.setPreviewFormat(ImageFormat.NV21); @@ -240,7 +255,12 @@ public class Camera1Engine extends CameraBaseEngine implements Size pictureSize = computeCaptureSize(Mode.PICTURE); params.setPictureSize(pictureSize.getWidth(), pictureSize.getHeight()); } - mCamera.setParameters(params); + try { + mCamera.setParameters(params); + } catch (Exception e) { + LOG.e("onStartPreview:", "Failed to set params for camera. Maybe incorrect parameter put in params?"); + throw new CameraException(e, CameraException.REASON_FAILED_TO_START_PREVIEW); + } mCamera.setPreviewCallbackWithBuffer(null); // Release anything left mCamera.setPreviewCallbackWithBuffer(this); // Add ourselves