如果你想在你的应用中实现条码扫描器而不依赖于其他应用,你可以使用ZXing Android Embedded,你只需要在你的 gradle 依赖中声明它的依赖并在你的内部使用它的特性应用程序。
要使用它,请将以下内容添加到您的 build.gradle 文件(项目/模块)中:
repositories {
jcenter()
}
dependencies {
compile 'com.journeyapps:zxing-android-embedded:3.2.0@aar'
compile 'com.google.zxing:core:3.2.1'
compile 'com.android.support:appcompat-v7:23.1.0' // Version 23+ is required
}
android {
buildToolsVersion '23.0.2' // Older versions may give compile errors
}
现在在您的代码中,您以这种方式开始扫描活动:
public void scanBarcode() {
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
integrator.setPrompt("Scan the barcode");
integrator.setCameraId(0); // Use a specific camera of the device
integrator.setBeepEnabled(false);
integrator.setBarcodeImageEnabled(true);
integrator.initiateScan();
}
并以这种方式处理结果:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanResult != null && scanResult.getContents() != null) {
String content = scanResult.getContents().toString();
// content = this is the content of the scanned barcode
// do something with the content info here
}
}
更多信息可以在 ZXing Android Embedded github repo 中找到,链接如下。
来源:https://github.com/journeyapps/zxing-android-embedded