From b65e9e446d9a0bed9c655616db4ef2c57fd72f6a Mon Sep 17 00:00:00 2001 From: Mattia Iavarone Date: Fri, 11 Aug 2017 22:02:14 +0200 Subject: [PATCH] Moved VideoQuality to enums --- README.md | 26 +++--- .../com/otaliastudios/cameraview/Camera1.java | 18 ++-- .../cameraview/CameraConstants.java | 8 -- .../cameraview/CameraController.java | 10 +-- .../otaliastudios/cameraview/CameraView.java | 21 +++-- .../cameraview/VideoQuality.java | 86 +++++++++++++++---- cameraview/src/main/res/values/attrs.xml | 14 +-- .../cameraview/demo/MainActivity.java | 17 ++-- 8 files changed, 121 insertions(+), 79 deletions(-) diff --git a/README.md b/README.md index c40c4e65..c4c84321 100644 --- a/README.md +++ b/README.md @@ -297,7 +297,7 @@ Most camera parameters can be controlled through XML attributes or linked method |[`cameraGrid`](#cameragrid)|`setGrid()`|`off` `draw3x3` `draw4x4` `drawPhi`|`off`| |[`cameraCropOutput`](#cameracropoutput)|`setCropOutput()`|`true` `false`|`false`| |[`cameraJpegQuality`](#camerajpegquality)|`setJpegQuality()`|`0 <= n <= 100`|`100`| -|[`cameraVideoQuality`](#cameravideoquality)|`setVideoQuality()`|`max480p` `max720p` `max1080p` `max2160p` `highest` `lowest`|`max480p`| +|[`cameraVideoQuality`](#cameravideoquality)|`setVideoQuality()`|`lowest` `highest` `maxQvga` `max480p` `max720p` `max1080p` `max2160p`|`max480p`| |[`cameraWhiteBalance`](#camerawhitebalance)|`setWhiteBalance()`|`auto` `incandescent` `fluorescent` `daylight` `cloudy`|`auto`| #### cameraSessionType @@ -364,13 +364,13 @@ cameraView.setJpegQuality(50); Sets the desired video quality. ```java -cameraView.setVideoQuality(CameraConstants.VIDEO_QUALITY_480P); -cameraView.setVideoQuality(CameraConstants.VIDEO_QUALITY_720P); -cameraView.setVideoQuality(CameraConstants.VIDEO_QUALITY_1080P); -cameraView.setVideoQuality(CameraConstants.VIDEO_QUALITY_2160P); -cameraView.setVideoQuality(CameraConstants.VIDEO_QUALITY_LOWEST); -cameraView.setVideoQuality(CameraConstants.VIDEO_QUALITY_HIGHEST); -cameraView.setVideoQuality(CameraConstants.VIDEO_QUALITY_QVGA); +cameraView.setVideoQuality(VideoQuality.LOWEST); +cameraView.setVideoQuality(VideoQuality.HIGHEST); +cameraView.setVideoQuality(VideoQuality.MAX_QVGA); +cameraView.setVideoQuality(VideoQuality.MAX_480P); +cameraView.setVideoQuality(VideoQuality.MAX_720P); +cameraView.setVideoQuality(VideoQuality.MAX_1080P); +cameraView.setVideoQuality(VideoQuality.MAX_2160P); ``` #### cameraWhiteBalance @@ -378,11 +378,11 @@ cameraView.setVideoQuality(CameraConstants.VIDEO_QUALITY_QVGA); Sets the desired white balance for the current session. ```java -cameraView.setWhiteBalance(CameraConstants.WHITE_BALANCE_AUTO); -cameraView.setWhiteBalance(CameraConstants.WHITE_BALANCE_INCANDESCENT); -cameraView.setWhiteBalance(CameraConstants.WHITE_BALANCE_FLUORESCENT); -cameraView.setWhiteBalance(CameraConstants.WHITE_BALANCE_DAYLIGHT); -cameraView.setWhiteBalance(CameraConstants.WHITE_BALANCE_CLOUDY); +cameraView.setWhiteBalance(WhiteBalance.AUTO); +cameraView.setWhiteBalance(WhiteBalance.INCANDESCENT); +cameraView.setWhiteBalance(WhiteBalance.FLUORESCENT); +cameraView.setWhiteBalance(WhiteBalance.DAYLIGHT); +cameraView.setWhiteBalance(WhiteBalance.CLOUDY); ``` ## Other APIs diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java index eede8a2e..ac88564b 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java @@ -321,7 +321,7 @@ class Camera1 extends CameraController { @Override - void setVideoQuality(int videoQuality) { + void setVideoQuality(VideoQuality videoQuality) { if (mIsCapturingVideo) { throw new IllegalStateException("Can't change video quality while recording a video."); } @@ -588,43 +588,43 @@ class Camera1 extends CameraController { @NonNull - private CamcorderProfile getCamcorderProfile(@VideoQuality int videoQuality) { + private CamcorderProfile getCamcorderProfile(VideoQuality videoQuality) { switch (videoQuality) { - case CameraConstants.VIDEO_QUALITY_HIGHEST: + case HIGHEST: return CamcorderProfile.get(mCameraId, CamcorderProfile.QUALITY_HIGH); - case CameraConstants.VIDEO_QUALITY_2160P: + case MAX_2160P: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && CamcorderProfile.hasProfile(CamcorderProfile.QUALITY_2160P)) { return CamcorderProfile.get(mCameraId, CamcorderProfile.QUALITY_2160P); } // Don't break. - case CameraConstants.VIDEO_QUALITY_1080P: + case MAX_1080P: if (CamcorderProfile.hasProfile(mCameraId, CamcorderProfile.QUALITY_1080P)) { return CamcorderProfile.get(mCameraId, CamcorderProfile.QUALITY_1080P); } // Don't break. - case CameraConstants.VIDEO_QUALITY_720P: + case MAX_720P: if (CamcorderProfile.hasProfile(mCameraId, CamcorderProfile.QUALITY_720P)) { return CamcorderProfile.get(mCameraId, CamcorderProfile.QUALITY_720P); } // Don't break. - case CameraConstants.VIDEO_QUALITY_480P: + case MAX_480P: if (CamcorderProfile.hasProfile(mCameraId, CamcorderProfile.QUALITY_480P)) { return CamcorderProfile.get(mCameraId, CamcorderProfile.QUALITY_480P); } // Don't break. - case CameraConstants.VIDEO_QUALITY_QVGA: + case MAX_QVGA: if (CamcorderProfile.hasProfile(mCameraId, CamcorderProfile.QUALITY_QVGA)) { return CamcorderProfile.get(mCameraId, CamcorderProfile.QUALITY_QVGA); } // Don't break. - case CameraConstants.VIDEO_QUALITY_LOWEST: + case LOWEST: default: // Fallback to lowest. return CamcorderProfile.get(mCameraId, CamcorderProfile.QUALITY_LOW); diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraConstants.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraConstants.java index 25415375..2c8c677c 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraConstants.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraConstants.java @@ -10,17 +10,9 @@ public class CameraConstants { public static final int SESSION_TYPE_PICTURE = 0; public static final int SESSION_TYPE_VIDEO = 1; - public static final int VIDEO_QUALITY_480P = 0; - public static final int VIDEO_QUALITY_720P = 1; - public static final int VIDEO_QUALITY_1080P = 2; - public static final int VIDEO_QUALITY_2160P = 3; - public static final int VIDEO_QUALITY_HIGHEST = 4; - public static final int VIDEO_QUALITY_LOWEST = 5; - public static final int VIDEO_QUALITY_QVGA = 6; static class Defaults { - static final int DEFAULT_VIDEO_QUALITY = VIDEO_QUALITY_480P; 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 4ed633df..527ee11b 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java @@ -14,7 +14,7 @@ abstract class CameraController implements Preview.SurfaceCallback { protected Facing mFacing; protected Flash mFlash; protected WhiteBalance mWhiteBalance; - @VideoQuality protected int mVideoQuality; + protected VideoQuality mVideoQuality; @SessionType protected int mSessionType; CameraController(CameraView.CameraCallbacks callback, Preview preview) { @@ -34,7 +34,7 @@ abstract class CameraController implements Preview.SurfaceCallback { abstract void setFacing(Facing facing); abstract void setFlash(Flash flash); abstract void setWhiteBalance(WhiteBalance whiteBalance); - abstract void setVideoQuality(@VideoQuality int videoQuality); + abstract void setVideoQuality(VideoQuality videoQuality); abstract void setSessionType(@SessionType int sessionType); abstract void setLocation(double latitude, double longitude); @@ -56,11 +56,7 @@ abstract class CameraController implements Preview.SurfaceCallback { final Facing getFacing() { return mFacing; } final Flash getFlash() { return mFlash; } final WhiteBalance getWhiteBalance() { return mWhiteBalance; } - - @VideoQuality - final int getVideoQuality() { - return mVideoQuality; - } + final VideoQuality getVideoQuality() { return mVideoQuality; } @SessionType final int getSessionType() { diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java index fe291c6a..2748c825 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java @@ -96,8 +96,8 @@ public class CameraView extends FrameLayout { 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())); + VideoQuality videoQuality = VideoQuality.fromValue(a.getInteger(R.styleable.CameraView_cameraVideoQuality, VideoQuality.DEFAULT.value())); int sessionType = a.getInteger(R.styleable.CameraView_cameraSessionType, Defaults.DEFAULT_SESSION_TYPE); - 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); GestureAction tapGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGestureTap, GestureAction.DEFAULT_TAP.value())); @@ -838,17 +838,17 @@ public class CameraView extends FrameLayout { * If it's not, a lower quality will be chosen, until a supported one is found. * If sessionType is video, this might trigger a camera restart and a change in preview size. * - * @see CameraConstants#VIDEO_QUALITY_LOWEST - * @see CameraConstants#VIDEO_QUALITY_QVGA - * @see CameraConstants#VIDEO_QUALITY_480P - * @see CameraConstants#VIDEO_QUALITY_720P - * @see CameraConstants#VIDEO_QUALITY_1080P - * @see CameraConstants#VIDEO_QUALITY_2160P - * @see CameraConstants#VIDEO_QUALITY_HIGHEST + * @see VideoQuality#LOWEST + * @see VideoQuality#HIGHEST + * @see VideoQuality#MAX_QVGA + * @see VideoQuality#MAX_480P + * @see VideoQuality#MAX_720P + * @see VideoQuality#MAX_1080P + * @see VideoQuality#MAX_2160P * * @param videoQuality requested video quality */ - public void setVideoQuality(@VideoQuality int videoQuality) { + public void setVideoQuality(VideoQuality videoQuality) { mCameraController.setVideoQuality(videoQuality); } @@ -857,8 +857,7 @@ public class CameraView extends FrameLayout { * Gets the current video quality. * @return the current video quality */ - @VideoQuality - public int getVideoQuality() { + public VideoQuality getVideoQuality() { return mCameraController.getVideoQuality(); } diff --git a/cameraview/src/main/options/com/otaliastudios/cameraview/VideoQuality.java b/cameraview/src/main/options/com/otaliastudios/cameraview/VideoQuality.java index ad99464e..0b6dec5d 100644 --- a/cameraview/src/main/options/com/otaliastudios/cameraview/VideoQuality.java +++ b/cameraview/src/main/options/com/otaliastudios/cameraview/VideoQuality.java @@ -1,19 +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.VIDEO_QUALITY_1080P; -import static com.otaliastudios.cameraview.CameraConstants.VIDEO_QUALITY_2160P; -import static com.otaliastudios.cameraview.CameraConstants.VIDEO_QUALITY_480P; -import static com.otaliastudios.cameraview.CameraConstants.VIDEO_QUALITY_720P; -import static com.otaliastudios.cameraview.CameraConstants.VIDEO_QUALITY_HIGHEST; -import static com.otaliastudios.cameraview.CameraConstants.VIDEO_QUALITY_LOWEST; -import static com.otaliastudios.cameraview.CameraConstants.VIDEO_QUALITY_QVGA; - -@Retention(RetentionPolicy.SOURCE) -@IntDef({VIDEO_QUALITY_QVGA, VIDEO_QUALITY_480P, VIDEO_QUALITY_720P, VIDEO_QUALITY_1080P, VIDEO_QUALITY_2160P, VIDEO_QUALITY_HIGHEST, VIDEO_QUALITY_LOWEST}) -public @interface VideoQuality { + +/** + * Constants for selecting the quality of video recordings. + * + * @see CameraView#setVideoQuality(VideoQuality) + */ +public enum VideoQuality { + + + /** + * Quality level corresponding to the lowest available resolution. + */ + LOWEST(0), + + /** + * Quality level corresponding to the highest available resolution. + */ + HIGHEST(1), + + /** + * Quality level corresponding to the QVGA (320x240) resolution. + */ + MAX_QVGA(2), + + /** + * Quality level corresponding to the 480p (720 x 480) resolution. + * Note that the horizontal resolution for 480p can also be other + * values, such as 640 or 704, instead of 720. + */ + MAX_480P(3), + + /** + * Quality level corresponding to the 720p (1280 x 720) resolution. + */ + MAX_720P(4), + + /** + * Quality level corresponding to the 1080p (1920 x 1080) resolution. + * Note that the vertical resolution for 1080p can also be 1088, + * instead of 1080 (used by some vendors to avoid cropping during + * video playback). + */ + MAX_1080P(5), + + /** + * Quality level corresponding to the 2160p (3840x2160) resolution. + */ + MAX_2160P(6); + + static final VideoQuality DEFAULT = MAX_480P; + + private int value; + + VideoQuality(int value) { + this.value = value; + } + + int value() { + return value; + } + + static VideoQuality fromValue(int value) { + VideoQuality[] list = VideoQuality.values(); + for (VideoQuality action : list) { + if (action.value() == value) { + return action; + } + } + return null; + } } diff --git a/cameraview/src/main/res/values/attrs.xml b/cameraview/src/main/res/values/attrs.xml index 6578e5bd..cb5f5c7c 100644 --- a/cameraview/src/main/res/values/attrs.xml +++ b/cameraview/src/main/res/values/attrs.xml @@ -62,13 +62,13 @@ - - - - - - - + + + + + + + diff --git a/demo/src/main/java/com/otaliastudios/cameraview/demo/MainActivity.java b/demo/src/main/java/com/otaliastudios/cameraview/demo/MainActivity.java index 135352fc..ad3f1b65 100644 --- a/demo/src/main/java/com/otaliastudios/cameraview/demo/MainActivity.java +++ b/demo/src/main/java/com/otaliastudios/cameraview/demo/MainActivity.java @@ -19,6 +19,7 @@ import com.otaliastudios.cameraview.CameraView; import com.otaliastudios.cameraview.Facing; import com.otaliastudios.cameraview.Grid; import com.otaliastudios.cameraview.Size; +import com.otaliastudios.cameraview.VideoQuality; import java.io.File; @@ -238,15 +239,15 @@ public class MainActivity extends AppCompatActivity implements View.OnLayoutChan @Override public void onCheckedChanged(RadioGroup group, int checkedId) { if (mCapturingVideo) return; - int videoQuality = CameraConstants.VIDEO_QUALITY_HIGHEST; + VideoQuality videoQuality = VideoQuality.HIGHEST; switch (checkedId) { - case R.id.videoQualityLowest: videoQuality = CameraConstants.VIDEO_QUALITY_LOWEST; break; - case R.id.videoQualityQvga: videoQuality = CameraConstants.VIDEO_QUALITY_QVGA; break; - case R.id.videoQuality480p: videoQuality = CameraConstants.VIDEO_QUALITY_480P; break; - case R.id.videoQuality720p: videoQuality = CameraConstants.VIDEO_QUALITY_720P; break; - case R.id.videoQuality1080p: videoQuality = CameraConstants.VIDEO_QUALITY_1080P; break; - case R.id.videoQuality2160p: videoQuality = CameraConstants.VIDEO_QUALITY_2160P; break; - case R.id.videoQualityHighest: videoQuality = CameraConstants.VIDEO_QUALITY_HIGHEST; break; + case R.id.videoQualityLowest: videoQuality = VideoQuality.LOWEST; break; + case R.id.videoQualityQvga: videoQuality = VideoQuality.MAX_QVGA; break; + case R.id.videoQuality480p: videoQuality = VideoQuality.MAX_480P; break; + case R.id.videoQuality720p: videoQuality = VideoQuality.MAX_720P; break; + case R.id.videoQuality1080p: videoQuality = VideoQuality.MAX_1080P; break; + case R.id.videoQuality2160p: videoQuality = VideoQuality.MAX_2160P; break; + case R.id.videoQualityHighest: videoQuality = VideoQuality.HIGHEST; break; } camera.setVideoQuality(videoQuality); message("Video quality changed!", false);