Class BarcodeScanner

java.lang.Object
com.codename1.ai.vision.BarcodeScanner
All Implemented Interfaces:
VisionAnalyzer<Barcode[]>, AutoCloseable

public final class BarcodeScanner extends Object

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.

  • Constructor Details

    • BarcodeScanner

      public BarcodeScanner()
      Creates an analyzer using the platform default backend and options.
      See Also:
    • BarcodeScanner

      public BarcodeScanner(VisionOptions options)
      Creates a reusable analyzer with explicit backend and result options.
      Parameters:
      options - configuration captured by this analyzer; null uses defaults
  • Method Details

    • isSupported

      public final boolean isSupported()
      Description copied from interface: VisionAnalyzer
      Tests the exact feature/backend pair configured for this analyzer.
      Specified by:
      isSupported in interface VisionAnalyzer<T>
      Returns:
      whether this analyzer's feature/backend is available and open
    • process

      public final AsyncResource<Barcode[]> process(VisionImage image)
      Starts one analysis using the retained native backend.
      Specified by:
      process in interface VisionAnalyzer<T>
      Parameters:
      image - immutable encoded or raw input
      Returns:
      asynchronous typed result
    • close

      public final void close()
      Idempotently releases the retained native backend.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface VisionAnalyzer<T>