android - Android - 无法使用 zxing 库检测二维码
问题描述
我正在开发一个 Android 应用程序来扫描二维码,使用 zxing 库如下:
首先是Gradle中库的集成:
implementation 'com.journeyapps:zxing-android-embedded:3.0.2@aar'
implementation 'com.google.zxing:core:3.3.0'
其次是 AndroidManifest.xml 中的活动:
<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);
}
相机已打开,看起来正在扫描,但无法检测和读取 QR 码,没有返回任何内容。
解决方案
我看不出您的代码有什么问题,但我可以为您提供适用于我的确切代码:
(我正在使用任何方向,这意味着您必须创建一个新的 java 类文件)
显现:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
<activity
android:name=".AnyOrientationCaptureActivity"
android:screenOrientation="fullSensor"
android:stateNotNeeded="true"
android:theme="@style/zxing_CaptureTheme"
android:windowSoftInputMode="stateAlwaysHidden"></activity>
构建.gradle:应用程序
implementation 'com.journeyapps:zxing-android-embedded:3.6.0'
implementation 'com.android.support:appcompat-v7:25.3.1'
AnyOrientationCaptureActivity Java:
package com.your.package;
import com.journeyapps.barcodescanner.CaptureActivity;
public class AnyOrientationCaptureActivity extends CaptureActivity {
}
主要活动:
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.setCaptureActivity(AnyOrientationCaptureActivity.class);
integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE);
integrator.setPrompt("Scan the QR code");
integrator.setOrientationLocked(false);
integrator.setBeepEnabled(false);
integrator.initiateScan();
...
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode != CUSTOMIZED_REQUEST_CODE && requestCode != IntentIntegrator.REQUEST_CODE) {
// This is important, otherwise the result will not be passed to the fragment
super.onActivityResult(requestCode, resultCode, data);
return;
}
switch (requestCode) {
case CUSTOMIZED_REQUEST_CODE: {
Toast.makeText(this, "REQUEST_CODE = " + requestCode, Toast.LENGTH_LONG).show();
break;
}
default:
break;
}
IntentResult result = IntentIntegrator.parseActivityResult(resultCode, data);
if(result.getContents() == null) {
Log.d("MainActivity", "Cancelled scan");
//Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Log.d("MainActivity", "Scanned");
String mvalue = result.getContents();
}
}
推荐阅读
- python - 如何合并具有不同列但相似数据的熊猫数据框?
- kubernetes - 无法启动 Kubernetes API 服务器
- jxbrowser - Jx浏览器。加载本地资源
- android - 尝试自动化移动应用程序时程序被挂起
- apache-kafka - Send/Process a image(.png or .jpg) from kafkaProducer to kafkaConsumer?
- c# - 已发布数组中的一个字段未绑定到模型
- ios - 如何在目标 c 中强制转换和转换数据类型
- angular - Typescript Angular 7,复制到剪贴板
- android - Android 9 中的 java.lang.IllegalStateException 错误
- vue.js - Vuejs v-bind 类没有动态应用类