无法使用Zxing库扫描二维码

Unable to scan QR code using Zxing library

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

清单文件

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

二维码扫描器片段

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'

其次,在您的 QR 码扫描器中 activity 添加以下代码:

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