Question

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.

Was it helpful?

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 :-)

OTHER TIPS

Zxing is open source and you can include it in your app (but the creator of this lib discourages this behavior reading in stackoveflow questions).

Although you must respect the Apache license 2.0 of ZXing.

Is quite simple:

  1. include the lib in your project
  2. correct some code for a problem in android while generating R
  3. the application in zxing manifest must be like this <application/>
  4. start an intent or create your own public class ScanActivity extends CaptureActivity and manage the results

    @Override
    public void handleDecode(Result rawResult, Bitmap barcode, float scaleFactor){
        String barcodeRead = rawResult.getText();
    
        if(!MyValidator.isMyBarcode(barcodeRead))return;
    
        Intent viewDetails = new Intent(this, DetailActivity.class);
        viewDetails.putExtra(DetailActivity.EXTRA_BARCODE, barcodeRead);
        viewDetails.putExtra(DetailActivity.EXTRA_SESSION, "some extras");
        startActivity(viewDetails);
    
        finish();
    }
    

The last but not the least add some credit to this lib in your app!!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top