Zbar和Zxing在android studio中

Tan*_*Kai 0 android

嗨,任何人都可以教我如何使用zbar/zxing为qr代码阅读器?我在线尝试了很多示例代码,但似乎都没有.顺便说一句我使用Android工作室.

And*_*ago 6

我用了zxing-android-embedded.为您处理zxing-core,自动在单独的线程上打开相机,甚至还有自定义用法的文档/示例.此外,作者经常承诺(每隔约2周)并对问题作出快速反应.它开箱即可扫描QR和条形码.

在AndroidManifest.xml中添加Camera的权限:

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
Run Code Online (Sandbox Code Playgroud)

将其添加到build.gradle(app):

repositories {
    jcenter()
}

dependencies {
    compile 'com.journeyapps:zxing-android-embedded:3.0.2@aar'
    compile 'com.google.zxing:core:3.2.0'
}
Run Code Online (Sandbox Code Playgroud)

在您的活动中创建回调:

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) {
    }
};
Run Code Online (Sandbox Code Playgroud)

的onCreate()

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

的onResume()

mBarcodeView.resume();
Run Code Online (Sandbox Code Playgroud)

的onPause()

mBarcodeView.pause();
Run Code Online (Sandbox Code Playgroud)

在Activity的XML布局中,添加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>
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!