Class Camera

java.lang.Object
com.codename1.camera.Camera

public final class Camera extends Object

Entry point for the low-level cross-platform camera API.

This API gives the application direct access to the device camera: live preview, frame streaming, still capture, video recording, flash and focus control. It is intended for use cases that the file-based com.codename1.capture.Capture API cannot serve - real-time barcode scanning, document boundary detection, custom in-app camera UIs.

Permissions: simply referencing classes in this package causes the build pipeline to inject NSCameraUsageDescription / NSMicrophoneUsageDescription (iOS) and android.permission.CAMERA / android.permission.RECORD_AUDIO plus the CameraX gradle dependencies (Android). Developers may override the plist strings via the ios.NSCameraUsageDescription build hint.

Coexistence with Capture: the old com.codename1.capture.Capture API continues to work unchanged. Both may be used in the same app, but only one camera consumer may hold the device at a time. Call CameraSession#pause() before invoking Capture.capturePhoto(...) and CameraSession#resume() afterwards.

if (Camera.isSupported()) {
    CameraSession s = Camera.open(Camera.getDefault(CameraFacing.BACK),
                                  new CameraSessionOptions());
    CameraView v = s.createView();
    // add v to a Form...
    s.setFrameListener(frame ->
        BarcodeScanner.scan(frame.getJpegBytes()).ready(codes -> { ... }));
}
  • Method Details

    • isSupported

      public static boolean isSupported()
      True when the running platform has a working camera implementation. False on platforms (or simulator runs) where the camera back-end could not be initialized.
    • getCameras

      public static CameraInfo[] getCameras()
      Enumerate cameras visible to the platform. May be empty.
    • getDefault

      public static CameraInfo getDefault(CameraFacing facing)
      Convenience that returns the first camera matching the given facing, or null if none. When no facing-specific camera is found and any camera exists, the first available camera is returned.
    • open

      public static CameraSession open(CameraInfo info, CameraSessionOptions opts)
      Open a camera session. Throws IllegalStateException if a session is already open; close the old session first.
    • requestPermissions

      public static void requestPermissions(boolean audio, SuccessCallback<Boolean> callback)
      Request runtime permission for camera (and optionally microphone). The callback receives true when both are granted, false otherwise. On iOS this is a no-op that delivers true immediately; the system prompts the first time the camera is actually started.