MCPcopy Create free account
hub / github.com/RustCV/RustCV / main

Function main

rustcv-camera/examples/camera_demo.rs:11–66  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

9use rustcv_camera::{Mat, VideoCapture};
10
11fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
12 println!("=== rustcv-camera: Camera Preview ===");
13
14 let mut cap = VideoCapture::open(0)?;
15 let width = cap.width() as usize;
16 let height = cap.height() as usize;
17 println!("Camera opened: {}x{}", width, height);
18
19 // Create display window.
20 // 创建显示窗口。
21 let mut window = Window::new("rustcv-camera", width, height, WindowOptions::default())?;
22
23 // Pre-allocate buffers — reused every frame, zero allocation in the hot loop.
24 // 预分配缓冲区 —— 每帧复用,热循环中零分配。
25 let mut frame = Mat::new();
26 let mut display_buf: Vec<u32> = vec![0; width * height];
27
28 // FPS calculation.
29 // FPS 计算。
30 let mut fps_timer = Instant::now();
31 let mut fps_count: u32 = 0;
32 let mut fps_display: f64 = 0.0;
33
34 println!("Press ESC to exit.");
35
36 while window.is_open() && !window.is_key_down(Key::Escape) {
37 if !cap.read(&mut frame)? {
38 break;
39 }
40
41 // Convert BGR Mat → u32 display buffer (0x00RRGGBB for minifb).
42 // 将 BGR Mat 转换为 u32 显示缓冲区(minifb 使用 0x00RRGGBB 格式)。
43 bgr_to_u32(frame.data(), &mut display_buf);
44
45 // Draw FPS text as simple digit overlay in top-left corner.
46 // 在左上角绘制简单的 FPS 数字叠加。
47 draw_fps_overlay(&mut display_buf, width, fps_display);
48
49 // Update window.
50 // 更新窗口。
51 window.update_with_buffer(&display_buf, width, height)?;
52
53 // Update FPS counter every second.
54 // 每秒更新一次 FPS 计数。
55 fps_count += 1;
56 let elapsed = fps_timer.elapsed().as_secs_f64();
57 if elapsed >= 1.0 {
58 fps_display = fps_count as f64 / elapsed;
59 fps_count = 0;
60 fps_timer = Instant::now();
61 }
62 }
63
64 println!("Done.");
65 Ok(())
66}
67
68/// Convert BGR byte slice to u32 display buffer (0x00RRGGBB).

Callers

nothing calls this directly

Calls 7

bgr_to_u32Function · 0.70
draw_fps_overlayFunction · 0.70
openFunction · 0.50
widthMethod · 0.45
heightMethod · 0.45
readMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected