Skip to content

Commit

Permalink
add camera error callback on camera.open
Browse files Browse the repository at this point in the history
  • Loading branch information
ferdian-s committed Jul 18, 2018
1 parent 36560d9 commit 54591b3
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 23 deletions.
51 changes: 28 additions & 23 deletions library/src/main/api14/com/google/android/cameraview/Camera1.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,30 +305,35 @@ private void openCamera() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
mCamera = Camera.open(mCameraId);
mCameraParameters = mCamera.getParameters();
// Supported preview sizes
mPreviewSizes.clear();
for (Camera.Size size : mCameraParameters.getSupportedPreviewSizes()) {
mPreviewSizes.add(new Size(size.width, size.height));
}
// Supported picture sizes;
mPictureSizes.clear();
for (Camera.Size size : mCameraParameters.getSupportedPictureSizes()) {
mPictureSizes.add(new Size(size.width, size.height));
}
// AspectRatio
if (mAspectRatio == null) {
mAspectRatio = Constants.DEFAULT_ASPECT_RATIO;
}
adjustCameraParameters();
mCamera.setDisplayOrientation(calcDisplayOrientation(mDisplayOrientation));
mCallback.onCameraOpened();
if (mPreview.isReady()) {
setUpPreview();
try {
mCamera = Camera.open(mCameraId);
mCameraParameters = mCamera.getParameters();
// Supported preview sizes
mPreviewSizes.clear();
for (Camera.Size size : mCameraParameters.getSupportedPreviewSizes()) {
mPreviewSizes.add(new Size(size.width, size.height));
}
// Supported picture sizes;
mPictureSizes.clear();
for (Camera.Size size : mCameraParameters.getSupportedPictureSizes()) {
mPictureSizes.add(new Size(size.width, size.height));
}
// AspectRatio
if (mAspectRatio == null) {
mAspectRatio = Constants.DEFAULT_ASPECT_RATIO;
}
adjustCameraParameters();
mCamera.setDisplayOrientation(calcDisplayOrientation(mDisplayOrientation));
mCallback.onCameraOpened();
if (mPreview.isReady()) {
setUpPreview();
}
mShowingPreview = true;
mCamera.startPreview();
} catch (Exception e) {
Log.e("Camera1", "on open camera error", e);
mCallback.onCameraError(e);
}
mShowingPreview = true;
mCamera.startPreview();
}
}, delay);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ interface Callback {

void onPictureTaken(byte[] data);

void onCameraError(Exception e);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,13 @@ public void onPictureTaken(byte[] data) {
}
}

@Override
public void onCameraError(Exception e) {
for (Callback callback : mCallbacks) {
callback.onCameraError(CameraView.this, e);
}
}

public void reserveRequestLayoutOnOpen() {
mRequestLayoutOnOpen = true;
}
Expand Down Expand Up @@ -732,6 +739,14 @@ public void onCameraOpened(CameraView cameraView) {
public void onCameraClosed(CameraView cameraView) {
}

/**
* Called when camera failed to open.
*
* @param cameraView The associated {@link CameraView}.
*/
public void onCameraError(CameraView cameraView, Exception e) {
}

/**
* Called when a picture is taken.
*
Expand Down

0 comments on commit 54591b3

Please sign in to comment.