How to add a box to barcode scanner using vision?

Viewed 240

In my Android App by clicking a button i'm opening a custom AlertDialog in which the user should be able to scan a barcode, the AlertDialog has in it a SurfaceView in which i start CameraSource with BarcodeDetector.

The issue is that it's not really clear to the user what he should do on that window with opened camera so i was wondering if it's possible to add like a tracker which draw a box around the barcode the user is trying to scan or something like this.

Here is my method which i call when building my AlertDialog:

private void initialiseDetectorsAndSources(SurfaceView surfaceView) {
        BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(this)
                .setBarcodeFormats(Barcode.ALL_FORMATS)
                .build();

        CameraSource cameraSource = new CameraSource.Builder(this, barcodeDetector)
                .setRequestedPreviewSize(1600, 1024)
                .setAutoFocusEnabled(true) //you should add this feature
                .build();

        surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                try {
                    if (ActivityCompat.checkSelfPermission(pterm.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
                        cameraSource.start(surfaceView.getHolder());
                    } else {
                        ActivityCompat.requestPermissions(pterm.this, new
                                String[]{Manifest.permission.CAMERA}, 201);
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }


            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            }

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


        barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
            @Override
            public void release() {
                // Toast.makeText(getApplicationContext(), "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.size() != 0) {


                    alertFidelityTxt.post(new Runnable() {

                        @Override
                        public void run() {

                            if (barcodes.valueAt(0).email != null) {
                                alertFidelityTxt.removeCallbacks(null);
                                codFidelity = barcodes.valueAt(0).email.address;
                                alertFidelityTxt.setText(codFidelity);
                                toneGen1.startTone(ToneGenerator.TONE_CDMA_PIP, 150);
                                fidelity.setText(codFidelity);
                                linearFidelity.setBackgroundColor(Color.parseColor("#00796B"));
                                alertFidelity.dismiss();
                            } else {
                                codFidelity = barcodes.valueAt(0).displayValue;
                                alertFidelityTxt.setText(codFidelity);
                                toneGen1.startTone(ToneGenerator.TONE_CDMA_PIP, 150);
                                fidelity.setText(codFidelity);
                                linearFidelity.setBackgroundColor(Color.parseColor("#00796B"));
                                alertFidelity.dismiss();

                            }
                        }
                    });

                }
            }
        });
    }
1 Answers

This is possible with third party library. I am giving useful bunch of code. Brief description of this library is on GitHub.


in gradle file please implement given below

//for qr code scanner
    implementation 'me.dm7.barcodescanner:zxing:1.9.13'

SimpleScannerActivity.java

public class SimpleScannerActivity extends Activity implements ZXingScannerView.ResultHandler {
    private ZXingScannerView mScannerView;

    @Override
    public void onCreate(Bundle state) {
        super.onCreate(state);
        mScannerView = new ZXingScannerView(this);   // Programmatically initialize the scanner view
        setContentView(mScannerView);                // Set the scanner view as the content view
    }

    @Override
    public void onResume() {
        super.onResume();
        mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
        mScannerView.startCamera();          // Start camera on resume
    }

    @Override
    public void onPause() {
        super.onPause();
        mScannerView.stopCamera();           // Stop camera on pause
    }

    @Override
    public void handleResult(Result rawResult) {
        // Do something with the result here
        Log.v(TAG, rawResult.getText()); // Prints scan results
        Log.v(TAG, rawResult.getBarcodeFormat().toString()); // Prints the scan format (qrcode, pdf417 etc.)

        // If you would like to resume scanning, call this method below:
        mScannerView.resumeCameraPreview(this);
    }
}

And don't forget to put CAMERA PERMISSION in your manifest file

<uses-feature android:name="android.hardware.camera" android:required="true" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera.autofocus" />
Related