Skip to content

Commit

Permalink
try catch to handle start camera
Browse files Browse the repository at this point in the history
adjustCameraParameter npe fix
  • Loading branch information
ferdian-s committed Jul 18, 2018
1 parent e946df5 commit c17d032
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,13 @@ void adjustCameraParameters() {
}
Size size = chooseOptimalSize(sizes);

// Always re-apply camera parameters
// Largest picture size in this ratio
final Size pictureSize = mPictureSizes.sizes(mAspectRatio).last();
final Size pictureSize;
if (mPictureSizes.sizes(mAspectRatio) == null) {
pictureSize = size;
} else {
// Largest picture size in this ratio
pictureSize = mPictureSizes.sizes(mAspectRatio).last();
}
if (mShowingPreview) {
mCamera.stopPreview();
}
Expand Down
44 changes: 38 additions & 6 deletions library/src/main/api21/com/google/android/cameraview/Camera2.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,14 @@ boolean start() {
if (!chooseCameraIdByFacing()) {
return false;
}
collectCameraInfo();
prepareImageReader();
startOpeningCamera();
try {
collectCameraInfo();
prepareImageReader();
startOpeningCamera();
} catch(Exception e) {
Log.e(TAG, "Failed to start camera2", e);
mCallback.onCameraError(e);
}
return true;
}

Expand Down Expand Up @@ -345,7 +350,10 @@ void takePicture() {
try {
lockFocus();
} catch (Exception e) {
Log.e(TAG, "Failed to lockFocus(). captureStillPicture instead and turn off mAutoFocus", e);
Log.e(TAG,
"Failed to lockFocus(). captureStillPicture instead and turn off "
+ "mAutoFocus",
e);
captureStillPicture();
mAutoFocus = false;
}
Expand Down Expand Up @@ -460,12 +468,36 @@ private void prepareImageReader() {
if (mImageReader != null) {
mImageReader.close();
}
Size largest = mPictureSizes.sizes(mAspectRatio).last();
mImageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(),
final Size pictureSize;
SortedSet<Size> sizes = mPictureSizes.sizes(mAspectRatio);
if (sizes == null) { // Not supported
mAspectRatio = chooseAspectRatio();
if (mPictureSizes.sizes(mAspectRatio) == null) {
pictureSize = chooseOptimalSize();
} else {
// Largest picture size in this ratio
pictureSize = mPictureSizes.sizes(mAspectRatio).last();
}
} else {
pictureSize = sizes.last();
}

mImageReader = ImageReader.newInstance(pictureSize.getWidth(), pictureSize.getHeight(),
ImageFormat.JPEG, /* maxImages */ 2);
mImageReader.setOnImageAvailableListener(mOnImageAvailableListener, null);
}

private AspectRatio chooseAspectRatio() {
AspectRatio r = null;
for (AspectRatio ratio : mPreviewSizes.ratios()) {
r = ratio;
if (ratio.equals(Constants.DEFAULT_ASPECT_RATIO)) {
return ratio;
}
}
return r;
}

/**
* <p>Starts opening a camera device.</p>
* <p>The result will be processed in {@link #mCameraDeviceCallback}.</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,11 @@ public boolean getAdjustViewBounds() {
* {@link #FACING_FRONT}.
*/
public void setFacing(@Facing int facing) {
mImpl.setFacing(facing);
try {
mImpl.setFacing(facing);
} catch (Exception e) {
mCallbacks.onCameraError(e);
}
}

/**
Expand Down Expand Up @@ -426,7 +430,11 @@ public boolean getAutoFocus() {
* @param flash The desired flash mode.
*/
public void setFlash(@Flash int flash) {
mImpl.setFlash(flash);
try {
mImpl.setFlash(flash);
} catch (Exception e) {
mCallbacks.onCameraError(e);
}
}

/**
Expand Down Expand Up @@ -505,7 +513,8 @@ protected void createWindowFrame() {
R.drawable.frame_face_mask);
int height = face.getHeight();
int width = face.getWidth();
canvas.drawText(title, centerX, centerY - (height / 2) - dpToPx(TITLE_MARGIN_BOTTOM), textPaint);
canvas.drawText(title, centerX,
centerY - (height / 2) - dpToPx(TITLE_MARGIN_BOTTOM), textPaint);
canvas.drawBitmap(faceMask, centerX - (width / 2), centerY - (height / 2), paint);
canvas.drawBitmap(face, centerX - (width / 2), centerY - (height / 2), null);
break;
Expand All @@ -517,7 +526,8 @@ protected void createWindowFrame() {
R.drawable.frame_face_id_mask);
int height = face.getHeight();
int width = face.getWidth();
canvas.drawText(title, centerX, centerY - (height / 2) - dpToPx(TITLE_MARGIN_BOTTOM), textPaint);
canvas.drawText(title, centerX,
centerY - (height / 2) - dpToPx(TITLE_MARGIN_BOTTOM), textPaint);
canvas.drawBitmap(faceMask, centerX - (width / 2), centerY - (height / 2), paint);
canvas.drawBitmap(face, centerX - (width / 2), centerY - (height / 2), null);
break;
Expand All @@ -533,7 +543,8 @@ protected void createWindowFrame() {
break;
}
case PASSPORT: {
Bitmap picture = BitmapFactory.decodeResource(getResources(), R.drawable.frame_passport);
Bitmap picture = BitmapFactory.decodeResource(getResources(),
R.drawable.frame_passport);
drawRoundedRectBitmap(picture, centerX, centerY, canvas, radius, paint, textPaint);
break;
}
Expand All @@ -549,7 +560,8 @@ private void drawRoundedRectBitmap(Bitmap picture, int centerX, int centerY, Can
int radius, Paint paint, Paint textPaint) {
int height = picture.getHeight();
int width = picture.getWidth();
canvas.drawText(title, centerX, centerY - (height / 2) - dpToPx(TITLE_MARGIN_BOTTOM), textPaint);
canvas.drawText(title, centerX, centerY - (height / 2) - dpToPx(TITLE_MARGIN_BOTTOM),
textPaint);
// draw masking
canvas.drawPath(
roundedRect(centerX - (width / 2), centerY - (height / 2),
Expand Down

0 comments on commit c17d032

Please sign in to comment.