Class VisionPipeline<T>

java.lang.Object
com.codename1.ai.vision.VisionPipeline<T>
All Implemented Interfaces:
AutoCloseable

public final class VisionPipeline<T> extends Object implements AutoCloseable

Connects a camera frame stream to a reusable analyzer with keep-only-latest backpressure. At most one analysis and one pending frame are retained, so a slow model cannot build an unbounded queue. Results and errors arrive on the EDT. The pipeline owns and closes the analyzer but not the camera session.

Use this when the application already owns a CameraSession -- it is taking photos or recording video as well as analyzing. When the screen only needs a preview that analyzes, VisionCameraView does the same thing and owns the session too, which is one less lifecycle to get right.

CameraSession session = Camera.open(Camera.getDefault(CameraFacing.BACK),
        new CameraSessionOptions().frameMaxFps(10).captureAudio(false));
form.add(BorderLayout.CENTER, session.createView());

VisionPipeline<Barcode[]> pipeline = new VisionPipeline<Barcode[]>(
        session, new BarcodeScanner(),
        new VisionPipelineListener<Barcode[]>() {
            public void result(Barcode[] codes, VisionImage source) {
                if (codes.length > 0) {
                    found(codes[0].getValue());
                }
            }
            public void error(Throwable error) {
                Log.e(error);
            }
        });

// Closing the pipeline detaches the frame listener and closes the
// analyzer. The session is yours to close.
form.addCloseListener(e -> {
    pipeline.close();
    session.close();
});
  • Constructor Details

    • VisionPipeline

      public VisionPipeline(CameraSession session, VisionAnalyzer<T> analyzer, VisionPipelineListener<T> listener)
      Attaches immediately as the session's frame listener.
      Parameters:
      session - active camera session whose frames should be analyzed
      analyzer - reusable analyzer owned by this pipeline
      listener - EDT result/error listener
  • Method Details

    • isBusy

      public boolean isBusy()
      Returns whether a frame is currently being analyzed.
      Returns:
      true while the open pipeline has an analysis in flight
    • close

      public void close()
      Stops accepting frames, detaches from the camera session, discards the pending frame, clears the busy state, and closes the analyzer. A pending frame already selected for dispatch is rechecked and discarded before it can reach the closed analyzer. Calling this method more than once has no effect.
      Specified by:
      close in interface AutoCloseable