public final class CodeScanner
- Object
- CodeScanner
A ready-made full-screen barcode and QR scanner.
This is the one-call entry point that BarcodeScanner deliberately
is not. It shows a camera form, decodes codes from the live frames, returns
the first accepted one, and restores the form the application was on. Use
BarcodeScanner directly when scanning a still image or when the
scanner has to look like the rest of a custom camera screen, and
VisionCameraView when the preview belongs inside a form of your own.
if (!CodeScanner.isSupported()) {
ToastBar.showErrorMessage("This device cannot scan codes");
return;
}
CodeScanner.scan().ready(code -> {
if (code == null) {
return; // the user pressed back
}
urlField.setText(code.getValue());
}).except(error -> Log.e(error));
Restrict the symbologies, and reword the screen, through
CodeScannerOptions:
CodeScanner.scan(new CodeScannerOptions()
.title("Boarding pass")
.hint("Hold the pass flat inside the frame")
.formats(BarcodeFormat.PDF417, BarcodeFormat.AZTEC))
.ready(code -> {
if (code != null) {
checkIn(code.getValue());
}
});
The result completes with null when the user backs out, which is
how the old CodeScanner cn1lib’s scanCanceled() maps onto an
AsyncResource. A failure to open the camera or run the decoder
arrives through AsyncResource.except instead, and closes the
scanner form.
Referencing this class is what tells the build pipeline to package barcode
scanning and the camera, exactly as referencing BarcodeScanner
does. It adds no other vision model.
Import the right one. Two older classes share this simple name and this
class replaces both: the cn1-codescan cn1lib’s
com.codename1.ext.codescan.CodeScanner, and the long-deprecated
CodeScanner in core, whose
getInstance() returns null on current targets. Make sure
the import reads:
import com.codename1.ai.vision.CodeScanner;
Migrating from either is mechanical. The cn1lib’s three-method
ScanResult callback becomes one asynchronous result:
scanCompleted is AsyncResource.ready, scanCanceled
is a null value, and scanError is
AsyncResource.except.
Methods
public static boolean isSupported() | Whether this device can run the scanner. |
public static AsyncResource<Barcode> scan() | Shows the scanner with the default wording, accepting every symbology the platform decodes. |
public static AsyncResource<Barcode> scan(CodeScannerOptions options) | Shows the scanner configured by options. |
Inherited methods
Method details
isSupported
public static boolean isSupported()Returns
true when scan() can open a working scannerscan
public static AsyncResource<Barcode> scan()Returns
null if the user backed outscan
public static AsyncResource<Barcode> scan(CodeScannerOptions options)options.Parameters
optionsCodeScannerOptions- the scanner configuration, or
nullfor the defaults
Returns
null if the user backed out