Accessing AutoFocus/Flash with Google Vision BarCode Reader

时光总嘲笑我的痴心妄想 提交于 2019-12-01 03:53:32

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.

Just use the CameraSource directly.

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

And you will have the ability to use flash/focus.

CameraSource
.setFlashMode()
.setFocusMode()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!