How turn on flashlight using Barcode Detection in

2019-05-25 02:16发布

I'm trying to reimplement Redlaser barcode Scanner using Google play services. And face to the problem with flashlight. Android hardware.Camera object can't be using in common with CameraSource from gms.vision. Is there any opportunity to working with flashlight and Google barcode scanner?

3条回答
2楼-- · 2019-05-25 02:37

Currently I'm using this code to find camera object:

private boolean findCameraObject(){
    if(mCameraSource == null) {
        return false;
    }

    Field[] declaredFields = null;
    try {
        declaredFields = CameraSource.class.getDeclaredFields();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    if(declaredFields == null) {
        return false;
    }

    for (Field field : declaredFields) {
        if (field.getType() == Camera.class) {
            field.setAccessible(true);
            try {
                Camera camera = (Camera) field.get(this.mCameraSource);
                if (camera != null) {
                    Camera.Parameters params = camera.getParameters();
                    params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
                    camera.setParameters(params);
                    setCamera(camera);
                    return true;
                }

                return false;
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            break;
        }
    }
    return false;
}
查看更多
Juvenile、少年°
3楼-- · 2019-05-25 02:40

Not sure I fully get what you're asking but my approach to this was to use the already created mCamerSource Object and setFlashMode() from there, this worked for me as I used a button to toggle the flash.

In your onCreate add this or in createCameraSource method just like in the samples ->

mCameraSource = builder
            .setFlashMode(useFlash ? Camera.Parameters.FLASH_MODE_TORCH : null)
            .build();

Then Make a method to toggle the flash, hope this helps.

private void ToggleFlash()
{
    fab_flash.startAnimation(spin_it);
    if(currentDrawalbe == FLASH_DEFAULT_STATE)
    {
        fab_flash.setImageResource(FLASH_TOGGLE_STATE);
        currentDrawalbe = FLASH_TOGGLE_STATE;
        mCameraSource.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
    }
    else
    {
        fab_flash.setImageResource(FLASH_DEFAULT_STATE);
        currentDrawalbe = FLASH_DEFAULT_STATE;
        mCameraSource.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
    }
}

The currentDrawable is just an image for the flash light icon, so basically if the image is a turned on flash light executes else clause otherwise if clause

查看更多
地球回转人心会变
4楼-- · 2019-05-25 02:48

The issue was that the camera API does not support opening the camera multiple times. Turning on the flashlight and starting CameraSource both require separate calls to open the camera. If you try to do both, the one that is requested last will fail.

The good news is that we recently open sourced the CameraSource implementation. This new version includes an option for turning on the flashlight, which should fix this issue. See here:

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

查看更多
登录 后发表回答