Skip to content

Commit

Permalink
#149 - Wrap CV2's OnConfigred in a try/catch (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaSpood authored Jan 17, 2024
1 parent ecc1a5e commit 6e117cc
Showing 1 changed file with 48 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -694,60 +694,64 @@ public void onConfigured(@NonNull final CameraCaptureSession cameraCaptureSessio
return;
}

// We need our threads
initializeFrameAnalyzerIfNeeded();
try {
// We need our threads
initializeFrameAnalyzerIfNeeded();

// If here, preview session is open and we can start the actual preview.
CameraBarcodeScanViewV2.this.captureSession = cameraCaptureSession;
// If here, preview session is open and we can start the actual preview.
CameraBarcodeScanViewV2.this.captureSession = cameraCaptureSession;

// Create builder
try {
captureRequestBuilder = CameraBarcodeScanViewV2.this.cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
} catch (CameraAccessException | IllegalStateException e) {
Log.e(TAG, "cannot use createCaptureRequest " + e);
cameraCaptureSession.close();
return;
}
// Create builder
try {
captureRequestBuilder = CameraBarcodeScanViewV2.this.cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
} catch (CameraAccessException | IllegalStateException e) {
Log.e(TAG, "cannot use createCaptureRequest " + e);
cameraCaptureSession.close();
return;
}

// Capture targets
captureRequestBuilder.addTarget(CameraBarcodeScanViewV2.this.camPreviewSurfaceView.getHolder().getSurface());
if (imageReader == null) {
Log.d(TAG, "stopping init - view is being stopped");
return;
}
captureRequestBuilder.addTarget(imageReader.getSurface());
// Capture targets
captureRequestBuilder.addTarget(CameraBarcodeScanViewV2.this.camPreviewSurfaceView.getHolder().getSurface());
if (imageReader == null) {
Log.d(TAG, "stopping init - view is being stopped");
return;
}
captureRequestBuilder.addTarget(imageReader.getSurface());

// Full auto (without this AF & AE are mostly disabled)
captureRequestBuilder.set(CaptureRequest.CONTROL_MODE, CaptureRequest.CONTROL_MODE_AUTO);
// Full auto (without this AF & AE are mostly disabled)
captureRequestBuilder.set(CaptureRequest.CONTROL_MODE, CaptureRequest.CONTROL_MODE_AUTO);

// AB
if (controlModeAb != null) {
captureRequestBuilder.set(CaptureRequest.CONTROL_AE_ANTIBANDING_MODE, controlModeAb);
}
// AB
if (controlModeAb != null) {
captureRequestBuilder.set(CaptureRequest.CONTROL_AE_ANTIBANDING_MODE, controlModeAb);
}

// AF & AE
if (afZones > 0) {
captureRequestBuilder.set(CaptureRequest.CONTROL_AF_REGIONS, new MeteringRectangle[]{getMeteringZone()});
}
if (controlModeAf != null) {
Log.d(TAG, "Setting AF mode to " + controlModeAf);
captureRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, controlModeAf);
}
// AF & AE
if (afZones > 0) {
captureRequestBuilder.set(CaptureRequest.CONTROL_AF_REGIONS, new MeteringRectangle[]{getMeteringZone()});
}
if (controlModeAf != null) {
Log.d(TAG, "Setting AF mode to " + controlModeAf);
captureRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, controlModeAf);
}

//captureRequestBuilder.set(CaptureRequest.CONTROL_AE_LOCK, false);
//captureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
//captureRequestBuilder.set(CaptureRequest.CONTROL_AWB_MODE, CaptureRequest.CONTROL_AWB_MODE_AUTO);
//captureRequestBuilder.set(CaptureRequest.CONTROL_AE_LOCK, false);
//captureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
//captureRequestBuilder.set(CaptureRequest.CONTROL_AWB_MODE, CaptureRequest.CONTROL_AWB_MODE_AUTO);

captureRequestBuilder.set(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, previewFpsRange);
captureRequestBuilder.set(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, previewFpsRange);

// GO for preview
CameraBarcodeScanViewV2.this.captureRequest = captureRequestBuilder.build();
// GO for preview
CameraBarcodeScanViewV2.this.captureRequest = captureRequestBuilder.build();

try {
captureSession.setRepeatingRequest(CameraBarcodeScanViewV2.this.captureRequest, null, backgroundHandler);
} catch (CameraAccessException | IllegalStateException e) {
Log.w(TAG, "Camera loop start has failed, this is usually due to changing resolution too fast. Error was: " + e.getMessage(), e);
CameraBarcodeScanViewV2.this.closeCamera();
try {
captureSession.setRepeatingRequest(CameraBarcodeScanViewV2.this.captureRequest, null, backgroundHandler);
} catch (CameraAccessException | IllegalStateException e) {
Log.w(TAG, "Camera loop start has failed, this is usually due to changing resolution too fast. Error was: " + e.getMessage(), e);
CameraBarcodeScanViewV2.this.closeCamera();
}
} catch (final NullPointerException e) {
Log.e(TAG, "Camera was destroyed while the initialization process was running", e);
}

Log.i(TAG, "Camera repeating capture request was set up " + CameraBarcodeScanViewV2.this.hashCode());
Expand Down

0 comments on commit 6e117cc

Please sign in to comment.