How turn on flashlight using Barcode Detection in

2019-05-25 01:47发布

问题:

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?

回答1:

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



回答2:

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



回答3:

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