Class BarcodeScanner
java.lang.Object
com.codename1.ai.vision.BarcodeScanner
- All Implemented Interfaces:
VisionAnalyzer<Barcode[]>, AutoCloseable
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 Summary
ConstructorsConstructorDescriptionCreates an analyzer using the platform default backend and options.BarcodeScanner(VisionOptions options) Creates a reusable analyzer with explicit backend and result options. -
Method Summary
Modifier and TypeMethodDescriptionfinal voidclose()Idempotently releases the retained native backend.final booleanTests the exact feature/backend pair configured for this analyzer.final AsyncResource<Barcode[]> process(VisionImage image) Starts one analysis using the retained native backend.
-
Constructor Details
-
BarcodeScanner
public BarcodeScanner()Creates an analyzer using the platform default backend and options.- See Also:
-
BarcodeScanner
Creates a reusable analyzer with explicit backend and result options.- Parameters:
options- configuration captured by this analyzer;nulluses defaults
-
-
Method Details
-
isSupported
public final boolean isSupported()Description copied from interface:VisionAnalyzerTests the exact feature/backend pair configured for this analyzer.- Specified by:
isSupportedin interfaceVisionAnalyzer<T>- Returns:
- whether this analyzer's feature/backend is available and open
-
process
Starts one analysis using the retained native backend.- Specified by:
processin interfaceVisionAnalyzer<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:
closein interfaceAutoCloseable- Specified by:
closein interfaceVisionAnalyzer<T>
-