0

Need Help in code optimization. I have developed an android app which scans QR code labeled on the product.

I have tested few scenarios where other apps like Inigma is working fine but my app fails to scan : following cases :

1 ) when I scan online generated QR code my app and inigma both works fine

.2) When I scan printed QR code on product such as bottle .my apps fail to scan but inigma scans perfectly .

where I am lagging ? can Anybody please help me out ? I have to try to solve this from past 1 week but not getting the solution.

Techincal Details: I am using ZXING library for QR code scanning.

Please help ... :(

Pradeep
  • 9,667
  • 13
  • 27
  • 34

1 Answers1

0

Try using this method and call the method at onResume() cycle

private void initialiseDetectorsAndSources() {
//        Bitmap bitmap = ((BitmapDrawable)getResources().getDrawable(R.drawable.ic_notfound)).getBitmap();
        BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(BarCodeScanActivity.this)
                .setBarcodeFormats(Barcode.ALL_FORMATS)
                .build();

        cameraSource = new CameraSource.Builder(this, barcodeDetector)
                .setRequestedPreviewSize(1920, 1080)
                .setFacing(CameraSource.CAMERA_FACING_BACK)
                .setAutoFocusEnabled(true) //you should add this feature
                .build();

        surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder surfaceHolder) {
                try {
                    if (ActivityCompat.checkSelfPermission(BarCodeScanActivity.this, Manifest.permission.CAMERA)
                                                                                    == PackageManager.PERMISSION_GRANTED) {
                        cameraSource.start(surfaceView.getHolder());
                    } else {
                        ActivityCompat.requestPermissions(BarCodeScanActivity.this, new
                                String[]{Manifest.permission.CAMERA}, AppUtils.CAMERA_PERMISSION);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

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

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder surfaceHolder) {

            }
        });

        barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
            @Override
            public void release() {
//                Toast.makeText(BarCodeScanActivity.this, "To prevent memory leaks barcode scanner has been stopped", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void receiveDetections(Detector.Detections<Barcode> detections) {
                final SparseArray<Barcode> barcodes = detections.getDetectedItems();

                if (barcodes != null && barcodes.size() > 0) {
                    available.post(new Runnable() {
                        @Override
                        public void run() {
                            String scandata = barcodes.valueAt(0).displayValue;
// the scandata is your barcode
                        }
                    });
                }
            }
        });
}
Chethan Kumar
  • 185
  • 1
  • 12