From 6b651aae65401ac341890de7008de4a394e6f75c Mon Sep 17 00:00:00 2001 From: Jacob Hargrave Date: Tue, 22 Mar 2016 11:48:35 -0400 Subject: [PATCH] Wrapping some code in try catches, so they don't fail on older devices. Samsung Tab 4 was crashing because it didn't have a flash or autofocus. --- android/ZBarScannerActivity.java | 37 ++++++++++++++++---------------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/android/ZBarScannerActivity.java b/android/ZBarScannerActivity.java index 881da3f..9f23fa2 100644 --- a/android/ZBarScannerActivity.java +++ b/android/ZBarScannerActivity.java @@ -333,19 +333,20 @@ implements SurfaceHolder.Callback { } - public void toggleFlash(View view) - { - - - camera.startPreview(); + public void toggleFlash(View view) { + camera.startPreview(); android.hardware.Camera.Parameters camParams = camera.getParameters(); //If the flash is set to off - if(camParams.getFlashMode().equals(Parameters.FLASH_MODE_OFF) && !(camParams.getFlashMode().equals(Parameters.FLASH_MODE_TORCH))&& !(camParams.getFlashMode().equals(Parameters.FLASH_MODE_ON))) - camParams.setFlashMode(Parameters.FLASH_MODE_TORCH); - else //if(camParams.getFlashMode() == Parameters.FLASH_MODE_ON || camParams.getFlashMode()== Parameters.FLASH_MODE_TORCH) - camParams.setFlashMode(Parameters.FLASH_MODE_OFF); - try - { + try { + if (camParams.getFlashMode().equals(Parameters.FLASH_MODE_OFF) && !(camParams.getFlashMode().equals(Parameters.FLASH_MODE_TORCH)) && !(camParams.getFlashMode().equals(Parameters.FLASH_MODE_ON))) + camParams.setFlashMode(Parameters.FLASH_MODE_TORCH); + else //if(camParams.getFlashMode() == Parameters.FLASH_MODE_ON || camParams.getFlashMode()== Parameters.FLASH_MODE_TORCH) + camParams.setFlashMode(Parameters.FLASH_MODE_OFF); + } catch(RuntimeException e) { + + } + + try { // camera.setParameters(camParams); camera.setPreviewDisplay(holder); camera.setPreviewCallback(previewCb); @@ -362,11 +363,7 @@ implements SurfaceHolder.Callback { //tryStartPreview(); //camParams.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); camera.setParameters(camParams); - - - } - catch(RuntimeException e) - { + } catch(RuntimeException e) { Log.d("csZBar", (new StringBuilder("Unsupported camera parameter reported for flash mode: ")).append(flashMode).toString()); } catch (IOException e) { Log.d("csZBar", (new StringBuilder("Wrong holder data")).append(flashMode).toString()); @@ -531,8 +528,12 @@ implements SurfaceHolder.Callback { //camParams.setFlashMode(Parameters.FLASH_MODE_TORCH); - camParams.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); - camera.setParameters(camParams); + try { + camParams.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); + camera.setParameters(camParams); + } catch (Exception e) { + // TODO: don't swallow + } camera.setPreviewDisplay(holder); camera.setPreviewCallback(previewCb);