camera
stableWebcam capture plugin providing a Camera class for opening devices, grabbing RGBA frames, and tuning resolution, FPS, and image properties, plus utility functions for frame buffer math and pixel manipulation.
use plugin camera::{Camera.new, close, is_open, …} Functions (19)
- Camera.new Open a camera device by index
- close Close the camera device
- is_open Check whether the camera is open
- capture Capture an RGBA frame as bytes
- get_resolution Get the current capture resolution
- set_resolution Set the capture resolution
- get_fps Get the configured frame rate
- set_fps Set the frame rate
- get_properties Get brightness, contrast, and saturation
- set_property Set an image property by name
- get_info Get full device info and state
- get_frame_count Get how many frames have been captured
- list_devices List available camera devices
- frame_buffer_size Compute the byte size of a frame buffer
- create_frame_buffer Allocate a zeroed RGBA frame buffer
- pixel_at Read one RGBA pixel from a frame
- set_pixel Write one RGBA pixel into a frame
- crop_frame Crop a rectangle out of an RGBA frame
- frame_info Compute frame metadata for given dimensions
Overview
The camera plugin models a webcam as a stateful Camera handle: you open a device by index, capture frames, and tune its resolution, frame rate, and image properties (brightness, contrast, saturation). Frames come back as raw RGBA bytes — 4 bytes per pixel, row-major, of length width * height * 4 — so the plugin also ships a set of pure utility functions for working with those buffers (sizing, allocating, reading and writing individual pixels, and cropping). The simulated devices return a gradient test pattern, which makes the plugin handy for prototyping image pipelines without real hardware. Reach for it whenever you need a capture source plus low-level frame math in the same place.
Common patterns
Open a device, capture a frame, and inspect a pixel:
use plugin camera::{Camera, pixel_at}
let cam = Camera.new(0)
let res = cam.get_resolution()
let frame = cam.capture()
let px = pixel_at(frame, res["width"], 0, 0)
print("top-left: r={px["r"]} g={px["g"]} b={px["b"]}")
cam.close()
Tune the camera, then make a thumbnail from the captured frame:
use plugin camera::{Camera, crop_frame}
let cam = Camera.new(0)
cam.set_resolution(1920, 1080)
cam.set_property("brightness", 0.7)
let frame = cam.capture()
let thumb = crop_frame(frame, 1920, 1080, 0, 0, 320, 240)
print("captured {cam.get_frame_count()} frame(s)")
cam.close()
Build a frame from scratch with the pure buffer helpers:
use plugin camera::{create_frame_buffer, set_pixel, pixel_at}
let buf = create_frame_buffer(8, 8)
let out = set_pixel(buf, 8, 3, 3, 255, 0, 0, 255)
let px = pixel_at(out, 8, 3, 3)
print("painted red: r={px["r"]}")
Open a camera device by index
Opens the camera device at index (as reported by list_devices) and returns a Camera handle. Errors if no device exists at that index.
use plugin camera::{Camera}
let cam = Camera.new(0)
let frame = cam.capture()
cam.close()
Close the camera device
Closes the camera device. Errors if the camera is already closed; subsequent capture calls will fail.
use plugin camera::{Camera}
let cam = Camera.new(0)
cam.close()
print(cam.is_open()) // false
Check whether the camera is open
Returns true while the camera is open and false after it has been closed.
use plugin camera::{Camera}
let cam = Camera.new(0)
if cam.is_open() {
let frame = cam.capture()
print("captured while open")
}
cam.close()
print("still open? {cam.is_open()}")
Capture an RGBA frame as bytes
Captures a single frame and returns it as raw RGBA bytes (4 bytes per pixel, row-major, length width * height * 4). Also increments the camera's frame counter. Errors if the camera is not open.
use plugin camera::{Camera, pixel_at}
let cam = Camera.new(0)
let res = cam.get_resolution()
let frame = cam.capture()
let px = pixel_at(frame, res["width"], 0, 0)
print("top-left pixel: r={px["r"]} g={px["g"]} b={px["b"]}")
Get the current capture resolution
Returns the current capture resolution as a table with width and height fields.
use plugin camera::{Camera}
let cam = Camera.new(0)
let res = cam.get_resolution()
print("{res["width"]}x{res["height"]}")
Set the capture resolution
Changes the capture resolution. Both dimensions must be positive integers; subsequent capture calls return frames at the new size.
use plugin camera::{Camera}
let cam = Camera.new(0)
cam.set_resolution(640, 480)
let frame = cam.capture() // 640*480*4 bytes
Get the configured frame rate
Returns the camera's configured frame rate.
use plugin camera::{Camera}
let cam = Camera.new(1)
let dt = 1.0 / cam.get_fps()
print("frame interval: {dt}s")
Set the frame rate
Sets the camera's frame rate. The value must be positive.
use plugin camera::{Camera}
let cam = Camera.new(1)
cam.set_fps(24.0)
print("fps: {cam.get_fps()}")
Get brightness, contrast, and saturation
Returns the current image adjustment properties. Each value is a float in the range 0.0 to 1.0 (default 0.5).
use plugin camera::{Camera}
let cam = Camera.new(0)
cam.set_property("contrast", 0.9)
cam.set_property("saturation", 0.3)
let props = cam.get_properties()
print("contrast={props["contrast"]} saturation={props["saturation"]}")
Set an image property by name
Sets an image property by name: "brightness", "contrast", or "saturation". The value is clamped to the 0.0–1.0 range. Errors on an unknown property name.
use plugin camera::{Camera}
let cam = Camera.new(0)
cam.set_property("brightness", 0.8)
let props = cam.get_properties()
print("brightness: {props["brightness"]}")
Get full device info and state
Returns a snapshot of the device's full state: its index, human-readable name, resolution, frame rate, open flag, and total frames captured.
use plugin camera::{Camera}
let cam = Camera.new(0)
let info = cam.get_info()
print("{info["name"]}: {info["width"]}x{info["height"]} @ {info["fps"]}fps")
Get how many frames have been captured
Returns how many frames this camera has captured since it was opened.
use plugin camera::{Camera}
let cam = Camera.new(0)
cam.capture()
cam.capture()
cam.capture()
print("frames: {cam.get_frame_count()}")
cam.close()
List available camera devices
Returns a list of available camera devices. Each entry is a table with index, name, width, height, and fps fields describing the device's default mode.
use plugin camera::{list_devices}
let devices = list_devices()
for d in devices {
print("[{d["index"]}] {d["name"]} {d["width"]}x{d["height"]} @ {d["fps"]}fps")
}
Compute the byte size of a frame buffer
Computes the byte size of a frame buffer: w * h * channels. Use 4 channels for RGBA.
use plugin camera::{frame_buffer_size}
let size = frame_buffer_size(1920, 1080, 4)
print("full HD RGBA frame: {size} bytes")
Allocate a zeroed RGBA frame buffer
Allocates a zeroed RGBA buffer of w * h * 4 bytes. Useful as a blank canvas for set_pixel.
use plugin camera::{create_frame_buffer, set_pixel}
let buf = create_frame_buffer(64, 64)
let painted = set_pixel(buf, 64, 32, 32, 255, 0, 0, 255)
Read one RGBA pixel from a frame
Reads the RGBA pixel at (x, y) from a frame buffer with row width w. Errors if the coordinates fall outside the buffer.
use plugin camera::{Camera, pixel_at}
let cam = Camera.new(2)
let frame = cam.capture()
let px = pixel_at(frame, 640, 100, 50)
print("r={px["r"]} g={px["g"]} b={px["b"]} a={px["a"]}")
Write one RGBA pixel into a frame
Returns a copy of the frame buffer with the pixel at (x, y) set to the given RGBA values (each 0–255). The original buffer is not modified. Errors if the coordinates are out of bounds.
use plugin camera::{create_frame_buffer, set_pixel, pixel_at}
let buf = create_frame_buffer(8, 8)
let out = set_pixel(buf, 8, 3, 3, 0, 255, 0, 255)
let px = pixel_at(out, 8, 3, 3)
print("green: {px["g"]}")
Crop a rectangle out of an RGBA frame
Crops a cw x ch rectangle starting at (x, y) out of an RGBA frame of size w x h, returning a new buffer of cw * ch * 4 bytes. Errors if the rectangle extends past the source frame.
use plugin camera::{Camera, crop_frame}
let cam = Camera.new(0)
let frame = cam.capture()
let thumb = crop_frame(frame, 1920, 1080, 0, 0, 320, 240)
Compute frame metadata for given dimensions
Computes metadata for an RGBA frame of the given dimensions: total byte size (w * h * 4) and row stride (w * 4), alongside the echoed width, height, and fps.
use plugin camera::{frame_info}
let info = frame_info(1280, 720, 60.0)
print("size={info["size"]} stride={info["stride"]}")
use plugin camera::{Camera, frame_info}
let cam = Camera.new(2)
let res = cam.get_resolution()
let info = frame_info(res["width"], res["height"], cam.get_fps())
print("{info["width"]}x{info["height"]} needs {info["size"]} bytes per frame")