Accessing AutoFocus/Flash with Google Vision BarCode Reader

后端 未结 3 1324
渐次进展
渐次进展 2021-01-12 07:43

I am playing with the original example of BarCode scanner here:

https://github.com/googlesamples/android-vision/blob/master/visionSamples/barcode-reader/app/src/main

3条回答
  •  时光说笑
    2021-01-12 08:24

    Just optimize your code as following and you have to call this method after building 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 to enable flash in Google Vision Api using camerasource object.

提交回复
热议问题