如何修复“ QR扫描仪未扫描QR码”?

时间:2019-03-10 14:06:11

标签: android qr-code zxing

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

我正在使用zxing库。

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

1 个答案:

答案 0 :(得分:0)

我想您的代码中没有onActivityResult,这就是为什么您无法捕获并返回任何结果的原因。

所以我很重视我的工作,它将为您提供帮助。

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