forked from processing/libprocessing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebcam.rs
More file actions
61 lines (53 loc) · 1.82 KB
/
webcam.rs
File metadata and controls
61 lines (53 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use bevy::prelude::Entity;
use processing_webcam::{
WebcamFormat, webcam_create, webcam_create_with_format, webcam_destroy, webcam_image,
webcam_is_connected, webcam_resolution,
};
use pyo3::{exceptions::PyRuntimeError, prelude::*};
use crate::graphics::Image;
#[pyclass(unsendable)]
pub struct Webcam {
entity: Entity,
}
#[pymethods]
impl Webcam {
#[new]
#[pyo3(signature = (width=None, height=None, framerate=None))]
pub fn new(
width: Option<u32>,
height: Option<u32>,
framerate: Option<u32>,
) -> PyResult<Self> {
let entity = match (width, height, framerate) {
(Some(w), Some(h), Some(fps)) => webcam_create_with_format(WebcamFormat::Exact {
resolution: bevy::math::UVec2::new(w, h),
framerate: fps,
}),
(Some(w), Some(h), None) => webcam_create_with_format(WebcamFormat::Resolution(
bevy::math::UVec2::new(w, h),
)),
(None, None, Some(fps)) => webcam_create_with_format(WebcamFormat::FrameRate(fps)),
_ => webcam_create(),
}
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
Ok(Self { entity })
}
pub fn is_connected(&self) -> PyResult<bool> {
webcam_is_connected(self.entity)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn resolution(&self) -> PyResult<(u32, u32)> {
webcam_resolution(self.entity)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn image(&self) -> PyResult<Image> {
let entity = webcam_image(self.entity)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
Ok(Image::from_entity(entity))
}
}
impl Drop for Webcam {
fn drop(&mut self) {
let _ = webcam_destroy(self.entity);
}
}