Class CodeScanner

java.lang.Object
com.codename1.ai.vision.CodeScanner

public final class CodeScanner extends Object

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(SuccessCallback, EasyThread) 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(SuccessCallback, EasyThread), scanCanceled is a null value, and scanError is AsyncResource.except(SuccessCallback, EasyThread).

  • Method Details

    • isSupported

      public static boolean isSupported()
      Whether this device can run the scanner. This is the check to make before offering a scan button: it covers both the camera and the barcode backend, either of which a target may lack.
      Returns:
      true when scan() can open a working scanner
    • scan

      public static AsyncResource<Barcode> scan()
      Shows the scanner with the default wording, accepting every symbology the platform decodes.
      Returns:
      the first decoded code, or null if the user backed out
    • scan

      public static AsyncResource<Barcode> scan(CodeScannerOptions options)
      Shows the scanner configured by options.
      Parameters:
      options - the scanner configuration, or null for the defaults
      Returns:
      the first accepted code, or null if the user backed out