Turn ON/OFF flashlight in zxing fragment lib

Android_Code_Chef picture Android_Code_Chef · Sep 23, 2015 · Viewed 12.6k times · Source

I have implemented Zxing barcode scanning library in my application. I have used following library: https://code.google.com/p/barcodefraglibv2/ I want to turn ON/OFF the flash light on click of a button when scanning but I am not able to do this. Library has exposed one function for the same but it is not working.

Code used is: fragment = (BarcodeFragment)getSupportFragmentManager().findFragmentById(R.id.sample); fragment.getCameraManager().setTorch(true);

Provide me any refrence code by which I can turn ON/OFF flashlight.

Answer

Rai_Gaurav picture Rai_Gaurav · May 10, 2017

You should go through the sample app in zxing-android-embedded library, there you can find CustomScannerActivity class which shows how to switch ON and OFF flash light below is the link:

https://github.com/journeyapps/zxing-android-embedded/blob/master/sample/src/main/java/example/zxing/CustomScannerActivity.java

Code sample from above link:

public class CustomScannerActivity extends Activity implements
    DecoratedBarcodeView.TorchListener {

private CaptureManager capture;
private DecoratedBarcodeView barcodeScannerView;
private Button switchFlashlightButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_custom_scanner);

    barcodeScannerView = (DecoratedBarcodeView)findViewById(R.id.zxing_barcode_scanner);
    barcodeScannerView.setTorchListener(this);

    switchFlashlightButton = (Button)findViewById(R.id.switch_flashlight);

    // if the device does not have flashlight in its camera,
    // then remove the switch flashlight button...
    if (!hasFlash()) {
        switchFlashlightButton.setVisibility(View.GONE);
    }

    capture = new CaptureManager(this, barcodeScannerView);
    capture.initializeFromIntent(getIntent(), savedInstanceState);
    capture.decode();
}

@Override
protected void onResume() {
    super.onResume();
    capture.onResume();
}

@Override
protected void onPause() {
    super.onPause();
    capture.onPause();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    capture.onDestroy();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    capture.onSaveInstanceState(outState);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    return barcodeScannerView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
}

/**
 * Check if the device's camera has a Flashlight.
 * @return true if there is Flashlight, otherwise false.
 */
private boolean hasFlash() {
    return getApplicationContext().getPackageManager()
            .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
}

public void switchFlashlight(View view) {
    if (getString(R.string.turn_on_flashlight).equals(switchFlashlightButton.getText())) {
        barcodeScannerView.setTorchOn();
    } else {
        barcodeScannerView.setTorchOff();
    }
}

@Override
public void onTorchOn() {
    switchFlashlightButton.setText(R.string.turn_off_flashlight);
}

@Override
public void onTorchOff() {
    switchFlashlightButton.setText(R.string.turn_on_flashlight);
} 
}