使用Google Vision条形码阅读器访问AutoFocus/Flash [英] Accessing AutoFocus/Flash with Google Vision BarCode Reader

查看:114
本文介绍了使用Google Vision条形码阅读器访问AutoFocus/Flash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里玩条形码扫描器的原始示例:

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

他们能够像这样在相机工厂内启动AutoFocus/Flash:

They are able to start the AutoFocus/Flash within the camera factory like this:

    // 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();

但是,cameraSource构建器上的此方法在当前版本中已消失,因此无法访问此设置.另外,我还需要在使用过程中更改FlashMode,所以也不是这样做的方法.我找到了访问相机的丑陋解决方案:

However this method on cameraSource builder is gone in current version and so this setting cannot be accessed. Also I need to change the FlashMode during usage, so that is not the way to do it either. I found this ugly solution to accessing 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;
}

尽管有效,但无济于事:调用getParameters().setFocusMode()时出现此异常:

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

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

很明显,我正在做的事情不是正确的方法,但是似乎没有关于它的文档.

Obviously what I am doing is not a right way to do it, but there seem to be no documentation about it.

感谢提示.

推荐答案

只需按以下方式优化代码,并且在构建Camera Source类之后必须调用此方法.

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;
}

这将帮助您使用camerasource对象在Google Vision Api中启用闪光灯.

This will help you to enable flash in Google Vision Api using camerasource object.

这篇关于使用Google Vision条形码阅读器访问AutoFocus/Flash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆