Moved Flash to enums

pull/1/head
Mattia Iavarone 7 years ago
parent cb53602daf
commit c122fbe80e
  1. 8
      cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java
  2. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java
  3. 20
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraConstants.java
  4. 8
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java
  5. 14
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraOptions.java
  6. 57
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java
  7. 26
      cameraview/src/main/java/com/otaliastudios/cameraview/Mapper.java
  8. 6
      cameraview/src/main/options/com/otaliastudios/cameraview/Facing.java
  9. 68
      cameraview/src/main/options/com/otaliastudios/cameraview/Flash.java
  10. 6
      cameraview/src/main/options/com/otaliastudios/cameraview/GestureAction.java
  11. 6
      demo/src/main/java/com/otaliastudios/cameraview/demo/MainActivity.java

@ -143,7 +143,7 @@ class Camera1 extends CameraController {
mExtraProperties = new ExtraProperties(params); mExtraProperties = new ExtraProperties(params);
mOptions = new CameraOptions(params); mOptions = new CameraOptions(params);
applyDefaultFocus(params); applyDefaultFocus(params);
mergeFlash(params, CameraConstants.Defaults.DEFAULT_FLASH); mergeFlash(params, Flash.DEFAULT);
mergeLocation(params, 0d, 0d); mergeLocation(params, 0d, 0d);
mergeWhiteBalance(params, CameraConstants.Defaults.DEFAULT_WHITE_BALANCE); mergeWhiteBalance(params, CameraConstants.Defaults.DEFAULT_WHITE_BALANCE);
params.setRecordingHint(mSessionType == SESSION_TYPE_VIDEO); params.setRecordingHint(mSessionType == SESSION_TYPE_VIDEO);
@ -271,8 +271,8 @@ class Camera1 extends CameraController {
} }
@Override @Override
void setFlash(@Flash int flash) { void setFlash(Flash flash) {
int old = mFlash; Flash old = mFlash;
mFlash = flash; mFlash = flash;
if (isCameraOpened()) { if (isCameraOpened()) {
synchronized (mLock) { synchronized (mLock) {
@ -283,7 +283,7 @@ class Camera1 extends CameraController {
} }
private boolean mergeFlash(Camera.Parameters params, @Flash int oldFlash) { private boolean mergeFlash(Camera.Parameters params, Flash oldFlash) {
if (mOptions.getSupportedFlash().contains(mFlash)) { if (mOptions.getSupportedFlash().contains(mFlash)) {
params.setFlashMode((String) mMapper.mapFlash(mFlash)); params.setFlashMode((String) mMapper.mapFlash(mFlash));
return true; return true;

@ -137,7 +137,7 @@ class Camera2 extends CameraController {
} }
@Override @Override
void setFlash(@Flash int flash) { void setFlash(Flash flash) {
} }

@ -5,16 +5,6 @@ public class CameraConstants {
public static final int PERMISSION_REQUEST_CODE = 16; public static final int PERMISSION_REQUEST_CODE = 16;
@Deprecated public static final int GESTURE_ACTION_NONE = 0;
@Deprecated public static final int FACING_BACK = 0;
@Deprecated public static final int FOCUS_CONTINUOUS = 1;
@Deprecated public static final int ZOOM_OFF = 0;
public static final int FLASH_OFF = 0;
public static final int FLASH_ON = 1;
public static final int FLASH_AUTO = 2;
public static final int FLASH_TORCH = 3;
public static final int GRID_OFF = 0; public static final int GRID_OFF = 0;
public static final int GRID_3X3 = 1; public static final int GRID_3X3 = 1;
public static final int GRID_4X4 = 2; public static final int GRID_4X4 = 2;
@ -41,17 +31,7 @@ public class CameraConstants {
static class Defaults { static class Defaults {
// TODO: Move to enum classes // TODO: Move to enum classes
static final int DEFAULT_GESTURE_ACTION_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;
// Deprecated
@Deprecated static final int DEFAULT_FOCUS = FOCUS_CONTINUOUS;
@Deprecated static final int DEFAULT_ZOOM = ZOOM_OFF;
// OK
static final int DEFAULT_FLASH = FLASH_OFF;
static final int DEFAULT_VIDEO_QUALITY = VIDEO_QUALITY_480P; static final int DEFAULT_VIDEO_QUALITY = VIDEO_QUALITY_480P;
static final int DEFAULT_WHITE_BALANCE = WHITE_BALANCE_AUTO; static final int DEFAULT_WHITE_BALANCE = WHITE_BALANCE_AUTO;
static final int DEFAULT_SESSION_TYPE = SESSION_TYPE_PICTURE; static final int DEFAULT_SESSION_TYPE = SESSION_TYPE_PICTURE;

@ -12,7 +12,7 @@ abstract class CameraController implements Preview.SurfaceCallback {
protected final Preview mPreview; protected final Preview mPreview;
protected Facing mFacing; protected Facing mFacing;
@Flash protected int mFlash; protected Flash mFlash;
@VideoQuality protected int mVideoQuality; @VideoQuality protected int mVideoQuality;
@WhiteBalance protected int mWhiteBalance; @WhiteBalance protected int mWhiteBalance;
@SessionType protected int mSessionType; @SessionType protected int mSessionType;
@ -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 facing); abstract void setFacing(Facing facing);
abstract void setFlash(@Flash int flash); abstract void setFlash(Flash 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);
abstract void setSessionType(@SessionType int sessionType); abstract void setSessionType(@SessionType int sessionType);
@ -56,9 +56,7 @@ abstract class CameraController implements Preview.SurfaceCallback {
final Facing getFacing() { final Facing getFacing() {
return mFacing; return mFacing;
} }
final Flash getFlash() {
@Flash
final int getFlash() {
return mFlash; return mFlash;
} }

@ -17,7 +17,7 @@ public class CameraOptions {
private Set<Integer> supportedWhiteBalance = new HashSet<>(5); private Set<Integer> supportedWhiteBalance = new HashSet<>(5);
private Set<Facing> supportedFacing = new HashSet<>(2); private Set<Facing> supportedFacing = new HashSet<>(2);
private Set<Integer> supportedFlash = new HashSet<>(4); private Set<Flash> supportedFlash = new HashSet<>(4);
private boolean zoomSupported; private boolean zoomSupported;
private boolean videoSnapshotSupported; private boolean videoSnapshotSupported;
@ -54,7 +54,7 @@ public class CameraOptions {
strings = params.getSupportedFlashModes(); strings = params.getSupportedFlashModes();
if (strings != null) { if (strings != null) {
for (String string : strings) { for (String string : strings) {
Integer value = mapper.unmapFlash(string); Flash value = mapper.unmapFlash(string);
if (value != null) supportedFlash.add(value); if (value != null) supportedFlash.add(value);
} }
} }
@ -93,14 +93,14 @@ public class CameraOptions {
/** /**
* Set of supported flash values. * Set of supported flash values.
* *
* @see CameraConstants#FLASH_AUTO * @see Flash#AUTO
* @see CameraConstants#FLASH_OFF * @see Flash#OFF
* @see CameraConstants#FLASH_ON * @see Flash#ON
* @see CameraConstants#FLASH_TORCH * @see Flash#TORCH
* @return a set of supported values. * @return a set of supported values.
*/ */
@NonNull @NonNull
public Set<Integer> getSupportedFlash() { public Set<Flash> getSupportedFlash() {
return supportedFlash; return supportedFlash;
} }

@ -38,11 +38,7 @@ import static android.view.View.MeasureSpec.UNSPECIFIED;
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
/**
* TODO: README for gestures
* TODO: deprecate setFocus, CONTINUOUS should be the default
*
*/
public class CameraView extends FrameLayout { public class CameraView extends FrameLayout {
private final static String TAG = CameraView.class.getSimpleName(); private final static String TAG = CameraView.class.getSimpleName();
@ -96,18 +92,17 @@ 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);
Facing facing = Facing.fromValue(a.getInteger(R.styleable.CameraView_cameraFacing, Defaults.DEFAULT_FACING)); Facing facing = Facing.fromValue(a.getInteger(R.styleable.CameraView_cameraFacing, Facing.DEFAULT.value()));
int flash = a.getInteger(R.styleable.CameraView_cameraFlash, Defaults.DEFAULT_FLASH); Flash flash = Flash.fromValue(a.getInteger(R.styleable.CameraView_cameraFlash, Flash.DEFAULT.value()));
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);
int videoQuality = a.getInteger(R.styleable.CameraView_cameraVideoQuality, Defaults.DEFAULT_VIDEO_QUALITY); int videoQuality = a.getInteger(R.styleable.CameraView_cameraVideoQuality, Defaults.DEFAULT_VIDEO_QUALITY);
int grid = a.getInteger(R.styleable.CameraView_cameraGrid, Defaults.DEFAULT_GRID); int grid = a.getInteger(R.styleable.CameraView_cameraGrid, Defaults.DEFAULT_GRID);
mJpegQuality = a.getInteger(R.styleable.CameraView_cameraJpegQuality, Defaults.DEFAULT_JPEG_QUALITY); mJpegQuality = a.getInteger(R.styleable.CameraView_cameraJpegQuality, Defaults.DEFAULT_JPEG_QUALITY);
mCropOutput = a.getBoolean(R.styleable.CameraView_cameraCropOutput, Defaults.DEFAULT_CROP_OUTPUT); mCropOutput = a.getBoolean(R.styleable.CameraView_cameraCropOutput, Defaults.DEFAULT_CROP_OUTPUT);
GestureAction tapGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGestureTap, Defaults.DEFAULT_GESTURE_ACTION_TAP)); GestureAction tapGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGestureTap, GestureAction.DEFAULT_TAP.value()));
// int doubleTapGesture = a.getInteger(R.styleable.CameraView_cameraGestureDoubleTap, Defaults.DEFAULT_GESTURE_ACTION_DOUBLE_TAP); GestureAction longTapGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGestureLongTap, GestureAction.DEFAULT_LONG_TAP.value()));
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, GestureAction.DEFAULT_PINCH.value()));
GestureAction pinchGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGesturePinch, Defaults.DEFAULT_GESTURE_ACTION_PINCH));
a.recycle(); a.recycle();
mCameraCallbacks = new CameraCallbacks(); mCameraCallbacks = new CameraCallbacks();
@ -736,14 +731,14 @@ public class CameraView extends FrameLayout {
/** /**
* Sets the flash mode. * Sets the flash mode.
* *
* @see CameraConstants#FLASH_OFF * @see Flash#OFF
* @see CameraConstants#FLASH_ON * @see Flash#ON
* @see CameraConstants#FLASH_AUTO * @see Flash#AUTO
* @see CameraConstants#FLASH_TORCH * @see Flash#TORCH
* @param flash desired flash mode. * @param flash desired flash mode.
*/ */
public void setFlash(@Flash int flash) { public void setFlash(Flash flash) {
mCameraController.setFlash(flash); mCameraController.setFlash(flash);
} }
@ -752,33 +747,31 @@ public class CameraView extends FrameLayout {
* Gets the current flash mode. * Gets the current flash mode.
* @return a flash mode * @return a flash mode
*/ */
@Flash public Flash getFlash() {
public int getFlash() {
return mCameraController.getFlash(); return mCameraController.getFlash();
} }
/** /**
* Toggles the flash mode between {@link CameraConstants#FLASH_OFF}, * Toggles the flash mode between {@link Flash#OFF},
* {@link CameraConstants#FLASH_ON} and {@link CameraConstants#FLASH_AUTO}, in this order. * {@link Flash#ON} and {@link Flash#AUTO}, in this order.
* *
* @return the new flash value * @return the new flash value
*/ */
@Flash public Flash toggleFlash() {
public int toggleFlash() { Flash flash = mCameraController.getFlash();
int flash = mCameraController.getFlash();
switch (flash) { switch (flash) {
case FLASH_OFF: case OFF:
setFlash(FLASH_ON); setFlash(Flash.ON);
break; break;
case FLASH_ON: case ON:
setFlash(FLASH_AUTO); setFlash(Flash.AUTO);
break; break;
case FLASH_AUTO: case AUTO:
case FLASH_TORCH: case TORCH:
setFlash(FLASH_OFF); setFlash(Flash.OFF);
break; break;
} }
@ -1325,7 +1318,7 @@ public class CameraView extends FrameLayout {
*/ */
@Deprecated @Deprecated
public int getFocus() { public int getFocus() {
return Defaults.DEFAULT_FOCUS; return 0;
} }
@ -1365,7 +1358,7 @@ public class CameraView extends FrameLayout {
*/ */
@Deprecated @Deprecated
public int getZoomMode() { public int getZoomMode() {
return Defaults.DEFAULT_ZOOM; return 0;
} }
} }

@ -6,23 +6,23 @@ import java.util.HashMap;
abstract class Mapper { abstract class Mapper {
abstract <T> T mapFlash(@Flash int internalConstant); abstract <T> T mapFlash(Flash internalConstant);
abstract <T> T mapFacing(Facing 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); abstract <T> Flash unmapFlash(T cameraConstant);
abstract <T> Facing 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<Flash, 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<Facing, 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(Flash.OFF, Camera.Parameters.FLASH_MODE_OFF);
FLASH.put(CameraConstants.FLASH_ON, Camera.Parameters.FLASH_MODE_ON); FLASH.put(Flash.ON, Camera.Parameters.FLASH_MODE_ON);
FLASH.put(CameraConstants.FLASH_AUTO, Camera.Parameters.FLASH_MODE_AUTO); FLASH.put(Flash.AUTO, Camera.Parameters.FLASH_MODE_AUTO);
FLASH.put(CameraConstants.FLASH_TORCH, Camera.Parameters.FLASH_MODE_TORCH); FLASH.put(Flash.TORCH, Camera.Parameters.FLASH_MODE_TORCH);
FACING.put(Facing.BACK, Camera.CameraInfo.CAMERA_FACING_BACK); FACING.put(Facing.BACK, Camera.CameraInfo.CAMERA_FACING_BACK);
FACING.put(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);
@ -33,7 +33,7 @@ abstract class Mapper {
} }
@Override @Override
<T> T mapFlash(int internalConstant) { <T> T mapFlash(Flash internalConstant) {
return (T) FLASH.get(internalConstant); return (T) FLASH.get(internalConstant);
} }
@ -57,7 +57,7 @@ abstract class Mapper {
} }
@Override @Override
<T> Integer unmapFlash(T cameraConstant) { <T> Flash unmapFlash(T cameraConstant) {
return reverseLookup(FLASH, cameraConstant); return reverseLookup(FLASH, cameraConstant);
} }
@ -80,18 +80,18 @@ abstract class Mapper {
} }
@Override @Override
<T> T mapFlash(@Flash int internalConstant) { <T> T mapFlash(Flash internalConstant) {
return null; return null;
} }
@Override @Override
<T> Integer unmapFlash(T cameraConstant) { <T> Flash unmapFlash(T cameraConstant) {
return 0; return null;
} }
@Override @Override
<T> Integer unmapWhiteBalance(T cameraConstant) { <T> Integer unmapWhiteBalance(T cameraConstant) {
return 0; return null;
} }
@Override @Override

@ -2,7 +2,7 @@ package com.otaliastudios.cameraview;
/** /**
* Facing values indicates which camera sensor should be used for the current session. * Facing value indicates which camera sensor should be used for the current session.
* *
* @see CameraView#setFacing(int) * @see CameraView#setFacing(int)
*/ */
@ -18,13 +18,15 @@ public enum Facing {
*/ */
FRONT(1); FRONT(1);
final static Facing DEFAULT = BACK;
private int value; private int value;
Facing(int value) { Facing(int value) {
this.value = value; this.value = value;
} }
private int value() { int value() {
return value; return value;
} }

@ -1,16 +1,64 @@
package com.otaliastudios.cameraview; package com.otaliastudios.cameraview;
import android.support.annotation.IntDef;
import java.lang.annotation.Retention; /**
import java.lang.annotation.RetentionPolicy; * Flash value indicates the flash mode to be used.
*
* @see CameraView#setFlash(int)
*/
public enum Flash {
import static com.otaliastudios.cameraview.CameraConstants.FLASH_AUTO; /**
import static com.otaliastudios.cameraview.CameraConstants.FLASH_OFF; * Flash is always off.
import static com.otaliastudios.cameraview.CameraConstants.FLASH_ON; */
import static com.otaliastudios.cameraview.CameraConstants.FLASH_TORCH; OFF(0),
@Retention(RetentionPolicy.SOURCE) /**
@IntDef({FLASH_OFF, FLASH_ON, FLASH_AUTO, FLASH_TORCH}) * Flash will be on when capturing.
public @interface Flash { * This is not guaranteed to be supported.
*
* @see CameraOptions#getSupportedFlash()
*/
ON(1),
/**
* Flash mode is chosen by the camera.
* This is not guaranteed to be supported.
*
* @see CameraOptions#getSupportedFlash()
*/
AUTO(2),
/**
* TODO: is this true?
* Flash is always on, working as a torch.
* This is not guaranteed to be supported.
*
* @see CameraOptions#getSupportedFlash()
*/
TORCH(3);
static final Flash DEFAULT = OFF;
private int value;
Flash(int value) {
this.value = value;
}
int value() {
return value;
}
static Flash fromValue(int value) {
Flash[] list = Flash.values();
for (Flash action : list) {
if (action.value() == value) {
return action;
}
}
return null;
}
} }

@ -61,13 +61,17 @@ public enum GestureAction {
EXPOSURE_CORRECTION(5); EXPOSURE_CORRECTION(5);
final static GestureAction DEFAULT_PINCH = NONE;
final static GestureAction DEFAULT_TAP = NONE;
final static GestureAction DEFAULT_LONG_TAP = NONE;
private int value; private int value;
GestureAction(int value) { GestureAction(int value) {
this.value = value; this.value = value;
} }
private int value() { int value() {
return value; return value;
} }

@ -197,15 +197,15 @@ public class MainActivity extends AppCompatActivity implements View.OnLayoutChan
void toggleFlash() { void toggleFlash() {
if (mCapturingPicture) return; if (mCapturingPicture) return;
switch (camera.toggleFlash()) { switch (camera.toggleFlash()) {
case CameraConstants.FLASH_ON: case ON:
message("Flash on!", false); message("Flash on!", false);
break; break;
case CameraConstants.FLASH_OFF: case OFF:
message("Flash off!", false); message("Flash off!", false);
break; break;
case CameraConstants.FLASH_AUTO: case AUTO:
message("Flash auto!", false); message("Flash auto!", false);
break; break;
} }

Loading…
Cancel
Save