package com.codename1.ai.vision

Vendor-neutral on-device vision APIs for still images and live camera frames: barcodes and QR codes, OCR, faces, image labels, body pose, person segmentation, and document correction.

Start at the level you need

The package is three layers deep, and most applications only ever touch the first one.

1. A finished screen. CodeScanner is a complete barcode and QR scanner in one call. It opens the camera, decodes, restores the form you came from, and hands back the code – or null if the user backed out. This is the replacement for the old CodeScanner cn1lib.

CodeScanner.scan().ready(code -> {
    if (code != null) {
        urlField.setText(code.getValue());
    }
});

2. A live preview inside your own form. VisionCameraView is a component that owns the camera, streams frames through the analyzer you give it, and delivers results on the EDT. It works with every analyzer, not just barcodes.

VisionCameraView<Face[]> view =
        new VisionCameraView<Face[]>(new FaceDetector());
view.setFacing(CameraFacing.FRONT);
view.setListener(new VisionPipelineListener<Face[]>() {
    public void result(Face[] faces, VisionImage source) {
        status.setText(faces.length + " face(s)");
    }
    public void error(Throwable error) {
        Log.e(error);
    }
});
form.add(BorderLayout.CENTER, view);

3. One image at a time. Each analyzer – TextRecognizer, BarcodeScanner, FaceDetector, ImageLabeler, PoseDetector, SelfieSegmenter, DocumentScanner – takes a VisionImage and returns a typed result. Create one, reuse it for a sequence, and close it.

TextRecognizer recognizer = new TextRecognizer();
recognizer.process(VisionImage.fromFile(path))
        .ready(result -> Log.p(result.getText()))
        .except(error -> Log.e(error));

Reading the results

Bounds and points are normalized to 0..1 rather than pixels, so a result computed on a camera frame can be drawn over an image of any size. VisionRect.toBounds(com.codename1.ui.Component) and VisionPoint.toPoint(com.codename1.ui.Component) convert them back. Names that would otherwise be string literals – symbologies, face landmarks, body joints – are constants on BarcodeFormat, FaceLandmarks and PoseLandmarks.

Availability

Call isSupported() before offering a feature: availability depends on the target, the linked backend, and the OS version. The automatic backend uses Apple Vision/Core Image on iOS and Mac Catalyst and ML Kit on Android; optional backends are selected with VisionBackends. In the simulator the results are whatever you scripted under Simulate > Vision, so a scanner screen can be built without a device.

Each analyzer is a separate build-time feature. Referencing one causes the builder to retain only its platform adapter and native dependency, so a barcode app does not carry the pose and OCR models.

Types

class BarcodePortable barcode observation with normalized geometry.
class BarcodeFormatThe normalized symbology names Barcode.getFormat() reports, as constants instead of literals.
class BarcodeScannerDecodes barcodes and QR codes out of a still image or a camera frame.
class CodeScannerA ready-made full-screen barcode and QR scanner.
class CodeScannerOptionsConfiguration for CodeScanner.scan(CodeScannerOptions).
class DocumentScanResultCorrected document pages returned as encoded image data.
class DocumentScannerFinds a page in a photo and returns it flattened, with the perspective corrected.
class FacePortable face observation.
class FaceDetectorFinds faces, their bounding boxes, their head angles, and – where the backend supports it – whether they are smiling.
class FaceLandmarksThe keys Face.getLandmarks() uses, as constants instead of literals.
class ImageLabelPortable ranked image-classification label.
class ImageLabelerClassifies what an image contains, as ranked labels with confidences.
class PosePortable body-pose result.
class PoseDetectorLocates body joints, for rep counting, form feedback, or gesture input.
class PoseLandmarksThe joint names Pose.Landmark.getName() reports, as constants instead of literals.
class SegmentationMaskDense per-pixel foreground confidence mask.
class SelfieSegmenterSeparates a person from the background, for background replacement or blur.
class TextRecognitionResultOCR output containing the complete recognized text and portable block-level geometry.
class TextRecognizerReads the text in an image.
class TextScriptWriting system selectors for TextRecognizer.
interface VisionAnalyzerReusable, closable on-device analyzer for still images or camera frames.
interface VisionBackendIdentifies a vision implementation.
class VisionBackendsVision backend selectors.
class VisionCameraViewA live camera preview that runs an analyzer over its frames.
class VisionExceptionFailure reported by an on-device vision backend.
enum VisionFeatureFeatures understood by the shared vision backend.
class VisionImageImmutable input for encoded still images or raw camera pixels.
class VisionMetadataOptional backend identity and backend-specific diagnostic strings attached to a vision result.
class VisionOptionsCommon analyzer configuration.
class VisionPipelineConnects a camera frame stream to a reusable analyzer with keep-only-latest backpressure.
interface VisionPipelineListenerReceives live vision results and recoverable analysis failures on the EDT.
class VisionPointImmutable point in a normalized, top-left-origin coordinate space.
class VisionRectImmutable normalized rectangle using a top-left origin.