From c967eeae68b0dfd42cfd7a03858c809eb4a21e61 Mon Sep 17 00:00:00 2001 From: Mattia Iavarone Date: Fri, 11 Aug 2017 16:22:50 +0200 Subject: [PATCH] Move facing values to enums --- README.md | 6 +-- .../com/otaliastudios/cameraview/Camera1.java | 12 ++--- .../com/otaliastudios/cameraview/Camera2.java | 3 +- .../cameraview/CameraConstants.java | 16 +++---- .../cameraview/CameraController.java | 7 ++- .../cameraview/CameraOptions.java | 12 ++--- .../otaliastudios/cameraview/CameraView.java | 28 ++++++------ .../com/otaliastudios/cameraview/Mapper.java | 34 +++++++------- .../com/otaliastudios/cameraview/Facing.java | 44 +++++++++++++++---- .../otaliastudios/cameraview/CameraUtils.java | 4 +- .../cameraview/demo/MainActivity.java | 5 ++- 11 files changed, 94 insertions(+), 77 deletions(-) diff --git a/README.md b/README.md index 38cc7e46..760c118a 100644 --- a/README.md +++ b/README.md @@ -319,8 +319,8 @@ cameraView.setSessionType(CameraConstants.SESSION_TYPE_VIDEO); Which camera to use, either back facing or front facing. ```java -cameraView.setFacing(CameraConstants.FACING_BACK); -cameraView.setFacing(CameraConstants.FACING_FRONT); +cameraView.setFacing(Facing.BACK); +cameraView.setFacing(Facing.FRONT); ``` #### cameraFlash @@ -399,7 +399,7 @@ Other APIs not mentioned above are provided, and are well documented and comment |`setZoom(float)`, `getZoom()`|Sets a zoom value, where 0 means camera zoomed out and 1 means zoomed in. No-op if zoom is not supported, or camera not started.| |`setExposureCorrection(float)`, `getExposureCorrection()`|Sets exposure compensation EV value, in camera stops. No-op if this is not supported. Should be between the bounds returned by CameraOptions.| |`setLocation(double, double)`|Sets latitude and longitude to be appended to picture/video metadata.| -|`toggleFacing()`|Toggles the facing value between `FACING_FRONT` and `FACING_BACK`.| +|`toggleFacing()`|Toggles the facing value between `Facing.FRONT` and `Facing.BACK`.| |`toggleFlash()`|Toggles the flash value between `FLASH_OFF`, `FLASH_ON`, and `FLASH_AUTO`.| |`startAutoFocus(float, float)`|Starts an autofocus process at the given coordinates, with respect to the view dimensions.| |`getPreviewSize()`|Returns the size of the preview surface. If CameraView was not constrained in its layout phase (e.g. it was `wrap_content`), this will return the same aspect ratio of CameraView.| diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java index f7b2253d..5827fea5 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java @@ -19,10 +19,6 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -import static com.otaliastudios.cameraview.CameraConstants.FOCUS_CONTINUOUS; -import static com.otaliastudios.cameraview.CameraConstants.FOCUS_FIXED; -import static com.otaliastudios.cameraview.CameraConstants.FOCUS_TAP; -import static com.otaliastudios.cameraview.CameraConstants.FOCUS_TAP_WITH_MARKER; import static com.otaliastudios.cameraview.CameraConstants.SESSION_TYPE_PICTURE; import static com.otaliastudios.cameraview.CameraConstants.SESSION_TYPE_VIDEO; @@ -244,7 +240,7 @@ class Camera1 extends CameraController { } @Override - void setFacing(@Facing int facing) { + void setFacing(Facing facing) { if (facing != mFacing) { mFacing = facing; if (collectCameraId() && isCameraOpened()) { @@ -467,7 +463,7 @@ class Camera1 extends CameraController { * It is meant to be fed to Camera.setDisplayOrientation(). */ private int computeSensorToDisplayOffset() { - if (mFacing == CameraConstants.FACING_FRONT) { + if (mFacing == Facing.FRONT) { // or: (360 - ((mSensorOffset + mDisplayOffset) % 360)) % 360; return ((mSensorOffset - mDisplayOffset) + 360 + 180) % 360; } else { @@ -481,7 +477,7 @@ class Camera1 extends CameraController { * This ignores flipping for facing camera. */ private int computeExifRotation() { - if (mFacing == CameraConstants.FACING_FRONT) { + if (mFacing == Facing.FRONT) { return (mSensorOffset - mDeviceOrientation + 360) % 360; } else { return (mSensorOffset + mDeviceOrientation) % 360; @@ -493,7 +489,7 @@ class Camera1 extends CameraController { * Whether the exif tag should include a 'flip' operation. */ private boolean computeExifFlip() { - return mFacing == CameraConstants.FACING_FRONT; + return mFacing == Facing.FRONT; } diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java index 35aa94b0..a30afbb3 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java @@ -27,7 +27,6 @@ class Camera2 extends CameraController { private CameraManager mCameraManager; private String mCameraId; - private int mFacing; private Size mCaptureSize; private Size mPreviewSize; @@ -100,7 +99,7 @@ class Camera2 extends CameraController { } @Override - void setFacing(@Facing int facing) { + void setFacing(Facing facing) { int internalFacing = mMapper.mapFacing(facing); final String[] ids; try { diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraConstants.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraConstants.java index de4a36b4..391f0cfb 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraConstants.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraConstants.java @@ -5,15 +5,15 @@ public class CameraConstants { public static final int PERMISSION_REQUEST_CODE = 16; - public static final int GESTURE_ACTION_NONE = 0; - public static final int GESTURE_ACTION_FOCUS = 1; - public static final int GESTURE_ACTION_FOCUS_WITH_MARKER = 2; - public static final int GESTURE_ACTION_CAPTURE = 3; - public static final int GESTURE_ACTION_ZOOM = 4; - public static final int GESTURE_ACTION_AE_CORRECTION = 5; + @Deprecated public static final int GESTURE_ACTION_NONE = 0; + @Deprecated public static final int GESTURE_ACTION_FOCUS = 1; + @Deprecated public static final int GESTURE_ACTION_FOCUS_WITH_MARKER = 2; + @Deprecated public static final int GESTURE_ACTION_CAPTURE = 3; + @Deprecated public static final int GESTURE_ACTION_ZOOM = 4; + @Deprecated public static final int GESTURE_ACTION_AE_CORRECTION = 5; - public static final int FACING_BACK = 0; - public static final int FACING_FRONT = 1; + @Deprecated public static final int FACING_BACK = 0; + @Deprecated public static final int FACING_FRONT = 1; public static final int FLASH_OFF = 0; public static final int FLASH_ON = 1; diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java index 56ef2f09..6214f0ef 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java @@ -11,7 +11,7 @@ abstract class CameraController implements Preview.SurfaceCallback { protected final CameraView.CameraCallbacks mCameraCallbacks; protected final Preview mPreview; - @Facing protected int mFacing; + protected Facing mFacing; @Flash protected int mFlash; @Focus protected int mFocus; @VideoQuality protected int mVideoQuality; @@ -32,7 +32,7 @@ abstract class CameraController implements Preview.SurfaceCallback { abstract boolean setZoom(float zoom); abstract boolean setExposureCorrection(float EVvalue); - abstract void setFacing(@Facing int facing); + abstract void setFacing(Facing facing); abstract void setFlash(@Flash int flash); abstract void setVideoQuality(@VideoQuality int videoQuality); abstract void setWhiteBalance(@WhiteBalance int whiteBalance); @@ -54,8 +54,7 @@ abstract class CameraController implements Preview.SurfaceCallback { @Nullable abstract ExtraProperties getExtraProperties(); @Nullable abstract CameraOptions getCameraOptions(); - @Facing - final int getFacing() { + final Facing getFacing() { return mFacing; } diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraOptions.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraOptions.java index c55ddec3..cda43d97 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraOptions.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraOptions.java @@ -16,7 +16,7 @@ import java.util.Set; public class CameraOptions { private Set supportedWhiteBalance = new HashSet<>(5); - private Set supportedFacing = new HashSet<>(2); + private Set supportedFacing = new HashSet<>(2); private Set supportedFlash = new HashSet<>(4); private boolean zoomSupported; @@ -37,7 +37,7 @@ public class CameraOptions { Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); for (int i = 0, count = Camera.getNumberOfCameras(); i < count; i++) { Camera.getCameraInfo(i, cameraInfo); - Integer value = mapper.unmapFacing(cameraInfo.facing); + Facing value = mapper.unmapFacing(cameraInfo.facing); if (value != null) supportedFacing.add(value); } @@ -80,12 +80,12 @@ public class CameraOptions { /** * Set of supported facing values. * - * @see CameraConstants#FACING_BACK - * @see CameraConstants#FACING_FRONT + * @see Facing#BACK + * @see Facing#FRONT * @return a set of supported values. */ @NonNull - public Set getSupportedFacing() { + public Set getSupportedFacing() { return supportedFacing; } @@ -145,7 +145,7 @@ public class CameraOptions { /** * Whether auto focus is supported. This means you can map gestures to - * {@link CameraConstants#GESTURE_ACTION_FOCUS} or {@link CameraConstants#GESTURE_ACTION_FOCUS_WITH_MARKER} + * {@link GestureAction#FOCUS} or {@link GestureAction#FOCUS_WITH_MARKER} * and focus will be changed on tap. * * @return whether auto focus is supported. diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java index 29137540..034413db 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java @@ -96,7 +96,7 @@ public class CameraView extends FrameLayout { @SuppressWarnings("WrongConstant") private void init(@NonNull Context context, @Nullable AttributeSet attrs) { TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CameraView, 0, 0); - int facing = a.getInteger(R.styleable.CameraView_cameraFacing, Defaults.DEFAULT_FACING); + Facing facing = Facing.fromValue(a.getInteger(R.styleable.CameraView_cameraFacing, Defaults.DEFAULT_FACING)); int flash = a.getInteger(R.styleable.CameraView_cameraFlash, Defaults.DEFAULT_FLASH); int sessionType = a.getInteger(R.styleable.CameraView_cameraSessionType, Defaults.DEFAULT_SESSION_TYPE); int whiteBalance = a.getInteger(R.styleable.CameraView_cameraWhiteBalance, Defaults.DEFAULT_WHITE_BALANCE); @@ -687,12 +687,12 @@ public class CameraView extends FrameLayout { /** * Sets which camera sensor should be used. * - * @see CameraConstants#FACING_FRONT - * @see CameraConstants#FACING_BACK + * @see Facing#FRONT + * @see Facing#BACK * * @param facing a facing value. */ - public void setFacing(@Facing final int facing) { + public void setFacing(final Facing facing) { run(new Runnable() { @Override public void run() { @@ -706,28 +706,26 @@ public class CameraView extends FrameLayout { * Gets the facing camera currently being used. * @return a facing value. */ - @Facing - public int getFacing() { + public Facing getFacing() { return mCameraController.getFacing(); } /** - * Toggles the facing value between {@link CameraConstants#FACING_BACK} - * and {@link CameraConstants#FACING_FRONT}. + * Toggles the facing value between {@link Facing#BACK} + * and {@link Facing#FRONT}. * * @return the new facing value */ - @Facing - public int toggleFacing() { - int facing = mCameraController.getFacing(); + public Facing toggleFacing() { + Facing facing = mCameraController.getFacing(); switch (facing) { - case FACING_BACK: - setFacing(FACING_FRONT); + case BACK: + setFacing(Facing.FRONT); break; - case FACING_FRONT: - setFacing(FACING_BACK); + case FRONT: + setFacing(Facing.BACK); break; } diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/Mapper.java b/cameraview/src/main/java/com/otaliastudios/cameraview/Mapper.java index f4d85b6f..a44b433b 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/Mapper.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/Mapper.java @@ -1,31 +1,30 @@ package com.otaliastudios.cameraview; import android.hardware.Camera; -import android.util.SparseArray; import java.util.HashMap; abstract class Mapper { abstract T mapFlash(@Flash int internalConstant); - abstract T mapFacing(@Facing int internalConstant); + abstract T mapFacing(Facing internalConstant); abstract T mapWhiteBalance(@WhiteBalance int internalConstant); @Flash abstract Integer unmapFlash(T cameraConstant); - @Facing abstract Integer unmapFacing(T cameraConstant); + abstract Facing unmapFacing(T cameraConstant); @WhiteBalance abstract Integer unmapWhiteBalance(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 FACING = new HashMap<>(); static { FLASH.put(CameraConstants.FLASH_OFF, Camera.Parameters.FLASH_MODE_OFF); FLASH.put(CameraConstants.FLASH_ON, Camera.Parameters.FLASH_MODE_ON); FLASH.put(CameraConstants.FLASH_AUTO, Camera.Parameters.FLASH_MODE_AUTO); FLASH.put(CameraConstants.FLASH_TORCH, Camera.Parameters.FLASH_MODE_TORCH); - FACING.put(CameraConstants.FACING_BACK, Camera.CameraInfo.CAMERA_FACING_BACK); - FACING.put(CameraConstants.FACING_FRONT, Camera.CameraInfo.CAMERA_FACING_FRONT); + FACING.put(Facing.BACK, Camera.CameraInfo.CAMERA_FACING_BACK); + FACING.put(Facing.FRONT, Camera.CameraInfo.CAMERA_FACING_FRONT); WB.put(CameraConstants.WHITE_BALANCE_AUTO, Camera.Parameters.WHITE_BALANCE_AUTO); WB.put(CameraConstants.WHITE_BALANCE_INCANDESCENT, Camera.Parameters.WHITE_BALANCE_INCANDESCENT); WB.put(CameraConstants.WHITE_BALANCE_FLUORESCENT, Camera.Parameters.WHITE_BALANCE_FLUORESCENT); @@ -39,7 +38,7 @@ abstract class Mapper { } @Override - T mapFacing(int internalConstant) { + T mapFacing(Facing internalConstant) { return (T) FACING.get(internalConstant); } @@ -48,8 +47,8 @@ abstract class Mapper { return (T) WB.get(internalConstant); } - private Integer reverseLookup(HashMap map, Object object) { - for (int value : map.keySet()) { + private T reverseLookup(HashMap map, Object object) { + for (T value : map.keySet()) { if (map.get(value).equals(object)) { return value; } @@ -63,7 +62,7 @@ abstract class Mapper { } @Override - Integer unmapFacing(T cameraConstant) { + Facing unmapFacing(T cameraConstant) { return reverseLookup(FACING, cameraConstant); } @@ -80,11 +79,6 @@ abstract class Mapper { return null; } - @Override - T mapFacing(@Facing int internalConstant) { - return null; - } - @Override T mapFlash(@Flash int internalConstant) { return null; @@ -96,15 +90,19 @@ abstract class Mapper { } @Override - Integer unmapFacing(T cameraConstant) { + Integer unmapWhiteBalance(T cameraConstant) { return 0; } @Override - Integer unmapWhiteBalance(T cameraConstant) { - return 0; + T mapFacing(Facing internalConstant) { + return null; } + @Override + Facing unmapFacing(T cameraConstant) { + return null; + } } } diff --git a/cameraview/src/main/options/com/otaliastudios/cameraview/Facing.java b/cameraview/src/main/options/com/otaliastudios/cameraview/Facing.java index 093b9d54..71b4967b 100644 --- a/cameraview/src/main/options/com/otaliastudios/cameraview/Facing.java +++ b/cameraview/src/main/options/com/otaliastudios/cameraview/Facing.java @@ -1,14 +1,40 @@ package com.otaliastudios.cameraview; -import android.support.annotation.IntDef; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; +/** + * Facing values indicates which camera sensor should be used for the current session. + * + * @see CameraView#setFacing(int) + */ +public enum Facing { -import static com.otaliastudios.cameraview.CameraConstants.FACING_BACK; -import static com.otaliastudios.cameraview.CameraConstants.FACING_FRONT; + /** + * Back-facing camera sensor. + */ + BACK(0), -@IntDef({FACING_BACK, FACING_FRONT}) -@Retention(RetentionPolicy.SOURCE) -public @interface Facing { -} \ No newline at end of file + /** + * Front-facing camera sensor. + */ + FRONT(1); + + private int value; + + Facing(int value) { + this.value = value; + } + + private int value() { + return value; + } + + static Facing fromValue(int value) { + Facing[] list = Facing.values(); + for (Facing action : list) { + if (action.value() == value) { + return action; + } + } + return null; + } +} diff --git a/cameraview/src/main/utils/com/otaliastudios/cameraview/CameraUtils.java b/cameraview/src/main/utils/com/otaliastudios/cameraview/CameraUtils.java index 313e22bf..bb2ba906 100644 --- a/cameraview/src/main/utils/com/otaliastudios/cameraview/CameraUtils.java +++ b/cameraview/src/main/utils/com/otaliastudios/cameraview/CameraUtils.java @@ -40,10 +40,10 @@ public class CameraUtils { * Facing value, so that a session can be started. * * @param context a valid context - * @param facing either {@link CameraConstants#FACING_BACK} or {@link CameraConstants#FACING_FRONT} + * @param facing either {@link Facing#BACK} or {@link Facing#FRONT} * @return true if such sensor exists */ - public static boolean hasCameraFacing(Context context, @Facing int facing) { + public static boolean hasCameraFacing(Context context, Facing facing) { int internal = new Mapper.Mapper1().mapFacing(facing); Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); for (int i = 0, count = Camera.getNumberOfCameras(); i < count; i++) { diff --git a/demo/src/main/java/com/otaliastudios/cameraview/demo/MainActivity.java b/demo/src/main/java/com/otaliastudios/cameraview/demo/MainActivity.java index 16ca25f9..52f54a12 100644 --- a/demo/src/main/java/com/otaliastudios/cameraview/demo/MainActivity.java +++ b/demo/src/main/java/com/otaliastudios/cameraview/demo/MainActivity.java @@ -16,6 +16,7 @@ import android.widget.Toast; import com.otaliastudios.cameraview.CameraConstants; import com.otaliastudios.cameraview.CameraListener; import com.otaliastudios.cameraview.CameraView; +import com.otaliastudios.cameraview.Facing; import com.otaliastudios.cameraview.Size; import java.io.File; @@ -187,11 +188,11 @@ public class MainActivity extends AppCompatActivity implements View.OnLayoutChan void toggleCamera() { if (mCapturingPicture) return; switch (camera.toggleFacing()) { - case CameraConstants.FACING_BACK: + case BACK: message("Switched to back camera!", false); break; - case CameraConstants.FACING_FRONT: + case FRONT: message("Switched to front camera!", false); break; }