diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraListener.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraListener.java index 34161eea..2fd07798 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraListener.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraListener.java @@ -169,5 +169,21 @@ public void onVideoRecordingEnd() { public void onPictureShutter() { } + + /** + * Notifies that a finger gesture just triggered a swipe left event. + * This can be used to exchange between different filters or to + * swap between front and back cameras. + */ + @UiThread + public void onSwipeLeft() {} + + /** + * Notifies that a finger gesture just triggered a swipe right event. + * This can be used to exchange between different filters or to + * swap between front and back cameras. + */ + @UiThread + public void onSwipeRight() {} } diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java index f5140eb2..a99702ce 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java @@ -154,6 +154,9 @@ public class CameraView extends FrameLayout implements LifecycleObserver { @VisibleForTesting TapGestureFinder mTapGestureFinder; @VisibleForTesting ScrollGestureFinder mScrollGestureFinder; + private float x1,x2; + static final int MIN_SWIPE_DISTANCE = 150; + // Views @VisibleForTesting GridLinesLayout mGridLinesLayout; @VisibleForTesting MarkerLayout mMarkerLayout; @@ -666,6 +669,32 @@ public boolean onTouchEvent(MotionEvent event) { onGesture(mTapGestureFinder, options); } + if(event.getAction() == MotionEvent.ACTION_DOWN) x1 = event.getX(); + if(event.getAction() == MotionEvent.ACTION_UP) { + x2 = event.getX(); + float deltaX = x2 - x1; + if(deltaX > MIN_SWIPE_DISTANCE) { + mUiHandler.post(new Runnable() { + @Override + public void run() { + for (CameraListener listener : mListeners) { + listener.onSwipeRight(); + } + } + }); + } + if (deltaX < MIN_SWIPE_DISTANCE) { + mUiHandler.post(new Runnable() { + @Override + public void run() { + for (CameraListener listener : mListeners) { + listener.onSwipeLeft(); + } + } + }); + } + } + return true; } 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 c694b0fc..7d153b62 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/engine/Camera1Engine.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/engine/Camera1Engine.java @@ -141,7 +141,11 @@ protected boolean collectCameraInfo(@NonNull Facing facing) { "Cameras:", Camera.getNumberOfCameras()); Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); for (int i = 0, count = Camera.getNumberOfCameras(); i < count; i++) { - Camera.getCameraInfo(i, cameraInfo); + try { + Camera.getCameraInfo(i, cameraInfo); + } catch (RuntimeException e) { + continue; + } if (cameraInfo.facing == internalFacing) { getAngles().setSensorOffset(facing, cameraInfo.orientation); mCameraId = i; @@ -524,8 +528,14 @@ public void setFlash(@NonNull Flash flash) { new Runnable() { @Override public void run() { - Camera.Parameters params = mCamera.getParameters(); - if (applyFlash(params, old)) mCamera.setParameters(params); + try { + Camera.Parameters params = mCamera.getParameters(); + if (applyFlash(params, old)) mCamera.setParameters(params); + } catch (RuntimeException e) { + LOG.e("onSetFlash:", "Failed to get params from camera. Maybe low level problem with camera or camera has already released?"); + //Should throw exception? Looks inappropriate at this point if camera is working... + //throw new CameraException(e, CameraException.REASON_FAILED_TO_START_PREVIEW); + } } }); } @@ -548,8 +558,14 @@ public void setLocation(@Nullable Location location) { new Runnable() { @Override public void run() { - Camera.Parameters params = mCamera.getParameters(); - if (applyLocation(params, oldLocation)) mCamera.setParameters(params); + try { + Camera.Parameters params = mCamera.getParameters(); + if (applyLocation(params, oldLocation)) mCamera.setParameters(params); + } catch (RuntimeException e) { + LOG.e("onSetLocation:", "Failed to get params from camera. Maybe low level problem with camera or camera has already released?"); + //Should throw exception? Looks inappropriate at this point if camera is working... + //throw new CameraException(e, CameraException.REASON_FAILED_TO_START_PREVIEW); + } } }); } @@ -576,8 +592,14 @@ public void setWhiteBalance(@NonNull WhiteBalance whiteBalance) { new Runnable() { @Override public void run() { - Camera.Parameters params = mCamera.getParameters(); - if (applyWhiteBalance(params, old)) mCamera.setParameters(params); + try { + Camera.Parameters params = mCamera.getParameters(); + if (applyWhiteBalance(params, old)) mCamera.setParameters(params); + } catch (RuntimeException e) { + LOG.e("onSetWhiteBalance:", "Failed to get params from camera. Maybe low level problem with camera or camera has already released?"); + //Should throw exception? Looks inappropriate at this point if camera is working... + //throw new CameraException(e, CameraException.REASON_FAILED_TO_START_PREVIEW); + } } }); } @@ -605,8 +627,14 @@ public void setHdr(@NonNull Hdr hdr) { new Runnable() { @Override public void run() { - Camera.Parameters params = mCamera.getParameters(); - if (applyHdr(params, old)) mCamera.setParameters(params); + try { + Camera.Parameters params = mCamera.getParameters(); + if (applyHdr(params, old)) mCamera.setParameters(params); + } catch (RuntimeException e) { + LOG.e("onSetHdr:", "Failed to get params from camera. Maybe low level problem with camera or camera has already released?"); + //Should throw exception? Looks inappropriate at this point if camera is working... + //throw new CameraException(e, CameraException.REASON_FAILED_TO_START_PREVIEW); + } } }); } @@ -631,12 +659,18 @@ public void setZoom(final float zoom, @Nullable final PointF[] points, final boo new Runnable() { @Override public void run() { - Camera.Parameters params = mCamera.getParameters(); - if (applyZoom(params, old)) { - mCamera.setParameters(params); - if (notify) { - getCallback().dispatchOnZoomChanged(mZoomValue, points); + try { + Camera.Parameters params = mCamera.getParameters(); + if (applyZoom(params, old)) { + mCamera.setParameters(params); + if (notify) { + getCallback().dispatchOnZoomChanged(mZoomValue, points); + } } + } catch (RuntimeException e) { + LOG.e("onSetZoom:", "Failed to get params from camera. Maybe low level problem with camera or camera has already released?"); + //Should throw exception? Looks inappropriate at this point if camera is working... + //throw new CameraException(e, CameraException.REASON_FAILED_TO_START_PREVIEW); } } }); @@ -666,13 +700,19 @@ public void setExposureCorrection(final float EVvalue, @NonNull final float[] bo new Runnable() { @Override public void run() { - Camera.Parameters params = mCamera.getParameters(); - if (applyExposureCorrection(params, old)) { - mCamera.setParameters(params); - if (notify) { - getCallback().dispatchOnExposureCorrectionChanged(mExposureCorrectionValue, - bounds, points); + try { + Camera.Parameters params = mCamera.getParameters(); + if (applyExposureCorrection(params, old)) { + mCamera.setParameters(params); + if (notify) { + getCallback().dispatchOnExposureCorrectionChanged(mExposureCorrectionValue, + bounds, points); + } } + } catch (RuntimeException e) { + LOG.e("onSetExposureCorrection:", "Failed to get params from camera. Maybe low level problem with camera or camera has already released?"); + //Should throw exception? Looks inappropriate at this point if camera is working... + //throw new CameraException(e, CameraException.REASON_FAILED_TO_START_PREVIEW); } } }); @@ -744,8 +784,14 @@ public void setPreviewFrameRate(float previewFrameRate) { new Runnable() { @Override public void run() { - Camera.Parameters params = mCamera.getParameters(); - if (applyPreviewFrameRate(params, old)) mCamera.setParameters(params); + try { + Camera.Parameters params = mCamera.getParameters(); + if (applyPreviewFrameRate(params, old)) mCamera.setParameters(params); + } catch (RuntimeException e) { + LOG.e("onSetPreviewFrameRate:", "Failed to get params from camera. Maybe low level problem with camera or camera has already released?"); + //Should throw exception? Looks inappropriate at this point if camera is working... + //throw new CameraException(e, CameraException.REASON_FAILED_TO_START_PREVIEW); + } } }); } @@ -875,7 +921,13 @@ public void run() { getPreview().getSurfaceSize()); MeteringRegions transformed = regions.transform(transform); - Camera.Parameters params = mCamera.getParameters(); + Camera.Parameters params; + try { + params = mCamera.getParameters(); + } catch (RuntimeException re) { + LOG.e("startAutoFocus:", "Failed to get camera parameters"); + throw new CameraException(re, CameraException.REASON_UNKNOWN); + } int maxAF = params.getMaxNumFocusAreas(); int maxAE = params.getMaxNumMeteringAreas(); if (maxAF > 0) params.setFocusAreas(transformed.get(maxAF, transform)); diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/video/encoding/VideoMediaEncoder.java b/cameraview/src/main/java/com/otaliastudios/cameraview/video/encoding/VideoMediaEncoder.java index 770c3ad7..4a818a35 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/video/encoding/VideoMediaEncoder.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/video/encoding/VideoMediaEncoder.java @@ -81,7 +81,11 @@ protected void onPrepare(@NonNull MediaEncoderEngine.Controller controller, long } mMediaCodec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE); mSurface = mMediaCodec.createInputSurface(); - mMediaCodec.start(); + try { + mMediaCodec.start(); + } catch (MediaCodec.CodecException e) { + throw new RuntimeException(e); + } } @EncoderThread