public final class BarcodeScanner

  1. Object
  2. BarcodeScanner

ImplementsAutoCloseable, VisionAnalyzer<Barcode[]>

Decodes barcodes and QR codes out of a still image or a camera frame.

Scanning a picture the user already has:

BarcodeScanner scanner = new BarcodeScanner();
if (!scanner.isSupported()) {
    ToastBar.showErrorMessage("This device cannot decode barcodes");
    scanner.close();
    return;
}
scanner.process(VisionImage.fromFile(photoPath)).ready(codes -> {
    for (Barcode code : codes) {
        Log.p(code.getFormat() + ": " + code.getValue());
    }
    scanner.close();
}).except(error -> {
    Log.e(error);
    scanner.close();
});

For scanning with the camera there is usually no reason to wire this up yourself. CodeScanner.scan() is a whole scanner screen in one call, and VisionCameraView embeds the live preview in a form of your own:

VisionCameraView<Barcode[]> view =
        new VisionCameraView<Barcode[]>(new BarcodeScanner());
view.setListener(new VisionPipelineListener<Barcode[]>() {
    public void result(Barcode[] codes, VisionImage source) {
        if (codes.length > 0) {
            found(codes[0]);
        }
    }
    public void error(Throwable error) {
        Log.e(error);
    }
});

Create one analyzer and reuse it for a sequence of images; it keeps the native detector alive between calls. Close it when you are finished.

Constructors

public BarcodeScanner()Creates an analyzer using the platform default backend and options.
public BarcodeScanner(VisionOptions options)Creates a reusable analyzer with explicit backend and result options.

Methods

public final synchronized boolean isSupported()Tests the exact feature/backend pair configured for this analyzer.
public final synchronized AsyncResource<Barcode[]> process(VisionImage image)Starts one analysis using the retained native backend.
public final synchronized void close()Idempotently releases the retained native backend.

Inherited methods

Constructor details

BarcodeScanner

public BarcodeScanner()
Creates an analyzer using the platform default backend and options.

BarcodeScanner

public BarcodeScanner(VisionOptions options)
Creates a reusable analyzer with explicit backend and result options.

Parameters

options VisionOptions
configuration captured by this analyzer; null uses defaults

Method details

isSupported

public final synchronized boolean isSupported()
Tests the exact feature/backend pair configured for this analyzer.

Returns

whether this analyzer’s feature/backend is available and open

process

public final synchronized AsyncResource<Barcode[]> process(VisionImage image)
Starts one analysis using the retained native backend.

Parameters

image VisionImage
immutable encoded or raw input

Returns

asynchronous typed result

close

public final synchronized void close()
Idempotently releases the retained native backend.