问题
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.
回答1:
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:
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 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
android:id="@+id/barcode_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
Hope this helps!
来源:https://stackoverflow.com/questions/31736605/zbar-and-zxing-in-android-studio