Sign in

    camera

    Native camera discovery and capture-mode helpers.

    moonbit
    native
    camera
    capture
    Download zip
    Author
    Version
    0.1.3
    License
    MIT
    Last updated
    3 months ago
    Downloads
    50

    #justjavac/camera

    CI coverage linux macos windows

    Native-only camera discovery helpers for MoonBit.

    fn main {
    for device in @camera.CameraDevice::list() {
    println(device.label())
    }
    }

    Run examples with --target native:

    moon run src/examples/list_cameras --target native moon run src/examples/parse_listing --target native

    CameraDevice

    pub(all) struct CameraDevice {
    id : String
    name : String
    facing : Facing
    mode : CaptureMode
    torch : Bool
    } derive(Eq,
    Debug
    )

    A camera device descriptor returned by the native discovery helpers.

    The descriptor is intentionally small and portable: id is stable within one parsed listing, name preserves the native display name, facing is a best effort hint, mode is the known or default capture mode, and torch reports whether torch-style illumination is expected to be available.

    CameraDevice::is_high_definition

    fn CameraDevice::is_high_definition(self : CameraDevice) -> Bool

    Whether the device default mode should be treated as HD.

    This checks the normalized default mode and returns true for devices whose default capture dimensions are at least 1280x720.

    Example

    test {
    let device = @camera.CameraDevice::{
    id: "camera-0",
    name: "HD Camera",
    facing: External,
    mode: { width: 1280, height: 720, fps: 30, pixel_format: Mjpeg },
    torch: false,
    }
    inspect(device.is_high_definition(), content="true")
    }

    CameraDevice::label

    fn CameraDevice::label(self : CameraDevice) -> String

    Produce a concise device label for camera pickers.

    The label uses facing:name so UI code can display predictable values while still preserving the native device name exactly as reported.

    Example

    test {
    let device = @camera.CameraDevice::{
    id: "camera-0",
    name: "USB Camera",
    facing: External,
    mode: { width: 1280, height: 720, fps: 30, pixel_format: Mjpeg },
    torch: false,
    }
    inspect(device.label(), content="external:USB Camera")
    }

    CameraDevice::list

    List camera devices visible to the current native platform.

    Discovery is intentionally lightweight: the backend uses platform APIs to enumerate camera names and turns them into portable descriptors. Applications that need negotiated frame formats can use these descriptors as a first discovery pass before opening a platform-specific capture session.

    Example

    test {
    for device in @camera.CameraDevice::list() {
    ignore(device.label())
    }
    }

    CameraDevice::parse_listing

    fn CameraDevice::parse_listing(output : String) -> Array[CameraDevice]

    Parse a native camera listing into camera descriptors.

    Each non-empty line is treated as one camera name. Native platform APIs usually omit capture modes at this discovery stage, so parsed devices receive a conservative 1280x720@30 MJPEG default mode and torch=false.

    Example

    test {
    let devices = @camera.CameraDevice::parse_listing(
    "Front Camera\nUSB Camera\n",
    )
    inspect(devices.length(), content="2")
    inspect(devices[0].label(), content="front:Front Camera")
    inspect(devices[1].label(), content="external:USB Camera")
    }

    CameraDevice::supports_low_latency_preview

    fn CameraDevice::supports_low_latency_preview(self : CameraDevice) -> Bool

    Whether the default mode is suitable for low-latency preview.

    A device is considered low-latency friendly when its normalized default mode runs at 60 frames per second or above.

    Example

    test {
    let device = @camera.CameraDevice::{
    id: "camera-0",
    name: "Fast Camera",
    facing: External,
    mode: { width: 640, height: 480, fps: 60, pixel_format: Yuy2 },
    torch: false,
    }
    inspect(device.supports_low_latency_preview(), content="true")
    }

    CaptureMode

    pub(all) struct CaptureMode {
    width : Int
    height : Int
    fps : Int
    pixel_format : PixelFormat
    } derive(Eq,
    Debug
    )

    A concrete camera capture mode with dimensions, frame rate, and pixel format.

    The values are intended to describe a usable preview or capture stream. Use CaptureMode::normalize before performing arithmetic with user-provided or native data, because platform APIs can report incomplete mode information.
    impl Show for CaptureMode

    CaptureMode::frame_interval_ms

    fn CaptureMode::frame_interval_ms(self : CaptureMode) -> Int

    Return the capture interval in milliseconds for one frame.

    The mode is normalized before the calculation, so an invalid fps value is treated as 1. The result uses integer milliseconds because native preview schedulers usually accept whole millisecond delays.

    Example

    test {
    let mode = @camera.CaptureMode::{
    width: 1280,
    height: 720,
    fps: 25,
    pixel_format: Mjpeg,
    }
    inspect(mode.frame_interval_ms(), content="40")
    }

    CaptureMode::normalize

    fn CaptureMode::normalize(self : CaptureMode) -> CaptureMode

    Normalize a camera capture mode into a safe baseline.

    Width, height, and frames per second are clamped to at least 1, while the pixel format is preserved. This keeps downstream frame timing and layout calculations from dividing by zero or producing negative sizes.

    Example

    test {
    let mode = @camera.CaptureMode::{
    width: 0,
    height: -720,
    fps: 0,
    pixel_format: Nv12,
    }.normalize()
    inspect(mode.width, content="1")
    inspect(mode.height, content="1")
    inspect(mode.fps, content="1")
    }

    Facing

    pub(all) enum Facing {
    Front
    Back
    External
    } derive(Eq,
    Debug
    )

    Camera lens placement as reported or inferred from the native device name.

    Native discovery APIs do not always expose a normalized facing field, so the package infers this value from common device names when necessary. Use it as a UI hint rather than as a hardware guarantee.
    impl Show for Facing

    Facing::name

    fn Facing::name(self : Facing) -> String

    Return the stable label for camera placement.

    The label is lowercase and intended for UI copy, logs, and serialized metadata where the enum constructor name would be too MoonBit-specific. Values are stable across releases unless the enum itself changes.

    Example

    test {
    inspect(@camera.Facing::Back.name(), content="back")
    }

    PixelFormat

    pub(all) enum PixelFormat {
    Mjpeg
    Yuy2
    Nv12
    Rgba
    } derive(Eq,
    Debug
    )

    Pixel formats commonly reported by native camera stacks and preview APIs.

    Discovery currently returns a conservative default format because platform APIs often separate device enumeration from stream negotiation. Applications that open a capture session can replace this with the negotiated format.
    impl Show for PixelFormat

    PixelFormat::name

    fn PixelFormat::name(self : PixelFormat) -> String

    Return the stable label for a pixel format.

    The label is lowercase and mirrors common native camera terminology such as mjpeg, yuy2, and nv12. Use it when printing modes or exposing device metadata to configuration files.

    Example

    test {
    inspect(@camera.PixelFormat::Nv12.name(), content="nv12")
    }

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io