pull/1/head
Dylan McIntyre 8 years ago
parent f7498f0c36
commit 945f35e1e7
  1. 131
      camerakit/src/main/java/com/flurgle/camerakit/Camera1.java
  2. 42
      camerakit/src/main/java/com/flurgle/camerakit/CameraView.java
  3. 19
      camerakit/src/main/java/com/flurgle/camerakit/CameraViewImpl.java
  4. 14
      camerakit/src/main/java/com/flurgle/camerakit/annotations/Facing.java
  5. 15
      camerakit/src/main/java/com/flurgle/camerakit/annotations/Flash.java
  6. 14
      camerakit/src/main/java/com/flurgle/camerakit/annotations/PictureMode.java
  7. 15
      camerakit/src/main/java/com/flurgle/camerakit/annotations/TapToFocus.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<String> 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<String> 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<String> 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<String> 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<String> 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;
}
}
}

@ -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();

@ -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;
}
}

@ -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 {
}

@ -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 {
}

@ -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 {
}

@ -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 {
}
Loading…
Cancel
Save