Skip to content

Commit

Permalink
catch "camera unlock failed" exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
derohimat committed Oct 3, 2018
1 parent 2da2f8c commit cedcb5c
Showing 1 changed file with 57 additions and 65 deletions.
122 changes: 57 additions & 65 deletions cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ class Camera1 extends CameraController implements Camera.PreviewCallback, Camera

private static final String TAG = Camera1.class.getSimpleName();
private static final CameraLogger LOG = CameraLogger.create(TAG);

private final int mPostFocusResetDelay = 3000;
private Camera mCamera;
private boolean mIsBound = false;

private final int mPostFocusResetDelay = 3000;
private Runnable mPostFocusResetRunnable = new Runnable() {
@Override
public void run() {
Expand All @@ -53,6 +51,46 @@ public void run() {
mMapper = new Mapper.Mapper1();
}

@WorkerThread
private static List<Camera.Area> computeMeteringAreas(double viewClickX, double viewClickY,
int viewWidth, int viewHeight,
int sensorToDisplay) {
// Event came in view coordinates. We must rotate to sensor coordinates.
// First, rescale to the -1000 ... 1000 range.
int displayToSensor = -sensorToDisplay;
viewClickX = -1000d + (viewClickX / (double) viewWidth) * 2000d;
viewClickY = -1000d + (viewClickY / (double) viewHeight) * 2000d;

// Apply rotation to this point.
// https://academo.org/demos/rotation-about-point/
double theta = ((double) displayToSensor) * Math.PI / 180;
double sensorClickX = viewClickX * Math.cos(theta) - viewClickY * Math.sin(theta);
double sensorClickY = viewClickX * Math.sin(theta) + viewClickY * Math.cos(theta);
LOG.i("focus:", "viewClickX:", viewClickX, "viewClickY:", viewClickY);
LOG.i("focus:", "sensorClickX:", sensorClickX, "sensorClickY:", sensorClickY);

// Compute the rect bounds.
Rect rect1 = computeMeteringArea(sensorClickX, sensorClickY, 150d);
int weight1 = 1000; // 150 * 150 * 1000 = more than 10.000.000
Rect rect2 = computeMeteringArea(sensorClickX, sensorClickY, 300d);
int weight2 = 100; // 300 * 300 * 100 = 9.000.000

List<Camera.Area> list = new ArrayList<>(2);
list.add(new Camera.Area(rect1, weight1));
list.add(new Camera.Area(rect2, weight2));
return list;
}

private static Rect computeMeteringArea(double centerX, double centerY, double size) {
double delta = size / 2d;
int top = (int) Math.max(centerY - delta, -1000);
int bottom = (int) Math.min(centerY + delta, 1000);
int left = (int) Math.max(centerX - delta, -1000);
int right = (int) Math.min(centerX + delta, 1000);
LOG.i("focus:", "computeMeteringArea:", "top:", top, "left:", left, "bottom:", bottom, "right:", right);
return new Rect(left, top, right, bottom);
}

private void schedule(@Nullable final Task<Void> task, final boolean ensureAvailable, final Runnable action) {
mHandler.post(new Runnable() {
@Override
Expand Down Expand Up @@ -278,9 +316,14 @@ public void onError(int error, Camera camera) {
Exception runtime = new RuntimeException(CameraLogger.lastMessage);
int reason;
switch (error) {
case Camera.CAMERA_ERROR_EVICTED: reason = CameraException.REASON_DISCONNECTED; break;
case Camera.CAMERA_ERROR_UNKNOWN: reason = CameraException.REASON_UNKNOWN; break;
default: reason = CameraException.REASON_UNKNOWN;
case Camera.CAMERA_ERROR_EVICTED:
reason = CameraException.REASON_DISCONNECTED;
break;
case Camera.CAMERA_ERROR_UNKNOWN:
reason = CameraException.REASON_UNKNOWN;
break;
default:
reason = CameraException.REASON_UNKNOWN;
}
throw new CameraException(runtime, reason);
}
Expand Down Expand Up @@ -403,7 +446,6 @@ private boolean mergePlaySound(boolean oldPlaySound) {
return false;
}


@Override
void setAudio(Audio audio) {
if (mAudio != audio) {
Expand All @@ -428,7 +470,6 @@ public void run() {
});
}


private boolean mergeFlash(Camera.Parameters params, Flash oldFlash) {
if (mCameraOptions.supports(mFlash)) {
params.setFlashMode((String) mMapper.map(mFlash));
Expand All @@ -438,7 +479,6 @@ private boolean mergeFlash(Camera.Parameters params, Flash oldFlash) {
return false;
}


// Choose the best default focus, based on session type.
private void applyDefaultFocus(Camera.Parameters params) {
List<String> modes = params.getSupportedFocusModes();
Expand All @@ -465,7 +505,6 @@ private void applyDefaultFocus(Camera.Parameters params) {
}
}


@Override
void setVideoQuality(VideoQuality videoQuality) {
final VideoQuality old = mVideoQuality;
Expand Down Expand Up @@ -539,7 +578,6 @@ public void onPictureTaken(byte[] data, final Camera camera) {
});
}


@Override
void captureSnapshot() {
LOG.v("captureSnapshot: scheduling");
Expand Down Expand Up @@ -596,6 +634,9 @@ public void run() {
});
}

// -----------------
// Video recording stuff.

@Override
public void onPreviewFrame(byte[] data, Camera camera) {
Frame frame = mFrameManager.getFrame(data,
Expand Down Expand Up @@ -626,10 +667,6 @@ private boolean isCameraAvailable() {
return false;
}

// -----------------
// Video recording stuff.


@Override
void startVideo(@NonNull final File videoFile) {
schedule(mStartVideoTask, true, new Runnable() {
Expand All @@ -639,8 +676,8 @@ public void run() {
if (mSessionType == SessionType.VIDEO) {
mVideoFile = videoFile;
mIsCapturingVideo = true;
initMediaRecorder();
try {
initMediaRecorder();
mMediaRecorder.prepare();
mMediaRecorder.start();
mCameraCallbacks.dispatchOnMediaRecorderChanged(mMediaRecorder);
Expand All @@ -667,6 +704,9 @@ public void run() {
});
}

// -----------------
// Zoom and simpler stuff.

@WorkerThread
private void endVideoImmediately() {
LOG.i("endVideoImmediately:", "is capturing:", mIsCapturingVideo);
Expand Down Expand Up @@ -743,8 +783,7 @@ public void onInfo(MediaRecorder mediaRecorder, int what, int extra) {
}

// -----------------
// Zoom and simpler stuff.

// Tap to focus stuff.

@Override
void setZoom(final float zoom, final PointF[] points, final boolean notify) {
Expand Down Expand Up @@ -791,10 +830,6 @@ public void run() {
});
}

// -----------------
// Tap to focus stuff.


@Override
void startAutoFocus(@Nullable final Gesture gesture, final PointF point) {
// Must get width and height from the UI thread.
Expand Down Expand Up @@ -839,52 +874,9 @@ public void onAutoFocus(boolean success, Camera camera) {
}


@WorkerThread
private static List<Camera.Area> computeMeteringAreas(double viewClickX, double viewClickY,
int viewWidth, int viewHeight,
int sensorToDisplay) {
// Event came in view coordinates. We must rotate to sensor coordinates.
// First, rescale to the -1000 ... 1000 range.
int displayToSensor = -sensorToDisplay;
viewClickX = -1000d + (viewClickX / (double) viewWidth) * 2000d;
viewClickY = -1000d + (viewClickY / (double) viewHeight) * 2000d;

// Apply rotation to this point.
// https://academo.org/demos/rotation-about-point/
double theta = ((double) displayToSensor) * Math.PI / 180;
double sensorClickX = viewClickX * Math.cos(theta) - viewClickY * Math.sin(theta);
double sensorClickY = viewClickX * Math.sin(theta) + viewClickY * Math.cos(theta);
LOG.i("focus:", "viewClickX:", viewClickX, "viewClickY:", viewClickY);
LOG.i("focus:", "sensorClickX:", sensorClickX, "sensorClickY:", sensorClickY);

// Compute the rect bounds.
Rect rect1 = computeMeteringArea(sensorClickX, sensorClickY, 150d);
int weight1 = 1000; // 150 * 150 * 1000 = more than 10.000.000
Rect rect2 = computeMeteringArea(sensorClickX, sensorClickY, 300d);
int weight2 = 100; // 300 * 300 * 100 = 9.000.000

List<Camera.Area> list = new ArrayList<>(2);
list.add(new Camera.Area(rect1, weight1));
list.add(new Camera.Area(rect2, weight2));
return list;
}


private static Rect computeMeteringArea(double centerX, double centerY, double size) {
double delta = size / 2d;
int top = (int) Math.max(centerY - delta, -1000);
int bottom = (int) Math.min(centerY + delta, 1000);
int left = (int) Math.max(centerX - delta, -1000);
int right = (int) Math.min(centerX + delta, 1000);
LOG.i("focus:", "computeMeteringArea:", "top:", top, "left:", left, "bottom:", bottom, "right:", right);
return new Rect(left, top, right, bottom);
}


// -----------------
// Size stuff.


@Nullable
private List<Size> sizesFromList(List<Camera.Size> sizes) {
if (sizes == null) return null;
Expand Down

0 comments on commit cedcb5c

Please sign in to comment.