-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathreuse_debug.rs
More file actions
94 lines (80 loc) · 2.79 KB
/
Copy pathreuse_debug.rs
File metadata and controls
94 lines (80 loc) · 2.79 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//! Debug camera reuse behavior
use nokhwa::{
pixel_format::RgbFormat,
utils::{CameraIndex, RequestedFormat, RequestedFormatType},
Camera,
};
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("\n=============================================================");
println!(" Camera Reuse Debug");
println!("=============================================================\n");
// Create first camera
println!("[1] Creating camera (first time)...");
let requested =
RequestedFormat::new::<RgbFormat>(RequestedFormatType::AbsoluteHighestResolution);
let mut camera = Camera::new(CameraIndex::Index(0), requested)?;
println!(" Format: {:?}", camera.camera_format());
// Open stream
println!("[2] Opening stream...");
camera.open_stream()?;
// Capture frame
let frame = camera.frame()?;
println!(
" Frame 1: {}x{}, {} bytes",
frame.resolution().width_x,
frame.resolution().height_y,
frame.buffer().len()
);
// Close stream
println!("[3] Closing stream...");
camera.stop_stream()?;
// Sleep a bit
std::thread::sleep(std::time::Duration::from_millis(500));
// Reopen stream (simulating registry reuse)
println!("[4] Reopening stream on SAME camera object...");
camera.open_stream()?;
// Capture another frame
let frame = camera.frame()?;
println!(
" Frame 2: {}x{}, {} bytes",
frame.resolution().width_x,
frame.resolution().height_y,
frame.buffer().len()
);
// Check MJPEG header
let buffer = frame.buffer();
if buffer.len() >= 3 {
println!(
" Has MJPEG header: {}",
buffer[0] == 0xFF && buffer[1] == 0xD8
);
}
camera.stop_stream()?;
// Now try creating a SECOND camera object for same device
println!("\n[5] Creating NEW camera object for same device...");
let requested2 =
RequestedFormat::new::<RgbFormat>(RequestedFormatType::AbsoluteHighestResolution);
let camera2_result = Camera::new(CameraIndex::Index(0), requested2);
match camera2_result {
Ok(mut camera2) => {
println!(
" Second camera created! Format: {:?}",
camera2.camera_format()
);
camera2.open_stream()?;
let frame = camera2.frame()?;
println!(
" Frame 3: {}x{}, {} bytes",
frame.resolution().width_x,
frame.resolution().height_y,
frame.buffer().len()
);
camera2.stop_stream()?;
}
Err(e) => {
println!(" ERROR creating second camera: {}", e);
}
}
println!("\n=============================================================\n");
Ok(())
}