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

Function main

rustcv-backend-avf/examples/camera_view.rs:40–167  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

38#[cfg(target_os = "macos")]
39#[tokio::main]
40async fn main() -> Result<()> {
41 println!("=== RustCV AVFoundation Backend Demo ===");
42
43 // ── 步骤 1:实例化驱动 ──────────────────────────────────────────────
44 let driver = AvfDriver::new();
45
46 // ── 步骤 2:枚举并打印设备列表 ─────────────────────────────────────
47 let devices = driver.list_devices()?;
48 if devices.is_empty() {
49 anyhow::bail!("No cameras found! Please connect a camera.");
50 }
51
52 println!("Found {} device(s):", devices.len());
53 for (i, dev) in devices.iter().enumerate() {
54 println!(" [{}] {} (id: {})", i, dev.name, dev.id,);
55 }
56
57 // 默认使用第一个设备
58 let target = &devices[0];
59 println!("\nOpening device: {}", target.name);
60
61 let width: usize = 640;
62 let height: usize = 480;
63
64 // ── 步骤 3:构建采集配置 ────────────────────────────────────────────
65 // macOS AVFoundation 返回 32BGRA 格式,直接对接 minifb 最高效。
66 // Priority::Required 强制要求分辨率,fps 和 format 作为偏好请求。
67 let config = CameraConfig::new()
68 .resolution(width as u32, height as u32, Priority::Required)
69 .fps(30, Priority::High)
70 .format(FourCC::new(b'B', b'G', b'R', b'A'), Priority::High);
71
72 // ── 步骤 4:打开设备,获取 Stream 和 Controls ─────────────────────
73 let (mut stream, _controls) = driver
74 .open(&target.id, config)
75 .context("Failed to open camera device")?;
76
77 // ── 步骤 5:启动流 ─────────────────────────────────────────────────
78 stream.start().await.context("Failed to start stream")?;
79 println!("Stream started! Press ESC to exit.");
80
81 // ── 创建 minifb 窗口 ───────────────────────────────────────────────
82 let mut window = Window::new(
83 "RustCV AVF — Camera Preview (ESC to quit)",
84 width,
85 height,
86 WindowOptions::default(),
87 )
88 .context("Failed to create window")?;
89
90 // minifb 0.24 上允许设置最大帧率(避免 CPU 空转)
91 window.limit_update_rate(Some(std::time::Duration::from_micros(33_333))); // ~30fps
92
93 // ── FPS 计量 ───────────────────────────────────────────────────────
94 let mut last_fps_time = Instant::now();
95 let mut frame_count: u32 = 0;
96 let mut display_fps: f64 = 0.0;
97

Callers

nothing calls this directly

Calls 11

bgra_to_rgb32Function · 0.85
draw_text_osdFunction · 0.85
formatMethod · 0.80
list_devicesMethod · 0.45
is_emptyMethod · 0.45
fpsMethod · 0.45
resolutionMethod · 0.45
openMethod · 0.45
startMethod · 0.45
next_frameMethod · 0.45
stopMethod · 0.45

Tested by

no test coverage detected