Access AutoFocus / Flash with Google Vision BarCode Reader

I play with the original BarCode scanner example here:

https://github.com/googlesamples/android-vision/blob/master/visionSamples/barcode-reader/app/src/main/java/com/google/android/gms/samples/vision/barcodereader/BarcodeCaptureActivity.java

They can run AutoFocus / Flash in the factory camera as follows:

// Creates and starts the camera. Note that this uses a higher resolution in comparison // to other detection examples to enable the barcode detector to detect small barcodes // at long distances. CameraSource.Builder builder = new CameraSource.Builder(getApplicationContext(), barcodeDetector) .setFacing(CameraSource.CAMERA_FACING_BACK) .setRequestedPreviewSize(1600, 1024) .setRequestedFps(15.0f); // make sure that auto focus is an available option if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { builder = builder.setFocusMode( autoFocus ? Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE : null); } mCameraSource = builder .setFlashMode(useFlash ? Camera.Parameters.FLASH_MODE_TORCH : null) .build(); 

However, this method on camSource builder is not available in the current version, so this option is not available. I also need to change FlashMode during use, so this is not the case either. I found this ugly solution to access the camera:

 public static Camera getCamera(@NonNull CameraSource cameraSource) { Field[] declaredFields = CameraSource.class.getDeclaredFields(); for (Field field : declaredFields) { if (field.getType() == Camera.class) { field.setAccessible(true); try { Camera camera = (Camera) field.get(cameraSource); if (camera != null) { return camera; } return null; } catch (IllegalAccessException e) { e.printStackTrace(); } break; } } return null; } 

Although this works, it does not help: when I getParameters().setFocusMode() I get this exception:

 Attempt to invoke virtual method 'android.hardware.Camera$Parameters android.hardware.Camera.getParameters()' on a null object reference 

Obviously, what I'm doing is not the right way to do this, but there seems to be no documentation about this.

Thanks for the tips.

+6
source share
1 answer

Just optimize your code as follows, and you should call this method after creating the Camera Source class.

 private Camera camera = null; boolean flashmode=false; private void flashOnButton() { camera=getCamera(mCameraSource); if (camera != null) { try { Camera.Parameters param = camera.getParameters(); param.setFlashMode(!flashmode?Camera.Parameters.FLASH_MODE_TORCH :Camera.Parameters.FLASH_MODE_OFF); camera.setParameters(param); flashmode = !flashmode; if(flashmode){ showToast("Flash Switched ON"); } else { showToast("Flash Switched Off"); } } catch (Exception e) { e.printStackTrace(); } } } private static Camera getCamera(@NonNull CameraSource cameraSource) { Field[] declaredFields = CameraSource.class.getDeclaredFields(); for (Field field : declaredFields) { if (field.getType() == Camera.class) { field.setAccessible(true); try { Camera camera = (Camera) field.get(cameraSource); if (camera != null) { return camera; } return null; } catch (IllegalAccessException e) { e.printStackTrace(); } break; } } return null; } 

This will help you enable flash in Google Vision Api using the sourceource object.

+4
source

All Articles