diff --git a/camerakit/src/main/api16/com/flurgle/camerakit/Camera1.java b/camerakit/src/main/api16/com/flurgle/camerakit/Camera1.java index f145d74b..01722219 100644 --- a/camerakit/src/main/api16/com/flurgle/camerakit/Camera1.java +++ b/camerakit/src/main/api16/com/flurgle/camerakit/Camera1.java @@ -5,6 +5,7 @@ import android.graphics.YuvImage; import android.hardware.Camera; import android.media.CamcorderProfile; import android.media.MediaRecorder; +import android.os.Handler; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.View; @@ -30,6 +31,7 @@ public class Camera1 extends CameraImpl { private static final int FOCUS_AREA_SIZE_DEFAULT = 300; private static final int FOCUS_METERING_AREA_WEIGHT_DEFAULT = 1000; + private static final int DELAY_MILLIS_BEFORE_RESETTING_FOCUS = 3000; private int mCameraId; private Camera mCamera; @@ -61,6 +63,8 @@ public class Camera1 extends CameraImpl { @VideoQuality private int mVideoQuality; + private Handler mHandler = new Handler(); + Camera1(CameraListener callback, PreviewImpl preview) { super(callback, preview); preview.setCallback(new PreviewImpl.Callback() { @@ -90,6 +94,7 @@ public class Camera1 extends CameraImpl { @Override void stop() { if (mCamera != null) mCamera.stopPreview(); + mHandler.removeCallbacksAndMessages(null); releaseCamera(); } @@ -501,14 +506,36 @@ public class Camera1 extends CameraImpl { if (event.getAction() == MotionEvent.ACTION_UP) { if (mCamera != null) { Camera.Parameters parameters = mCamera.getParameters(); - if (parameters.getMaxNumMeteringAreas() > 0) { - Rect rect = calculateFocusArea(event.getX(), event.getY()); + String focusMode = parameters.getFocusMode(); + Rect rect = calculateFocusArea(event.getX(), event.getY()); + List meteringAreas = new ArrayList<>(); + meteringAreas.add(new Camera.Area(rect, getFocusMeteringAreaWeight())); + if (parameters.getMaxNumFocusAreas() != 0 && focusMode != null && + (focusMode.equals(Camera.Parameters.FOCUS_MODE_AUTO) || + focusMode.equals(Camera.Parameters.FOCUS_MODE_MACRO) || + focusMode.equals(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE) || + focusMode.equals(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) + ) { + parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); + parameters.setFocusAreas(meteringAreas); + if (parameters.getMaxNumMeteringAreas() > 0) { + parameters.setMeteringAreas(meteringAreas); + } + if(!parameters.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_AUTO)) { + return false; //cannot autoFocus + } + mCamera.setParameters(parameters); + mCamera.autoFocus(new Camera.AutoFocusCallback() { + @Override + public void onAutoFocus(boolean success, Camera camera) { + resetFocus(success, camera); + } + }); + } else if (parameters.getMaxNumMeteringAreas() > 0) { if(!parameters.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_AUTO)) { return false; //cannot autoFocus } parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); - List meteringAreas = new ArrayList<>(); - meteringAreas.add(new Camera.Area(rect, getFocusMeteringAreaWeight())); parameters.setFocusAreas(meteringAreas); parameters.setMeteringAreas(meteringAreas); @@ -516,18 +543,7 @@ public class Camera1 extends CameraImpl { mCamera.autoFocus(new Camera.AutoFocusCallback() { @Override public void onAutoFocus(boolean success, Camera camera) { - camera.cancelAutoFocus(); - Camera.Parameters params = camera.getParameters(); - if (params.getFocusMode() != Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE) { - params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); - params.setFocusAreas(null); - params.setMeteringAreas(null); - camera.setParameters(params); - } - - if (mAutofocusCallback != null) { - mAutofocusCallback.onAutoFocus(success, camera); - } + resetFocus(success, camera); } }); } else { @@ -547,6 +563,29 @@ public class Camera1 extends CameraImpl { }); } + private void resetFocus(final boolean success, final Camera camera) { + mHandler.removeCallbacksAndMessages(null); + mHandler.postDelayed(new Runnable() { + @Override + public void run() { + if (camera != null) { + camera.cancelAutoFocus(); + Camera.Parameters params = camera.getParameters(); + if (params.getFocusMode() != Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE) { + params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); + params.setFocusAreas(null); + params.setMeteringAreas(null); + camera.setParameters(params); + } + + if (mAutofocusCallback != null) { + mAutofocusCallback.onAutoFocus(success, camera); + } + } + } + }, DELAY_MILLIS_BEFORE_RESETTING_FOCUS); + } + private Rect calculateFocusArea(float x, float y) { int centerX = clamp(Float.valueOf((x / mPreview.getView().getWidth()) * 2000 - 1000).intValue(), getFocusAreaSize()); int centerY = clamp(Float.valueOf((y / mPreview.getView().getHeight()) * 2000 - 1000).intValue(), getFocusAreaSize()); diff --git a/camerakit/src/main/types/com/flurgle/camerakit/Focus.java b/camerakit/src/main/types/com/flurgle/camerakit/Focus.java index e186fcd2..ee0b31aa 100644 --- a/camerakit/src/main/types/com/flurgle/camerakit/Focus.java +++ b/camerakit/src/main/types/com/flurgle/camerakit/Focus.java @@ -8,8 +8,9 @@ import java.lang.annotation.RetentionPolicy; import static com.flurgle.camerakit.CameraKit.Constants.FOCUS_TAP; import static com.flurgle.camerakit.CameraKit.Constants.FOCUS_OFF; import static com.flurgle.camerakit.CameraKit.Constants.FOCUS_CONTINUOUS; +import static com.flurgle.camerakit.CameraKit.Constants.FOCUS_TAP_WITH_MARKER; @Retention(RetentionPolicy.SOURCE) -@IntDef({FOCUS_CONTINUOUS, FOCUS_TAP, FOCUS_OFF}) +@IntDef({FOCUS_CONTINUOUS, FOCUS_TAP, FOCUS_OFF, FOCUS_TAP_WITH_MARKER}) public @interface Focus { } \ No newline at end of file