Android - 无法使用 zxing 库检测 QR 码

标签 android qr-code 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 :

<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();

最后,解析从scanner得到的信息(这个永远不会执行)

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);
            }

摄像头已打开,看起来正在扫描,但无法检测和读取二维码,没有返回任何内容。

最佳答案

我看不出您的代码有什么问题,但我可以为您提供适合我的确切代码:

(我正在使用任意方向,这意味着您必须创建一个新的 java 类文件)

list :

<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>

build.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 {

}

主要 Activity :

   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();
    }
}

关于Android - 无法使用 zxing 库检测 QR 码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54981587/

相关文章:

ios - 如何在 Swift 4 中设置 QRCode 版本

Expo Go 无法连接项目

android - 在 Android 中将 RecyclerView 与 StaggeredGridLayoutManager 一起使用时,单元格不会交错

android - TabLayout - 未找到属性的资源标识符

android - 从 stylable 读取 sp

java - 将android连接到远程sql server

optimization - 短网址的二维码编码模式

java - 多个扫描按钮 Zxing

android - 为什么Android模拟器中的相机预览会旋转90度?

android - Zxing 2.2 在 Eclipse 中导入为库项目(不是 .jar)