Search code examples
androidqr-codezxing

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


I'm trying to develop an Android app that can scan QR codes. But the problem is, it only auto focus on the QR code but not returning any results. Is there a way to fix this?

I'm using zxing libraries.

Hello guys, can someone help me?


Solution

  • I guess you dont have onActivityResult in your code and that is why you cant capture and return any result.

    So I am attaching my work and it would help you.

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

    Here is the onActivityResult and I think you dont have it.

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