-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathpiston.rs
54 lines (50 loc) · 1.65 KB
/
piston.rs
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
extern crate camera_capture;
extern crate image;
extern crate piston_window;
use image::ConvertBuffer;
use piston_window::{clear, PistonWindow, Texture, TextureSettings, WindowSettings};
fn main() {
let mut window: PistonWindow = WindowSettings::new("piston: image", [300, 300])
.exit_on_esc(true)
.build()
.unwrap();
let mut tex: Option<Texture<_>> = None;
let (sender, receiver) = std::sync::mpsc::channel();
let imgthread = std::thread::spawn(move || {
let res1 = camera_capture::create(0);
if let Err(e) = res1 {
eprintln!("could not open camera: {}", e);
std::process::exit(1);
}
let res2 = res1.unwrap().fps(5.0).unwrap().start();
if let Err(e) = res2 {
eprintln!("could retrieve data from camera: {}", e);
std::process::exit(2);
}
let cam = res2.unwrap();
for frame in cam {
if sender.send(frame.convert()).is_err() {
break;
}
}
});
while let Some(e) = window.next() {
if let Ok(frame) = receiver.try_recv() {
if let Some(mut t) = tex {
t.update(&mut window.encoder, &frame).unwrap();
tex = Some(t);
} else {
tex =
Texture::from_image(&mut window.factory, &frame, &TextureSettings::new()).ok();
}
}
window.draw_2d(&e, |c, g| {
clear([1.0; 4], g);
if let Some(ref t) = tex {
piston_window::image(t, c.transform, g);
}
});
}
drop(receiver);
imgthread.join().unwrap();
}