diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java index 758bb895..1d54cde2 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java @@ -31,7 +31,6 @@ class Camera1 extends CameraController implements Camera.PreviewCallback, Camera private Camera mCamera; private boolean mIsBound = false; - private final int mPostFocusResetDelay = 3000; private Runnable mPostFocusResetRunnable = new Runnable() { @Override public void run() { @@ -891,7 +890,9 @@ class Camera1 extends CameraController implements Camera.PreviewCallback, Camera // TODO lock auto exposure and white balance for a while mCameraCallbacks.dispatchOnFocusEnd(gesture, success, p); mHandler.get().removeCallbacks(mPostFocusResetRunnable); - mHandler.get().postDelayed(mPostFocusResetRunnable, mPostFocusResetDelay); + if (shouldResetAutoFocus()) { + mHandler.get().postDelayed(mPostFocusResetRunnable, getAutoFocusResetDelay()); + } } }); } catch (RuntimeException e) { diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java index 4b28bb31..027e4a5d 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java @@ -72,6 +72,7 @@ abstract class CameraController implements protected Size mCaptureSize; protected Size mPreviewStreamSize; protected int mPreviewFormat; + protected long mAutoFocusResetDelayMillis; protected int mSensorOffset; private int mDisplayOffset; @@ -325,6 +326,8 @@ abstract class CameraController implements mSnapshotMaxHeight = maxHeight; } + final void setAutoFocusResetDelay(long delayMillis) { mAutoFocusResetDelayMillis = delayMillis; } + //endregion //region Abstract setters and APIs @@ -465,6 +468,12 @@ abstract class CameraController implements return mPictureRecorder != null; } + final long getAutoFocusResetDelay() { return mAutoFocusResetDelayMillis; } + + final boolean shouldResetAutoFocus() { + return mAutoFocusResetDelayMillis > 0 && mAutoFocusResetDelayMillis != Long.MAX_VALUE; + } + //endregion //region Orientation utils diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java index deafbe50..35317995 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java @@ -48,6 +48,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { public final static int PERMISSION_REQUEST_CODE = 16; + final static long DEFAULT_AUTOFOCUS_RESET_DELAY_MILLIS = 3000; final static boolean DEFAULT_PLAY_SOUNDS = true; // Self managed parameters @@ -113,6 +114,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { int videoMaxDuration = a.getInteger(R.styleable.CameraView_cameraVideoMaxDuration, 0); int videoBitRate = a.getInteger(R.styleable.CameraView_cameraVideoBitRate, 0); int audioBitRate = a.getInteger(R.styleable.CameraView_cameraAudioBitRate, 0); + long autoFocusResetDelay = (long) a.getInteger(R.styleable.CameraView_cameraAutoFocusResetDelay, (int) DEFAULT_AUTOFOCUS_RESET_DELAY_MILLIS); // Picture size selector List pictureConstraints = new ArrayList<>(3); @@ -219,6 +221,7 @@ public class CameraView extends FrameLayout implements LifecycleObserver { setVideoMaxSize(videoMaxSize); setVideoMaxDuration(videoMaxDuration); setVideoBitRate(videoBitRate); + setAutoFocusResetDelay(autoFocusResetDelay); // Apply gestures mapGesture(Gesture.TAP, tapGesture); @@ -1049,6 +1052,24 @@ public class CameraView extends FrameLayout implements LifecycleObserver { } + /** + * Sets the current delay in milliseconds to reset the focus after an autofocus process. + * + * @param delayMillis desired delay (in milliseconds). If the delay + * is less than or equal to 0 or equal to Long.MAX_VALUE, + * the autofocus will not be reset. + */ + public void setAutoFocusResetDelay(long delayMillis) { + mCameraController.setAutoFocusResetDelay(delayMillis); + } + + /** + * Returns the current delay in milliseconds to reset the focus after an autofocus process. + * @return the current autofocus reset delay in milliseconds. + */ + public long getAutoFocusResetDelay() { return mCameraController.getAutoFocusResetDelay(); } + + /** * Starts an autofocus process at the given coordinates, with respect * to the view width and height. diff --git a/cameraview/src/main/res/values/attrs.xml b/cameraview/src/main/res/values/attrs.xml index 5ea86273..c687a7d0 100644 --- a/cameraview/src/main/res/values/attrs.xml +++ b/cameraview/src/main/res/values/attrs.xml @@ -124,5 +124,7 @@ + + \ No newline at end of file diff --git a/docs/_posts/2018-12-20-more-features.md b/docs/_posts/2018-12-20-more-features.md index a971f84a..0cbc969e 100644 --- a/docs/_posts/2018-12-20-more-features.md +++ b/docs/_posts/2018-12-20-more-features.md @@ -15,7 +15,8 @@ disqus: 1 + app:cameraGridColor="@color/black" + app:cameraAutoFocusResetDelay="0"/> ``` ##### cameraPlaySounds @@ -56,6 +57,20 @@ cameraView.setGridColor(Color.WHITE); cameraView.setGridColor(Color.BLACK); ``` +##### cameraAutoFocusResetDelay + +Lets you control how an auto-focus operation is reset after completed. +Setting a value <= 0 or == Long.MAX_VALUE will not reset the auto-focus. +This is useful for low end devices that have slow auto-focus capabilities. +Defaults to 3 seconds. + +```java +cameraView.setCameraAutoFocusResetDelay(1000); // 1 second +cameraView.setCameraAutoFocusResetDelay(0); // NO reset +cameraView.setCameraAutoFocusResetDelay(-1); // NO reset +cameraView.setCameraAutoFocusResetDelay(Long.MAX_VALUE); // NO reset + + ### UI Orientation Within a Camera app, it's common to rotate buttons and other UI elements as the device is tilted around.