When selecting a camera format from a list of available options, you can use FormatRequestType to define a strategy for picking the best match. This is an optional helper; alternatively, you can use crate::camera::Camera::enumerate_formats for a simpler approach.
Available strategies:
Closest: Attempts to find the CameraFormat closest to a preferred resolution and frame rate. It can optionally take Range constraints for resolution and frame_rate to filter valid candidates.HighestFrameRate: Filters formats within a specific frame_rate range and sorts them to prioritize the highest available rate.HighestResolution: Filters formats within a specific resolution range and sorts them to prioritize the highest available resolution.Exact: Returns only formats that exactly match the provided resolution and frame_rate.Any: Returns all available formats without filtering or sorting.
use nokhwa::format_request::FormatRequestType;
use nokhwa::types::{Resolution, FrameRate};
use nokhwa::ranges::Range;
// Example: Requesting the closest format to a specific target
let strategy = FormatRequestType::Closest {
resolution: Some(Range::new(Resolution::new(1280, 720), Resolution::new(1920, 1080))),
preferred_resolution: Some(Resolution::new(1920, 1080)),
frame_rate: None,
preferred_frame_rate: Some(FrameRate::from_fps(30.0)),
};