From 945f35e1e79b7487d5c250059598e316eb234cfd Mon Sep 17 00:00:00 2001 From: Dylan McIntyre Date: Tue, 24 Jan 2017 19:23:25 -0500 Subject: [PATCH] Refactor --- .../java/com/flurgle/camerakit/Camera1.java | 131 +++++++++--------- .../com/flurgle/camerakit/CameraView.java | 42 +++--- .../com/flurgle/camerakit/CameraViewImpl.java | 19 +-- .../flurgle/camerakit/annotations/Facing.java | 14 ++ .../flurgle/camerakit/annotations/Flash.java | 15 ++ .../camerakit/annotations/PictureMode.java | 14 ++ .../camerakit/annotations/TapToFocus.java | 15 ++ 7 files changed, 148 insertions(+), 102 deletions(-) create mode 100644 camerakit/src/main/java/com/flurgle/camerakit/annotations/Facing.java create mode 100644 camerakit/src/main/java/com/flurgle/camerakit/annotations/Flash.java create mode 100644 camerakit/src/main/java/com/flurgle/camerakit/annotations/PictureMode.java create mode 100644 camerakit/src/main/java/com/flurgle/camerakit/annotations/TapToFocus.java diff --git a/camerakit/src/main/java/com/flurgle/camerakit/Camera1.java b/camerakit/src/main/java/com/flurgle/camerakit/Camera1.java index 0d5cc0df..877ee4f5 100644 --- a/camerakit/src/main/java/com/flurgle/camerakit/Camera1.java +++ b/camerakit/src/main/java/com/flurgle/camerakit/Camera1.java @@ -21,10 +21,7 @@ import java.util.TreeSet; public class Camera1 extends CameraViewImpl { - private static final int INVALID_CAMERA_ID = -1; - private static final SparseArrayCompat FLASH_MODES = new SparseArrayCompat<>(); - static { FLASH_MODES.put(CameraKit.Constants.FLASH_OFF, Camera.Parameters.FLASH_MODE_OFF); FLASH_MODES.put(CameraKit.Constants.FLASH_ON, Camera.Parameters.FLASH_MODE_ON); @@ -34,11 +31,8 @@ public class Camera1 extends CameraViewImpl { private static File VIDEO_FILE; private int mCameraId; - Camera mCamera; - private Camera.Parameters mCameraParameters; - private final Camera.CameraInfo mCameraInfo = new Camera.CameraInfo(); private boolean mShowingPreview; @@ -72,7 +66,7 @@ public class Camera1 extends CameraViewImpl { @Override void start() { - chooseCamera(); + setFacingInternal(mFacing); openCamera(); if (mPreview.isReady()) { setUpPreview(); @@ -100,13 +94,25 @@ public class Camera1 extends CameraViewImpl { if (mFacing == facing) { return; } - mFacing = facing; - if (isCameraOpened()) { + if (setFacingInternal(facing) && isCameraOpened()) { stop(); start(); } } + private boolean setFacingInternal(int facing) { + for (int i = 0, count = Camera.getNumberOfCameras(); i < count; i++) { + Camera.getCameraInfo(i, mCameraInfo); + if (mCameraInfo.facing == facing) { + mCameraId = i; + mFacing = facing; + return true; + } + } + + return false; + } + @Override int getFacing() { return mFacing; @@ -122,11 +128,62 @@ public class Camera1 extends CameraViewImpl { } } + private boolean setFlashInternal(int flash) { + if (isCameraOpened()) { + List modes = mCameraParameters.getSupportedFlashModes(); + String mode = FLASH_MODES.get(flash); + if (modes != null && modes.contains(mode)) { + mCameraParameters.setFlashMode(mode); + mFlash = flash; + return true; + } + String currentMode = FLASH_MODES.get(mFlash); + if (modes == null || !modes.contains(currentMode)) { + mCameraParameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF); + mFlash = CameraKit.Constants.FLASH_OFF; + return true; + } + return false; + } else { + mFlash = flash; + return false; + } + } + @Override int getFlash() { return mFlash; } + @Override + void setAutoFocus(boolean autoFocus) { + if (autoFocus == mAutoFocus) { + return; + } + if (setAutoFocusInternal(autoFocus)) { + mCamera.setParameters(mCameraParameters); + } + } + + private boolean setAutoFocusInternal(boolean autoFocus) { + mAutoFocus = autoFocus; + if (isCameraOpened()) { + final List modes = mCameraParameters.getSupportedFocusModes(); + if (autoFocus && modes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) { + mCameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); + } else if (modes.contains(Camera.Parameters.FOCUS_MODE_FIXED)) { + mCameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_FIXED); + } else if (modes.contains(Camera.Parameters.FOCUS_MODE_INFINITY)) { + mCameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_INFINITY); + } else { + mCameraParameters.setFocusMode(modes.get(0)); + } + return true; + } else { + return false; + } + } + @Override boolean getAutoFocus() { if (!isCameraOpened()) { @@ -206,8 +263,6 @@ public class Camera1 extends CameraViewImpl { @Override void startVideo() { - if (!canRecordAudio()) return; - try { prepareMediaRecorder(); } catch (IOException e) { @@ -227,7 +282,7 @@ public class Camera1 extends CameraViewImpl { mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); - mMediaRecorder.setProfile(CamcorderProfile.get(mFacing == CameraKit.Constants.FACING_BACK ? CamcorderProfile.QUALITY_HIGH : CamcorderProfile.QUALITY_LOW)); + mMediaRecorder.setProfile(CamcorderProfile.get(mFacing == CameraKit.Constants.FACING_BACK ? CamcorderProfile.QUALITY_HIGH : CamcorderProfile.QUALITY_480P)); mMediaRecorder.setOutputFile(VIDEO_FILE.getAbsolutePath()); mMediaRecorder.setOrientationHint(mCameraInfo.orientation); @@ -349,17 +404,6 @@ public class Camera1 extends CameraViewImpl { return result; } - private void chooseCamera() { - for (int i = 0, count = Camera.getNumberOfCameras(); i < count; i++) { - Camera.getCameraInfo(i, mCameraInfo); - if (mCameraInfo.facing == mFacing) { - mCameraId = i; - return; - } - } - mCameraId = INVALID_CAMERA_ID; - } - private void openCamera() { if (mCamera != null) { releaseCamera(); @@ -391,45 +435,4 @@ public class Camera1 extends CameraViewImpl { } } - private boolean setAutoFocusInternal(boolean autoFocus) { - mAutoFocus = autoFocus; - if (isCameraOpened()) { - final List modes = mCameraParameters.getSupportedFocusModes(); - if (autoFocus && modes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) { - mCameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); - } else if (modes.contains(Camera.Parameters.FOCUS_MODE_FIXED)) { - mCameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_FIXED); - } else if (modes.contains(Camera.Parameters.FOCUS_MODE_INFINITY)) { - mCameraParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_INFINITY); - } else { - mCameraParameters.setFocusMode(modes.get(0)); - } - return true; - } else { - return false; - } - } - - private boolean setFlashInternal(int flash) { - if (isCameraOpened()) { - List modes = mCameraParameters.getSupportedFlashModes(); - String mode = FLASH_MODES.get(flash); - if (modes != null && modes.contains(mode)) { - mCameraParameters.setFlashMode(mode); - mFlash = flash; - return true; - } - String currentMode = FLASH_MODES.get(mFlash); - if (modes == null || !modes.contains(currentMode)) { - mCameraParameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF); - mFlash = CameraKit.Constants.FLASH_OFF; - return true; - } - return false; - } else { - mFlash = flash; - return false; - } - } - } diff --git a/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java b/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java index 467dfc30..f0f1fa2d 100644 --- a/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java +++ b/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java @@ -8,7 +8,6 @@ import android.content.pm.PackageManager; import android.content.res.TypedArray; import android.os.Parcel; import android.os.Parcelable; -import android.support.annotation.IntDef; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.ActivityCompat; @@ -21,11 +20,13 @@ import android.util.AttributeSet; import android.view.Display; import android.widget.FrameLayout; +import com.flurgle.camerakit.annotations.Facing; +import com.flurgle.camerakit.annotations.Flash; +import com.flurgle.camerakit.annotations.PictureMode; +import com.flurgle.camerakit.annotations.TapToFocus; import com.flurgle.camerakit.utils.DisplayOrientationDetector; import java.io.File; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; import static com.flurgle.camerakit.CameraKit.Constants.FACING_BACK; import static com.flurgle.camerakit.CameraKit.Constants.FACING_FRONT; @@ -34,8 +35,6 @@ import static com.flurgle.camerakit.CameraKit.Constants.FLASH_OFF; import static com.flurgle.camerakit.CameraKit.Constants.FLASH_ON; import static com.flurgle.camerakit.CameraKit.Constants.PICTURE_MODE_QUALITY; import static com.flurgle.camerakit.CameraKit.Constants.PICTURE_MODE_SPEED; -import static com.flurgle.camerakit.CameraKit.Constants.TAP_TO_FOCUS_INVISIBLE; -import static com.flurgle.camerakit.CameraKit.Constants.TAP_TO_FOCUS_OFF; import static com.flurgle.camerakit.CameraKit.Constants.TAP_TO_FOCUS_VISIBLE; public class CameraView extends FrameLayout { @@ -44,37 +43,24 @@ public class CameraView extends FrameLayout { private static final int DEFAULT_CAPTURE_SIZE = -1; - @IntDef({FACING_BACK, FACING_FRONT}) - @Retention(RetentionPolicy.SOURCE) - @interface Facing { - } - - @Retention(RetentionPolicy.SOURCE) - @IntDef({FLASH_OFF, FLASH_ON, FLASH_AUTO}) - @interface Flash { - } - - @Retention(RetentionPolicy.SOURCE) - @IntDef({PICTURE_MODE_QUALITY, PICTURE_MODE_SPEED}) - @interface PictureMode { - } - - @Retention(RetentionPolicy.SOURCE) - @IntDef({TAP_TO_FOCUS_VISIBLE, TAP_TO_FOCUS_INVISIBLE, TAP_TO_FOCUS_OFF}) - @interface TapToFocus { - } - + @Facing private int mFacing; + @Facing private int mDefaultFacing; + @Flash private int mFlash; + @Flash private int mDefaultFlash; + @PictureMode private int mPictureMode; private boolean mCropOutput; + @TapToFocus private int mTapToFocus; + private boolean mAutoFocus; private float mCaptureSize; @@ -142,6 +128,11 @@ public class CameraView extends FrameLayout { setFacing(mFacing); setFlash(mFlash); + setPictureMode(mPictureMode); + setCropOutput(mCropOutput); + setTapToFocus(mTapToFocus); + setAutoFocus(mAutoFocus); + setCaptureSize(mCaptureSize); mDisplayOrientationDetector = new DisplayOrientationDetector(context) { @Override @@ -192,7 +183,6 @@ public class CameraView extends FrameLayout { int permissionCheck = ContextCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA); if (permissionCheck == PackageManager.PERMISSION_GRANTED) { mWaitingForPermission = false; - mCameraImpl.setCanRecordAudio(true); mCameraImpl.start(); } else { requestCameraPermission(); diff --git a/camerakit/src/main/java/com/flurgle/camerakit/CameraViewImpl.java b/camerakit/src/main/java/com/flurgle/camerakit/CameraViewImpl.java index e9294374..c455f583 100644 --- a/camerakit/src/main/java/com/flurgle/camerakit/CameraViewImpl.java +++ b/camerakit/src/main/java/com/flurgle/camerakit/CameraViewImpl.java @@ -2,14 +2,15 @@ package com.flurgle.camerakit; import android.view.View; +import com.flurgle.camerakit.annotations.Facing; +import com.flurgle.camerakit.annotations.Flash; + public abstract class CameraViewImpl { protected CameraListener mCameraListener; protected final PreviewImpl mPreview; - private boolean mCanRecordAudio; - CameraViewImpl(CameraListener callback, PreviewImpl preview) { mCameraListener = callback; mPreview = preview; @@ -25,14 +26,16 @@ public abstract class CameraViewImpl { abstract boolean isCameraOpened(); - abstract void setFacing(int facing); + abstract void setFacing(@Facing int facing); abstract int getFacing(); - abstract void setFlash(int flash); + abstract void setFlash(@Flash int flash); abstract int getFlash(); + abstract void setAutoFocus(boolean autoFocus); + abstract boolean getAutoFocus(); abstract void capturePicture(); @@ -53,12 +56,4 @@ public abstract class CameraViewImpl { return mCameraListener != null ? mCameraListener : new CameraListener() {}; } - public void setCanRecordAudio(boolean canRecordAudio) { - this.mCanRecordAudio = canRecordAudio; - } - - protected boolean canRecordAudio() { - return mCanRecordAudio; - } - } diff --git a/camerakit/src/main/java/com/flurgle/camerakit/annotations/Facing.java b/camerakit/src/main/java/com/flurgle/camerakit/annotations/Facing.java new file mode 100644 index 00000000..426269e2 --- /dev/null +++ b/camerakit/src/main/java/com/flurgle/camerakit/annotations/Facing.java @@ -0,0 +1,14 @@ +package com.flurgle.camerakit.annotations; + +import android.support.annotation.IntDef; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +import static com.flurgle.camerakit.CameraKit.Constants.FACING_BACK; +import static com.flurgle.camerakit.CameraKit.Constants.FACING_FRONT; + +@IntDef({FACING_BACK, FACING_FRONT}) +@Retention(RetentionPolicy.SOURCE) +public @interface Facing { +} \ No newline at end of file diff --git a/camerakit/src/main/java/com/flurgle/camerakit/annotations/Flash.java b/camerakit/src/main/java/com/flurgle/camerakit/annotations/Flash.java new file mode 100644 index 00000000..c1ab2c22 --- /dev/null +++ b/camerakit/src/main/java/com/flurgle/camerakit/annotations/Flash.java @@ -0,0 +1,15 @@ +package com.flurgle.camerakit.annotations; + +import android.support.annotation.IntDef; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +import static com.flurgle.camerakit.CameraKit.Constants.FLASH_AUTO; +import static com.flurgle.camerakit.CameraKit.Constants.FLASH_OFF; +import static com.flurgle.camerakit.CameraKit.Constants.FLASH_ON; + +@Retention(RetentionPolicy.SOURCE) +@IntDef({FLASH_OFF, FLASH_ON, FLASH_AUTO}) +public @interface Flash { +} \ No newline at end of file diff --git a/camerakit/src/main/java/com/flurgle/camerakit/annotations/PictureMode.java b/camerakit/src/main/java/com/flurgle/camerakit/annotations/PictureMode.java new file mode 100644 index 00000000..d2f91c24 --- /dev/null +++ b/camerakit/src/main/java/com/flurgle/camerakit/annotations/PictureMode.java @@ -0,0 +1,14 @@ +package com.flurgle.camerakit.annotations; + +import android.support.annotation.IntDef; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +import static com.flurgle.camerakit.CameraKit.Constants.PICTURE_MODE_QUALITY; +import static com.flurgle.camerakit.CameraKit.Constants.PICTURE_MODE_SPEED; + +@Retention(RetentionPolicy.SOURCE) +@IntDef({PICTURE_MODE_QUALITY, PICTURE_MODE_SPEED}) +public @interface PictureMode { +} diff --git a/camerakit/src/main/java/com/flurgle/camerakit/annotations/TapToFocus.java b/camerakit/src/main/java/com/flurgle/camerakit/annotations/TapToFocus.java new file mode 100644 index 00000000..998c843c --- /dev/null +++ b/camerakit/src/main/java/com/flurgle/camerakit/annotations/TapToFocus.java @@ -0,0 +1,15 @@ +package com.flurgle.camerakit.annotations; + +import android.support.annotation.IntDef; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +import static com.flurgle.camerakit.CameraKit.Constants.TAP_TO_FOCUS_INVISIBLE; +import static com.flurgle.camerakit.CameraKit.Constants.TAP_TO_FOCUS_OFF; +import static com.flurgle.camerakit.CameraKit.Constants.TAP_TO_FOCUS_VISIBLE; + +@Retention(RetentionPolicy.SOURCE) +@IntDef({TAP_TO_FOCUS_VISIBLE, TAP_TO_FOCUS_INVISIBLE, TAP_TO_FOCUS_OFF}) +public @interface TapToFocus { +} \ No newline at end of file