extend camera errors by exception specific information

pull/59/head
Tim H 8 years ago
parent 4aef821f41
commit 77160163b8
  1. 9
      README.md
  2. 55
      cameraview/src/main/java/com/otaliastudios/cameraview/Camera1.java
  3. 30
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraConfigurationFailedException.java
  4. 5
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java
  5. 34
      cameraview/src/main/java/com/otaliastudios/cameraview/CapturingVideoFailedException.java

@ -200,7 +200,13 @@ camera.addCameraListener(new CameraListener() {
} }
else if (exception instanceof CameraConfigurationFailedException) { else if (exception instanceof CameraConfigurationFailedException) {
// The previously started setting change failed, // The previously started setting change failed,
// but the camera should be still available. // but the camera should be still available.
if (exception.getConfiguration() == CONFIGURATION_FACING) {
// change GUI to reflect failed changes
}
else if (exception.getConfiguration() == CONFIGURATION_WHITE_BALANCE) {
// I don't care
}
} }
else if (exception instanceof CapturingFailedException) { else if (exception instanceof CapturingFailedException) {
// The previously started capturing (of any kind) failed, but the camera // The previously started capturing (of any kind) failed, but the camera
@ -224,6 +230,7 @@ camera.addCameraListener(new CameraListener() {
else if (exception instanceof CapturingVideoFailedException) { else if (exception instanceof CapturingVideoFailedException) {
// The previously started video capturing failed, but the camera // The previously started video capturing failed, but the camera
// should be still available. // should be still available.
Video failedVideoFile = exception.getVideo();
} }
} }
else { else {

@ -24,6 +24,17 @@ import static android.hardware.Camera.CAMERA_ERROR_SERVER_DIED;
import static android.hardware.Camera.CAMERA_ERROR_UNKNOWN; import static android.hardware.Camera.CAMERA_ERROR_UNKNOWN;
import static android.media.MediaRecorder.MEDIA_ERROR_SERVER_DIED; import static android.media.MediaRecorder.MEDIA_ERROR_SERVER_DIED;
import static android.media.MediaRecorder.MEDIA_RECORDER_ERROR_UNKNOWN; import static android.media.MediaRecorder.MEDIA_RECORDER_ERROR_UNKNOWN;
import static com.otaliastudios.cameraview.CameraConfigurationFailedException.CONFIGURATION_EXPOSURE_CORRECTION;
import static com.otaliastudios.cameraview.CameraConfigurationFailedException.CONFIGURATION_FACING;
import static com.otaliastudios.cameraview.CameraConfigurationFailedException.CONFIGURATION_FLASH;
import static com.otaliastudios.cameraview.CameraConfigurationFailedException.CONFIGURATION_FOCUS;
import static com.otaliastudios.cameraview.CameraConfigurationFailedException.CONFIGURATION_HDR;
import static com.otaliastudios.cameraview.CameraConfigurationFailedException.CONFIGURATION_LOCATION;
import static com.otaliastudios.cameraview.CameraConfigurationFailedException.CONFIGURATION_OTHER;
import static com.otaliastudios.cameraview.CameraConfigurationFailedException.CONFIGURATION_UNKNOWN;
import static com.otaliastudios.cameraview.CameraConfigurationFailedException.CONFIGURATION_VIDEO_QUALITY;
import static com.otaliastudios.cameraview.CameraConfigurationFailedException.CONFIGURATION_WHITE_BALANCE;
import static com.otaliastudios.cameraview.CameraConfigurationFailedException.CONFIGURATION_ZOOM;
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@ -59,7 +70,7 @@ class Camera1 extends CameraController {
// problem may be device-specific to the Samsung Galaxy J5 // problem may be device-specific to the Samsung Galaxy J5
// TODO why does it fail occasionally and is it possible to prevent such errors? // TODO why does it fail occasionally and is it possible to prevent such errors?
CameraException cameraException = new CameraConfigurationFailedException("Failed to " + CameraException cameraException = new CameraConfigurationFailedException("Failed to " +
"reset auto focus.", e); "reset auto focus.", CONFIGURATION_FOCUS, e);
mCameraCallbacks.onError(cameraException); mCameraCallbacks.onError(cameraException);
} }
} }
@ -205,11 +216,11 @@ class Camera1 extends CameraController {
} }
else if (errorCode == CAMERA_ERROR_UNKNOWN) { else if (errorCode == CAMERA_ERROR_UNKNOWN) {
cameraException = new CameraConfigurationFailedException( cameraException = new CameraConfigurationFailedException(
"Unspecified camera error."); "Unspecified camera error.", CONFIGURATION_UNKNOWN);
} }
else { else {
cameraException = new CameraConfigurationFailedException( cameraException = new CameraConfigurationFailedException(
"Received camera error code: " + errorCode); "Received camera error code: " + errorCode, CONFIGURATION_OTHER);
} }
// redirect error // redirect error
@ -320,7 +331,8 @@ class Camera1 extends CameraController {
} }
catch (Exception e) { catch (Exception e) {
CameraException cameraException = CameraException cameraException =
new CameraConfigurationFailedException("Failed to set the location.", e); new CameraConfigurationFailedException("Failed to set the location.",
CONFIGURATION_LOCATION, e);
mCameraCallbacks.onError(cameraException); mCameraCallbacks.onError(cameraException);
} }
} }
@ -359,7 +371,8 @@ class Camera1 extends CameraController {
// -> undo failed configuration change // -> undo failed configuration change
mFacing = oldFacing; mFacing = oldFacing;
CameraException cameraException = CameraException cameraException =
new CameraConfigurationFailedException("Failed to set the camera facing.", e); new CameraConfigurationFailedException("Failed to set the camera facing.",
CONFIGURATION_FACING, e);
mCameraCallbacks.onError(cameraException); mCameraCallbacks.onError(cameraException);
} }
} }
@ -380,7 +393,8 @@ class Camera1 extends CameraController {
catch (Exception e) { catch (Exception e) {
// TODO handle, !mergeWhiteBalance, too? // TODO handle, !mergeWhiteBalance, too?
CameraException cameraException = CameraException cameraException =
new CameraConfigurationFailedException("Failed to set the white balance.", e); new CameraConfigurationFailedException("Failed to set the white balance.",
CONFIGURATION_WHITE_BALANCE, e);
mCameraCallbacks.onError(cameraException); mCameraCallbacks.onError(cameraException);
} }
} }
@ -409,7 +423,8 @@ class Camera1 extends CameraController {
catch (Exception e) { catch (Exception e) {
// TODO handle, !mergeHdr, too? // TODO handle, !mergeHdr, too?
CameraException cameraException = CameraException cameraException =
new CameraConfigurationFailedException("Failed to set hdr.", e); new CameraConfigurationFailedException("Failed to set hdr.", CONFIGURATION_HDR,
e);
mCameraCallbacks.onError(cameraException); mCameraCallbacks.onError(cameraException);
} }
} }
@ -449,7 +464,8 @@ class Camera1 extends CameraController {
catch (Exception e) { catch (Exception e) {
// TODO handle, !mergeFlash, too? // TODO handle, !mergeFlash, too?
CameraException cameraException = CameraException cameraException =
new CameraConfigurationFailedException("Failed to set flash.", e); new CameraConfigurationFailedException("Failed to set flash.",
CONFIGURATION_FLASH, e);
mCameraCallbacks.onError(cameraException); mCameraCallbacks.onError(cameraException);
} }
} }
@ -523,7 +539,8 @@ class Camera1 extends CameraController {
} }
catch (Exception e) { catch (Exception e) {
CameraException cameraException = CameraException cameraException =
new CameraConfigurationFailedException("Failed to set video quality.", e); new CameraConfigurationFailedException("Failed to set video quality.",
CONFIGURATION_VIDEO_QUALITY, e);
mCameraCallbacks.onError(cameraException); mCameraCallbacks.onError(cameraException);
} }
} }
@ -734,7 +751,7 @@ class Camera1 extends CameraController {
} catch (Exception e) { } catch (Exception e) {
CameraException cameraException = CameraException cameraException =
new CapturingVideoFailedException("Error while starting MediaRecorder. " + new CapturingVideoFailedException("Error while starting MediaRecorder. " +
"Swallowing.", e); "Swallowing.", videoFile, e);
mCameraCallbacks.onError(cameraException); mCameraCallbacks.onError(cameraException);
mVideoFile = null; mVideoFile = null;
mCamera.lock(); mCamera.lock();
@ -743,7 +760,8 @@ class Camera1 extends CameraController {
} }
} else { } else {
CameraException cameraException = CameraException cameraException =
new CapturingVideoFailedException("Can't record video while session type is picture"); new CapturingVideoFailedException("Can't record video while session type is " +
"picture", videoFile);
mCameraCallbacks.onError(cameraException); mCameraCallbacks.onError(cameraException);
return false; return false;
} }
@ -816,7 +834,7 @@ class Camera1 extends CameraController {
cameraException = new CapturingVideoFailedException( cameraException = new CapturingVideoFailedException(
"Media server died while capturing a video. In this case, the" + "Media server died while capturing a video. In this case, the" +
"application must release the MediaRecorder object and " + "application must release the MediaRecorder object and " +
"instantiate a new one." + extraInfo); "instantiate a new one." + extraInfo, mVideoFile);
} }
else { else {
cameraException = new CameraUnavailableException( cameraException = new CameraUnavailableException(
@ -828,12 +846,13 @@ class Camera1 extends CameraController {
else if (what == MEDIA_RECORDER_ERROR_UNKNOWN) { else if (what == MEDIA_RECORDER_ERROR_UNKNOWN) {
// TODO may we need a CapturingVideoFailedException or CameraUnavailableException here, too? // TODO may we need a CapturingVideoFailedException or CameraUnavailableException here, too?
cameraException = new CameraConfigurationFailedException( cameraException = new CameraConfigurationFailedException(
"Unspecified media recorder error." + extraInfo); "Unspecified media recorder error." + extraInfo, CONFIGURATION_UNKNOWN);
} }
else { else {
// TODO may we need a CapturingVideoFailedException or CameraUnavailableException here, too? // TODO may we need a CapturingVideoFailedException or CameraUnavailableException here, too?
cameraException = new CameraConfigurationFailedException( cameraException = new CameraConfigurationFailedException(
"Received media recorder error code: " + what + "." + extraInfo); "Received media recorder error code: " + what + "." + extraInfo,
CONFIGURATION_OTHER);
} }
// redirect error // redirect error
@ -941,7 +960,8 @@ class Camera1 extends CameraController {
} }
catch (Exception e) { catch (Exception e) {
CameraException cameraException = CameraException cameraException =
new CameraConfigurationFailedException("Failed to set zoom.", e); new CameraConfigurationFailedException("Failed to set zoom.",
CONFIGURATION_ZOOM, e);
mCameraCallbacks.onError(cameraException); mCameraCallbacks.onError(cameraException);
return false; return false;
} }
@ -967,7 +987,8 @@ class Camera1 extends CameraController {
} }
catch (Exception e) { catch (Exception e) {
CameraException cameraException = CameraException cameraException =
new CameraConfigurationFailedException("Failed to set exposure correction.", e); new CameraConfigurationFailedException("Failed to set exposure correction.",
CONFIGURATION_EXPOSURE_CORRECTION, e);
mCameraCallbacks.onError(cameraException); mCameraCallbacks.onError(cameraException);
return false; return false;
} }
@ -1014,7 +1035,7 @@ class Camera1 extends CameraController {
// at least getParameters and setParameters may fail. // at least getParameters and setParameters may fail.
// TODO why do they fail and is it possible to prevent such errors? // TODO why do they fail and is it possible to prevent such errors?
CameraException cameraException = new CameraConfigurationFailedException("Failed to " + CameraException cameraException = new CameraConfigurationFailedException("Failed to " +
"start auto focus.", e); "start auto focus.", CONFIGURATION_FOCUS, e);
mCameraCallbacks.onError(cameraException); mCameraCallbacks.onError(cameraException);
return false; return false;
} }

@ -6,11 +6,37 @@ package com.otaliastudios.cameraview;
*/ */
public class CameraConfigurationFailedException extends CameraException { public class CameraConfigurationFailedException extends CameraException {
CameraConfigurationFailedException(String message) { // possible values for this.configuration
public final static int CONFIGURATION_UNKNOWN = -1;
public final static int CONFIGURATION_OTHER = 0;
public final static int CONFIGURATION_FACING = 1;
public final static int CONFIGURATION_FLASH = 2;
public final static int CONFIGURATION_FOCUS = 3;
public final static int CONFIGURATION_ZOOM = 4;
public final static int CONFIGURATION_VIDEO_QUALITY = 5;
public final static int CONFIGURATION_JPEG_QUALITY = 6;
public final static int CONFIGURATION_HDR = 7;
public final static int CONFIGURATION_LOCATION = 8;
public final static int CONFIGURATION_EXPOSURE_CORRECTION = 9;
public final static int CONFIGURATION_WHITE_BALANCE = 10;
private int configuration;
CameraConfigurationFailedException(String message, int configuration) {
super(message); super(message);
this.configuration = configuration;
} }
CameraConfigurationFailedException(String message, Throwable cause) { CameraConfigurationFailedException(String message, int configuration, Throwable cause) {
super(message, cause); super(message, cause);
this.configuration = configuration;
}
/**
* Get the type of configuration that failed.
* @return
*/
public int getConfiguration() {
return configuration;
} }
} }

@ -35,6 +35,7 @@ import static android.view.View.MeasureSpec.EXACTLY;
import static android.view.View.MeasureSpec.UNSPECIFIED; import static android.view.View.MeasureSpec.UNSPECIFIED;
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT; import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static com.otaliastudios.cameraview.CameraConfigurationFailedException.CONFIGURATION_JPEG_QUALITY;
public class CameraView extends FrameLayout { public class CameraView extends FrameLayout {
@ -1034,7 +1035,7 @@ public class CameraView extends FrameLayout {
IllegalArgumentException illegalArgumentException = new IllegalArgumentException illegalArgumentException = new
IllegalArgumentException("JPEG quality should be > 0 and <= 100"); IllegalArgumentException("JPEG quality should be > 0 and <= 100");
CameraException cameraException = new CameraConfigurationFailedException("Could not" + CameraException cameraException = new CameraConfigurationFailedException("Could not" +
" set JpegQuality", illegalArgumentException); " set JpegQuality", CONFIGURATION_JPEG_QUALITY, illegalArgumentException);
mCameraCallbacks.onError(cameraException); mCameraCallbacks.onError(cameraException);
} }
mJpegQuality = jpegQuality; mJpegQuality = jpegQuality;
@ -1206,7 +1207,7 @@ public class CameraView extends FrameLayout {
public void startCapturingVideo(File file, long durationMillis) { public void startCapturingVideo(File file, long durationMillis) {
if (durationMillis < 500) { if (durationMillis < 500) {
CameraException cameraException = new CapturingVideoFailedException("Video duration" + CameraException cameraException = new CapturingVideoFailedException("Video duration" +
" can't be < 500 milliseconds"); " can't be < 500 milliseconds", file);
mCameraCallbacks.onError(cameraException); mCameraCallbacks.onError(cameraException);
} }
startCapturingVideo(file); startCapturingVideo(file);

@ -1,4 +1,7 @@
package com.otaliastudios.cameraview; package com.otaliastudios.cameraview;
import android.support.annotation.NonNull;
import java.io.File;
/** /**
* An object of this class describes an error that occurred during the normal runtime of the camera. * An object of this class describes an error that occurred during the normal runtime of the camera.
@ -6,11 +9,38 @@ package com.otaliastudios.cameraview;
*/ */
public class CapturingVideoFailedException extends CapturingFailedException { public class CapturingVideoFailedException extends CapturingFailedException {
CapturingVideoFailedException(String message) { @NonNull
private File video;
/**
*
* @param message
* @param video The video file that was meant to store the captured video.
*/
CapturingVideoFailedException(String message, @NonNull File video) {
super(message); super(message);
this.video = video;
} }
CapturingVideoFailedException(String message, Throwable cause) { /**
*
* @param message
* @param video The video file that was meant to store the captured video.
* @param cause
*/
CapturingVideoFailedException(String message, @NonNull File video, Throwable cause) {
super(message, cause); super(message, cause);
this.video = video;
}
/**
* Get the video file that was meant to store the captured video.
* The physical file itself will usually not exist (anymore), but you can use the file object
* information for further processing.
* @return
*/
@NonNull
public File getVideo() {
return video;
} }
} }
Loading…
Cancel
Save