0

Hi can anyone teach me how to use zbar/zxing for qr code reader? I have tried many of the sample codes online but none seems to work. Btw im using android studio.

Tan Chong Kai
  • 332
  • 1
  • 4
  • 17
  • 1
    You should avoid posting questions asking others to setup a library for you, try instead to show us what you are working on and post a specific question about what problems you are having or what code you don't understand. – Andrea Thacker Jul 31 '15 at 15:58

1 Answers1

6

I used zxing-android-embedded. Handles the zxing-core for you, automatically opens the camera on a separate thread, and even has docs/examples for custom usage. Also, the authors commit often (every ~2 weeks) and respond fast to issues. It scans QR's and Barcodes out of the box.

Add the permissions for the Camera in your AndroidManifest.xml:

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

Add this to your build.gradle (app):

repositories {
    jcenter()
}

dependencies {
    compile 'com.journeyapps:zxing-android-embedded:3.0.2@aar'
    compile 'com.google.zxing:core:3.2.0'
}

Create a callback in your Activity:

private BarcodeCallback callback = new BarcodeCallback() {
    @Override
    public void barcodeResult(BarcodeResult result) {
        // Do something with the scanned QR code.
        // result.getText() returns decoded QR code.
        fooBar(result.getText());
        }

    @Override
    public void possibleResultPoints(List<ResultPoint> resultPoints) {
    }
};

onCreate()

mBarcodeView = (BarcodeView) findViewById(R.id.barcode_view);
// Choose only one!!!
mBarcodeView.decodeSingle(callback);
// Or
mBarcodeView.decodeContinuous(callback);

onResume()

mBarcodeView.resume();

onPause()

mBarcodeView.pause();

In your Activity's XML layout, add the BarcodeView

<com.journeyapps.barcodescanner.BarcodeView
    android:id="@+id/barcode_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true">

</com.journeyapps.barcodescanner.BarcodeView>

Hope this helps!