Dimitris Sioulas Dimitris Sioulas - 3 months ago 33
Android Question

Enable flashlight in QR CODE READER

I want to scan a qr code with flashlight enabled in android app. I am using this library https://github.com/dlazaro66/QRCodeReaderView. Is there any way to enable the flashlight when I have already opened my camera for scan?

Answer

You will have to modify QRCodeReaderView.java file from your library. Replace the "surfaceCreated" method with this one:

@Override
public void surfaceCreated(SurfaceHolder holder) {
    boolean useFlash = true;

    try {
        // Indicate camera, our View dimensions
        mCameraManager.openDriver(holder,this.getWidth(),this.getHeight());
    } catch (IOException e) {
        Log.w(TAG, "Can not openDriver: "+e.getMessage());
        mCameraManager.closeDriver();
    }

    try {
        mQRCodeReader = new QRCodeReader();
        if (useFlash){
            Parameters p = cam.getParameters();
            p.setFlashMode(Parameters.FLASH_MODE_TORCH);
            mCameraManager.setParameters(p);
        }

        mCameraManager.startPreview();
    } catch (Exception e) {
        Log.e(TAG, "Exception: " + e.getMessage());
        mCameraManager.closeDriver();
    }
}