使用 Java 扫描 QR 并从中获取信息时出错

Error scanning QR and get Info from It using Java

提问人:takis Paras 提问时间:9/27/2023 更新时间:9/27/2023 访问量:22

问:

我有以下代码:

接收QRFragment.java:

package de.androidcrypto.nfchcendefemulator;

import static android.content.Context.VIBRATOR_SERVICE;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
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 com.google.zxing.*;
import com.journeyapps.barcodescanner.CaptureManager;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.client.android.Intents;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;

public class ReceiveQRFragment extends Fragment {

    private static final int CAMERA_PERMISSION_REQUEST_CODE = 100;
    private CaptureManager captureManager;
    private TextView scanResultTextView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_receive_qr, container, false);
        // Initialize UI elements and event handlers here if needed.
        scanResultTextView = rootView.findViewById(R.id.scanResultTextView);
        return rootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // Check for camera permission and request it if necessary.
        if (ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED) {
            // Permission is not granted, request it.
            ActivityCompat.requestPermissions(requireActivity(), new String[]{Manifest.permission.CAMERA},
                    CAMERA_PERMISSION_REQUEST_CODE);
        }

        // Initialize the CaptureManager
        captureManager = new CaptureManager(getActivity(), (DecoratedBarcodeView) view.findViewById(R.id.zxing_barcode_scanner));
        captureManager.initializeFromIntent(getActivity().getIntent(), savedInstanceState);
        captureManager.decode();
        
    }

    @Override
    public void onResume() {
        super.onResume();
        captureManager.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        captureManager.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        captureManager.onDestroy();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == CAMERA_PERMISSION_REQUEST_CODE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Camera permission granted, start QR code scanner.
                captureManager.onResume();
            } else {
                // Camera permission denied, show a message.
                Toast.makeText(requireContext(), "Camera permission is required to scan QR codes.", Toast.LENGTH_SHORT).show();
            }
        }
    }

    // Add this method to handle the QR code scan result
    private void handleQRCodeScanResult(String result) {
        if (result != null) {
            // QR code scanned successfully, display the result.
            displaySuccessMessage(result);
        } else {
            // QR code not found or other scanning error.
            Toast.makeText(requireContext(), "QR code not found.", Toast.LENGTH_SHORT).show();
        }
    }

    // Add this method to display a success message
    private void displaySuccessMessage(String message) {
        scanResultTextView.setText(message);
        scanResultTextView.setVisibility(View.VISIBLE);
    }
}

和以下片段:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- DecoratedBarcodeView for displaying the camera preview and scanning QR codes -->
    <com.journeyapps.barcodescanner.DecoratedBarcodeView
        android:id="@+id/zxing_barcode_scanner"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- Button to trigger the QR code scanning -->
    <Button
        android:id="@+id/btnScanQR"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Scan QR Code" />

    <!-- TextView to display the success message -->
    <TextView
        android:id="@+id/scanResultTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/btnScanQR"
        android:visibility="gone" 
        android:text="Scan completed successfully"
        android:textColor="#00FF00" 
        android:textStyle="bold" />

</RelativeLayout>

当我打开我的应用程序时,它会显示允许相机,当我这样做时,它会打开相机扫描二维码。但是,当我尝试扫描二维码并找到一个时,我的手机会发出哔哔声,整个应用程序正在关闭。知道为什么会这样吗?

Java 错误处理 二维码 阅读器

评论


答: 暂无答案