繁体   English   中英

无法使用 Zxing 库扫描二维码

[英]Unable to scan QR code using Zxing library

我正在尝试将 Zxing 集成到我的 android 应用程序中,以便用户可以扫描二维码并返回二维码的内容。 我可以打开条形码扫描仪,尽管它看起来像是在做一些事情,但它不会扫描二维码。 我已经在条形码上对其进行了测试,并且可以正常工作,因此问题似乎是特定于二维码的。 我在下面包含了一些代码片段。

清单文件

<activity
            android:name="com.journeyapps.barcodescanner.CaptureActivity"
            android:screenOrientation="portrait"
            tools:replace="android:screenOrientation"
            android:stateNotNeeded="true"/>

QR 扫描仪片段

package com.example.ntuevent.ui.qrScanner;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.example.ntuevent.R;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

public class QRScannerFragment extends Fragment implements View.OnClickListener {

    private QRScannerViewModel qrScannerViewModel;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        qrScannerViewModel =
                ViewModelProviders.of(this).get(QRScannerViewModel.class);
        View root = inflater.inflate(R.layout.fragment_qr_scanner, container, false);
        final TextView textView = root.findViewById(R.id.text_qr_scanner);
        qrScannerViewModel.getText().observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });

        root.findViewById(R.id.qr_scanner_button).setOnClickListener(this);

        return root;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.qr_scanner_button:
                /* Request camera access */
                ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, 1);
                launchQrScanner();
        }
    }

    private void launchQrScanner() {
        if (validateCameraPermission()) {
            /* Start the scanner */
            IntentIntegrator intentIntegrator = new IntentIntegrator(getActivity());

            /* Customisation options */
            intentIntegrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
            intentIntegrator.setPrompt("Scan a barcode");
            intentIntegrator.setCameraId(0);  // Use a specific camera of the device
            intentIntegrator.setBeepEnabled(false);
            intentIntegrator.setOrientationLocked(true);


            /* Start QR scanner */
            intentIntegrator.initiateScan();
        }
    }

    private boolean validateCameraPermission() {
        /* Validates if app has access to camera */
        if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(getContext().getApplicationContext(), "Enable camera permissions to access this feature", Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);

        if (intentResult != null) {
            if (intentResult.getContents() == null)
                Toast.makeText(getContext().getApplicationContext(), "Scan was cancelled", Toast.LENGTH_SHORT).show();
            else
                Toast.makeText(getContext().getApplicationContext(), intentResult.getContents(), Toast.LENGTH_SHORT).show();
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}

任何帮助都会很棒!

喜欢我的二维码扫描仪,我将此代码用于 2 个项目,并且没有任何问题。

首先,将以下库添加到您的 Gradle:

  implementation 'me.dm7.barcodescanner:zxing:1.9.13'
  implementation 'com.journeyapps:zxing-android-embedded:3.6.0@aar'
  implementation 'com.google.zxing:core:3.3.3'

其次,在您的二维码扫描仪活动中添加以下代码:

private IntentIntegrator qrScan;

在 onCreate 添加下面:

    qrScan = new IntentIntegrator(this);
    qrScan.setOrientationLocked(false);
    qrScan.initiateScan();

在 onCreate 之后添加以下内容:

  @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   IntentResult result = IntentIntegrator.parseActivityResult(requestCode, 
   resultCode, data);
    if (result != null) {
        if (result.getContents() == null) {
         //scan have an error 
        } else {
           //scan is successful 
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM