From 324f0c8a4731c3018de3784d3051f5525cb333c6 Mon Sep 17 00:00:00 2001 From: Mattia Iavarone Date: Fri, 11 Aug 2017 21:46:33 +0200 Subject: [PATCH] Moved WhiteBalance to enums --- .../com/otaliastudios/cameraview/Camera1.java | 8 +- .../com/otaliastudios/cameraview/Camera2.java | 2 +- .../cameraview/CameraConstants.java | 10 +-- .../cameraview/CameraController.java | 18 ++--- .../cameraview/CameraOptions.java | 16 ++-- .../otaliastudios/cameraview/CameraView.java | 17 ++-- .../com/otaliastudios/cameraview/Mapper.java | 25 +++--- .../cameraview/WhiteBalance.java | 79 ++++++++++++++++--- 8 files changed, 108 insertions(+), 67 deletions(-) diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java index 9146615b..eede8a2e 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java @@ -145,7 +145,7 @@ class Camera1 extends CameraController { applyDefaultFocus(params); mergeFlash(params, Flash.DEFAULT); mergeLocation(params, 0d, 0d); - mergeWhiteBalance(params, CameraConstants.Defaults.DEFAULT_WHITE_BALANCE); + mergeWhiteBalance(params, WhiteBalance.DEFAULT); params.setRecordingHint(mSessionType == SESSION_TYPE_VIDEO); mCamera.setParameters(params); } @@ -250,8 +250,8 @@ class Camera1 extends CameraController { } @Override - void setWhiteBalance(@WhiteBalance int whiteBalance) { - int old = mWhiteBalance; + void setWhiteBalance(WhiteBalance whiteBalance) { + WhiteBalance old = mWhiteBalance; mWhiteBalance = whiteBalance; if (isCameraOpened()) { 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)) { params.setWhiteBalance((String) mMapper.mapWhiteBalance(mWhiteBalance)); return true; diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java index 1b7f7887..a67a46d8 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera2.java @@ -152,7 +152,7 @@ class Camera2 extends CameraController { } @Override - void setWhiteBalance(@WhiteBalance int whiteBalance) { + void setWhiteBalance(WhiteBalance whiteBalance) { } diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraConstants.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraConstants.java index 645a4dc0..25415375 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraConstants.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraConstants.java @@ -1,6 +1,8 @@ package com.otaliastudios.cameraview; +import android.hardware.Camera; + public class CameraConstants { 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_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 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_JPEG_QUALITY = 100; static final boolean DEFAULT_CROP_OUTPUT = false; diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java index 350e7c49..4ed633df 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java @@ -13,8 +13,8 @@ abstract class CameraController implements Preview.SurfaceCallback { protected Facing mFacing; protected Flash mFlash; + protected WhiteBalance mWhiteBalance; @VideoQuality protected int mVideoQuality; - @WhiteBalance protected int mWhiteBalance; @SessionType protected int mSessionType; CameraController(CameraView.CameraCallbacks callback, Preview preview) { @@ -33,8 +33,8 @@ abstract class CameraController implements Preview.SurfaceCallback { abstract boolean setExposureCorrection(float EVvalue); abstract void setFacing(Facing facing); abstract void setFlash(Flash flash); + abstract void setWhiteBalance(WhiteBalance whiteBalance); abstract void setVideoQuality(@VideoQuality int videoQuality); - abstract void setWhiteBalance(@WhiteBalance int whiteBalance); abstract void setSessionType(@SessionType int sessionType); abstract void setLocation(double latitude, double longitude); @@ -53,17 +53,9 @@ abstract class CameraController implements Preview.SurfaceCallback { @Nullable abstract ExtraProperties getExtraProperties(); @Nullable abstract CameraOptions getCameraOptions(); - final Facing getFacing() { - return mFacing; - } - final Flash getFlash() { - return mFlash; - } - - @WhiteBalance - final int getWhiteBalance() { - return mWhiteBalance; - } + final Facing getFacing() { return mFacing; } + final Flash getFlash() { return mFlash; } + final WhiteBalance getWhiteBalance() { return mWhiteBalance; } @VideoQuality final int getVideoQuality() { diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraOptions.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraOptions.java index e80d1ddd..de1ec244 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraOptions.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraOptions.java @@ -15,7 +15,7 @@ import java.util.Set; */ public class CameraOptions { - private Set supportedWhiteBalance = new HashSet<>(5); + private Set supportedWhiteBalance = new HashSet<>(5); private Set supportedFacing = new HashSet<>(2); private Set supportedFlash = new HashSet<>(4); @@ -45,7 +45,7 @@ public class CameraOptions { strings = params.getSupportedWhiteBalance(); if (strings != null) { for (String string : strings) { - Integer value = mapper.unmapWhiteBalance(string); + WhiteBalance value = mapper.unmapWhiteBalance(string); if (value != null) supportedWhiteBalance.add(value); } } @@ -108,15 +108,15 @@ public class CameraOptions { /** * Set of supported white balance values. * - * @see CameraConstants#WHITE_BALANCE_AUTO - * @see CameraConstants#WHITE_BALANCE_CLOUDY - * @see CameraConstants#WHITE_BALANCE_DAYLIGHT - * @see CameraConstants#WHITE_BALANCE_FLUORESCENT - * @see CameraConstants#WHITE_BALANCE_INCANDESCENT + * @see WhiteBalance#AUTO + * @see WhiteBalance#INCANDESCENT + * @see WhiteBalance#FLUORESCENT + * @see WhiteBalance#DAYLIGHT + * @see WhiteBalance#CLOUDY * @return a set of supported values. */ @NonNull - public Set getSupportedWhiteBalance() { + public Set getSupportedWhiteBalance() { return supportedWhiteBalance; } diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java index ef022947..fe291c6a 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java @@ -95,8 +95,8 @@ public class CameraView extends FrameLayout { 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())); 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 whiteBalance = a.getInteger(R.styleable.CameraView_cameraWhiteBalance, Defaults.DEFAULT_WHITE_BALANCE); int videoQuality = a.getInteger(R.styleable.CameraView_cameraVideoQuality, Defaults.DEFAULT_VIDEO_QUALITY); mJpegQuality = a.getInteger(R.styleable.CameraView_cameraJpegQuality, Defaults.DEFAULT_JPEG_QUALITY); 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. * - * @see CameraConstants#WHITE_BALANCE_AUTO - * @see CameraConstants#WHITE_BALANCE_CLOUDY - * @see CameraConstants#WHITE_BALANCE_DAYLIGHT - * @see CameraConstants#WHITE_BALANCE_FLUORESCENT - * @see CameraConstants#WHITE_BALANCE_INCANDESCENT + * @see WhiteBalance#AUTO + * @see WhiteBalance#INCANDESCENT + * @see WhiteBalance#FLUORESCENT + * @see WhiteBalance#DAYLIGHT + * @see WhiteBalance#CLOUDY * * @param whiteBalance desired white balance behavior. */ - public void setWhiteBalance(@WhiteBalance int whiteBalance) { + public void setWhiteBalance(WhiteBalance whiteBalance) { mCameraController.setWhiteBalance(whiteBalance); } @@ -673,8 +673,7 @@ public class CameraView extends FrameLayout { * Returns the current white balance behavior. * @return white balance value. */ - @WhiteBalance - public int getWhiteBalance() { + public WhiteBalance getWhiteBalance() { return mCameraController.getWhiteBalance(); } diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/Mapper.java b/cameraview/src/main/java/com/otaliastudios/cameraview/Mapper.java index 9796995e..80b25cba 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/Mapper.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/Mapper.java @@ -8,14 +8,15 @@ abstract class Mapper { abstract T mapFlash(Flash internalConstant); abstract T mapFacing(Facing internalConstant); - abstract T mapWhiteBalance(@WhiteBalance int internalConstant); + abstract T mapWhiteBalance(WhiteBalance internalConstant); abstract Flash unmapFlash(T cameraConstant); abstract Facing unmapFacing(T cameraConstant); - @WhiteBalance abstract Integer unmapWhiteBalance(T cameraConstant); + abstract WhiteBalance 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 WB = new HashMap<>(); private static final HashMap FACING = new HashMap<>(); static { @@ -25,11 +26,11 @@ abstract class Mapper { 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); - 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_DAYLIGHT, Camera.Parameters.WHITE_BALANCE_DAYLIGHT); - WB.put(CameraConstants.WHITE_BALANCE_CLOUDY, Camera.Parameters.WHITE_BALANCE_CLOUDY_DAYLIGHT); + WB.put(WhiteBalance.AUTO, Camera.Parameters.WHITE_BALANCE_AUTO); + WB.put(WhiteBalance.INCANDESCENT, Camera.Parameters.WHITE_BALANCE_INCANDESCENT); + WB.put(WhiteBalance.FLUORESCENT, Camera.Parameters.WHITE_BALANCE_FLUORESCENT); + WB.put(WhiteBalance.DAYLIGHT, Camera.Parameters.WHITE_BALANCE_DAYLIGHT); + WB.put(WhiteBalance.CLOUDY, Camera.Parameters.WHITE_BALANCE_CLOUDY_DAYLIGHT); } @Override @@ -43,7 +44,7 @@ abstract class Mapper { } @Override - T mapWhiteBalance(int internalConstant) { + T mapWhiteBalance(WhiteBalance internalConstant) { return (T) WB.get(internalConstant); } @@ -67,7 +68,7 @@ abstract class Mapper { } @Override - Integer unmapWhiteBalance(T cameraConstant) { + WhiteBalance unmapWhiteBalance(T cameraConstant) { return reverseLookup(WB, cameraConstant); } } @@ -75,7 +76,7 @@ abstract class Mapper { static class Mapper2 extends Mapper { @Override - T mapWhiteBalance(@WhiteBalance int internalConstant) { + T mapWhiteBalance(WhiteBalance internalConstant) { return null; } @@ -90,7 +91,7 @@ abstract class Mapper { } @Override - Integer unmapWhiteBalance(T cameraConstant) { + WhiteBalance unmapWhiteBalance(T cameraConstant) { return null; } diff --git a/cameraview/src/main/options/com/otaliastudios/cameraview/WhiteBalance.java b/cameraview/src/main/options/com/otaliastudios/cameraview/WhiteBalance.java index ce5c05cc..0d4e89c1 100644 --- a/cameraview/src/main/options/com/otaliastudios/cameraview/WhiteBalance.java +++ b/cameraview/src/main/options/com/otaliastudios/cameraview/WhiteBalance.java @@ -1,18 +1,73 @@ 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; -import static com.otaliastudios.cameraview.CameraConstants.WHITE_BALANCE_FLUORESCENT; -import static com.otaliastudios.cameraview.CameraConstants.WHITE_BALANCE_DAYLIGHT; -import static com.otaliastudios.cameraview.CameraConstants.WHITE_BALANCE_CLOUDY; +/** + * White balance values control the white balance settings. + * + * @see CameraView#setWhiteBalance(WhiteBalance) + */ +public enum WhiteBalance { -@Retention(RetentionPolicy.SOURCE) -@IntDef({WHITE_BALANCE_AUTO, WHITE_BALANCE_INCANDESCENT, WHITE_BALANCE_FLUORESCENT, - WHITE_BALANCE_DAYLIGHT, WHITE_BALANCE_CLOUDY}) -public @interface WhiteBalance { + /** + * Automatic white balance selection (AWB). + * This is not guaranteed to be supported. + * + * @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; + } } \ No newline at end of file