Moved WhiteBalance to enums

pull/1/head
Mattia Iavarone 7 years ago
parent 776238a5df
commit 324f0c8a47
  1. 8
      cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java
  2. 2
      cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java
  3. 10
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraConstants.java
  4. 18
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java
  5. 16
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraOptions.java
  6. 17
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java
  7. 25
      cameraview/src/main/java/com/otaliastudios/cameraview/Mapper.java
  8. 79
      cameraview/src/main/options/com/otaliastudios/cameraview/WhiteBalance.java

@ -145,7 +145,7 @@ class Camera1 extends CameraController {
applyDefaultFocus(params); applyDefaultFocus(params);
mergeFlash(params, Flash.DEFAULT); mergeFlash(params, Flash.DEFAULT);
mergeLocation(params, 0d, 0d); mergeLocation(params, 0d, 0d);
mergeWhiteBalance(params, CameraConstants.Defaults.DEFAULT_WHITE_BALANCE); mergeWhiteBalance(params, WhiteBalance.DEFAULT);
params.setRecordingHint(mSessionType == SESSION_TYPE_VIDEO); params.setRecordingHint(mSessionType == SESSION_TYPE_VIDEO);
mCamera.setParameters(params); mCamera.setParameters(params);
} }
@ -250,8 +250,8 @@ class Camera1 extends CameraController {
} }
@Override @Override
void setWhiteBalance(@WhiteBalance int whiteBalance) { void setWhiteBalance(WhiteBalance whiteBalance) {
int old = mWhiteBalance; WhiteBalance old = mWhiteBalance;
mWhiteBalance = whiteBalance; mWhiteBalance = whiteBalance;
if (isCameraOpened()) { if (isCameraOpened()) {
synchronized (mLock) { synchronized (mLock) {
@ -261,7 +261,7 @@ class Camera1 extends CameraController {
} }
} }
private boolean mergeWhiteBalance(Camera.Parameters params, @WhiteBalance int oldWhiteBalance) { private boolean mergeWhiteBalance(Camera.Parameters params, WhiteBalance oldWhiteBalance) {
if (mOptions.getSupportedWhiteBalance().contains(mWhiteBalance)) { if (mOptions.getSupportedWhiteBalance().contains(mWhiteBalance)) {
params.setWhiteBalance((String) mMapper.mapWhiteBalance(mWhiteBalance)); params.setWhiteBalance((String) mMapper.mapWhiteBalance(mWhiteBalance));
return true; return true;

@ -152,7 +152,7 @@ class Camera2 extends CameraController {
} }
@Override @Override
void setWhiteBalance(@WhiteBalance int whiteBalance) { void setWhiteBalance(WhiteBalance whiteBalance) {
} }

@ -1,6 +1,8 @@
package com.otaliastudios.cameraview; package com.otaliastudios.cameraview;
import android.hardware.Camera;
public class CameraConstants { public class CameraConstants {
public static final int PERMISSION_REQUEST_CODE = 16; public static final int PERMISSION_REQUEST_CODE = 16;
@ -16,17 +18,9 @@ public class CameraConstants {
public static final int VIDEO_QUALITY_LOWEST = 5; public static final int VIDEO_QUALITY_LOWEST = 5;
public static final int VIDEO_QUALITY_QVGA = 6; public static final int VIDEO_QUALITY_QVGA = 6;
public static final int WHITE_BALANCE_AUTO = 0;
public static final int WHITE_BALANCE_INCANDESCENT = 1;
public static final int WHITE_BALANCE_FLUORESCENT = 2;
public static final int WHITE_BALANCE_DAYLIGHT = 3;
public static final int WHITE_BALANCE_CLOUDY = 4;
static class Defaults { static class Defaults {
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_SESSION_TYPE = SESSION_TYPE_PICTURE; static final int DEFAULT_SESSION_TYPE = SESSION_TYPE_PICTURE;
static final int DEFAULT_JPEG_QUALITY = 100; static final int DEFAULT_JPEG_QUALITY = 100;
static final boolean DEFAULT_CROP_OUTPUT = false; static final boolean DEFAULT_CROP_OUTPUT = false;

@ -13,8 +13,8 @@ abstract class CameraController implements Preview.SurfaceCallback {
protected Facing mFacing; protected Facing mFacing;
protected Flash mFlash; protected Flash mFlash;
protected WhiteBalance mWhiteBalance;
@VideoQuality protected int mVideoQuality; @VideoQuality protected int mVideoQuality;
@WhiteBalance protected int mWhiteBalance;
@SessionType protected int mSessionType; @SessionType protected int mSessionType;
CameraController(CameraView.CameraCallbacks callback, Preview preview) { CameraController(CameraView.CameraCallbacks callback, Preview preview) {
@ -33,8 +33,8 @@ abstract class CameraController implements Preview.SurfaceCallback {
abstract boolean setExposureCorrection(float EVvalue); abstract boolean setExposureCorrection(float EVvalue);
abstract void setFacing(Facing facing); abstract void setFacing(Facing facing);
abstract void setFlash(Flash flash); abstract void setFlash(Flash flash);
abstract void setWhiteBalance(WhiteBalance whiteBalance);
abstract void setVideoQuality(@VideoQuality int videoQuality); abstract void setVideoQuality(@VideoQuality int videoQuality);
abstract void setWhiteBalance(@WhiteBalance int whiteBalance);
abstract void setSessionType(@SessionType int sessionType); abstract void setSessionType(@SessionType int sessionType);
abstract void setLocation(double latitude, double longitude); abstract void setLocation(double latitude, double longitude);
@ -53,17 +53,9 @@ abstract class CameraController implements Preview.SurfaceCallback {
@Nullable abstract ExtraProperties getExtraProperties(); @Nullable abstract ExtraProperties getExtraProperties();
@Nullable abstract CameraOptions getCameraOptions(); @Nullable abstract CameraOptions getCameraOptions();
final Facing getFacing() { final Facing getFacing() { return mFacing; }
return mFacing; final Flash getFlash() { return mFlash; }
} final WhiteBalance getWhiteBalance() { return mWhiteBalance; }
final Flash getFlash() {
return mFlash;
}
@WhiteBalance
final int getWhiteBalance() {
return mWhiteBalance;
}
@VideoQuality @VideoQuality
final int getVideoQuality() { final int getVideoQuality() {

@ -15,7 +15,7 @@ import java.util.Set;
*/ */
public class CameraOptions { public class CameraOptions {
private Set<Integer> supportedWhiteBalance = new HashSet<>(5); private Set<WhiteBalance> supportedWhiteBalance = new HashSet<>(5);
private Set<Facing> supportedFacing = new HashSet<>(2); private Set<Facing> supportedFacing = new HashSet<>(2);
private Set<Flash> supportedFlash = new HashSet<>(4); private Set<Flash> supportedFlash = new HashSet<>(4);
@ -45,7 +45,7 @@ public class CameraOptions {
strings = params.getSupportedWhiteBalance(); strings = params.getSupportedWhiteBalance();
if (strings != null) { if (strings != null) {
for (String string : strings) { for (String string : strings) {
Integer value = mapper.unmapWhiteBalance(string); WhiteBalance value = mapper.unmapWhiteBalance(string);
if (value != null) supportedWhiteBalance.add(value); if (value != null) supportedWhiteBalance.add(value);
} }
} }
@ -108,15 +108,15 @@ public class CameraOptions {
/** /**
* Set of supported white balance values. * Set of supported white balance values.
* *
* @see CameraConstants#WHITE_BALANCE_AUTO * @see WhiteBalance#AUTO
* @see CameraConstants#WHITE_BALANCE_CLOUDY * @see WhiteBalance#INCANDESCENT
* @see CameraConstants#WHITE_BALANCE_DAYLIGHT * @see WhiteBalance#FLUORESCENT
* @see CameraConstants#WHITE_BALANCE_FLUORESCENT * @see WhiteBalance#DAYLIGHT
* @see CameraConstants#WHITE_BALANCE_INCANDESCENT * @see WhiteBalance#CLOUDY
* @return a set of supported values. * @return a set of supported values.
*/ */
@NonNull @NonNull
public Set<Integer> getSupportedWhiteBalance() { public Set<WhiteBalance> getSupportedWhiteBalance() {
return supportedWhiteBalance; return supportedWhiteBalance;
} }

@ -95,8 +95,8 @@ public class CameraView extends FrameLayout {
Facing facing = Facing.fromValue(a.getInteger(R.styleable.CameraView_cameraFacing, Facing.DEFAULT.value())); 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())); Flash flash = Flash.fromValue(a.getInteger(R.styleable.CameraView_cameraFlash, Flash.DEFAULT.value()));
Grid grid = Grid.fromValue(a.getInteger(R.styleable.CameraView_cameraGrid, Grid.DEFAULT.value())); Grid grid = Grid.fromValue(a.getInteger(R.styleable.CameraView_cameraGrid, Grid.DEFAULT.value()));
WhiteBalance whiteBalance = WhiteBalance.fromValue(a.getInteger(R.styleable.CameraView_cameraWhiteBalance, WhiteBalance.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 videoQuality = a.getInteger(R.styleable.CameraView_cameraVideoQuality, Defaults.DEFAULT_VIDEO_QUALITY); int videoQuality = a.getInteger(R.styleable.CameraView_cameraVideoQuality, Defaults.DEFAULT_VIDEO_QUALITY);
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);
@ -656,15 +656,15 @@ public class CameraView extends FrameLayout {
/** /**
* Sets desired white balance to current camera session. * Sets desired white balance to current camera session.
* *
* @see CameraConstants#WHITE_BALANCE_AUTO * @see WhiteBalance#AUTO
* @see CameraConstants#WHITE_BALANCE_CLOUDY * @see WhiteBalance#INCANDESCENT
* @see CameraConstants#WHITE_BALANCE_DAYLIGHT * @see WhiteBalance#FLUORESCENT
* @see CameraConstants#WHITE_BALANCE_FLUORESCENT * @see WhiteBalance#DAYLIGHT
* @see CameraConstants#WHITE_BALANCE_INCANDESCENT * @see WhiteBalance#CLOUDY
* *
* @param whiteBalance desired white balance behavior. * @param whiteBalance desired white balance behavior.
*/ */
public void setWhiteBalance(@WhiteBalance int whiteBalance) { public void setWhiteBalance(WhiteBalance whiteBalance) {
mCameraController.setWhiteBalance(whiteBalance); mCameraController.setWhiteBalance(whiteBalance);
} }
@ -673,8 +673,7 @@ public class CameraView extends FrameLayout {
* Returns the current white balance behavior. * Returns the current white balance behavior.
* @return white balance value. * @return white balance value.
*/ */
@WhiteBalance public WhiteBalance getWhiteBalance() {
public int getWhiteBalance() {
return mCameraController.getWhiteBalance(); return mCameraController.getWhiteBalance();
} }

@ -8,14 +8,15 @@ abstract class Mapper {
abstract <T> T mapFlash(Flash 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 internalConstant);
abstract <T> Flash 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); abstract <T> WhiteBalance unmapWhiteBalance(T cameraConstant);
static class Mapper1 extends Mapper { static class Mapper1 extends Mapper {
private static final HashMap<Flash, 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<WhiteBalance, String> WB = new HashMap<>();
private static final HashMap<Facing, Integer> FACING = new HashMap<>(); private static final HashMap<Facing, Integer> FACING = new HashMap<>();
static { static {
@ -25,11 +26,11 @@ abstract class Mapper {
FLASH.put(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(WhiteBalance.AUTO, Camera.Parameters.WHITE_BALANCE_AUTO);
WB.put(CameraConstants.WHITE_BALANCE_INCANDESCENT, Camera.Parameters.WHITE_BALANCE_INCANDESCENT); WB.put(WhiteBalance.INCANDESCENT, Camera.Parameters.WHITE_BALANCE_INCANDESCENT);
WB.put(CameraConstants.WHITE_BALANCE_FLUORESCENT, Camera.Parameters.WHITE_BALANCE_FLUORESCENT); WB.put(WhiteBalance.FLUORESCENT, Camera.Parameters.WHITE_BALANCE_FLUORESCENT);
WB.put(CameraConstants.WHITE_BALANCE_DAYLIGHT, Camera.Parameters.WHITE_BALANCE_DAYLIGHT); WB.put(WhiteBalance.DAYLIGHT, Camera.Parameters.WHITE_BALANCE_DAYLIGHT);
WB.put(CameraConstants.WHITE_BALANCE_CLOUDY, Camera.Parameters.WHITE_BALANCE_CLOUDY_DAYLIGHT); WB.put(WhiteBalance.CLOUDY, Camera.Parameters.WHITE_BALANCE_CLOUDY_DAYLIGHT);
} }
@Override @Override
@ -43,7 +44,7 @@ abstract class Mapper {
} }
@Override @Override
<T> T mapWhiteBalance(int internalConstant) { <T> T mapWhiteBalance(WhiteBalance internalConstant) {
return (T) WB.get(internalConstant); return (T) WB.get(internalConstant);
} }
@ -67,7 +68,7 @@ abstract class Mapper {
} }
@Override @Override
<T> Integer unmapWhiteBalance(T cameraConstant) { <T> WhiteBalance unmapWhiteBalance(T cameraConstant) {
return reverseLookup(WB, cameraConstant); return reverseLookup(WB, cameraConstant);
} }
} }
@ -75,7 +76,7 @@ abstract class Mapper {
static class Mapper2 extends Mapper { static class Mapper2 extends Mapper {
@Override @Override
<T> T mapWhiteBalance(@WhiteBalance int internalConstant) { <T> T mapWhiteBalance(WhiteBalance internalConstant) {
return null; return null;
} }
@ -90,7 +91,7 @@ abstract class Mapper {
} }
@Override @Override
<T> Integer unmapWhiteBalance(T cameraConstant) { <T> WhiteBalance unmapWhiteBalance(T cameraConstant) {
return null; return null;
} }

@ -1,18 +1,73 @@
package com.otaliastudios.cameraview; package com.otaliastudios.cameraview;
import android.support.annotation.IntDef;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import static com.otaliastudios.cameraview.CameraConstants.WHITE_BALANCE_AUTO; /**
import static com.otaliastudios.cameraview.CameraConstants.WHITE_BALANCE_INCANDESCENT; * White balance values control the white balance settings.
import static com.otaliastudios.cameraview.CameraConstants.WHITE_BALANCE_FLUORESCENT; *
import static com.otaliastudios.cameraview.CameraConstants.WHITE_BALANCE_DAYLIGHT; * @see CameraView#setWhiteBalance(WhiteBalance)
import static com.otaliastudios.cameraview.CameraConstants.WHITE_BALANCE_CLOUDY; */
public enum WhiteBalance {
@Retention(RetentionPolicy.SOURCE) /**
@IntDef({WHITE_BALANCE_AUTO, WHITE_BALANCE_INCANDESCENT, WHITE_BALANCE_FLUORESCENT, * Automatic white balance selection (AWB).
WHITE_BALANCE_DAYLIGHT, WHITE_BALANCE_CLOUDY}) * This is not guaranteed to be supported.
public @interface WhiteBalance { *
* @see CameraOptions#getSupportedWhiteBalance()
*/
AUTO(0),
/**
* White balance appropriate for incandescent light.
* This is not guaranteed to be supported.
*
* @see CameraOptions#getSupportedWhiteBalance()
*/
INCANDESCENT(1),
/**
* White balance appropriate for fluorescent light.
* This is not guaranteed to be supported.
*
* @see CameraOptions#getSupportedWhiteBalance()
*/
FLUORESCENT(2),
/**
* White balance appropriate for daylight captures.
* This is not guaranteed to be supported.
*
* @see CameraOptions#getSupportedWhiteBalance()
*/
DAYLIGHT(3),
/**
* White balance appropriate for pictures in cloudy conditions.
* This is not guaranteed to be supported.
*
* @see CameraOptions#getSupportedWhiteBalance()
*/
CLOUDY(4);
static final WhiteBalance DEFAULT = AUTO;
private int value;
WhiteBalance(int value) {
this.value = value;
}
int value() {
return value;
}
static WhiteBalance fromValue(int value) {
WhiteBalance[] list = WhiteBalance.values();
for (WhiteBalance action : list) {
if (action.value() == value) {
return action;
}
}
return null;
}
} }
Loading…
Cancel
Save