Search code examples
androidzxing

ZXing QR Code/Barcode scanning


I had tried to integrate QRCode scanner to my application by firing an intent The code I used is the follwing

            Intent intent = new Intent("com.google.zxing.client.android.SCAN");

            intent.putExtra("SCAN_MODE", "QR_CODE_MODE");

            startActivityForResult(intent, 0);

//The Response collection

  public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
    if (resultCode == RESULT_OK) {
    // Handle successful scan
    String capturedQrValue =
    intent.getStringExtra("SCAN_RESULT");
    Log.i("info", "captureQrValue :" + capturedQrValue);
    String format =
    intent.getStringExtra("SCAN_RESULT_FORMAT");
    Toast.makeText(
    One.this,
    "Scan Result:" + capturedQrValue + " scan format :"+ format,Toast.LENGTH_SHORT).show();
    } else if (resultCode == RESULT_CANCELED) {
    // Handle cancel
    }
    } else {
    }

Even though I am facing a problem that it is not working if the ZXing sample barcode scanner is not installed.

EDIT:

  • Is it possible to make Barcode Scanner as a part of my app. Am not interested in downloading Barcode Scanner.

  • Also is there any camera or android version specification for ZXing QRCode scanning???

Not working means - it's asking to install Barcode Scanner from google play which I need to avoid.


Solution

  • I would prefer/advise to use Zbar instead of zxing. zxing takes time to read qr code when compared to zbar and also u may need to install their own(zxing) app - Barcode Scanner for scanning. I have tried both and I found zbar better. Download zbar source code from here. Unzip the file and import the ZBarScannerLibrary as a library project. Add this library to your android application. Also add this to your scanButton `

    if (check_if_camera_is_available) {
    
        Intent intent = new Intent(MyWallet.this, ZBarScannerActivity.class);
        startActivityForResult(intent, ZBAR_SCANNER_REQUEST);
    } 
    else 
    {
    
        Toast.makeText(MyWallet.this, "Camera Unavailable", Toast.LENGTH_SHORT).show();
    
    }`
    

    Also declare these variables in your activity otherwise you may hit error

    private static final int ZBAR_SCANNER_REQUEST = 0;
    private static final int ZBAR_QR_SCANNER_REQUEST = 1;
    

    Finally in your manifest file add this

    <activity android:name="com.dm.zbar.android.scanner.ZBarScannerActivity"
                  android:screenOrientation="landscape"
                  android:label="@string/app_name" />
    
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-feature android:name="android.hardware.camera" android:required="false"/>
    

    Hope this helps :-)