仿微信二维码扫描,基于journeyapps/ zxing-android-embedded开发
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.journeyapps.barcodescanner.CompoundBarcodeView
android:id="@+id/zxing_barcode_scanner"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Put the following code in your Activity.onCreate() method
private void initCaptureManager(Bundle savedInstanceState) {
barcodeScannerView = (CompoundBarcodeView) findViewById(R.id.zxing_barcode_scanner);
capture = new CaptureManager(this, barcodeScannerView);
capture.initializeFromIntent(getIntent(), savedInstanceState);
capture.setOnBarcodeCallBack(this);
capture.decode();
}
You can receive and handle your result after scan through implement the interface CaptureManager.OnBarcodeCallBack
@Override
public void onBarcodeResult(String content) {
Intent data = new Intent();
data.putExtra(EXTRA_SCAN_RESULT, content);
setResult(RESULT_OK, data);
finish();
}
And Handling the Scanner's lifecycle in your Activity
@Override
protected void onResume() {
super.onResume();
capture.onResume();
}
@Override
protected void onPause() {
super.onPause();
capture.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
capture.onDestroy();
}
You could also handling the OnKeyDownEvent by call the CompoundBarcodeView.onKeyDown(keyCode, event).
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return barcodeScannerView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
}
-
download the sample
-
Android Studio : File - New - Import Module, and choose source directory
-
Add the following to your build.gradle file:
dependencies { compile project(':zxing-barcoder-embed') }
Notice: For more information you can visit the repository zxing-android-embedded. And Thanks for the help journeyapps again.
Name: ZTerry Liu
E-mail: [email protected]
Subject: BarcodeScaner

