Skip to content

Commit

Permalink
Add null guards for CV1
Browse files Browse the repository at this point in the history
  • Loading branch information
DaSpood committed Nov 2, 2023
1 parent 9a61dce commit 10c08a9
Showing 1 changed file with 50 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,11 @@ private void setAreas(Camera.Parameters prms) {
}

protected void refreshAutofocusZone() {
if (this.cam == null) {
Log.w(VIEW_LOG_TAG, "refreshAutofocusZone: No camera instance, make sure camera was properly initialized and `cleanup()` or `closeCamera()` were not called previously");
return;
}

if (autoFocusMode == null) {
return;
}
Expand All @@ -327,6 +332,11 @@ protected void refreshAutofocusZone() {
}

public void setPreviewResolution(Point newResolution) {
if (this.cam == null) {
Log.w(VIEW_LOG_TAG, "setPreviewResolution: No camera instance, make sure camera was properly initialized and `cleanup()` or `closeCamera()` were not called previously");
return;
}

Camera.Parameters prms = this.cam.getParameters();
pauseCamera();
resolution.currentPreviewResolution = newResolution;
Expand All @@ -337,6 +347,11 @@ public void setPreviewResolution(Point newResolution) {
}

private void setInitialBuffers(Camera.Parameters prms) {
if (this.cam == null) {
Log.w(VIEW_LOG_TAG, "setInitialBuffers: No camera instance, make sure camera was properly initialized and `cleanup()` or `closeCamera()` were not called previously");
return;
}

previewBufferSize = (int) (resolution.currentPreviewResolution.x * resolution.currentPreviewResolution.y * ImageFormat.getBitsPerPixel(prms.getPreviewFormat()) / 8f);
for (int i = 0; i < Runtime.getRuntime().availableProcessors() * 2; i++) {
this.cam.addCallbackBuffer(new byte[previewBufferSize]);
Expand Down Expand Up @@ -381,6 +396,11 @@ public boolean getTorchOn() {

@Override
void setTorchInternal(boolean value) {
if (this.cam == null) {
Log.w(VIEW_LOG_TAG, "setTorchInternal: No camera instance, make sure camera was properly initialized and `cleanup()` or `closeCamera()` were not called previously");
return;
}

Camera.Parameters prms = this.cam.getParameters();
if (value) {
prms.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
Expand All @@ -403,6 +423,11 @@ void setTorchInternal(boolean value) {
* @param prms Instance of camera configuration to apply
*/
public void setCameraParameters(Camera.Parameters prms) {
if (this.cam == null) {
Log.w(VIEW_LOG_TAG, "setCameraParameters: No camera instance, make sure camera was properly initialized and `cleanup()` or `closeCamera()` were not called previously");
return;
}

try {
this.cam.setParameters(prms);
} catch (Exception e) {
Expand Down Expand Up @@ -468,6 +493,11 @@ public void onPreviewFrame(byte[] data, Camera camera) {
}

public void giveBufferBackInternal(FrameAnalysisContext<byte[]> ctx) {
if (this.cam == null) {
Log.w(VIEW_LOG_TAG, "giveBufferBackInternal: No camera instance, make sure camera was properly initialized and `cleanup()` or `closeCamera()` were not called previously");
return;
}

if (ctx.originalImage.length == previewBufferSize) {
this.cam.addCallbackBuffer(ctx.originalImage);
}
Expand All @@ -482,6 +512,8 @@ public void pauseCamera() {
if (this.cam != null) {
this.cam.setPreviewCallbackWithBuffer(null);
this.cam.stopPreview();
} else {
Log.w(VIEW_LOG_TAG, "pauseCamera: No camera instance, make sure camera was properly initialized and `cleanup()` or `closeCamera()` were not called previously");
}
post(() -> {
if (this.targetView != null) {
Expand All @@ -496,6 +528,8 @@ public void resumeCamera() {
if (scanningStarted) {
this.cam.setPreviewCallback(this);
}
} else {
Log.w(VIEW_LOG_TAG, "resumeCamera: No camera instance, make sure camera was properly initialized and `cleanup()` or `closeCamera()` were not called previously");
}
post(() -> {
if (this.targetView != null) {
Expand All @@ -505,6 +539,10 @@ public void resumeCamera() {
}

public void startScanner() {
if (this.cam == null) {
Log.w(VIEW_LOG_TAG, "startScanner: No camera instance, make sure camera was properly initialized and `cleanup()` or `closeCamera()` were not called previously");
return;
}
Log.d(VIEW_LOG_TAG, "Scanning started");
this.scanningStarted = true;
this.cam.setPreviewCallback(this);
Expand All @@ -520,20 +558,24 @@ public void pauseScanner() {
////////////////////////////////////////////////////////////////////////////////////////////////
// Clean up methods
public void cleanUp() {
if (this.cam != null) {
Log.i(TAG, "Removing all camera callbacks and stopping it");
this.cam.setPreviewCallback(null);
this.cam.stopPreview();
this.setOnClickListener(null);
this.lastSuccessfulScanData = null;
this.frameAnalyser.close();
this.frameAnalyser = null;
if (this.cam == null) {
Log.w(VIEW_LOG_TAG, "cleanup: No camera instance, make sure camera was properly initialized and `cleanup()` or `closeCamera()` were not called previously");
return;
}

Log.i(TAG, "Removing all camera callbacks and stopping it");
this.cam.setPreviewCallback(null);
this.cam.stopPreview();
this.setOnClickListener(null);
this.lastSuccessfulScanData = null;
this.frameAnalyser.close();
this.frameAnalyser = null;
closeCamera();
}

void closeCamera() {
if (this.cam == null) {
Log.w(VIEW_LOG_TAG, "closeCamera: No camera instance, make sure camera was properly initialized and `cleanup()` or `closeCamera()` were not called previously");
return;
}
Log.i(TAG, "Camera is being released");
Expand Down

0 comments on commit 10c08a9

Please sign in to comment.