Class VisionCameraView<T>

java.lang.Object
com.codename1.ui.Component
com.codename1.ui.Container
com.codename1.ai.vision.VisionCameraView<T>
Type Parameters:
T - the analyzer's result type
All Implemented Interfaces:
Animation, Editable, StyleListener, AutoCloseable, Iterable<Component>

public class VisionCameraView<T> extends Container implements AutoCloseable

A live camera preview that runs an analyzer over its frames.

This is the whole camera-to-analyzer pipeline as one component: it opens the camera when it is shown, streams frames into the analyzer with keep-only-the-newest backpressure, delivers results on the EDT, and releases the camera when the form is left. Add it to a form and implement the listener; there is no session, frame listener, or VisionImage conversion to write.

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

Form form = new Form("Faces", new BorderLayout());
form.add(BorderLayout.CENTER, view);
form.add(BorderLayout.SOUTH, countLabel);
form.show();

The analyzer is supplied by the caller rather than named by a feature constant, which is deliberate: the build pipeline decides which native vision dependency to package from the analyzer classes an application references, so constructing the analyzer in application code is what keeps a face-detection app from also carrying the barcode and pose models.

The preview is a native view. Codename One components cannot be painted over it on every platform -- iOS renders native peers behind the Codename One layer, Android renders them in front -- so put a scanner's reticle, hints, and buttons around the preview rather than on top of it. Result geometry such as Face.getBounds() can still be drawn in a component beside the preview using VisionRect.toBounds(com.codename1.ui.Component).

One camera session may be open at a time, so a second view (or a Capture call) while this one is showing reports an error to the listener instead of stealing the hardware.

  • Constructor Details

    • VisionCameraView

      public VisionCameraView(VisionAnalyzer<T> analyzer)

      Creates a view that runs analyzer over the back camera.

      The view borrows the analyzer: leaving the form releases the camera but keeps the analyzer usable, and close() releases it for good.

      Parameters:
      analyzer - the reusable analyzer to run over each frame
      Throws:
      NullPointerException - if analyzer is null
  • Method Details

    • isSupported

      public boolean isSupported()
      Whether the current platform can open a camera and run this view's analyzer. Check this before showing the view: a target without either piece reports failures through the listener rather than throwing.
      Returns:
      true when both the camera and the analyzer are available
    • setFacing

      public void setFacing(CameraFacing value)

      Selects which camera to open. Takes effect the next time the view is shown; the default is CameraFacing.BACK.

      A front camera's preview is requested mirrored, the way a selfie view behaves. The simulator honors that; the iOS and Android previews do not mirror yet. Note also that a device without the requested camera falls back to whatever it has, and the mirroring follows the camera that actually opened rather than the one asked for.

      Parameters:
      value - the camera to prefer, or null for the back camera
    • getFacing

      public CameraFacing getFacing()
      Returns:
      the camera this view opens
    • setMaxFps

      public void setMaxFps(int value)
      Caps how many frames per second reach the analyzer. Frames arriving while an analysis is in flight are dropped in favor of the newest one regardless of this value, so the cap mainly saves the camera the work of producing frames nothing will read. The default is 10.
      Parameters:
      value - frames per second, or 0 for uncapped
    • getMaxFps

      public int getMaxFps()
      Returns:
      the frame rate cap, or 0 when uncapped
    • setScaleType

      public void setScaleType(ScaleType value)

      How the preview is fitted into this component's bounds. The default is ScaleType.CROP, which fills the view the way a camera app does.

      The simulator honors every mode. The iOS and Android previews render fixed at fill-and-crop today, because neither port implements the scaling hook yet, so a non-default mode is a request those targets currently ignore rather than a promise.

      Parameters:
      value - the scaling to apply, or null for the default
    • getScaleType

      public ScaleType getScaleType()
      Returns:
      how the preview is fitted into this component's bounds
    • setListener

      public void setListener(VisionPipelineListener<T> value)
      Installs the callback for analysis results and recoverable failures. Both are delivered on the EDT.
      Parameters:
      value - the listener, or null to stop receiving results
    • getListener

      public VisionPipelineListener<T> getListener()
      Returns:
      the installed result listener, or null
    • getSession

      public CameraSession getSession()
      The open camera session, for torch, zoom, focus, or a still capture.
      Returns:
      the session while the view is showing, or null
    • setTorchEnabled

      public void setTorchEnabled(boolean on)
      Turns the torch on or off, when the open camera has one. A camera without a flash ignores this.
      Parameters:
      on - whether the torch should be lit
    • isRunning

      public boolean isRunning()
      Returns:
      true while the camera is open and frames are flowing
    • start

      public void start()
      Opens the camera and starts analyzing. Called automatically when the view is shown; calling it again while running does nothing.
    • stop

      public void stop()
      Stops analyzing and releases the camera, leaving the analyzer usable. Called automatically when the view stops being shown.
    • close

      public void close()
      Releases the camera and the analyzer. Use this when the view will not be shown again; simply navigating to another form already releases the camera. Calling it more than once has no effect.
      Specified by:
      close in interface AutoCloseable
    • initComponent

      protected void initComponent()
      Opens the camera when the view becomes part of a shown form.
      Overrides:
      initComponent in class Component
    • deinitialize

      protected void deinitialize()
      Releases the camera when the view stops being shown.
      Overrides:
      deinitialize in class Component