Move facing values to enums

pull/1/head
Mattia Iavarone 7 years ago
parent a531f8e5dd
commit c967eeae68
  1. 6
      README.md
  2. 12
      cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java
  3. 3
      cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java
  4. 16
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraConstants.java
  5. 7
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java
  6. 12
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraOptions.java
  7. 28
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java
  8. 34
      cameraview/src/main/java/com/otaliastudios/cameraview/Mapper.java
  9. 42
      cameraview/src/main/options/com/otaliastudios/cameraview/Facing.java
  10. 4
      cameraview/src/main/utils/com/otaliastudios/cameraview/CameraUtils.java
  11. 5
      demo/src/main/java/com/otaliastudios/cameraview/demo/MainActivity.java

@ -319,8 +319,8 @@ cameraView.setSessionType(CameraConstants.SESSION_TYPE_VIDEO);
Which camera to use, either back facing or front facing. Which camera to use, either back facing or front facing.
```java ```java
cameraView.setFacing(CameraConstants.FACING_BACK); cameraView.setFacing(Facing.BACK);
cameraView.setFacing(CameraConstants.FACING_FRONT); cameraView.setFacing(Facing.FRONT);
``` ```
#### cameraFlash #### 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.| |`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.| |`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.| |`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`.| |`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.| |`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.| |`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.|

@ -19,10 +19,6 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; 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_PICTURE;
import static com.otaliastudios.cameraview.CameraConstants.SESSION_TYPE_VIDEO; import static com.otaliastudios.cameraview.CameraConstants.SESSION_TYPE_VIDEO;
@ -244,7 +240,7 @@ class Camera1 extends CameraController {
} }
@Override @Override
void setFacing(@Facing int facing) { void setFacing(Facing facing) {
if (facing != mFacing) { if (facing != mFacing) {
mFacing = facing; mFacing = facing;
if (collectCameraId() && isCameraOpened()) { if (collectCameraId() && isCameraOpened()) {
@ -467,7 +463,7 @@ class Camera1 extends CameraController {
* It is meant to be fed to Camera.setDisplayOrientation(). * It is meant to be fed to Camera.setDisplayOrientation().
*/ */
private int computeSensorToDisplayOffset() { private int computeSensorToDisplayOffset() {
if (mFacing == CameraConstants.FACING_FRONT) { if (mFacing == Facing.FRONT) {
// or: (360 - ((mSensorOffset + mDisplayOffset) % 360)) % 360; // or: (360 - ((mSensorOffset + mDisplayOffset) % 360)) % 360;
return ((mSensorOffset - mDisplayOffset) + 360 + 180) % 360; return ((mSensorOffset - mDisplayOffset) + 360 + 180) % 360;
} else { } else {
@ -481,7 +477,7 @@ class Camera1 extends CameraController {
* This ignores flipping for facing camera. * This ignores flipping for facing camera.
*/ */
private int computeExifRotation() { private int computeExifRotation() {
if (mFacing == CameraConstants.FACING_FRONT) { if (mFacing == Facing.FRONT) {
return (mSensorOffset - mDeviceOrientation + 360) % 360; return (mSensorOffset - mDeviceOrientation + 360) % 360;
} else { } else {
return (mSensorOffset + mDeviceOrientation) % 360; return (mSensorOffset + mDeviceOrientation) % 360;
@ -493,7 +489,7 @@ class Camera1 extends CameraController {
* Whether the exif tag should include a 'flip' operation. * Whether the exif tag should include a 'flip' operation.
*/ */
private boolean computeExifFlip() { private boolean computeExifFlip() {
return mFacing == CameraConstants.FACING_FRONT; return mFacing == Facing.FRONT;
} }

@ -27,7 +27,6 @@ class Camera2 extends CameraController {
private CameraManager mCameraManager; private CameraManager mCameraManager;
private String mCameraId; private String mCameraId;
private int mFacing;
private Size mCaptureSize; private Size mCaptureSize;
private Size mPreviewSize; private Size mPreviewSize;
@ -100,7 +99,7 @@ class Camera2 extends CameraController {
} }
@Override @Override
void setFacing(@Facing int facing) { void setFacing(Facing facing) {
int internalFacing = mMapper.mapFacing(facing); int internalFacing = mMapper.mapFacing(facing);
final String[] ids; final String[] ids;
try { try {

@ -5,15 +5,15 @@ public class CameraConstants {
public static final int PERMISSION_REQUEST_CODE = 16; public static final int PERMISSION_REQUEST_CODE = 16;
public static final int GESTURE_ACTION_NONE = 0; @Deprecated public static final int GESTURE_ACTION_NONE = 0;
public static final int GESTURE_ACTION_FOCUS = 1; @Deprecated public static final int GESTURE_ACTION_FOCUS = 1;
public static final int GESTURE_ACTION_FOCUS_WITH_MARKER = 2; @Deprecated public static final int GESTURE_ACTION_FOCUS_WITH_MARKER = 2;
public static final int GESTURE_ACTION_CAPTURE = 3; @Deprecated public static final int GESTURE_ACTION_CAPTURE = 3;
public static final int GESTURE_ACTION_ZOOM = 4; @Deprecated public static final int GESTURE_ACTION_ZOOM = 4;
public static final int GESTURE_ACTION_AE_CORRECTION = 5; @Deprecated public static final int GESTURE_ACTION_AE_CORRECTION = 5;
public static final int FACING_BACK = 0; @Deprecated public static final int FACING_BACK = 0;
public static final int FACING_FRONT = 1; @Deprecated public static final int FACING_FRONT = 1;
public static final int FLASH_OFF = 0; public static final int FLASH_OFF = 0;
public static final int FLASH_ON = 1; public static final int FLASH_ON = 1;

@ -11,7 +11,7 @@ abstract class CameraController implements Preview.SurfaceCallback {
protected final CameraView.CameraCallbacks mCameraCallbacks; protected final CameraView.CameraCallbacks mCameraCallbacks;
protected final Preview mPreview; protected final Preview mPreview;
@Facing protected int mFacing; protected Facing mFacing;
@Flash protected int mFlash; @Flash protected int mFlash;
@Focus protected int mFocus; @Focus protected int mFocus;
@VideoQuality protected int mVideoQuality; @VideoQuality protected int mVideoQuality;
@ -32,7 +32,7 @@ abstract class CameraController implements Preview.SurfaceCallback {
abstract boolean setZoom(float zoom); abstract boolean setZoom(float zoom);
abstract boolean setExposureCorrection(float EVvalue); abstract boolean setExposureCorrection(float EVvalue);
abstract void setFacing(@Facing int facing); abstract void setFacing(Facing facing);
abstract void setFlash(@Flash int flash); abstract void setFlash(@Flash int flash);
abstract void setVideoQuality(@VideoQuality int videoQuality); abstract void setVideoQuality(@VideoQuality int videoQuality);
abstract void setWhiteBalance(@WhiteBalance int whiteBalance); abstract void setWhiteBalance(@WhiteBalance int whiteBalance);
@ -54,8 +54,7 @@ abstract class CameraController implements Preview.SurfaceCallback {
@Nullable abstract ExtraProperties getExtraProperties(); @Nullable abstract ExtraProperties getExtraProperties();
@Nullable abstract CameraOptions getCameraOptions(); @Nullable abstract CameraOptions getCameraOptions();
@Facing final Facing getFacing() {
final int getFacing() {
return mFacing; return mFacing;
} }

@ -16,7 +16,7 @@ import java.util.Set;
public class CameraOptions { public class CameraOptions {
private Set<Integer> supportedWhiteBalance = new HashSet<>(5); private Set<Integer> supportedWhiteBalance = new HashSet<>(5);
private Set<Integer> supportedFacing = new HashSet<>(2); private Set<Facing> supportedFacing = new HashSet<>(2);
private Set<Integer> supportedFlash = new HashSet<>(4); private Set<Integer> supportedFlash = new HashSet<>(4);
private boolean zoomSupported; private boolean zoomSupported;
@ -37,7 +37,7 @@ public class CameraOptions {
Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
for (int i = 0, count = Camera.getNumberOfCameras(); i < count; i++) { for (int i = 0, count = Camera.getNumberOfCameras(); i < count; i++) {
Camera.getCameraInfo(i, cameraInfo); Camera.getCameraInfo(i, cameraInfo);
Integer value = mapper.unmapFacing(cameraInfo.facing); Facing value = mapper.unmapFacing(cameraInfo.facing);
if (value != null) supportedFacing.add(value); if (value != null) supportedFacing.add(value);
} }
@ -80,12 +80,12 @@ public class CameraOptions {
/** /**
* Set of supported facing values. * Set of supported facing values.
* *
* @see CameraConstants#FACING_BACK * @see Facing#BACK
* @see CameraConstants#FACING_FRONT * @see Facing#FRONT
* @return a set of supported values. * @return a set of supported values.
*/ */
@NonNull @NonNull
public Set<Integer> getSupportedFacing() { public Set<Facing> getSupportedFacing() {
return supportedFacing; return supportedFacing;
} }
@ -145,7 +145,7 @@ public class CameraOptions {
/** /**
* Whether auto focus is supported. This means you can map gestures to * 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. * and focus will be changed on tap.
* *
* @return whether auto focus is supported. * @return whether auto focus is supported.

@ -96,7 +96,7 @@ public class CameraView extends FrameLayout {
@SuppressWarnings("WrongConstant") @SuppressWarnings("WrongConstant")
private void init(@NonNull Context context, @Nullable AttributeSet attrs) { private void init(@NonNull Context context, @Nullable AttributeSet attrs) {
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CameraView, 0, 0); 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 flash = a.getInteger(R.styleable.CameraView_cameraFlash, Defaults.DEFAULT_FLASH);
int sessionType = a.getInteger(R.styleable.CameraView_cameraSessionType, Defaults.DEFAULT_SESSION_TYPE); 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 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. * Sets which camera sensor should be used.
* *
* @see CameraConstants#FACING_FRONT * @see Facing#FRONT
* @see CameraConstants#FACING_BACK * @see Facing#BACK
* *
* @param facing a facing value. * @param facing a facing value.
*/ */
public void setFacing(@Facing final int facing) { public void setFacing(final Facing facing) {
run(new Runnable() { run(new Runnable() {
@Override @Override
public void run() { public void run() {
@ -706,28 +706,26 @@ public class CameraView extends FrameLayout {
* Gets the facing camera currently being used. * Gets the facing camera currently being used.
* @return a facing value. * @return a facing value.
*/ */
@Facing public Facing getFacing() {
public int getFacing() {
return mCameraController.getFacing(); return mCameraController.getFacing();
} }
/** /**
* Toggles the facing value between {@link CameraConstants#FACING_BACK} * Toggles the facing value between {@link Facing#BACK}
* and {@link CameraConstants#FACING_FRONT}. * and {@link Facing#FRONT}.
* *
* @return the new facing value * @return the new facing value
*/ */
@Facing public Facing toggleFacing() {
public int toggleFacing() { Facing facing = mCameraController.getFacing();
int facing = mCameraController.getFacing();
switch (facing) { switch (facing) {
case FACING_BACK: case BACK:
setFacing(FACING_FRONT); setFacing(Facing.FRONT);
break; break;
case FACING_FRONT: case FRONT:
setFacing(FACING_BACK); setFacing(Facing.BACK);
break; break;
} }

@ -1,31 +1,30 @@
package com.otaliastudios.cameraview; package com.otaliastudios.cameraview;
import android.hardware.Camera; import android.hardware.Camera;
import android.util.SparseArray;
import java.util.HashMap; import java.util.HashMap;
abstract class Mapper { abstract class Mapper {
abstract <T> T mapFlash(@Flash int internalConstant); abstract <T> T mapFlash(@Flash int internalConstant);
abstract <T> T mapFacing(@Facing int internalConstant); abstract <T> T mapFacing(Facing internalConstant);
abstract <T> T mapWhiteBalance(@WhiteBalance int internalConstant); abstract <T> T mapWhiteBalance(@WhiteBalance int internalConstant);
@Flash abstract <T> Integer unmapFlash(T cameraConstant); @Flash abstract <T> Integer unmapFlash(T cameraConstant);
@Facing abstract <T> Integer unmapFacing(T cameraConstant); abstract <T> Facing unmapFacing(T cameraConstant);
@WhiteBalance abstract <T> Integer unmapWhiteBalance(T cameraConstant); @WhiteBalance abstract <T> Integer unmapWhiteBalance(T cameraConstant);
static class Mapper1 extends Mapper { static class Mapper1 extends Mapper {
private static final HashMap<Integer, String> FLASH = new HashMap<>(); private static final HashMap<Integer, String> FLASH = new HashMap<>();
private static final HashMap<Integer, String> WB = new HashMap<>(); private static final HashMap<Integer, String> WB = new HashMap<>();
private static final HashMap<Integer, Integer> FACING = new HashMap<>(); private static final HashMap<Facing, Integer> FACING = new HashMap<>();
static { static {
FLASH.put(CameraConstants.FLASH_OFF, Camera.Parameters.FLASH_MODE_OFF); FLASH.put(CameraConstants.FLASH_OFF, Camera.Parameters.FLASH_MODE_OFF);
FLASH.put(CameraConstants.FLASH_ON, Camera.Parameters.FLASH_MODE_ON); FLASH.put(CameraConstants.FLASH_ON, Camera.Parameters.FLASH_MODE_ON);
FLASH.put(CameraConstants.FLASH_AUTO, Camera.Parameters.FLASH_MODE_AUTO); FLASH.put(CameraConstants.FLASH_AUTO, Camera.Parameters.FLASH_MODE_AUTO);
FLASH.put(CameraConstants.FLASH_TORCH, Camera.Parameters.FLASH_MODE_TORCH); FLASH.put(CameraConstants.FLASH_TORCH, Camera.Parameters.FLASH_MODE_TORCH);
FACING.put(CameraConstants.FACING_BACK, Camera.CameraInfo.CAMERA_FACING_BACK); FACING.put(Facing.BACK, Camera.CameraInfo.CAMERA_FACING_BACK);
FACING.put(CameraConstants.FACING_FRONT, Camera.CameraInfo.CAMERA_FACING_FRONT); 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_AUTO, Camera.Parameters.WHITE_BALANCE_AUTO);
WB.put(CameraConstants.WHITE_BALANCE_INCANDESCENT, Camera.Parameters.WHITE_BALANCE_INCANDESCENT); WB.put(CameraConstants.WHITE_BALANCE_INCANDESCENT, Camera.Parameters.WHITE_BALANCE_INCANDESCENT);
WB.put(CameraConstants.WHITE_BALANCE_FLUORESCENT, Camera.Parameters.WHITE_BALANCE_FLUORESCENT); WB.put(CameraConstants.WHITE_BALANCE_FLUORESCENT, Camera.Parameters.WHITE_BALANCE_FLUORESCENT);
@ -39,7 +38,7 @@ abstract class Mapper {
} }
@Override @Override
<T> T mapFacing(int internalConstant) { <T> T mapFacing(Facing internalConstant) {
return (T) FACING.get(internalConstant); return (T) FACING.get(internalConstant);
} }
@ -48,8 +47,8 @@ abstract class Mapper {
return (T) WB.get(internalConstant); return (T) WB.get(internalConstant);
} }
private Integer reverseLookup(HashMap<Integer, ?> map, Object object) { private <T> T reverseLookup(HashMap<T, ?> map, Object object) {
for (int value : map.keySet()) { for (T value : map.keySet()) {
if (map.get(value).equals(object)) { if (map.get(value).equals(object)) {
return value; return value;
} }
@ -63,7 +62,7 @@ abstract class Mapper {
} }
@Override @Override
<T> Integer unmapFacing(T cameraConstant) { <T> Facing unmapFacing(T cameraConstant) {
return reverseLookup(FACING, cameraConstant); return reverseLookup(FACING, cameraConstant);
} }
@ -80,11 +79,6 @@ abstract class Mapper {
return null; return null;
} }
@Override
<T> T mapFacing(@Facing int internalConstant) {
return null;
}
@Override @Override
<T> T mapFlash(@Flash int internalConstant) { <T> T mapFlash(@Flash int internalConstant) {
return null; return null;
@ -96,15 +90,19 @@ abstract class Mapper {
} }
@Override @Override
<T> Integer unmapFacing(T cameraConstant) { <T> Integer unmapWhiteBalance(T cameraConstant) {
return 0; return 0;
} }
@Override @Override
<T> Integer unmapWhiteBalance(T cameraConstant) { <T> T mapFacing(Facing internalConstant) {
return 0; return null;
} }
@Override
<T> Facing unmapFacing(T cameraConstant) {
return null;
}
} }
} }

@ -1,14 +1,40 @@
package com.otaliastudios.cameraview; 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) * Front-facing camera sensor.
public @interface Facing { */
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;
}
} }

@ -40,10 +40,10 @@ public class CameraUtils {
* Facing value, so that a session can be started. * Facing value, so that a session can be started.
* *
* @param context a valid context * @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 * @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); int internal = new Mapper.Mapper1().mapFacing(facing);
Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
for (int i = 0, count = Camera.getNumberOfCameras(); i < count; i++) { for (int i = 0, count = Camera.getNumberOfCameras(); i < count; i++) {

@ -16,6 +16,7 @@ import android.widget.Toast;
import com.otaliastudios.cameraview.CameraConstants; import com.otaliastudios.cameraview.CameraConstants;
import com.otaliastudios.cameraview.CameraListener; import com.otaliastudios.cameraview.CameraListener;
import com.otaliastudios.cameraview.CameraView; import com.otaliastudios.cameraview.CameraView;
import com.otaliastudios.cameraview.Facing;
import com.otaliastudios.cameraview.Size; import com.otaliastudios.cameraview.Size;
import java.io.File; import java.io.File;
@ -187,11 +188,11 @@ public class MainActivity extends AppCompatActivity implements View.OnLayoutChan
void toggleCamera() { void toggleCamera() {
if (mCapturingPicture) return; if (mCapturingPicture) return;
switch (camera.toggleFacing()) { switch (camera.toggleFacing()) {
case CameraConstants.FACING_BACK: case BACK:
message("Switched to back camera!", false); message("Switched to back camera!", false);
break; break;
case CameraConstants.FACING_FRONT: case FRONT:
message("Switched to front camera!", false); message("Switched to front camera!", false);
break; break;
} }

Loading…
Cancel
Save