From e87d8feb4cace38e70cdc64981c4a587ec86156f Mon Sep 17 00:00:00 2001 From: Dylan McIntyre Date: Mon, 3 Apr 2017 22:50:45 -0400 Subject: [PATCH] add variable video quality --- .../api16/com/flurgle/camerakit/Camera1.java | 59 ++++++++++++++++++- .../api21/com/flurgle/camerakit/Camera2.java | 5 ++ .../com/flurgle/camerakit/CameraImpl.java | 1 + .../java/com/flurgle/camerakit/CameraKit.java | 8 +++ .../com/flurgle/camerakit/CameraView.java | 10 ++++ camerakit/src/main/res/values/attrs.xml | 9 +++ .../com/flurgle/camerakit/VideoQuality.java | 18 ++++++ 7 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 camerakit/src/main/types/com/flurgle/camerakit/VideoQuality.java diff --git a/camerakit/src/main/api16/com/flurgle/camerakit/Camera1.java b/camerakit/src/main/api16/com/flurgle/camerakit/Camera1.java index 754f8312..f145d74b 100644 --- a/camerakit/src/main/api16/com/flurgle/camerakit/Camera1.java +++ b/camerakit/src/main/api16/com/flurgle/camerakit/Camera1.java @@ -8,7 +8,6 @@ import android.media.MediaRecorder; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.View; -import android.widget.Toast; import java.io.File; import java.io.IOException; @@ -59,6 +58,9 @@ public class Camera1 extends CameraImpl { @Zoom private int mZoom; + @VideoQuality + private int mVideoQuality; + Camera1(CameraListener callback, PreviewImpl preview) { super(callback, preview); preview.setCallback(new PreviewImpl.Callback() { @@ -192,6 +194,11 @@ public class Camera1 extends CameraImpl { this.mZoom = zoom; } + @Override + void setVideoQuality(int videoQuality) { + this.mVideoQuality = videoQuality; + } + @Override void captureImage() { switch (mMethod) { @@ -402,7 +409,8 @@ public class Camera1 extends CameraImpl { mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT); - mMediaRecorder.setProfile(CamcorderProfile.get(mCameraId, CamcorderProfile.QUALITY_HIGH)); + + mMediaRecorder.setProfile(getCamcorderProfile(mVideoQuality)); mVideoFile = new File(mPreview.getView().getContext().getExternalFilesDir(null), "video.mp4"); mMediaRecorder.setOutputFile(mVideoFile.getAbsolutePath()); @@ -419,6 +427,53 @@ public class Camera1 extends CameraImpl { } } + private CamcorderProfile getCamcorderProfile(@VideoQuality int videoQuality) { + CamcorderProfile camcorderProfile = null; + switch (videoQuality) { + case CameraKit.Constants.VIDEO_QUALITY_480P: + if (CamcorderProfile.hasProfile(mCameraId, CamcorderProfile.QUALITY_480P)) { + camcorderProfile = CamcorderProfile.get(mCameraId, CamcorderProfile.QUALITY_480P); + } else { + return getCamcorderProfile(CameraKit.Constants.VIDEO_QUALITY_LOWEST); + } + break; + + case CameraKit.Constants.VIDEO_QUALITY_720P: + if (CamcorderProfile.hasProfile(mCameraId, CamcorderProfile.QUALITY_720P)) { + camcorderProfile = CamcorderProfile.get(mCameraId, CamcorderProfile.QUALITY_720P); + } else { + return getCamcorderProfile(CameraKit.Constants.VIDEO_QUALITY_480P); + } + break; + + case CameraKit.Constants.VIDEO_QUALITY_1080P: + if (CamcorderProfile.hasProfile(mCameraId, CamcorderProfile.QUALITY_1080P)) { + camcorderProfile = CamcorderProfile.get(mCameraId, CamcorderProfile.QUALITY_1080P); + } else { + return getCamcorderProfile(CameraKit.Constants.VIDEO_QUALITY_720P); + } + break; + + case CameraKit.Constants.VIDEO_QUALITY_2160P: + try { + camcorderProfile = CamcorderProfile.get(mCameraId, CamcorderProfile.QUALITY_2160P); + } catch (Exception e) { + return getCamcorderProfile(CameraKit.Constants.VIDEO_QUALITY_HIGHEST); + } + break; + + case CameraKit.Constants.VIDEO_QUALITY_HIGHEST: + camcorderProfile = CamcorderProfile.get(mCameraId, CamcorderProfile.QUALITY_HIGH); + break; + + case CameraKit.Constants.VIDEO_QUALITY_LOWEST: + camcorderProfile = CamcorderProfile.get(mCameraId, CamcorderProfile.QUALITY_LOW); + break; + } + + return camcorderProfile; + } + void setTapToAutofocusListener(Camera.AutoFocusCallback callback) { if (this.mFocus != FOCUS_TAP) { throw new IllegalArgumentException("Please set the camera to FOCUS_TAP."); diff --git a/camerakit/src/main/api21/com/flurgle/camerakit/Camera2.java b/camerakit/src/main/api21/com/flurgle/camerakit/Camera2.java index 34e3e4ea..6936a411 100644 --- a/camerakit/src/main/api21/com/flurgle/camerakit/Camera2.java +++ b/camerakit/src/main/api21/com/flurgle/camerakit/Camera2.java @@ -119,6 +119,11 @@ class Camera2 extends CameraImpl { } + @Override + void setVideoQuality(int videoQuality) { + + } + @Override void captureImage() { diff --git a/camerakit/src/main/base/com/flurgle/camerakit/CameraImpl.java b/camerakit/src/main/base/com/flurgle/camerakit/CameraImpl.java index 80e6b8c5..90b5ed8b 100644 --- a/camerakit/src/main/base/com/flurgle/camerakit/CameraImpl.java +++ b/camerakit/src/main/base/com/flurgle/camerakit/CameraImpl.java @@ -20,6 +20,7 @@ abstract class CameraImpl { abstract void setFocus(@Focus int focus); abstract void setMethod(@Method int method); abstract void setZoom(@Zoom int zoom); + abstract void setVideoQuality(@VideoQuality int videoQuality); abstract void captureImage(); abstract void startVideo(); diff --git a/camerakit/src/main/java/com/flurgle/camerakit/CameraKit.java b/camerakit/src/main/java/com/flurgle/camerakit/CameraKit.java index 4bdd1365..578b12e1 100644 --- a/camerakit/src/main/java/com/flurgle/camerakit/CameraKit.java +++ b/camerakit/src/main/java/com/flurgle/camerakit/CameraKit.java @@ -38,6 +38,13 @@ public class CameraKit { public static final int PERMISSIONS_LAZY = 1; public static final int PERMISSIONS_PICTURE = 2; + 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; + } static class Defaults { @@ -48,6 +55,7 @@ public class CameraKit { static final int DEFAULT_ZOOM = Constants.ZOOM_OFF; static final int DEFAULT_METHOD = Constants.METHOD_STANDARD; static final int DEFAULT_PERMISSIONS = Constants.PERMISSIONS_STRICT; + static final int DEFAULT_VIDEO_QUALITY = Constants.VIDEO_QUALITY_480P; static final int DEFAULT_JPEG_QUALITY = 100; static final boolean DEFAULT_CROP_OUTPUT = false; diff --git a/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java b/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java index 43edd18c..058fb652 100644 --- a/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java +++ b/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java @@ -53,6 +53,9 @@ public class CameraView extends FrameLayout { @Permissions private int mPermissions; + @VideoQuality + private int mVideoQuality; + private int mJpegQuality; private boolean mCropOutput; private boolean mAdjustViewBounds; @@ -83,6 +86,7 @@ public class CameraView extends FrameLayout { mMethod = a.getInteger(R.styleable.CameraView_ckMethod, CameraKit.Defaults.DEFAULT_METHOD); mZoom = a.getInteger(R.styleable.CameraView_ckZoom, CameraKit.Defaults.DEFAULT_ZOOM); mPermissions = a.getInteger(R.styleable.CameraView_ckPermissions, CameraKit.Defaults.DEFAULT_PERMISSIONS); + mVideoQuality = a.getInteger(R.styleable.CameraView_ckVideoQuality, CameraKit.Defaults.DEFAULT_VIDEO_QUALITY); mJpegQuality = a.getInteger(R.styleable.CameraView_ckJpegQuality, CameraKit.Defaults.DEFAULT_JPEG_QUALITY); mCropOutput = a.getBoolean(R.styleable.CameraView_ckCropOutput, CameraKit.Defaults.DEFAULT_CROP_OUTPUT); mAdjustViewBounds = a.getBoolean(R.styleable.CameraView_android_adjustViewBounds, CameraKit.Defaults.DEFAULT_ADJUST_VIEW_BOUNDS); @@ -102,6 +106,7 @@ public class CameraView extends FrameLayout { setMethod(mMethod); setZoom(mZoom); setPermissions(mPermissions); + setVideoQuality(mVideoQuality); mDisplayOrientationDetector = new DisplayOrientationDetector(context) { @Override @@ -256,6 +261,11 @@ public class CameraView extends FrameLayout { this.mPermissions = permissions; } + public void setVideoQuality(@VideoQuality int videoQuality) { + this.mVideoQuality = videoQuality; + mCameraImpl.setVideoQuality(mVideoQuality); + } + public void setJpegQuality(int jpegQuality) { this.mJpegQuality = jpegQuality; } diff --git a/camerakit/src/main/res/values/attrs.xml b/camerakit/src/main/res/values/attrs.xml index 3e7d1b0f..e9f52705 100644 --- a/camerakit/src/main/res/values/attrs.xml +++ b/camerakit/src/main/res/values/attrs.xml @@ -43,6 +43,15 @@ + + + + + + + + + diff --git a/camerakit/src/main/types/com/flurgle/camerakit/VideoQuality.java b/camerakit/src/main/types/com/flurgle/camerakit/VideoQuality.java new file mode 100644 index 00000000..1d7d832f --- /dev/null +++ b/camerakit/src/main/types/com/flurgle/camerakit/VideoQuality.java @@ -0,0 +1,18 @@ +package com.flurgle.camerakit; + +import android.support.annotation.IntDef; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +import static com.flurgle.camerakit.CameraKit.Constants.VIDEO_QUALITY_1080P; +import static com.flurgle.camerakit.CameraKit.Constants.VIDEO_QUALITY_2160P; +import static com.flurgle.camerakit.CameraKit.Constants.VIDEO_QUALITY_480P; +import static com.flurgle.camerakit.CameraKit.Constants.VIDEO_QUALITY_720P; +import static com.flurgle.camerakit.CameraKit.Constants.VIDEO_QUALITY_HIGHEST; +import static com.flurgle.camerakit.CameraKit.Constants.VIDEO_QUALITY_LOWEST; + +@Retention(RetentionPolicy.SOURCE) +@IntDef({VIDEO_QUALITY_480P, VIDEO_QUALITY_720P, VIDEO_QUALITY_1080P, VIDEO_QUALITY_2160P, VIDEO_QUALITY_HIGHEST, VIDEO_QUALITY_LOWEST}) +public @interface VideoQuality { +}