diff --git a/camerakit/src/main/api16/com/flurgle/camerakit/Camera1.java b/camerakit/src/main/api16/com/flurgle/camerakit/Camera1.java index c63112d6..91159d89 100644 --- a/camerakit/src/main/api16/com/flurgle/camerakit/Camera1.java +++ b/camerakit/src/main/api16/com/flurgle/camerakit/Camera1.java @@ -32,7 +32,7 @@ class Camera1 extends CameraImpl { private static final String TAG = Camera1.class.getSimpleName(); - private static final int FOCUS_AREA_SIZE_DEFAULT = 300; + private static final int FOCUS_AREA_SIZE_DEFAULT = 200; private static final int FOCUS_METERING_AREA_WEIGHT_DEFAULT = 1000; private static final int DELAY_MILLIS_BEFORE_RESETTING_FOCUS = 3000; @@ -61,6 +61,7 @@ class Camera1 extends CameraImpl { @SessionType private int mSessionType; private double mLatitude; private double mLongitude; + private boolean mFocusOnTap; private Handler mHandler = new Handler(); private ConstantMapper.MapperImpl mMapper = new ConstantMapper.Mapper1(); @@ -217,43 +218,38 @@ class Camera1 extends CameraImpl { void setFocus(@Focus int focus) { int old = mFocus; mFocus = focus; + if (mCameraParameters == null) return; switch (focus) { case FOCUS_CONTINUOUS: - if (mCameraParameters != null) { - detachFocusTapListener(); - final List modes = mCameraParameters.getSupportedFocusModes(); - if (modes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) { - mCameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); - } else { - mFocus = old; - } + mFocusOnTap = false; + final List modes = mCameraParameters.getSupportedFocusModes(); + if (modes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) { + mCameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); + } else { + mFocus = old; } break; case FOCUS_TAP: case FOCUS_TAP_WITH_MARKER: - if (mCameraParameters != null) { - attachFocusTapListener(); - final List modes = mCameraParameters.getSupportedFocusModes(); - if (modes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) { - mCameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); - } else { - mFocus = old; - } + mFocusOnTap = true; + final List modes1 = mCameraParameters.getSupportedFocusModes(); + if (modes1.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) { + mCameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); + } else { + mFocus = old; } break; case FOCUS_OFF: - if (mCameraParameters != null) { - detachFocusTapListener(); - final List modes = mCameraParameters.getSupportedFocusModes(); - if (modes.contains(Camera.Parameters.FOCUS_MODE_FIXED)) { - mCameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_FIXED); - } else if (modes.contains(Camera.Parameters.FOCUS_MODE_INFINITY)) { - mCameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_INFINITY); - } else { - mCameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); - } + mFocusOnTap = false; + final List modes2 = mCameraParameters.getSupportedFocusModes(); + if (modes2.contains(Camera.Parameters.FOCUS_MODE_FIXED)) { + mCameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_FIXED); + } else if (modes2.contains(Camera.Parameters.FOCUS_MODE_INFINITY)) { + mCameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_INFINITY); + } else { + mCameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); } break; } @@ -421,8 +417,8 @@ class Camera1 extends CameraImpl { mCaptureSize = new Size(camcorderProfile.videoFrameWidth, camcorderProfile.videoFrameHeight); } mPreviewSize = computePreviewSize(previewSizes, mCaptureSize, mPreview.getSurfaceSize()); - Log.e("Camera1", "CaptureSize is "+mCaptureSize.toString()); - Log.e("Camera1", "PreviewSize is "+mPreviewSize.toString()); + // Log.e("Camera1", "CaptureSize is "+mCaptureSize.toString()); + // Log.e("Camera1", "PreviewSize is "+mPreviewSize.toString()); } private void adjustCameraParameters() { @@ -433,8 +429,6 @@ class Camera1 extends CameraImpl { ); mCameraParameters.setPreviewSize(getPreviewSize().getWidth(), getPreviewSize().getHeight()); mCameraParameters.setPictureSize(getCaptureSize().getWidth(), getCaptureSize().getHeight()); - // int rotation = calculateCaptureRotation(); - // mCameraParameters.setRotation(rotation); setFocus(mFocus); setFlash(mFlash); setLocation(mLatitude, mLongitude); @@ -547,11 +541,39 @@ class Camera1 extends CameraImpl { // Tap to focus stuff. - void setTapToAutofocusListener(Camera.AutoFocusCallback callback) { - if (this.mFocus != FOCUS_TAP) { - throw new IllegalArgumentException("Please set the camera to FOCUS_TAP."); + @Override + void onTouchEvent(MotionEvent event) { + if (!mFocusOnTap) return; + if (mCamera == null) return; + if (event.getAction() != MotionEvent.ACTION_UP) return; + Camera.Parameters parameters = mCamera.getParameters(); + String focusMode = parameters.getFocusMode(); + Rect rect = calculateFocusArea(event.getX(), event.getY()); + List meteringAreas = new ArrayList<>(); + meteringAreas.add(new Camera.Area(rect, getFocusMeteringAreaWeight())); + + boolean focusSupported = parameters.getMaxNumFocusAreas() > 0 && focusMode != null; + focusSupported = focusSupported && (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)); + boolean autofocusSupported = parameters.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_AUTO); + boolean meteringAreaSupported = parameters.getMaxNumMeteringAreas() > 0; + + if (focusSupported) { + parameters.setFocusAreas(meteringAreas); + if (meteringAreaSupported) parameters.setMeteringAreas(meteringAreas); + if (autofocusSupported) { + parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); + mCamera.setParameters(parameters); + mCamera.autoFocus(new Camera.AutoFocusCallback() { + @Override + public void onAutoFocus(boolean success, Camera camera) { + resetFocus(success, camera); + } + }); + } } - this.mAutofocusCallback = callback; } @@ -565,74 +587,6 @@ class Camera1 extends CameraImpl { } - private void detachFocusTapListener() { - mPreview.getView().setOnTouchListener(null); - } - - - private void attachFocusTapListener() { - mPreview.getView().setOnTouchListener(new View.OnTouchListener() { - @Override - public boolean onTouch(View v, MotionEvent event) { - if (event.getAction() != MotionEvent.ACTION_UP) return false; - if (mCamera == null) return false; - Camera.Parameters parameters = mCamera.getParameters(); - 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); - parameters.setFocusAreas(meteringAreas); - parameters.setMeteringAreas(meteringAreas); - - mCamera.setParameters(parameters); - mCamera.autoFocus(new Camera.AutoFocusCallback() { - @Override - public void onAutoFocus(boolean success, Camera camera) { - resetFocus(success, camera); - } - }); - } else { - mCamera.autoFocus(new Camera.AutoFocusCallback() { - @Override - public void onAutoFocus(boolean success, Camera camera) { - if (mAutofocusCallback != null) { - mAutofocusCallback.onAutoFocus(success, camera); - } - } - }); - } - return true; - } - }); - } - - private void resetFocus(final boolean success, final Camera camera) { mHandler.removeCallbacksAndMessages(null); mHandler.postDelayed(new Runnable() { @@ -648,9 +602,7 @@ class Camera1 extends CameraImpl { camera.setParameters(params); } - if (mAutofocusCallback != null) { - mAutofocusCallback.onAutoFocus(success, camera); - } + if (mAutofocusCallback != null) mAutofocusCallback.onAutoFocus(success, camera); } } }, DELAY_MILLIS_BEFORE_RESETTING_FOCUS); diff --git a/camerakit/src/main/api21/com/flurgle/camerakit/Camera2.java b/camerakit/src/main/api21/com/flurgle/camerakit/Camera2.java index af261999..40a218cc 100644 --- a/camerakit/src/main/api21/com/flurgle/camerakit/Camera2.java +++ b/camerakit/src/main/api21/com/flurgle/camerakit/Camera2.java @@ -12,6 +12,7 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.util.Log; import android.util.SizeF; +import android.view.MotionEvent; import java.io.File; import java.util.ArrayList; @@ -250,6 +251,11 @@ class Camera2 extends CameraImpl { return mCamera != null; } + @Override + void onTouchEvent(MotionEvent event) { + + } + @Nullable @Override ExtraProperties getExtraProperties() { diff --git a/camerakit/src/main/base/com/flurgle/camerakit/CameraImpl.java b/camerakit/src/main/base/com/flurgle/camerakit/CameraImpl.java index d4727b6a..d3c18bdb 100644 --- a/camerakit/src/main/base/com/flurgle/camerakit/CameraImpl.java +++ b/camerakit/src/main/base/com/flurgle/camerakit/CameraImpl.java @@ -2,6 +2,7 @@ package com.flurgle.camerakit; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.view.MotionEvent; import java.io.File; @@ -39,6 +40,8 @@ abstract class CameraImpl { abstract boolean shouldFlipSizes(); // Wheter the Sizes should be flipped to match the view orientation. abstract boolean isCameraOpened(); + abstract void onTouchEvent(MotionEvent event); + @Nullable abstract ExtraProperties getExtraProperties(); diff --git a/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java b/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java index 283a5f6e..176dec7a 100644 --- a/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java +++ b/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java @@ -265,6 +265,19 @@ public class CameraView extends FrameLayout implements LifecycleObserver { } + @Override + public boolean onInterceptTouchEvent(MotionEvent ev) { + return true; // Steal our own events. + } + + @Override + public boolean onTouchEvent(MotionEvent event) { + // And dispatch to everyone. + mFocusMarkerLayout.onTouchEvent(event); // For drawing focus marker. + mCameraImpl.onTouchEvent(event); // For focus behavior. + return true; + } + /** * Returns whether the camera has started showing its preview. * @return whether the camera has started diff --git a/camerakit/src/main/java/com/flurgle/camerakit/FocusMarkerLayout.java b/camerakit/src/main/java/com/flurgle/camerakit/FocusMarkerLayout.java index 247ca40f..1261078e 100644 --- a/camerakit/src/main/java/com/flurgle/camerakit/FocusMarkerLayout.java +++ b/camerakit/src/main/java/com/flurgle/camerakit/FocusMarkerLayout.java @@ -36,7 +36,7 @@ public class FocusMarkerLayout extends FrameLayout { if (action == MotionEvent.ACTION_UP && isEnabled()) { focus(event.getX(), event.getY()); } - return false; // We didn't consume, pass to parent + return false; // We didn't consume, pass } public void focus(float mx, float my) {