【发布时间】:2019-03-04 10:45:14
【问题描述】:
我正在开发一个Android App来扫描二维码,使用zxing库如下:
首先是Gradle中库的集成:
implementation 'com.journeyapps:zxing-android-embedded:3.0.2@aar'
implementation 'com.google.zxing:core:3.3.0'
其次是AndroidManifest.xml中的activity:
<activity
android:name="com.journeyapps.barcodescanner.CaptureActivity"
android:screenOrientation="fullSensor"
tools:replace="screenOrientation" />
然后是按下按钮时扫描二维码的代码:
IntentIntegrator integrator = new IntentIntegrator(getActivity());
integrator.setPrompt("Start scanning");
integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
integrator.setOrientationLocked(false);
integrator.initiateScan();
最后,解析从扫描仪获得的信息(这永远不会执行)
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null) {
if(result.getContents() == null) {
Toast.makeText(getActivity(), "Cancelled", Toast.LENGTH_LONG).show();
} else {
String code = result.getContents();
textView.setText(code);
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
相机已打开,看起来正在扫描,但无法检测和读取二维码,没有返回任何内容。
【问题讨论】:
-
请将相机远离二维码并检查..如果目标太近则有问题
-
感谢@SantanuSur,虽然不是很人性化,但它可以工作