From 59fba7b80f9c3dd826dacb6cf5600dbdcf9710d9 Mon Sep 17 00:00:00 2001 From: Mattia Iavarone Date: Thu, 10 Aug 2017 22:19:13 +0200 Subject: [PATCH] Removed FOCUS useless stuff. We default to the best focus mode. --- .../com/otaliastudios/cameraview/Camera1.java | 61 +++---- .../com/otaliastudios/cameraview/Camera2.java | 5 - .../cameraview/CameraConstants.java | 36 ++--- .../cameraview/CameraController.java | 1 - .../cameraview/CameraOptions.java | 23 --- .../otaliastudios/cameraview/CameraView.java | 153 ++++++++++-------- .../com/otaliastudios/cameraview/Mapper.java | 28 ---- cameraview/src/main/res/values/attrs.xml | 52 +++--- 8 files changed, 146 insertions(+), 213 deletions(-) diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java index 19d83b57..f7b2253d 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java @@ -146,7 +146,7 @@ class Camera1 extends CameraController { Camera.Parameters params = mCamera.getParameters(); mExtraProperties = new ExtraProperties(params); mOptions = new CameraOptions(params); - mergeFocus(params, CameraConstants.Defaults.DEFAULT_FOCUS); + applyDefaultFocus(params); mergeFlash(params, CameraConstants.Defaults.DEFAULT_FLASH); mergeLocation(params, 0d, 0d); mergeWhiteBalance(params, CameraConstants.Defaults.DEFAULT_WHITE_BALANCE); @@ -297,49 +297,30 @@ class Camera1 extends CameraController { } - @Override - void setFocus(@Focus int focus) { - int old = mFocus; - mFocus = focus; - if (isCameraOpened()) { - synchronized (mLock) { - Camera.Parameters params = mCamera.getParameters(); - if (mergeFocus(params, old)) mCamera.setParameters(params); - } - } - } - + // Choose the best default focus, based on session type. + private void applyDefaultFocus(Camera.Parameters params) { + List modes = params.getSupportedFocusModes(); - private boolean mergeFocus(Camera.Parameters params, @Focus int oldFocus) { - if (!mOptions.getSupportedFocus().contains(mFocus)) { - mFocus = oldFocus; - return false; + if (mSessionType == SESSION_TYPE_VIDEO && + modes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) { + params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO); + return; } - switch (mFocus) { - case FOCUS_CONTINUOUS: - params.setFocusMode((String) mMapper.mapFocus(FOCUS_CONTINUOUS)); - return true; - - case FOCUS_TAP: - case FOCUS_TAP_WITH_MARKER: - // We are sure camera supports auto focus. - // Now we must just find a fallback, because FOCUS_AUTO is used only on tap. - if (mOptions.getSupportedFocus().contains(FOCUS_CONTINUOUS)) { - params.setFocusMode((String) mMapper.mapFocus(FOCUS_CONTINUOUS)); - return true; - } - // Don't break. + if (modes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) { + params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); + return; + } - case FOCUS_FIXED: - if (mOptions.getSupportedFocus().contains(FOCUS_FIXED)) { - params.setFocusMode((String) mMapper.mapFocus(FOCUS_FIXED)); - return true; - } + if (modes.contains(Camera.Parameters.FOCUS_MODE_INFINITY)) { + params.setFocusMode(Camera.Parameters.FOCUS_MODE_INFINITY); + return; } - mFocus = oldFocus; - return false; + if (modes.contains(Camera.Parameters.FOCUS_MODE_FIXED)) { + params.setFocusMode(Camera.Parameters.FOCUS_MODE_FIXED); + return; + } } @@ -706,7 +687,7 @@ class Camera1 extends CameraController { int maxAE = params.getMaxNumMeteringAreas(); if (maxAF > 0) params.setFocusAreas(maxAF > 1 ? meteringAreas2 : meteringAreas1); if (maxAE > 0) params.setMeteringAreas(maxAE > 1 ? meteringAreas2 : meteringAreas1); - params.setFocusMode((String) mMapper.mapFocus(FOCUS_TAP)); // auto + params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); mCamera.setParameters(params); mCameraCallbacks.dispatchOnFocusStart(gesture, p); // TODO this is not guaranteed to be called... Fix. @@ -734,7 +715,7 @@ class Camera1 extends CameraController { Camera.Parameters params = mCamera.getParameters(); params.setFocusAreas(null); params.setMeteringAreas(null); - mergeFocus(params, mFocus); // Revert to internal focus (CONTINUOUS_PICTURE if present). + applyDefaultFocus(params); // Revert to internal focus. mCamera.setParameters(params); } } diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java index b794c548..35aa94b0 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java @@ -142,11 +142,6 @@ class Camera2 extends CameraController { } - @Override - void setFocus(@Focus int focus) { - - } - @Override void setSessionType(@SessionType int sessionType) { diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraConstants.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraConstants.java index fb1fb1d4..de4a36b4 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraConstants.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraConstants.java @@ -20,11 +20,6 @@ public class CameraConstants { public static final int FLASH_AUTO = 2; public static final int FLASH_TORCH = 3; - public static final int FOCUS_FIXED = 0; - public static final int FOCUS_CONTINUOUS = 1; - public static final int FOCUS_TAP = 2; - public static final int FOCUS_TAP_WITH_MARKER = 3; - public static final int GRID_OFF = 0; public static final int GRID_3X3 = 1; public static final int GRID_4X4 = 2; @@ -33,15 +28,6 @@ public class CameraConstants { public static final int SESSION_TYPE_PICTURE = 0; public static final int SESSION_TYPE_VIDEO = 1; - @Deprecated public static final int ZOOM_OFF = 0; - @Deprecated public static final int ZOOM_PINCH = 1; - - @Deprecated public static final int CAPTURE_METHOD_STANDARD = 0; - @Deprecated public static final int CAPTURE_METHOD_FRAME = 1; - - @Deprecated public static final int PERMISSIONS_VIDEO = 0; - @Deprecated public static final int PERMISSIONS_PICTURE = 1; - public static final int VIDEO_QUALITY_480P = 0; public static final int VIDEO_QUALITY_720P = 1; public static final int VIDEO_QUALITY_1080P = 2; @@ -56,20 +42,25 @@ public class CameraConstants { public static final int WHITE_BALANCE_DAYLIGHT = 3; public static final int WHITE_BALANCE_CLOUDY = 4; + @Deprecated public static final int FOCUS_FIXED = 0; + @Deprecated public static final int FOCUS_CONTINUOUS = 1; + @Deprecated public static final int FOCUS_TAP = 2; + @Deprecated public static final int FOCUS_TAP_WITH_MARKER = 3; + @Deprecated public static final int ZOOM_OFF = 0; + @Deprecated public static final int ZOOM_PINCH = 1; + @Deprecated public static final int CAPTURE_METHOD_STANDARD = 0; + @Deprecated public static final int CAPTURE_METHOD_FRAME = 1; + @Deprecated public static final int PERMISSIONS_VIDEO = 0; + @Deprecated public static final int PERMISSIONS_PICTURE = 1; static class Defaults { static final int DEFAULT_GESTURE_ACTION_TAP = GESTURE_ACTION_NONE; - // static final int DEFAULT_GESTURE_ACTION_DOUBLE_TAP = GESTURE_ACTION_NONE; + // WIP static final int DEFAULT_GESTURE_ACTION_DOUBLE_TAP = GESTURE_ACTION_NONE; static final int DEFAULT_GESTURE_ACTION_LONG_TAP = GESTURE_ACTION_NONE; static final int DEFAULT_GESTURE_ACTION_PINCH = GESTURE_ACTION_NONE; - static final int DEFAULT_FACING = FACING_BACK; static final int DEFAULT_FLASH = FLASH_OFF; - static final int DEFAULT_FOCUS = FOCUS_CONTINUOUS; - @Deprecated static final int DEFAULT_ZOOM = ZOOM_OFF; - @Deprecated static final int DEFAULT_METHOD = CAPTURE_METHOD_STANDARD; - @Deprecated static final int DEFAULT_PERMISSIONS = PERMISSIONS_PICTURE; static final int DEFAULT_VIDEO_QUALITY = VIDEO_QUALITY_480P; static final int DEFAULT_WHITE_BALANCE = WHITE_BALANCE_AUTO; static final int DEFAULT_SESSION_TYPE = SESSION_TYPE_PICTURE; @@ -77,5 +68,10 @@ public class CameraConstants { static final int DEFAULT_GRID = GRID_OFF; static final boolean DEFAULT_CROP_OUTPUT = false; + @Deprecated static final int DEFAULT_FOCUS = FOCUS_CONTINUOUS; + @Deprecated static final int DEFAULT_ZOOM = ZOOM_OFF; + @Deprecated static final int DEFAULT_METHOD = CAPTURE_METHOD_STANDARD; + @Deprecated static final int DEFAULT_PERMISSIONS = PERMISSIONS_PICTURE; + } } diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java index 4107aaf8..56ef2f09 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java @@ -34,7 +34,6 @@ abstract class CameraController implements Preview.SurfaceCallback { abstract boolean setExposureCorrection(float EVvalue); abstract void setFacing(@Facing int facing); abstract void setFlash(@Flash int flash); - abstract void setFocus(@Focus int focus); abstract void setVideoQuality(@VideoQuality int videoQuality); abstract void setWhiteBalance(@WhiteBalance int whiteBalance); abstract void setSessionType(@SessionType int sessionType); diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraOptions.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraOptions.java index b1ac1462..c55ddec3 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraOptions.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraOptions.java @@ -18,7 +18,6 @@ public class CameraOptions { private Set supportedWhiteBalance = new HashSet<>(5); private Set supportedFacing = new HashSet<>(2); private Set supportedFlash = new HashSet<>(4); - @Deprecated private Set supportedFocus = new HashSet<>(4); private boolean zoomSupported; private boolean videoSnapshotSupported; @@ -60,13 +59,6 @@ public class CameraOptions { } } - // Focus - strings = params.getSupportedFocusModes(); // Never null. - for (String string : strings) { - Integer value = mapper.unmapFocus(string); - if (value != null) supportedFocus.add(value); - } - zoomSupported = params.isZoomSupported(); videoSnapshotSupported = params.isVideoSnapshotSupported(); autoFocusSupported = params.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_AUTO); @@ -129,21 +121,6 @@ public class CameraOptions { } - /** - * Set of supported focus values. - * - * @see CameraConstants#FOCUS_FIXED - * @see CameraConstants#FOCUS_CONTINUOUS - * @see CameraConstants#FOCUS_TAP - * @see CameraConstants#FOCUS_TAP_WITH_MARKER - * @return a set of supported values. - */ - @NonNull - public Set getSupportedFocus() { - return supportedFocus; - } - - /** * Whether zoom is supported. If this is false, pinch-to-zoom * will not work and {@link CameraView#setZoom(float)} will have no effect. diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java index e530b3ca..224b09af 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java @@ -98,7 +98,6 @@ public class CameraView extends FrameLayout { TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CameraView, 0, 0); int facing = a.getInteger(R.styleable.CameraView_cameraFacing, Defaults.DEFAULT_FACING); int flash = a.getInteger(R.styleable.CameraView_cameraFlash, Defaults.DEFAULT_FLASH); - int focus = a.getInteger(R.styleable.CameraView_cameraFocus, Defaults.DEFAULT_FOCUS); int sessionType = a.getInteger(R.styleable.CameraView_cameraSessionType, Defaults.DEFAULT_SESSION_TYPE); int whiteBalance = a.getInteger(R.styleable.CameraView_cameraWhiteBalance, Defaults.DEFAULT_WHITE_BALANCE); int videoQuality = a.getInteger(R.styleable.CameraView_cameraVideoQuality, Defaults.DEFAULT_VIDEO_QUALITY); @@ -124,7 +123,6 @@ public class CameraView extends FrameLayout { mIsStarted = false; setFacing(facing); setFlash(flash); - setFocus(focus); setSessionType(sessionType); setVideoQuality(videoQuality); setWhiteBalance(whiteBalance); @@ -169,6 +167,10 @@ public class CameraView extends FrameLayout { } + // Smart measuring behavior + // ------------------------ + + private String ms(int mode) { switch (mode) { case AT_MOST: return "AT_MOST"; @@ -305,6 +307,10 @@ public class CameraView extends FrameLayout { } + // Gesture APIs and touch control + // ------------------------------ + + /** * Maps a {@link Gesture} to a certain gesture action. * For example, you can assign zoom control to the pinch gesture by just calling: @@ -406,6 +412,10 @@ public class CameraView extends FrameLayout { } + // Lifecycle APIs + // -------------- + + /** * Returns whether the camera has started showing its preview. * @return whether the camera has started @@ -520,6 +530,10 @@ public class CameraView extends FrameLayout { } + // Public APIs for parameters and controls + // --------------------------------------- + + /** * Returns a {@link CameraOptions} instance holding supported options for this camera * session. This might change over time. It's better to hold a reference from @@ -763,54 +777,13 @@ public class CameraView extends FrameLayout { * @param x should be >= 0 and <= getWidth() * @param y should be >= 0 and <= getHeight() */ - public void startFocus(float x, float y) { + public void startAutoFocus(float x, float y) { if (x < 0 || x > getWidth()) throw new IllegalArgumentException("x should be >= 0 and <= getWidth()"); if (y < 0 || y > getHeight()) throw new IllegalArgumentException("y should be >= 0 and <= getHeight()"); mCameraController.startAutoFocus(null, new PointF(x, y)); } - /** - * Sets the current focus behavior. - * - * @see CameraConstants#FOCUS_CONTINUOUS - * @see CameraConstants#FOCUS_FIXED - * @see CameraConstants#FOCUS_TAP - * @see CameraConstants#FOCUS_TAP_WITH_MARKER - - * @param focus a Focus value. - */ - public void setFocus(@Focus int focus) { - mCameraController.setFocus(focus); - } - - - /** - * Gets the current focus behavior. - * @return a focus behavior - */ - @Focus - public int getFocus() { - return mCameraController.getFocus(); - } - - - /** - * This does nothing. - * @deprecated - */ - @Deprecated - public void setCaptureMethod(@Method int method) {} - - - /** - * This does nothing. - * @deprecated - */ - @Deprecated - public void setPermissionPolicy(@Permissions int permissions) {} - - /** * Set the current session type to either picture or video. * When sessionType is video, @@ -852,30 +825,6 @@ public class CameraView extends FrameLayout { } - /** - * Sets the zoom mode for the current session. - * - * @see CameraConstants#ZOOM_OFF - * @see CameraConstants#ZOOM_PINCH - * - * @deprecated use {@link #mapGesture(Gesture, int)} to map zoom control to gestures - */ - @Deprecated - public void setZoomMode(@ZoomMode int zoom) { - } - - - /** - * Gets the current zoom mode. - * @deprecated use {@link #mapGesture(Gesture, int)} to map zoom control to gestures - */ - @ZoomMode - @Deprecated - public int getZoomMode() { - return ZOOM_OFF; - } - - /** * Sets video recording quality. This is not guaranteed to be supported by current device. * If it's not, a lower quality will be chosen, until a supported one is found. @@ -1135,6 +1084,11 @@ public class CameraView extends FrameLayout { } } + + // Callbacks and dispatch + // ---------------------- + + class CameraCallbacks { private ArrayList mListeners; @@ -1334,4 +1288,67 @@ public class CameraView extends FrameLayout { } + // Deprecated stuff + // ---------------- + + + /** + * This does nothing. + * @deprecated + */ + @Deprecated + public void setFocus(@Focus int focus) { + } + + + /** + * This does nothing. + * @deprecated + */ + @Deprecated + @Focus + public int getFocus() { + return FOCUS_FIXED; + } + + + /** + * This does nothing. + * @deprecated + */ + @Deprecated + public void setCaptureMethod(@Method int method) {} + + + /** + * This does nothing. + * @deprecated + */ + @Deprecated + public void setPermissionPolicy(@Permissions int permissions) {} + + + /** + * Sets the zoom mode for the current session. + * + * @see CameraConstants#ZOOM_OFF + * @see CameraConstants#ZOOM_PINCH + * + * @deprecated use {@link #mapGesture(Gesture, int)} to map zoom control to gestures + */ + @Deprecated + public void setZoomMode(@ZoomMode int zoom) { + } + + + /** + * Gets the current zoom mode. + * @deprecated use {@link #mapGesture(Gesture, int)} to map zoom control to gestures + */ + @ZoomMode + @Deprecated + public int getZoomMode() { + return ZOOM_OFF; + } + } diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/Mapper.java b/cameraview/src/main/java/com/otaliastudios/cameraview/Mapper.java index 30071d1d..f4d85b6f 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/Mapper.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/Mapper.java @@ -10,17 +10,14 @@ abstract class Mapper { abstract T mapFlash(@Flash int internalConstant); abstract T mapFacing(@Facing int internalConstant); abstract T mapWhiteBalance(@WhiteBalance int internalConstant); - abstract T mapFocus(@Focus int internalConstant); @Flash abstract Integer unmapFlash(T cameraConstant); @Facing abstract Integer unmapFacing(T cameraConstant); @WhiteBalance abstract Integer unmapWhiteBalance(T cameraConstant); - @Focus abstract Integer unmapFocus(T cameraConstant); static class Mapper1 extends Mapper { private static final HashMap FLASH = new HashMap<>(); private static final HashMap WB = new HashMap<>(); private static final HashMap FACING = new HashMap<>(); - private static final HashMap FOCUS = new HashMap<>(); static { FLASH.put(CameraConstants.FLASH_OFF, Camera.Parameters.FLASH_MODE_OFF); @@ -34,11 +31,6 @@ abstract class Mapper { WB.put(CameraConstants.WHITE_BALANCE_FLUORESCENT, Camera.Parameters.WHITE_BALANCE_FLUORESCENT); WB.put(CameraConstants.WHITE_BALANCE_DAYLIGHT, Camera.Parameters.WHITE_BALANCE_DAYLIGHT); WB.put(CameraConstants.WHITE_BALANCE_CLOUDY, Camera.Parameters.WHITE_BALANCE_CLOUDY_DAYLIGHT); - // TODO FOCUS_MODE_FIXED is rarely supported. - FOCUS.put(CameraConstants.FOCUS_FIXED, Camera.Parameters.FOCUS_MODE_FIXED); - FOCUS.put(CameraConstants.FOCUS_CONTINUOUS, Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); - FOCUS.put(CameraConstants.FOCUS_TAP, Camera.Parameters.FOCUS_MODE_AUTO); - FOCUS.put(CameraConstants.FOCUS_TAP_WITH_MARKER, Camera.Parameters.FOCUS_MODE_AUTO); } @Override @@ -56,11 +48,6 @@ abstract class Mapper { return (T) WB.get(internalConstant); } - @Override - T mapFocus(@Focus int internalConstant) { - return (T) FOCUS.get(internalConstant); - } - private Integer reverseLookup(HashMap map, Object object) { for (int value : map.keySet()) { if (map.get(value).equals(object)) { @@ -84,12 +71,6 @@ abstract class Mapper { Integer unmapWhiteBalance(T cameraConstant) { return reverseLookup(WB, cameraConstant); } - - // This will ignore FOCUS_TAP_WITH_MARKER but it's fine - @Override - Integer unmapFocus(T cameraConstant) { - return reverseLookup(FOCUS, cameraConstant); - } } static class Mapper2 extends Mapper { @@ -124,15 +105,6 @@ abstract class Mapper { return 0; } - @Override - T mapFocus(@Focus int internalConstant) { - return null; - } - - @Override - Integer unmapFocus(T cameraConstant) { - return 0; - } } } diff --git a/cameraview/src/main/res/values/attrs.xml b/cameraview/src/main/res/values/attrs.xml index 26db5d2e..589b0387 100644 --- a/cameraview/src/main/res/values/attrs.xml +++ b/cameraview/src/main/res/values/attrs.xml @@ -1,16 +1,14 @@ - - - + - + @@ -29,7 +27,6 @@ - @@ -42,13 +39,6 @@ - - - - - - - @@ -57,11 +47,6 @@ - - - - - @@ -69,21 +54,11 @@ - - - - - - - - - - @@ -98,6 +73,27 @@ + + + + + + + \ No newline at end of file