Skip to content Skip to sidebar Skip to footer

While Using The Intentintegrator From The Zxing Library, Can I Add A Flash Button To The Barcode Scanner Via The Intent?

I am scanning barcodes and QR codes from an Android app via Intent using the ZXing Library and its port of the Android Application. I added the following two lines in my Gradle dep

Solution 1:

I had overlooked this page on Embedding BarcodeView and these sample activities which show how to customise the Barcode Scanner according to your needs. The example activity that helped me was CustomScannerActivity.

There isn't a option in the IntentIntegrator class to implement a flash button natively. Instead I should make a custom layout for the Barcode Scanner, use it in a custom activity and call this activity from the IntentIntegrator.

I have two activities. One is the ScannerActivity and other one is the CallingActivity. A mistake that confused me for a while was that I created an instance of IntentIntegrator in the onCreate() method of ScannerActivity. It should be in the CallingActivity.

In the example given a Button is used and the text of the Button is changed according to the flash. I created a new Android Layout called activity_custom_scanner where I replaced the Button with a ToggleButton and used images for the button instead to get my desired Flash On/Off Button.

So my ScannerActivity looks like this:

publicclassCustomScannerActivityextendsActivityimplementsCompoundBarcodeView.TorchListener {

    privatestatic final int BarCodeScannerViewControllerUserCanceledErrorCode = 99991;

    privatestatic final StringTAG = CustomScannerActivity.class.getSimpleName();

    privateCaptureManager capture;
    privateCompoundBarcodeView barcodeScannerView;
    privateToggleButton switchFlashlightButton;

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_scanner);

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

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

        switchFlashlightButton.setText(null);
        switchFlashlightButton.setTextOn(null);
        switchFlashlightButton.setTextOff(null);

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

        switchFlashlightButton.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener() {
            @OverridepublicvoidonCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // Save the state hereif (isChecked) {
                    barcodeScannerView.setTorchOn();
                } else {
                    barcodeScannerView.setTorchOff();
                }
            }
        });

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

    @OverrideprotectedvoidonResume() {
        super.onResume();
        capture.onResume();
    }

    @OverrideprotectedvoidonPause() {
        super.onPause();
        capture.onPause();
    }

    @OverrideprotectedvoidonDestroy() {
        super.onDestroy();
        capture.onDestroy();
    }

    @OverrideprotectedvoidonSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        capture.onSaveInstanceState(outState);
    }

    @OverridepublicbooleanonKeyDown(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.
     */privatebooleanhasFlash() {
        returngetApplicationContext().getPackageManager()
                .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
    }

    @OverridepublicvoidonTorchOn() {
        // necessary override..
    }


    @OverridepublicvoidonTorchOff() {
        // necessary override..
    }

}

And the CallingActivity looks like this:

publicclassCallingActivityextendsActivity {

    privatestaticfinalStringTAG= CallingActivity.class.getSimpleName();

    privatestaticfinalintBarCodeScannerViewControllerUserCanceledErrorCode=99991;

    String uuid;

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        uuid = getIntent().getStringExtra("uuid");
        newIntentIntegrator(this).setOrientationLocked(false).setCaptureActivity(CustomScannerActivity.class).initiateScan();
    }



    publicvoidonActivityResult(int requestCode, int resultCode, Intent intent) {

        IntentResultscanResult= IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);

        if (resultCode == RESULT_OK) {
            if (scanResult != null) {

                // handle scan result
                Log.i(TAG, "Text from Barcode Scanner: " + scanResult.getContents());
                getIntent().putExtra("data", scanResult.getContents());
                getIntent().putExtra("uuid", uuid);
            }
        }
        elseif (resultCode == RESULT_CANCELED) {
            getIntent().putExtra("error", "User canceled");
            getIntent().putExtra("error_code", BarCodeScannerViewControllerUserCanceledErrorCode);
        }
        else
        {
            getIntent().putExtra("error", getString(R.string.scanner_error));
            getIntent().putExtra("error_code", BarCodeScannerViewControllerUserCanceledErrorCode);
        }

        setResult(resultCode, this.getIntent());

        this.finish();
    }

}

I am not sure if it's the perfect way, but that's how I did it.

Hope it helps someone!

Solution 2:

There is a setTorchOn method in CompoundBarcodeView so you can check that method out and try to implement it for your needs. Hope it helps.

Solution 3:

This question is probably too old, but you can use the Volume buttons to turn on/off the torch while scanning.

Post a Comment for "While Using The Intentintegrator From The Zxing Library, Can I Add A Flash Button To The Barcode Scanner Via The Intent?"