public class VisionCameraView<T>

  1. Object
  2. Component
  3. Container
  4. VisionCameraView

ImplementsAnimation, AutoCloseable, Editable, Iterable<Component>, StyleListener

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.

Type parameters

<T>
the analyzer’s result type

Constructors

public VisionCameraView(VisionAnalyzer<T> analyzer)Creates a view that runs analyzer over the back camera.

Methods

public boolean isSupported()Whether the current platform can open a camera and run this view’s analyzer.
public void setFacing(CameraFacing value)Selects which camera to open.
public CameraFacing getFacing()
public void setMaxFps(int value)Caps how many frames per second reach the analyzer.
public int getMaxFps()
public void setScaleType(ScaleType value)How the preview is fitted into this component’s bounds.
public ScaleType getScaleType()
public void setListener(VisionPipelineListener<T> value)Installs the callback for analysis results and recoverable failures.
public VisionPipelineListener<T> getListener()
public CameraSession getSession()The open camera session, for torch, zoom, focus, or a still capture.
public void setTorchEnabled(boolean on)Turns the torch on or off, when the open camera has one.
public boolean isRunning()
public void start()Opens the camera and starts analyzing.
public void stop()Stops analyzing and releases the camera, leaving the analyzer usable.
public void close()Releases the camera and the analyzer.
protected void initComponent()Opens the camera when the view becomes part of a shown form.
protected void deinitialize()Releases the camera when the view stops being shown.

Inherited fields

Inherited methods

From Container

encloseIn, encloseIn, initLaf, getUIManager, setUIManager, isSurface, add, addAll, add, add, add, add, add, getLeadComponent, setLeadComponent, getLeadParent, keyPressed, keyReleased, getLayout, setLayout, invalidate, setShouldLayout, setShouldCalcPreferredSize, getLayoutWidth, getLayoutHeight, applyRTL, constrainWidthWhenScrollable, constrainHeightWhenScrollable, addComponent, addComponent, addComponent, addComponent, replaceAndWait, replaceAndWait, replace, replaceAndWait, replace, createReplaceTransition, isEnabled, setEnabled, removeComponent, cancelRepaints, flushReplace, removeAll, revalidateWithAnimationSafety, revalidate, revalidateLater, forceRevalidate, clearClientProperties, paint, paintGlass, layoutContainer, isSafeArea, setSafeArea, isSafeAreaRoot, getSafeAreaRoot, setSafeAreaRoot, getComponentCount, getComponentAt, getComponentIndex, contains, scrollComponentToVisible, getClosestComponentTo, getResponderAt, getComponentAt, findDropTargetAt, pointerPressed, calcPreferredSize, paramString, refreshTheme, isScrollableX, setScrollableX, isScrollableY, setScrollableY, getSideGap, getBottomGap, setScrollable, setCellRenderer, getScrollIncrement, setScrollIncrement, findFirstFocusable, dragInitiated, fireClicked, isSelectableInteraction, getGridPosY, paintComponentBackground, getGridPosX, animateHierarchyAndWait, createAnimateHierarchy, animateHierarchy, animateHierarchyFadeAndWait, createAnimateHierarchyFade, animateHierarchyFade, animateLayoutFadeAndWait, createAnimateLayoutFadeAndWait, animateLayoutFade, createAnimateLayoutFade, animateLayoutAndWait, animateLayout, updateTabIndices, createAnimateLayout, drop, createAnimateMotion, morph, morphAndWait, animateUnlayout, animateUnlayoutAndWait, createAnimateUnlayout, getChildrenAsList, iterator, iterator

From Component

setSameSize, isSetCursorSupported, parsePreferredSize, getDefaultDragTransparency, setDefaultDragTransparency, getEditingDelegate, setEditingDelegate, getCursor, setCursor, showNativeOverlay, hideNativeOverlay, updateNativeOverlay, getNativeOverlay, getAllStyles, getSameWidth, setSameWidth, getSameHeight, setSameHeight, getX, setX, getOuterX, getInnerX, getY, setY, getOuterY, getInnerY, isVisible, setVisible, getClientProperty, stripMarginAndPadding, putClientProperty, getDirtyRegion, setDirtyRegion, isOpaque, setOpaque, getWidth, setWidth, getOuterWidth, getInnerWidth, getHeight, setHeight, getOuterHeight, getInnerHeight, isDragRegion, getDragRegionStatus, getBaseline, getBaselineResizeBehavior, getPreferredSizeStr, setPreferredSizeStr, getPreferredSize, setPreferredSize, getScrollDimension, calcScrollSize, setScrollSize, getPreferredW, setPreferredW, getPreferredH, setPreferredH, getOuterPreferredH, getInnerPreferredH, getOuterPreferredW, getInnerPreferredW, setSize, getUIID, setUIID, setUIIDFinal, setUIID, getInlineAllStyles, setInlineAllStyles, getInlineSelectedStyles, setInlineSelectedStyles, getInlineUnselectedStyles, setInlineUnselectedStyles, getInlineDisabledStyles, setInlineDisabledStyles, getInlinePressedStyles, setInlinePressedStyles, remove, getParent, getOwner, setOwner, isOwnedBy, containsOrOwns, addFocusListener, removeFocusListener, addScrollListener, removeScrollListener, getSelectCommandText, setSelectCommandText, getLabelForComponent, setLabelForComponent, focusGained, focusLost, paintBackgrounds, paintShadows, getAbsoluteX, getAbsoluteY, isInClippingRegion, paintIntersectingComponentsAbove, paintScrollbars, paintScrollbarX, getScrollOpacity, getSelectedRect, paintScrollbarY, paintComponent, paintComponent, getBorder, getScrollable, paintBackground, isScrollable, getScrollX, setScrollX, getScrollY, setScrollY, onScrollX, onScrollY, getDraggedx, getDraggedy, contains, visibleBoundsContains, hasFixedPreferredSize, getBounds, getBounds, getVisibleBounds, getVisibleBounds, isFocusable, setFocusable, onSetFocusable, resetFocusable, getTabIndex, setTabIndex, getPreferredTabIndex, setPreferredTabIndex, isTraversable, setTraversable, handlesInput, setHandlesInput, consumesRawTextInput, hasFocus, setFocus, getComponentForm, getTopLevelContainer, repaint, repaint, longKeyPress, keyRepeated, registerForAnimation, deregisterFromAnimation, getAnimationManager, getScrollAnimationSpeed, setScrollAnimationSpeed, isBlockLead, setBlockLead, isIgnorePointerEvents, setIgnorePointerEvents, isRippleEffect, setRippleEffect, getInlineStylesTheme, setInlineStylesTheme, shouldRenderComponentSelection, isHideInLandscape, setHideInLandscape, createStyleAnimation, isSmoothScrolling, setSmoothScrolling, pointerHover, stopScrollMomentum, pointerHoverReleased, pointerHoverPressed, pinch, pinchReleased, pinch, rotation, isPinchBlocksDragAndDrop, setPinchBlocksDragAndDrop, pointerDragged, getDragImage, getDragTransparency, setDragTransparency, toImage, drawDraggedImage, draggingOver, dragEnter, dragExit, addPullToRefresh, setPullToRefresh, respondsToPointerEvents, pointerDragged, isStickyDrag, pointerPressed, isDragAndDropOperation, pointerReleased, longPointerPress, pointerReleased, setVerticalScrollBounds, setHorizontalScrollBounds, isVScrollThumbGrabbed, isHScrollThumbGrabbed, isVScrollThumbHover, isHScrollThumbHover, isTensileDragEnabled, setTensileDragEnabled, getTextSelectionSupport, addDropListener, removeDropListener, addDragOverListener, removeDragOverListener, isNativeDragSource, setNativeDragSource, getNativeDragOperation, setNativeDragOperation, createNativeDragOperation, isNativeDropTarget, setNativeDropTarget, getAcceptedDropMimeTypes, setAcceptedDropMimeTypes, getAcceptedDropActions, setAcceptedDropActions, canAcceptNativeDrop, nativeDragEnter, nativeDragOver, nativeDragExit, nativeDrop, addNativeDropListener, removeNativeDropListener, addNativeDragOverListener, removeNativeDragOverListener, dragFinished, addDragFinishedListener, addStateChangeListener, removeStateChangeListener, addPointerPressedListener, addLongPressListener, addContextMenuListener, removeContextMenuListener, addMouseWheelListener, removeMouseWheelListener, addStylusListener, removeStylusListener, mouseWheel, paintRippleOverlay, removePointerPressedListener, removeLongPressListener, removeDragFinishedListener, addPointerReleasedListener, removePointerReleasedListener, addPointerDraggedListener, removePointerDraggedListener, getDragSpeed, getStyle, getPressedStyle, setPressedStyle, initUnselectedStyle, initPressedStyle, initDisabledStyle, initSelectedStyle, getUnselectedStyle, setUnselectedStyle, getSelectedStyle, setSelectedStyle, getDisabledStyle, setDisabledStyle, installDefaultPainter, requestFocus, toString, refreshTheme, refreshTheme, isDragActivated, animate, scrollRectToVisible, scrollRectToVisible, paintBorder, paintBorderBackground, isCellRenderer, isScrollVisible, setScrollVisible, setIsScrollVisible, startEditingAsync, stopEditing, isEditing, isEditable, laidOut, isInitialized, setInitialized, styleChanged, getNextFocusDown, setNextFocusDown, getNextFocusUp, setNextFocusUp, getNextFocusLeft, setNextFocusLeft, getNextFocusRight, setNextFocusRight, getName, setName, initCustomStyle, deinitializeCustomStyle, isRTL, setRTL, isTactileTouch, isTactileTouch, setTactileTouch, getPropertyNames, getPropertyTypes, getPropertyTypeNames, getPropertyValue, setPropertyValue, paintLockRelease, paintLock, isSnapToGrid, setSnapToGrid, shouldBlockSideSwipe, shouldBlockSideSwipeLeft, shouldBlockSideSwipeRight, blocksSideSwipe, isFlatten, setFlatten, getTensileLength, setTensileLength, isGrabsPointerEvents, setGrabsPointerEvents, getScrollOpacityChangeSpeed, setScrollOpacityChangeSpeed, growShrink, isAlwaysTensile, setAlwaysTensile, isDraggable, setDraggable, isDropTarget, setDropTarget, isChildOf, isHideInPortrait, setHideInPortrait, getBindablePropertyNames, getBindablePropertyTypes, bindProperty, unbindProperty, getBoundPropertyValue, setBoundPropertyValue, getCloudBoundProperty, setCloudBoundProperty, getCloudDestinationProperty, setCloudDestinationProperty, getComponentState, setComponentState, setHidden, isHidden, setHidden, isHidden, announceForAccessibility, getAccessibilityText, setAccessibilityText, getSemantics, getAccessibilityNode, accessibilityChanged, accessibilityChanged, getTooltip, setTooltip

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 VisionAnalyzer<T>
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 CameraFacing
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 int
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 ScaleType
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 VisionPipelineListener<T>
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 boolean
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.

initComponent

protected void initComponent()
Opens the camera when the view becomes part of a shown form.

deinitialize

protected void deinitialize()
Releases the camera when the view stops being shown.