fetch errors that prevent the camera from being initialized in the first place

pull/59/head
Tim H 8 years ago
parent da61217f7b
commit d2d6978a3c
  1. 23
      README.md
  2. 158
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java

@ -162,6 +162,29 @@ camera.addCameraListener(new CameraListener() {
}); });
``` ```
Some rare exceptions can occur during initialization of the camera. This prevents any further camera
usage and occurs before you can attach your custom error handler.
So you should additionally wrap the camera initialization in a try-catch-block:
```java
try {
cameraView = new CameraView(context);
}
catch (CameraException e) {
// camera is (temporary) unavailable
}
```
Or if you created the CameraView by adding it to your XML layout:
```java
try {
inflater.inflate(R.layout.camera, mainContainer, false);
}
catch (CameraException e) {
// camera is (temporary) unavailable
}
```
#### Advanced Custom Handler #### Advanced Custom Handler
Furthermore, you can distinguish different error types. E.g. some of them imply that the Furthermore, you can distinguish different error types. E.g. some of them imply that the
CameraView should be disabled or restarted while others may be ignored in some use cases. CameraView should be disabled or restarted while others may be ignored in some use cases.

@ -75,11 +75,22 @@ public class CameraView extends FrameLayout {
private Handler mUiHandler; private Handler mUiHandler;
private WorkerHandler mWorkerHandler; private WorkerHandler mWorkerHandler;
/**
*
* @param context
* @throws CameraUnavailableException if the initialization failed for any (camera-related) reason.
*/
public CameraView(@NonNull Context context) { public CameraView(@NonNull Context context) {
super(context, null); super(context, null);
init(context, null); init(context, null);
} }
/**
*
* @param context
* @param attrs
* @throws CameraUnavailableException if the initialization failed for any (camera-related) reason.
*/
public CameraView(@NonNull Context context, @Nullable AttributeSet attrs) { public CameraView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs); super(context, attrs);
init(context, attrs); init(context, attrs);
@ -87,74 +98,89 @@ public class CameraView extends FrameLayout {
//region Init //region Init
/**
*
* @param context
* @param attrs
* @throws CameraUnavailableException if the initialization failed for any (camera-related) reason.
*/
@SuppressWarnings("WrongConstant") @SuppressWarnings("WrongConstant")
private void init(@NonNull Context context, @Nullable AttributeSet attrs) { private void init(@NonNull Context context, @Nullable AttributeSet attrs) {
setWillNotDraw(false); try {
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CameraView, 0, 0); setWillNotDraw(false);
// Self managed TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CameraView, 0, 0);
int jpegQuality = a.getInteger(R.styleable.CameraView_cameraJpegQuality, DEFAULT_JPEG_QUALITY); // Self managed
boolean cropOutput = a.getBoolean(R.styleable.CameraView_cameraCropOutput, DEFAULT_CROP_OUTPUT); int jpegQuality = a.getInteger(R.styleable.CameraView_cameraJpegQuality, DEFAULT_JPEG_QUALITY);
boolean playSounds = a.getBoolean(R.styleable.CameraView_cameraPlaySounds, DEFAULT_PLAY_SOUNDS); boolean cropOutput = a.getBoolean(R.styleable.CameraView_cameraCropOutput, DEFAULT_CROP_OUTPUT);
boolean playSounds = a.getBoolean(R.styleable.CameraView_cameraPlaySounds, DEFAULT_PLAY_SOUNDS);
// Camera controller params
Facing facing = Facing.fromValue(a.getInteger(R.styleable.CameraView_cameraFacing, Facing.DEFAULT.value())); // Camera controller params
Flash flash = Flash.fromValue(a.getInteger(R.styleable.CameraView_cameraFlash, Flash.DEFAULT.value())); Facing facing = Facing.fromValue(a.getInteger(R.styleable.CameraView_cameraFacing, Facing.DEFAULT.value()));
Grid grid = Grid.fromValue(a.getInteger(R.styleable.CameraView_cameraGrid, Grid.DEFAULT.value())); Flash flash = Flash.fromValue(a.getInteger(R.styleable.CameraView_cameraFlash, Flash.DEFAULT.value()));
WhiteBalance whiteBalance = WhiteBalance.fromValue(a.getInteger(R.styleable.CameraView_cameraWhiteBalance, WhiteBalance.DEFAULT.value())); Grid grid = Grid.fromValue(a.getInteger(R.styleable.CameraView_cameraGrid, Grid.DEFAULT.value()));
VideoQuality videoQuality = VideoQuality.fromValue(a.getInteger(R.styleable.CameraView_cameraVideoQuality, VideoQuality.DEFAULT.value())); WhiteBalance whiteBalance = WhiteBalance.fromValue(a.getInteger(R.styleable.CameraView_cameraWhiteBalance, WhiteBalance.DEFAULT.value()));
SessionType sessionType = SessionType.fromValue(a.getInteger(R.styleable.CameraView_cameraSessionType, SessionType.DEFAULT.value())); VideoQuality videoQuality = VideoQuality.fromValue(a.getInteger(R.styleable.CameraView_cameraVideoQuality, VideoQuality.DEFAULT.value()));
Hdr hdr = Hdr.fromValue(a.getInteger(R.styleable.CameraView_cameraHdr, Hdr.DEFAULT.value())); SessionType sessionType = SessionType.fromValue(a.getInteger(R.styleable.CameraView_cameraSessionType, SessionType.DEFAULT.value()));
Audio audio = Audio.fromValue(a.getInteger(R.styleable.CameraView_cameraAudio, Audio.DEFAULT.value())); Hdr hdr = Hdr.fromValue(a.getInteger(R.styleable.CameraView_cameraHdr, Hdr.DEFAULT.value()));
Audio audio = Audio.fromValue(a.getInteger(R.styleable.CameraView_cameraAudio, Audio.DEFAULT.value()));
// Gestures
GestureAction tapGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGestureTap, GestureAction.DEFAULT_TAP.value())); // Gestures
GestureAction longTapGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGestureLongTap, GestureAction.DEFAULT_LONG_TAP.value())); GestureAction tapGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGestureTap, GestureAction.DEFAULT_TAP.value()));
GestureAction pinchGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGesturePinch, GestureAction.DEFAULT_PINCH.value())); GestureAction longTapGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGestureLongTap, GestureAction.DEFAULT_LONG_TAP.value()));
GestureAction scrollHorizontalGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGestureScrollHorizontal, GestureAction.DEFAULT_SCROLL_HORIZONTAL.value())); GestureAction pinchGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGesturePinch, GestureAction.DEFAULT_PINCH.value()));
GestureAction scrollVerticalGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGestureScrollVertical, GestureAction.DEFAULT_SCROLL_VERTICAL.value())); GestureAction scrollHorizontalGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGestureScrollHorizontal, GestureAction.DEFAULT_SCROLL_HORIZONTAL.value()));
a.recycle(); GestureAction scrollVerticalGesture = GestureAction.fromValue(a.getInteger(R.styleable.CameraView_cameraGestureScrollVertical, GestureAction.DEFAULT_SCROLL_VERTICAL.value()));
a.recycle();
// Components
mCameraCallbacks = new Callbacks(); // Components
mCameraController = instantiateCameraController(mCameraCallbacks); mCameraCallbacks = new Callbacks();
mUiHandler = new Handler(Looper.getMainLooper()); mCameraController = instantiateCameraController(mCameraCallbacks);
mWorkerHandler = WorkerHandler.get("CameraViewWorker"); mUiHandler = new Handler(Looper.getMainLooper());
mWorkerHandler = WorkerHandler.get("CameraViewWorker");
// Views
mGridLinesLayout = new GridLinesLayout(context); // Views
mPinchGestureLayout = new PinchGestureLayout(context); mGridLinesLayout = new GridLinesLayout(context);
mTapGestureLayout = new TapGestureLayout(context); mPinchGestureLayout = new PinchGestureLayout(context);
mScrollGestureLayout = new ScrollGestureLayout(context); mTapGestureLayout = new TapGestureLayout(context);
addView(mGridLinesLayout); mScrollGestureLayout = new ScrollGestureLayout(context);
addView(mPinchGestureLayout); addView(mGridLinesLayout);
addView(mTapGestureLayout); addView(mPinchGestureLayout);
addView(mScrollGestureLayout); addView(mTapGestureLayout);
addView(mScrollGestureLayout);
// Apply self managed
setCropOutput(cropOutput); // Apply self managed
setJpegQuality(jpegQuality); setCropOutput(cropOutput);
setPlaySounds(playSounds); setJpegQuality(jpegQuality);
setPlaySounds(playSounds);
// Apply camera controller params
setFacing(facing); // Apply camera controller params
setFlash(flash); setFacing(facing);
setSessionType(sessionType); setFlash(flash);
setVideoQuality(videoQuality); setSessionType(sessionType);
setWhiteBalance(whiteBalance); setVideoQuality(videoQuality);
setGrid(grid); setWhiteBalance(whiteBalance);
setHdr(hdr); setGrid(grid);
setAudio(audio); setHdr(hdr);
setAudio(audio);
// Apply gestures
mapGesture(Gesture.TAP, tapGesture); // Apply gestures
// mapGesture(Gesture.DOUBLE_TAP, doubleTapGesture); mapGesture(Gesture.TAP, tapGesture);
mapGesture(Gesture.LONG_TAP, longTapGesture); // mapGesture(Gesture.DOUBLE_TAP, doubleTapGesture);
mapGesture(Gesture.PINCH, pinchGesture); mapGesture(Gesture.LONG_TAP, longTapGesture);
mapGesture(Gesture.SCROLL_HORIZONTAL, scrollHorizontalGesture); mapGesture(Gesture.PINCH, pinchGesture);
mapGesture(Gesture.SCROLL_VERTICAL, scrollVerticalGesture); mapGesture(Gesture.SCROLL_HORIZONTAL, scrollHorizontalGesture);
mapGesture(Gesture.SCROLL_VERTICAL, scrollVerticalGesture);
if (!isInEditMode()) {
mOrientationHelper = new OrientationHelper(context, mCameraCallbacks); if (!isInEditMode()) {
mOrientationHelper = new OrientationHelper(context, mCameraCallbacks);
}
}
catch (CameraException e) {
// Some rare exceptions can occur during the camera initialization.
// For instance, Camera.getCameraInfo() may fail for inevitable hardware reasons.
// If the initialization failed for any (camera-related) reason, the camera gets
// completely unavailable.
throw new CameraUnavailableException("Failed to initialize the camera.", e);
} }
} }

Loading…
Cancel
Save