3

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?

pm0733464
  • 2,862
  • 14
  • 16
Splash
  • 71
  • 1
  • 5

5 Answers5

4

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

wax911
  • 429
  • 3
  • 12
3

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

pm0733464
  • 2,862
  • 14
  • 16
  • 5
    Do you have any plans to re-integrate this open-source CameraSource back into the official play-services-vision package? – Greeny Dec 16 '15 at 13:45
2

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;
}
Splash
  • 71
  • 1
  • 5
2

I am using this code and it is working successfully Replace : cameraSource.start(surfaceView.getHolder()); OR cameraSource.start(); TO setFlash ();

BY

import java.lang.reflect.Field;
import android.hardware.Camera;

Don't Forgot AndroidManifest:

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<permission android:name="android.permission.FLASHLIGHT"
    android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
    android:protectionLevel="normal"/>

Then ADD this methods

 public static Camera getCamera(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;
}


public void setFlash () throws IOException {
   getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
    cameraSource.start(surfaceView.getHolder());
        Camera _cam = getCamera (cameraSource);
        if (_cam != null) {
            Camera.Parameters _pareMeters = _cam.getParameters();
            _pareMeters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
            _cam.setParameters(_pareMeters);
            _cam.startPreview();
        }
}
0
Add permossion on AndroidManifest file 

 <uses-feature android:name="android.hardware.camera" />
 <uses-feature android:name="android.hardware.camera.autofocus" />
 <permission android:name="android.permission.FLASHLIGHT"
        android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
        android:protectionLevel="normal"/>
        
Turn on torch on Android, using Kotlin.

cameraProvider.bindToLifecycle((LifecycleOwner)this,
                 cameraSelector, imageAnalysis, preview);


Get Camera for cameraProvider..

camera = cameraProvider!!.bindToLifecycle(
                    (this as LifecycleOwner),
                    cameraSelector!!, Preview
                )
                
to turn on/off the FlashLight
private fun navigateflash(isFlash : Boolean) {
            try {
               if(camera!= null && camera!!.cameraInfo.hasFlashUnit()){
                   if(isFlash){
                       camera!!.cameraControl.enableTorch(false); // or false
                       isFlashOn = false
                   }else{
                       camera!!.getCameraControl().enableTorch(true); // or false
                       isFlashOn = true
                   }
               }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
Devesh Kumawat
  • 173
  • 1
  • 6