Skip to content

Commit

Permalink
Add null guards for CV2
Browse files Browse the repository at this point in the history
  • Loading branch information
DaSpood committed Nov 2, 2023
1 parent 10c08a9 commit 753e628
Showing 1 changed file with 48 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,37 @@ public CameraBarcodeScanViewV2(@NonNull Context context, @Nullable AttributeSet
init();
}

/**
* Checks all attributes necessary for camera operations and returns true if any is null.
* None of these attributes are expected to be null unless the camera is either uninitialized, paused or closed.
*
* @param caller The name of the caller function, for logging
* @return true if any camera-related attribute is null.
*/
private boolean anyAttributeMissing(String caller) {
if (cameraManager == null) {
Log.w(TAG, caller + ": No cameraManager instance, make sure camera was properly initialized");
return true;
}
if (captureSession == null) {
Log.w(TAG, caller + ": No captureSession instance, make sure camera was properly initialized and `pauseCamera()` or `closeCamera()` were not called previously");
return true;
}
if (captureRequestBuilder == null) {
Log.w(TAG, caller + ": No captureRequestBuilder instance, make sure camera was properly initialized and `closeCamera()` was not called previously");
return true;
}
if (cameraDevice == null) {
Log.w(TAG, caller + ": No cameraDevice instance, make sure camera was properly initialized and not used by another application or view, and `closeCamera()` was not called previously");
return true;
}
if (imageReader == null) {
Log.w(TAG, caller + ": No imageReader instance, make sure camera was properly initialized and `pauseCamera()` or `closeCamera()` were not called previously");
return true;
}
return false;
}

private void init() {
Log.d(TAG, "Camera2 specific initialization start");

Expand Down Expand Up @@ -287,6 +318,10 @@ private MeteringRectangle getMeteringZone() {
}

protected void refreshAutofocusZone() {
if (anyAttributeMissing("refreshAutofocusZone")) {
return;
}

if (afZones > 0) {
CameraCaptureSession.CaptureCallback captureHandler = new CameraCaptureSession.CaptureCallback() {
@Override
Expand Down Expand Up @@ -380,13 +415,17 @@ private synchronized void closeCamera() {
if (null != captureSession) {
Log.d(TAG, " * Closing camera capture session");

captureRequestBuilder.removeTarget(this.camPreviewSurfaceView.getHolder().getSurface());
captureRequestBuilder = null;

captureSession.close();
captureSession = null;
}

if (null != captureRequestBuilder) {
Log.d(TAG, " * Clearing camera capture builder");

captureRequestBuilder.removeTarget(this.camPreviewSurfaceView.getHolder().getSurface());
captureRequestBuilder = null;
}

if (null != cameraDevice) {
Log.d(TAG, " * Camera device closing");
cameraDevice.close();
Expand Down Expand Up @@ -452,9 +491,10 @@ public void resumeCamera() {

@Override
void setTorchInternal(boolean value) {
if (captureSession == null) {
if (anyAttributeMissing("setTorchInternal")) {
return;
}

try {
if (value) {
captureRequestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_TORCH);
Expand Down Expand Up @@ -768,7 +808,7 @@ public void onClosed(@NonNull CameraCaptureSession session) {
}

// Sanity check
if (image.getPlanes()[0].getPixelStride() != 1) {
if (image.getPlanes()[0].getPixelStride() != 1 || image.getPlanes()[0].getBuffer() == null) {
throw new RuntimeException("not a luminance buffer");
}

Expand Down Expand Up @@ -918,7 +958,7 @@ private YuvImage mediaImageToYuvImage(Image image) {
// Copy Y channel.
int yRowStride = yPlane.getRowStride();
int yPixelStride = yPlane.getPixelStride();
for(int y = 0; y < height; ++y) {
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
nv21[index++] = yBuffer.get(y * yRowStride + x * yPixelStride);
}
Expand All @@ -931,7 +971,7 @@ private YuvImage mediaImageToYuvImage(Image image) {
int uvWidth = width / 2;
int uvHeight = height / 2;

for(int y = 0; y < uvHeight; ++y) {
for (int y = 0; y < uvHeight; ++y) {
for (int x = 0; x < uvWidth; ++x) {
int bufferIndex = (y * uvRowStride) + (x * uvPixelStride);
// V channel.
Expand All @@ -949,7 +989,7 @@ public byte[] getLatestSuccessfulScanJpeg() {
if (lastSuccessfulScanData != null) {
Log.d(TAG, "Convert last saved image to JPEG");
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
YuvImage img = mediaImageToYuvImage((Image)lastSuccessfulScanData.originalImage);
YuvImage img = mediaImageToYuvImage((Image) lastSuccessfulScanData.originalImage);
Rect imgRect = new Rect(0, 0, resolution.currentPreviewResolution.x, resolution.currentPreviewResolution.y);
img.compressToJpeg(imgRect, 100, buffer);

Expand Down

0 comments on commit 753e628

Please sign in to comment.