Zbar and Zxing in android studio

Hi, can anyone teach me how to use zbar / zxing to read qr code? I have tried many code examples online, but no one works. Btw im using android studio.

0
android
source share
1 answer

I used zxing-android-embedded . It handles zxing-core for you, automatically opens the camera in a separate stream, and even has documents / examples for user use. In addition, the authors record often (every ~ 2 weeks) and quickly respond to problems. It scans QR and barcodes out of the box.

Add permissions for the camera in AndroidManifest.xml:

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

Add this to your build.gradle (app) file:

 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 XML layout add 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!

+5
source share

All Articles