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. 70
      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);
mOptions = new CameraOptions(params);
applyDefaultFocus(params);
mergeFlash(params, CameraConstants.Defaults.DEFAULT_FLASH);
mergeFlash(params, Flash.DEFAULT);
mergeLocation(params, 0d, 0d);
mergeWhiteBalance(params, CameraConstants.Defaults.DEFAULT_WHITE_BALANCE);
params.setRecordingHint(mSessionType == SESSION_TYPE_VIDEO);
@ -271,8 +271,8 @@ class Camera1 extends CameraController {
}
@Override
void setFlash(@Flash int flash) {
int old = mFlash;
void setFlash(Flash flash) {
Flash old = mFlash;
mFlash = flash;
if (isCameraOpened()) {
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)) {
params.setFlashMode((String) mMapper.mapFlash(mFlash));
return true;

@ -137,7 +137,7 @@ class Camera2 extends CameraController {
}
@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;
@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_3X3 = 1;
public static final int GRID_4X4 = 2;
@ -41,17 +31,7 @@ public class CameraConstants {
static class Defaults {
// 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_WHITE_BALANCE = WHITE_BALANCE_AUTO;
static final int DEFAULT_SESSION_TYPE = SESSION_TYPE_PICTURE;

@ -12,7 +12,7 @@ abstract class CameraController implements Preview.SurfaceCallback {
protected final Preview mPreview;
protected Facing mFacing;
@Flash protected int mFlash;
protected Flash mFlash;
@VideoQuality protected int mVideoQuality;
@WhiteBalance protected int mWhiteBalance;
@SessionType protected int mSessionType;
@ -32,7 +32,7 @@ abstract class CameraController implements Preview.SurfaceCallback {
abstract boolean setZoom(float zoom);
abstract boolean setExposureCorrection(float EVvalue);
abstract void setFacing(Facing facing);
abstract void setFlash(@Flash int flash);
abstract void setFlash(Flash flash);
abstract void setVideoQuality(@VideoQuality int videoQuality);
abstract void setWhiteBalance(@WhiteBalance int whiteBalance);
abstract void setSessionType(@SessionType int sessionType);
@ -56,9 +56,7 @@ abstract class CameraController implements Preview.SurfaceCallback {
final Facing getFacing() {
return mFacing;
}
@Flash
final int getFlash() {
final Flash getFlash() {
return mFlash;
}

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

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

@ -6,23 +6,23 @@ import java.util.HashMap;
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 mapWhiteBalance(@WhiteBalance int internalConstant);
@Flash abstract <T> Integer unmapFlash(T cameraConstant);
abstract <T> Flash unmapFlash(T cameraConstant);
abstract <T> Facing unmapFacing(T cameraConstant);
@WhiteBalance abstract <T> Integer unmapWhiteBalance(T cameraConstant);
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<Facing, Integer> 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);
FLASH.put(Flash.OFF, Camera.Parameters.FLASH_MODE_OFF);
FLASH.put(Flash.ON, Camera.Parameters.FLASH_MODE_ON);
FLASH.put(Flash.AUTO, Camera.Parameters.FLASH_MODE_AUTO);
FLASH.put(Flash.TORCH, Camera.Parameters.FLASH_MODE_TORCH);
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);
@ -33,7 +33,7 @@ abstract class Mapper {
}
@Override
<T> T mapFlash(int internalConstant) {
<T> T mapFlash(Flash internalConstant) {
return (T) FLASH.get(internalConstant);
}
@ -57,7 +57,7 @@ abstract class Mapper {
}
@Override
<T> Integer unmapFlash(T cameraConstant) {
<T> Flash unmapFlash(T cameraConstant) {
return reverseLookup(FLASH, cameraConstant);
}
@ -80,18 +80,18 @@ abstract class Mapper {
}
@Override
<T> T mapFlash(@Flash int internalConstant) {
<T> T mapFlash(Flash internalConstant) {
return null;
}
@Override
<T> Integer unmapFlash(T cameraConstant) {
return 0;
<T> Flash unmapFlash(T cameraConstant) {
return null;
}
@Override
<T> Integer unmapWhiteBalance(T cameraConstant) {
return 0;
return null;
}
@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)
*/
@ -18,13 +18,15 @@ public enum Facing {
*/
FRONT(1);
final static Facing DEFAULT = BACK;
private int value;
Facing(int value) {
this.value = value;
}
private int value() {
int value() {
return value;
}

@ -1,16 +1,64 @@
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;
import static com.otaliastudios.cameraview.CameraConstants.FLASH_ON;
import static com.otaliastudios.cameraview.CameraConstants.FLASH_TORCH;
/**
* Flash is always off.
*/
OFF(0),
@Retention(RetentionPolicy.SOURCE)
@IntDef({FLASH_OFF, FLASH_ON, FLASH_AUTO, FLASH_TORCH})
public @interface Flash {
}
/**
* Flash will be on when capturing.
* 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);
final static GestureAction DEFAULT_PINCH = NONE;
final static GestureAction DEFAULT_TAP = NONE;
final static GestureAction DEFAULT_LONG_TAP = NONE;
private int value;
GestureAction(int value) {
this.value = value;
}
private int value() {
int value() {
return value;
}

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

Loading…
Cancel
Save