如何修复 "QR scanner is not scanning QR codes"?

How to fix "QR scanner is not scanning QR codes"?

我正在尝试开发一个可以扫描 QR 码的 Android 应用程序。但问题是,它只自动聚焦二维码,但不返回任何结果。有办法解决这个问题吗?

我正在使用 zxing 库。

大家好,有人可以帮帮我吗?

我猜你的代码中没有 onActivityResult,这就是你无法捕获和 return 任何结果的原因。

所以我附上了我的工作,它会对你有所帮助。

 cameraView = (SurfaceView) v.findViewById(R.id.cameraView);
 textResult = (TextView) v.findViewById(R.id.textView);
 barcodeDetector = new BarcodeDetector.Builder(v.getContext())
            .setBarcodeFormats(Barcode.QR_CODE)
            .build();

    cameraSource = new CameraSource.Builder(v.getContext(), barcodeDetector)
            .setRequestedPreviewSize(640, 640).build();


    cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            if (ActivityCompat.checkSelfPermission(v.getContext(), android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                //Request permission
                ActivityCompat.requestPermissions(getActivity(),
                        new String[]{android.Manifest.permission.CAMERA}, RequestCameraPermissionID);
                return;
            }
            try {
                cameraSource.start(cameraView.getHolder());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

        }

        @Override
        public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
            cameraSource.stop();

        }
    });

    barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
        @Override
        public void release() {

        }

        @Override
        public void receiveDetections(Detector.Detections<Barcode> detections) {
            final SparseArray<Barcode> qrcodes = detections.getDetectedItems();
            if (qrcodes.size() != 0) {

                textResult.post(new Runnable() {
                    @Override
                    public void run() {
                        //Create vibrate
                        Vibrator vibrator = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE);
                        vibrator.vibrate(1000);
                        textResult.setText(qrcodes.valueAt(0).displayValue);
                        showResultDialogue(qrcodes.valueAt(0).displayValue);

                    }
                });

            }//End if Statement
        }
    });

这是 onActivityResult,我想你没有。

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    //We will get scan results here
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    //check for null
    if (result != null) {
        if (result.getContents() == null) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(getContext());
            alertDialog.setTitle("Error");
            alertDialog.setMessage("Scanning Error. Please try Again");
            alertDialog.show();
        } else {
            //show dialogue with result
            showResultDialogue(result.getContents());
        }
    } else {
        // This is important, otherwise the result will not be passed to the fragment
        super.onActivityResult(requestCode, resultCode, data);
    }
}

//method to construct dialogue with scan results
public void showResultDialogue(final String result) {

    final AlertDialog.Builder builder;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder = new AlertDialog.Builder(getContext(), android.R.style.Theme_Material_Dialog_Alert);

    } else {
        builder = new AlertDialog.Builder(getContext());
    }

    //If the text of QR Code is success then Check-in Successful.
    //if(result.equalsIgnoreCase("success")){

    //Stop the scanner
    barcodeDetector.release();

    builder.setTitle("Successfully Scan QR Code")
            .setMessage("Result --->  " + result)
            .setPositiveButton("DONE", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent(getContext(), MainActivity.class);
                    startActivity(intent);
                    //Click ok and Back to Main Page
                }
            }).create().show();