Skip to main content

freya_camera/
lib.rs

1//! Camera capture for Freya.
2//!
3//! - [`use_camera`](use_camera::use_camera): a hook that streams frames from a
4//!   camera into reactive state.
5//! - [`CameraViewer`](camera_viewer::CameraViewer): a component that renders
6//!   the captured frames.
7//!
8//! This crate is reexported in `freya::camera`.
9//!
10//! # Example
11//!
12//! ```rust, no_run
13//! use freya::{
14//!     camera::*,
15//!     prelude::*,
16//! };
17//!
18//! fn app() -> impl IntoElement {
19//!     let camera = use_camera(CameraConfig::default);
20//!     CameraViewer::new(camera)
21//! }
22//! ```
23//!
24//! On macOS the system requires a one time authorization step before any
25//! camera can be opened. Call [`init`] from `main` to request it.
26
27pub mod camera;
28pub mod camera_viewer;
29pub(crate) mod capture;
30pub mod use_camera;
31
32pub use nokhwa;
33
34/// Request access to the system cameras. Always `true` on Linux/Windows.
35/// On macOS, blocks on the authorization prompt and returns whether access was granted;
36/// call once from `main` before launching the app.
37#[cfg(target_os = "macos")]
38pub fn init() -> bool {
39    use std::sync::mpsc;
40
41    let (tx, rx) = mpsc::channel();
42    nokhwa::nokhwa_initialize(move |granted| {
43        let _ = tx.send(granted);
44    });
45    rx.recv().unwrap_or(false)
46}
47
48#[cfg(not(target_os = "macos"))]
49pub fn init() -> bool {
50    true
51}
52
53pub mod prelude {
54    pub use crate::{
55        camera::*,
56        camera_viewer::*,
57        use_camera::*,
58    };
59}