Skip to main content

freya_camera/
camera.rs

1//! Camera configuration types.
2
3pub use nokhwa::{
4    NokhwaError as CameraError,
5    utils::{
6        CameraIndex,
7        CameraInfo,
8    },
9};
10use nokhwa::{
11    query as nokhwa_query,
12    utils::ApiBackend,
13};
14
15/// Requested capture format. The negotiated values are reported via [`StreamInfo`].
16#[derive(Clone, Debug, PartialEq, Eq, Hash, Default)]
17pub enum CameraFormat {
18    /// Highest framerate available, any resolution.
19    #[default]
20    HighestFrameRate,
21    /// Highest resolution available, any framerate.
22    HighestResolution,
23    /// Highest framerate at the given resolution.
24    Resolution { width: u32, height: u32 },
25    /// Closest match to the given resolution and framerate.
26    Exact {
27        width: u32,
28        height: u32,
29        frame_rate: u32,
30    },
31}
32
33/// Configuration used to open a camera.
34#[derive(Clone, Debug, PartialEq, Eq, Hash)]
35pub struct CameraConfig {
36    pub device: CameraIndex,
37    pub format: CameraFormat,
38}
39
40impl Default for CameraConfig {
41    fn default() -> Self {
42        Self {
43            device: CameraIndex::Index(0),
44            format: CameraFormat::default(),
45        }
46    }
47}
48
49impl CameraConfig {
50    pub fn new() -> Self {
51        Self::default()
52    }
53
54    pub fn device(mut self, device: CameraIndex) -> Self {
55        self.device = device;
56        self
57    }
58
59    pub fn format(mut self, format: CameraFormat) -> Self {
60        self.format = format;
61        self
62    }
63}
64
65/// Negotiated information about a running camera.
66#[derive(Clone, Debug, PartialEq, Eq)]
67pub struct StreamInfo {
68    pub width: u32,
69    pub height: u32,
70    pub frame_rate: u32,
71}
72
73/// Enumerate the cameras available on the system.
74///
75/// # Example
76///
77/// ```rust, no_run
78/// use freya::camera::*;
79///
80/// for device in query().unwrap_or_default() {
81///     println!("{}: {}", device.human_name(), device.description());
82/// }
83/// ```
84pub fn query() -> Result<Vec<CameraInfo>, CameraError> {
85    nokhwa_query(ApiBackend::Auto)
86}