Class CameraView

All Implemented Interfaces:
Animation, Editable, StyleListener, Iterable<Component>

public final class CameraView extends Container

Component that renders a live camera preview. Created via CameraSession#createView() and added to a form like any other component.

CameraInfo info = Camera.getDefault(CameraFacing.BACK);
CameraSession session = Camera.open(info, new CameraSessionOptions());
CameraView view = session.createView();
view.setScaleType(ScaleType.CROP);

Form f = new Form("Camera", new BorderLayout());
f.add(BorderLayout.CENTER, view);
f.show();

session.setFrameListener(frame ->
    BarcodeScanner.scan(frame.getJpegBytes()).ready(codes -> {
        if (codes.length > 0) Log.p("scanned " + codes[0]);
    }));

The view holds a back-reference to its CameraSession; closing the session while the view is still attached to a form leaves the view rendering its last frame as a still image.

  • Method Details

    • getSession

      public CameraSession getSession()
      The CameraSession backing this view.
    • setMirrored

      public void setMirrored(boolean m)
      Whether to horizontally mirror the preview. Usually true for front cameras (matches the "mirror" feel users expect from a selfie view).
    • isMirrored

      public boolean isMirrored()
      True when the preview is horizontally mirrored.
    • setScaleType

      public void setScaleType(ScaleType s)
      How the live preview is fitted inside the component bounds. Defaults to ScaleType#CROP (filling the view, cropping at edges).
    • getScaleType

      public ScaleType getScaleType()
      Current scale type. Defaults to ScaleType#CROP.
    • getPreviewPeer

      public PeerComponent getPreviewPeer()
      Exposed for ports that need to update the preview node directly.