From a531f8e5dd167264cb18a69c8798204790cd7c2e Mon Sep 17 00:00:00 2001 From: Mattia Iavarone Date: Fri, 11 Aug 2017 16:04:51 +0200 Subject: [PATCH] Move gesture actions to enums --- README.md | 12 +-- .../otaliastudios/cameraview/CameraView.java | 44 +++++----- .../com/otaliastudios/cameraview/Gesture.java | 64 -------------- .../com/otaliastudios/cameraview/Gesture.java | 62 ++++++++++++++ .../cameraview/GestureAction.java | 85 +++++++++++++++++-- 5 files changed, 167 insertions(+), 100 deletions(-) delete mode 100644 cameraview/src/main/java/com/otaliastudios/cameraview/Gesture.java create mode 100644 cameraview/src/main/options/com/otaliastudios/cameraview/Gesture.java diff --git a/README.md b/README.md index 3b406ada..38cc7e46 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ compile 'com.otaliastudios:cameraview:1.0.0'

-*This is a fork of [CameraKit-Android library](https://github.com/gogopop/CameraKit-Android), originally a fork of [Google's CameraView library](https://github.com/google/cameraview). The library at this point has been completely rewritten and refactored. See [below](#roadmap) for a list of what was done. This works better than any other library I have tried, and I would be grateful for any issue, suggestion or contribution.* +*This is a fork of [CameraKit-Android library](https://github.com/gogopop/CameraKit-Android), originally a fork of [Google's CameraView library](https://github.com/google/cameraview), but has been completely rewritten. See [below](#roadmap) for a list of what was done. Feel free to contribute - this is under active development.* ### Features @@ -217,9 +217,9 @@ camera.addCameraListener(new CameraListener() { `CameraView` listen to lots of different gestures inside its bounds. You have the chance to map these gestures to particular actions or camera controls, using `mapGesture()`. This lets you emulate typical behaviors in a single line: ```java -cameraView.mapGesture(Gesture.PINCH, CameraConstants.GESTURE_ACTION_ZOOM); // Pinch to zoom! -cameraView.mapGesture(Gesture.TAP, CameraConstants.GESTURE_ACTION_FOCUS_WITH_MARKER); // Tap to focus! -cameraView.mapGesture(Gesture.LONG_TAP, CameraConstants.GESTURE_ACTION_CAPTURE); // Long tap to shoot! +cameraView.mapGesture(Gesture.PINCH, GestureAction.ZOOM); // Pinch to zoom! +cameraView.mapGesture(Gesture.TAP, GestureAction.FOCUS_WITH_MARKER); // Tap to focus! +cameraView.mapGesture(Gesture.LONG_TAP, GestureAction.CAPTURE); // Long tap to shoot! ``` Simple as that. More gestures are coming. There are two things to be noted: @@ -283,7 +283,7 @@ Most camera parameters can be controlled through XML attributes or linked method app:cameraFlash="off" app:cameraGrid="off" app:cameraSessionType="picture" - app:cameraCropOutput="true" + app:cameraCropOutput="false" app:cameraJpegQuality="100" app:cameraVideoQuality="480p" app:cameraWhiteBalance="auto" /> @@ -392,7 +392,7 @@ Other APIs not mentioned above are provided, and are well documented and comment |Method|Description| |------|-----------| |`isStarted()`|Returns true if `start()` was called succesfully. This does not mean that camera is open or showing preview.| -|`mapGesture(Gesture, int)`|Maps a certain gesture to a certain action. No-op if the action is not supported.| +|`mapGesture(Gesture, GestureAction)`|Maps a certain gesture to a certain action. No-op if the action is not supported.| |`clearGesture(Gesture)`|Clears any action mapped to the given gesture.| |`getCameraOptions()`|If camera was started, returns non-null object with information about what is supported.| |`getExtraProperties()`|If camera was started, returns non-null object with extra information about the camera sensor. Not very useful at the moment.| diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java index f3948c1d..29137540 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java @@ -81,7 +81,7 @@ public class CameraView extends FrameLayout { private float mZoomValue; private float mExposureCorrectionValue; - private HashMap mGestureMap = new HashMap<>(4); + private HashMap mGestureMap = new HashMap<>(4); public CameraView(@NonNull Context context) { super(context, null); @@ -104,10 +104,10 @@ public class CameraView extends FrameLayout { int grid = a.getInteger(R.styleable.CameraView_cameraGrid, Defaults.DEFAULT_GRID); mJpegQuality = a.getInteger(R.styleable.CameraView_cameraJpegQuality, Defaults.DEFAULT_JPEG_QUALITY); mCropOutput = a.getBoolean(R.styleable.CameraView_cameraCropOutput, Defaults.DEFAULT_CROP_OUTPUT); - int tapGesture = a.getInteger(R.styleable.CameraView_cameraGestureTap, Defaults.DEFAULT_GESTURE_ACTION_TAP); + GestureAction tapGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGestureTap, Defaults.DEFAULT_GESTURE_ACTION_TAP)); // int doubleTapGesture = a.getInteger(R.styleable.CameraView_cameraGestureDoubleTap, Defaults.DEFAULT_GESTURE_ACTION_DOUBLE_TAP); - int longTapGesture = a.getInteger(R.styleable.CameraView_cameraGestureLongTap, Defaults.DEFAULT_GESTURE_ACTION_LONG_TAP); - int pinchGesture = a.getInteger(R.styleable.CameraView_cameraGesturePinch, Defaults.DEFAULT_GESTURE_ACTION_PINCH); + GestureAction longTapGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGestureLongTap, Defaults.DEFAULT_GESTURE_ACTION_LONG_TAP)); + GestureAction pinchGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGesturePinch, Defaults.DEFAULT_GESTURE_ACTION_PINCH)); a.recycle(); mCameraCallbacks = new CameraCallbacks(); @@ -326,24 +326,24 @@ public class CameraView extends FrameLayout { * @param action which action should be assigned * @return true if this action could be assigned to this gesture */ - public boolean mapGesture(@NonNull Gesture gesture, @GestureAction int action) { + public boolean mapGesture(@NonNull Gesture gesture, GestureAction action) { if (gesture.isAssignableTo(action)) { mGestureMap.put(gesture, action); switch (gesture) { case PINCH: - mPinchGestureLayout.enable(mGestureMap.get(Gesture.PINCH) != GESTURE_ACTION_NONE); + mPinchGestureLayout.enable(mGestureMap.get(Gesture.PINCH) != GestureAction.NONE); break; case TAP: // case DOUBLE_TAP: case LONG_TAP: - mTapGestureLayout.enable(mGestureMap.get(Gesture.TAP) != GESTURE_ACTION_NONE || - // mGestureMap.get(Gesture.DOUBLE_TAP) != GESTURE_ACTION_NONE || - mGestureMap.get(Gesture.LONG_TAP) != GESTURE_ACTION_NONE); + mTapGestureLayout.enable(mGestureMap.get(Gesture.TAP) != GestureAction.NONE || + // mGestureMap.get(Gesture.DOUBLE_TAP) != GestureAction.NONE || + mGestureMap.get(Gesture.LONG_TAP) != GestureAction.NONE); break; } return true; } - mapGesture(gesture, GESTURE_ACTION_NONE); + mapGesture(gesture, GestureAction.NONE); return false; } @@ -353,7 +353,7 @@ public class CameraView extends FrameLayout { * @param gesture which gesture to clear */ public void clearGesture(@NonNull Gesture gesture) { - mGestureMap.put(gesture, GESTURE_ACTION_NONE); + mGestureMap.put(gesture, GestureAction.NONE); } @@ -369,10 +369,10 @@ public class CameraView extends FrameLayout { CameraOptions options = mCameraController.getCameraOptions(); // Non null if (mPinchGestureLayout.onTouchEvent(event)) { - int action = mGestureMap.get(Gesture.PINCH); + GestureAction action = mGestureMap.get(Gesture.PINCH); // This currently can be zoom or AE. // Camera can either support these or not. - if (action == GESTURE_ACTION_ZOOM) { + if (action == GestureAction.ZOOM) { float oldValue = mZoomValue; float newValue = mPinchGestureLayout.scaleValue(oldValue, 0, 1); PointF[] points = mPinchGestureLayout.getPoints(); @@ -381,7 +381,7 @@ public class CameraView extends FrameLayout { mCameraCallbacks.dispatchOnZoomChanged(newValue, points); } - } else if (action == GESTURE_ACTION_AE_CORRECTION) { + } else if (action == GestureAction.EXPOSURE_CORRECTION) { float oldValue = mExposureCorrectionValue; float minValue = options.getExposureCorrectionMinValue(); float maxValue = options.getExposureCorrectionMaxValue(); @@ -396,14 +396,14 @@ public class CameraView extends FrameLayout { } else if (mTapGestureLayout.onTouchEvent(event)) { Gesture gesture = mTapGestureLayout.getGestureType(); - int action = mGestureMap.get(gesture); + GestureAction action = mGestureMap.get(gesture); // This currently can be capture, focus or focusWithMaker. // Camera can either support these or not. - if (action == GESTURE_ACTION_CAPTURE) { + if (action == GestureAction.CAPTURE) { capturePicture(); - } else if (action == GESTURE_ACTION_FOCUS || - action == GESTURE_ACTION_FOCUS_WITH_MARKER) { + } else if (action == GestureAction.FOCUS || + action == GestureAction.FOCUS_WITH_MARKER) { PointF point = mTapGestureLayout.getPoint(); mCameraController.startAutoFocus(gesture, point); // This will call onFocusStart and onFocusEnd } @@ -1235,7 +1235,7 @@ public class CameraView extends FrameLayout { uiHandler.post(new Runnable() { @Override public void run() { - if (gesture != null && mGestureMap.get(gesture) == GESTURE_ACTION_FOCUS_WITH_MARKER) { + if (gesture != null && mGestureMap.get(gesture) == GestureAction.FOCUS_WITH_MARKER) { mTapGestureLayout.onFocusStart(point); } @@ -1252,7 +1252,7 @@ public class CameraView extends FrameLayout { uiHandler.post(new Runnable() { @Override public void run() { - if (gesture != null && mGestureMap.get(gesture) == GESTURE_ACTION_FOCUS_WITH_MARKER) { + if (gesture != null && mGestureMap.get(gesture) == GestureAction.FOCUS_WITH_MARKER) { mTapGestureLayout.onFocusEnd(success); } @@ -1357,7 +1357,7 @@ public class CameraView extends FrameLayout { * @see CameraConstants#ZOOM_PINCH * * @param zoom no-op - * @deprecated use {@link #mapGesture(Gesture, int)} to map zoom control to gestures + * @deprecated use {@link #mapGesture(Gesture, GestureAction)} to map zoom control to gestures */ @Deprecated public void setZoomMode(@ZoomMode int zoom) { @@ -1367,7 +1367,7 @@ public class CameraView extends FrameLayout { /** * Gets the current zoom mode. * @return no-op - * @deprecated use {@link #mapGesture(Gesture, int)} to map zoom control to gestures + * @deprecated use {@link #mapGesture(Gesture, GestureAction)} to map zoom control to gestures */ @ZoomMode @Deprecated diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/Gesture.java b/cameraview/src/main/java/com/otaliastudios/cameraview/Gesture.java deleted file mode 100644 index d4d91d39..00000000 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/Gesture.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.otaliastudios.cameraview; - - -import java.util.Arrays; -import java.util.List; - -import static com.otaliastudios.cameraview.CameraConstants.*; - - -/** - * Gestures listen to finger gestures over the {@link CameraView} bounds and can be mapped - * to one or more camera controls using XML attributes or {@link CameraView#mapGesture(Gesture, int)}. - * - * Not every gesture can control a certain action. For example, pinch gestures can only control - * continuous values, such as zoom or AE correction. Single point gestures, on the other hand, - * can only control point actions such as focusing or capturing a picture. - */ -public enum Gesture { - - /** - * Pinch gesture, typically assigned to the zoom control. - * This gesture can be mapped to: - * - * - {@link CameraConstants#GESTURE_ACTION_ZOOM} - * - {@link CameraConstants#GESTURE_ACTION_AE_CORRECTION} - * - {@link CameraConstants#GESTURE_ACTION_NONE} - */ - PINCH(GESTURE_ACTION_ZOOM, GESTURE_ACTION_AE_CORRECTION), - - /** - * Single tap gesture, typically assigned to the focus control. - * This gesture can be mapped to: - * - * - {@link CameraConstants#GESTURE_ACTION_FOCUS} - * - {@link CameraConstants#GESTURE_ACTION_FOCUS_WITH_MARKER} - * - {@link CameraConstants#GESTURE_ACTION_CAPTURE} - * - {@link CameraConstants#GESTURE_ACTION_NONE} - */ - TAP(GESTURE_ACTION_FOCUS, GESTURE_ACTION_FOCUS_WITH_MARKER, GESTURE_ACTION_CAPTURE), - // DOUBLE_TAP(GESTURE_ACTION_FOCUS, GESTURE_ACTION_FOCUS_WITH_MARKER, GESTURE_ACTION_CAPTURE), - - /** - * Long tap gesture. - * This gesture can be mapped to: - * - * - {@link CameraConstants#GESTURE_ACTION_FOCUS} - * - {@link CameraConstants#GESTURE_ACTION_FOCUS_WITH_MARKER} - * - {@link CameraConstants#GESTURE_ACTION_CAPTURE} - * - {@link CameraConstants#GESTURE_ACTION_NONE} - */ - LONG_TAP(GESTURE_ACTION_FOCUS, GESTURE_ACTION_FOCUS_WITH_MARKER, GESTURE_ACTION_CAPTURE); - - - Gesture(@GestureAction Integer... controls) { - mControls = Arrays.asList(controls); - } - - private List mControls; - - boolean isAssignableTo(@GestureAction Integer control) { - return control == GESTURE_ACTION_NONE || mControls.contains(control); - } - -} diff --git a/cameraview/src/main/options/com/otaliastudios/cameraview/Gesture.java b/cameraview/src/main/options/com/otaliastudios/cameraview/Gesture.java new file mode 100644 index 00000000..45f1c2be --- /dev/null +++ b/cameraview/src/main/options/com/otaliastudios/cameraview/Gesture.java @@ -0,0 +1,62 @@ +package com.otaliastudios.cameraview; + + +import java.util.Arrays; +import java.util.List; + + +/** + * Gestures listen to finger gestures over the {@link CameraView} bounds and can be mapped + * to one or more camera controls using XML attributes or {@link CameraView#mapGesture(Gesture, GestureAction)}. + * + * Not every gesture can control a certain action. For example, pinch gestures can only control + * continuous values, such as zoom or AE correction. Single point gestures, on the other hand, + * can only control point actions such as focusing or capturing a picture. + */ +public enum Gesture { + + /** + * Pinch gesture, typically assigned to the zoom control. + * This gesture can be mapped to: + * + * - {@link GestureAction#ZOOM} + * - {@link GestureAction#EXPOSURE_CORRECTION} + * - {@link GestureAction#NONE} + */ + PINCH(GestureAction.ZOOM, GestureAction.EXPOSURE_CORRECTION), + + /** + * Single tap gesture, typically assigned to the focus control. + * This gesture can be mapped to: + * + * - {@link GestureAction#FOCUS} + * - {@link GestureAction#FOCUS_WITH_MARKER} + * - {@link GestureAction#CAPTURE} + * - {@link GestureAction#NONE} + */ + TAP(GestureAction.FOCUS, GestureAction.FOCUS_WITH_MARKER, GestureAction.CAPTURE), + // DOUBLE_TAP(GestureAction.FOCUS, GestureAction.FOCUS_WITH_MARKER, GestureAction.CAPTURE), + + /** + * Long tap gesture. + * This gesture can be mapped to: + * + * - {@link GestureAction#FOCUS} + * - {@link GestureAction#FOCUS_WITH_MARKER} + * - {@link GestureAction#CAPTURE} + * - {@link GestureAction#NONE} + */ + LONG_TAP(GestureAction.FOCUS, GestureAction.FOCUS_WITH_MARKER, GestureAction.CAPTURE); + + + Gesture(GestureAction... controls) { + mControls = Arrays.asList(controls); + } + + private List mControls; + + boolean isAssignableTo(GestureAction control) { + return control == GestureAction.NONE || mControls.contains(control); + } + +} diff --git a/cameraview/src/main/options/com/otaliastudios/cameraview/GestureAction.java b/cameraview/src/main/options/com/otaliastudios/cameraview/GestureAction.java index e615c042..c61b5592 100644 --- a/cameraview/src/main/options/com/otaliastudios/cameraview/GestureAction.java +++ b/cameraview/src/main/options/com/otaliastudios/cameraview/GestureAction.java @@ -1,14 +1,83 @@ package com.otaliastudios.cameraview; -import android.support.annotation.IntDef; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; +/** + * Gestures actions are actions over camera controls that can be mapped to certain gestures over + * the screen, using XML attributes or {@link CameraView#mapGesture(Gesture, GestureAction)}. + * + * Not every gesture can control a certain action. For example, pinch gestures can only control + * continuous values, such as zoom or AE correction. Single point gestures, on the other hand, + * can only control point actions such as focusing or capturing a picture. + */ +public enum GestureAction { -import static com.otaliastudios.cameraview.CameraConstants.*; + /** + * No action. This can be mapped to any gesture to disable it. + */ + NONE(0), -@Retention(RetentionPolicy.SOURCE) -@IntDef({GESTURE_ACTION_NONE, GESTURE_ACTION_FOCUS, GESTURE_ACTION_FOCUS_WITH_MARKER, - GESTURE_ACTION_AE_CORRECTION, GESTURE_ACTION_CAPTURE, GESTURE_ACTION_ZOOM}) -public @interface GestureAction { + /** + * Auto focus control, typically assigned to the tap gesture. + * This action can be mapped to: + * + * - {@link Gesture#TAP} + * - {@link Gesture#LONG_TAP} + */ + FOCUS(1), + + /** + * Auto focus control, typically assigned to the tap gesture. + * On top of {@link #FOCUS}, this will draw a default marker on screen. + * This action can be mapped to: + * + * - {@link Gesture#TAP} + * - {@link Gesture#LONG_TAP} + */ + FOCUS_WITH_MARKER(2), + + /** + * When triggered, this action will fire a picture shoot. + * This action can be mapped to: + * + * - {@link Gesture#TAP} + * - {@link Gesture#LONG_TAP} + */ + CAPTURE(3), + + /** + * Zoom control, typically assigned to the pinch gesture. + * This action can be mapped to: + * + * - {@link Gesture#PINCH} + */ + ZOOM(4), + + /** + * Exposure correction control. + * This action can be mapped to: + * + * - {@link Gesture#PINCH} + */ + EXPOSURE_CORRECTION(5); + + + private int value; + + GestureAction(int value) { + this.value = value; + } + + private int value() { + return value; + } + + static GestureAction fromValue(int value) { + GestureAction[] list = GestureAction.values(); + for (GestureAction action : list) { + if (action.value() == value) { + return action; + } + } + return null; + } }