Browse Tag

detector

Barcode Detector – Simple example

1. Dependencies:

dependencies {
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'com.google.android.gms:play-services:8.1.0'
}

2. Initialization code:

    private BarcodeDetector detector;
    ...
    private void initAndCheckDetector() {
        detector = new BarcodeDetector.Builder(getActivity().getApplicationContext())
                .setBarcodeFormats(Barcode.DATA_MATRIX | Barcode.QR_CODE | Barcode.EAN_13)
                .build();
        if (!detector.isOperational()) {
            Toast.makeText(getActivity().getApplicationContext(), "Could not set up the detector!", Toast.LENGTH_SHORT).show();
            detector = null;
        }
    }

Recognize Barcode:

    private void recognizeBarcode(Bitmap myBitmap ) {
        if (detector!= null) {
            Frame frame = new Frame.Builder().setBitmap(myBitmap).build();
            SparseArray<Barcode> barcodes = detector.detect(frame);
            Barcode code = barcodes.valueAt(0);
            String result = code.rawValue;
        }
    }

Barcode Detection with the Mobile Vision API