pub struct Provider { /* private fields */ }Expand description
Type alias for the global error callback
§Thread Safety
Provider implements Send to allow moving the provider between threads.
However, the underlying C++ implementation is NOT thread-safe.
Important: You must ensure that:
- Only one thread accesses the
Providerat a time - Use
Arc<Mutex<Provider>>or similar synchronization if sharing between threads - If you need to integrate with an async runtime, wrap the
Provideryourself (e.g. with a mutex and a dedicated worker thread)
§Example (Safe Multi-threaded Usage)
use std::sync::{Arc, Mutex};
use ccap::Provider;
let provider = Arc::new(Mutex::new(Provider::new()?));
let provider_clone = Arc::clone(&provider);
std::thread::spawn(move || {
let mut guard = provider_clone.lock().unwrap();
// Safe: mutex ensures exclusive access
guard.grab_frame(1000).ok();
});Implementations§
Source§impl Provider
impl Provider
Sourcepub fn new() -> Result<Self>
pub fn new() -> Result<Self>
Create a new camera provider
Examples found in repository?
3fn find_camera_names() -> Result<Vec<String>> {
4 // Create a temporary provider to query devices
5 let provider = Provider::new()?;
6 let devices = provider.list_devices()?;
7
8 if !devices.is_empty() {
9 println!("## Found {} video capture device:", devices.len());
10 for (index, name) in devices.iter().enumerate() {
11 println!(" {}: {}", index, name);
12 }
13 } else {
14 eprintln!("Failed to find any video capture device.");
15 }
16
17 Ok(devices)
18}More examples
3fn main() -> Result<()> {
4 // Set error callback to receive error notifications
5 Provider::set_error_callback(|error_code, description| {
6 eprintln!(
7 "Error occurred - Code: {}, Description: {}",
8 error_code, description
9 );
10 });
11
12 let temp_provider = Provider::new()?;
13 let devices = temp_provider.list_devices()?;
14 let camera_index = Utils::select_camera(&devices)?;
15
16 // Use device index instead of name to avoid issues
17 let camera_index_i32 = i32::try_from(camera_index).map_err(|_| {
18 CcapError::InvalidParameter(format!(
19 "camera index {} does not fit into i32",
20 camera_index
21 ))
22 })?;
23 let mut provider = Provider::with_device(camera_index_i32)?;
24 provider.open()?;
25 provider.start()?;
26
27 if !provider.is_started() {
28 eprintln!("Failed to start camera!");
29 return Ok(());
30 }
31
32 println!("Camera started successfully.");
33
34 // Capture frames
35 for i in 0..10 {
36 match provider.grab_frame(3000) {
37 Ok(Some(frame)) => {
38 println!(
39 "VideoFrame {} grabbed: width = {}, height = {}, bytes: {}, format: {:?}",
40 frame.index(),
41 frame.width(),
42 frame.height(),
43 frame.data_size(),
44 frame.pixel_format()
45 );
46 }
47 Ok(None) => {
48 eprintln!("Failed to grab frame {}!", i);
49 return Ok(());
50 }
51 Err(e) => {
52 eprintln!("Error grabbing frame {}: {}", i, e);
53 return Ok(());
54 }
55 }
56 }
57
58 println!("Captured 10 frames, stopping...");
59 let _ = provider.stop();
60 Ok(())
61}4fn main() -> Result<()> {
5 // Enable verbose log to see debug information
6 Utils::set_log_level(LogLevel::Verbose);
7
8 // Set error callback to receive error notifications
9 Provider::set_error_callback(|error_code, description| {
10 eprintln!(
11 "Camera Error - Code: {}, Description: {}",
12 error_code, description
13 );
14 });
15
16 // Create a camera provider
17 let mut provider = Provider::new()?;
18
19 // Open default device
20 provider.open()?;
21 provider.start_capture()?;
22
23 if !provider.is_started() {
24 eprintln!("Failed to start camera!");
25 return Ok(());
26 }
27
28 // Print the real resolution and fps after camera started
29 let real_width = provider.get_property(PropertyName::Width)? as u32;
30 let real_height = provider.get_property(PropertyName::Height)? as u32;
31 let real_fps = provider.get_property(PropertyName::FrameRate)?;
32
33 println!(
34 "Camera started successfully, real resolution: {}x{}, real fps: {}",
35 real_width, real_height, real_fps
36 );
37
38 // Create capture directory
39 let capture_dir = "./image_capture";
40 if !std::path::Path::new(capture_dir).exists() {
41 fs::create_dir_all(capture_dir).map_err(|e| {
42 ccap::CcapError::InvalidParameter(format!("Failed to create directory: {}", e))
43 })?;
44 }
45
46 // Capture frames (3000 ms timeout when grabbing frames)
47 let mut frame_count = 0;
48 while let Some(frame) = provider.grab_frame(3000)? {
49 let frame_info = frame.info()?;
50 println!(
51 "VideoFrame {} grabbed: width = {}, height = {}, bytes: {}",
52 frame_info.frame_index, frame_info.width, frame_info.height, frame_info.size_in_bytes
53 );
54
55 // Save frame to directory
56 match Utils::dump_frame_to_directory(&frame, capture_dir) {
57 Ok(dump_file) => {
58 println!("VideoFrame saved to: {}", dump_file);
59 }
60 Err(e) => {
61 eprintln!("Failed to save frame: {}", e);
62 }
63 }
64
65 frame_count += 1;
66 if frame_count >= 10 {
67 println!("Captured 10 frames, stopping...");
68 break;
69 }
70 }
71
72 Ok(())
73}6fn main() -> Result<()> {
7 // Enable verbose log to see debug information
8 Utils::set_log_level(LogLevel::Verbose);
9
10 // Set error callback to receive error notifications
11 Provider::set_error_callback(|error_code, description| {
12 eprintln!(
13 "Camera Error - Code: {}, Description: {}",
14 error_code, description
15 );
16 });
17
18 let temp_provider = Provider::new()?;
19 let devices = temp_provider.list_devices()?;
20 if devices.is_empty() {
21 eprintln!("No camera devices found!");
22 return Ok(());
23 }
24
25 for (i, device) in devices.iter().enumerate() {
26 println!("## Found video capture device: {}: {}", i, device);
27 }
28
29 // Select camera device (automatically use first device for testing)
30 let device_index = 0;
31
32 // Create provider with selected device
33 let mut provider = Provider::with_device(device_index)?;
34
35 // Set camera properties
36 let requested_width = 1920;
37 let requested_height = 1080;
38 let requested_fps = 60.0;
39
40 provider.set_property(PropertyName::Width, requested_width as f64)?;
41 provider.set_property(PropertyName::Height, requested_height as f64)?;
42 provider.set_property(
43 PropertyName::PixelFormatOutput,
44 PixelFormat::Bgra32 as u32 as f64,
45 )?;
46 provider.set_property(PropertyName::FrameRate, requested_fps)?;
47
48 // Open and start camera
49 provider.open()?;
50 provider.start()?;
51
52 if !provider.is_started() {
53 eprintln!("Failed to start camera!");
54 return Ok(());
55 }
56
57 // Get real camera properties
58 let real_width = provider.get_property(PropertyName::Width)? as i32;
59 let real_height = provider.get_property(PropertyName::Height)? as i32;
60 let real_fps = provider.get_property(PropertyName::FrameRate)?;
61
62 println!("Camera started successfully, requested resolution: {}x{}, real resolution: {}x{}, requested fps {}, real fps: {}",
63 requested_width, requested_height, real_width, real_height, requested_fps, real_fps);
64
65 // Create directory for captures (using std::fs)
66 std::fs::create_dir_all("./image_capture")
67 .map_err(|e| ccap::CcapError::FileOperationFailed(e.to_string()))?;
68
69 // Statistics tracking
70 let frame_count = Arc::new(Mutex::new(0u32));
71 let frame_count_clone = frame_count.clone();
72
73 // Set frame callback
74 provider.set_new_frame_callback(move |frame| {
75 let mut count = frame_count_clone.lock().unwrap();
76 *count += 1;
77
78 println!(
79 "VideoFrame {} grabbed: width = {}, height = {}, bytes: {}",
80 frame.index(),
81 frame.width(),
82 frame.height(),
83 frame.data_size()
84 );
85
86 // Try to save frame to directory
87 if let Ok(filename) = Utils::dump_frame_to_directory(frame, "./image_capture") {
88 println!("VideoFrame saved to: {}", filename);
89 } else {
90 eprintln!("Failed to save frame!");
91 }
92
93 true // no need to retain the frame
94 })?;
95
96 // Wait for 5 seconds to capture frames
97 println!("Capturing frames for 5 seconds...");
98 thread::sleep(Duration::from_secs(5));
99
100 // Get final count
101 let final_count = *frame_count.lock().unwrap();
102 println!("Captured {} frames, stopping...", final_count);
103
104 // Remove callback before dropping
105 let _ = provider.remove_new_frame_callback();
106
107 Ok(())
108}Sourcepub fn with_device(device_index: i32) -> Result<Self>
pub fn with_device(device_index: i32) -> Result<Self>
Create a provider with a specific device index
Examples found in repository?
3fn main() -> Result<()> {
4 // Set error callback to receive error notifications
5 Provider::set_error_callback(|error_code, description| {
6 eprintln!(
7 "Error occurred - Code: {}, Description: {}",
8 error_code, description
9 );
10 });
11
12 let temp_provider = Provider::new()?;
13 let devices = temp_provider.list_devices()?;
14 let camera_index = Utils::select_camera(&devices)?;
15
16 // Use device index instead of name to avoid issues
17 let camera_index_i32 = i32::try_from(camera_index).map_err(|_| {
18 CcapError::InvalidParameter(format!(
19 "camera index {} does not fit into i32",
20 camera_index
21 ))
22 })?;
23 let mut provider = Provider::with_device(camera_index_i32)?;
24 provider.open()?;
25 provider.start()?;
26
27 if !provider.is_started() {
28 eprintln!("Failed to start camera!");
29 return Ok(());
30 }
31
32 println!("Camera started successfully.");
33
34 // Capture frames
35 for i in 0..10 {
36 match provider.grab_frame(3000) {
37 Ok(Some(frame)) => {
38 println!(
39 "VideoFrame {} grabbed: width = {}, height = {}, bytes: {}, format: {:?}",
40 frame.index(),
41 frame.width(),
42 frame.height(),
43 frame.data_size(),
44 frame.pixel_format()
45 );
46 }
47 Ok(None) => {
48 eprintln!("Failed to grab frame {}!", i);
49 return Ok(());
50 }
51 Err(e) => {
52 eprintln!("Error grabbing frame {}: {}", i, e);
53 return Ok(());
54 }
55 }
56 }
57
58 println!("Captured 10 frames, stopping...");
59 let _ = provider.stop();
60 Ok(())
61}More examples
6fn main() -> Result<()> {
7 // Enable verbose log to see debug information
8 Utils::set_log_level(LogLevel::Verbose);
9
10 // Set error callback to receive error notifications
11 Provider::set_error_callback(|error_code, description| {
12 eprintln!(
13 "Camera Error - Code: {}, Description: {}",
14 error_code, description
15 );
16 });
17
18 let temp_provider = Provider::new()?;
19 let devices = temp_provider.list_devices()?;
20 if devices.is_empty() {
21 eprintln!("No camera devices found!");
22 return Ok(());
23 }
24
25 for (i, device) in devices.iter().enumerate() {
26 println!("## Found video capture device: {}: {}", i, device);
27 }
28
29 // Select camera device (automatically use first device for testing)
30 let device_index = 0;
31
32 // Create provider with selected device
33 let mut provider = Provider::with_device(device_index)?;
34
35 // Set camera properties
36 let requested_width = 1920;
37 let requested_height = 1080;
38 let requested_fps = 60.0;
39
40 provider.set_property(PropertyName::Width, requested_width as f64)?;
41 provider.set_property(PropertyName::Height, requested_height as f64)?;
42 provider.set_property(
43 PropertyName::PixelFormatOutput,
44 PixelFormat::Bgra32 as u32 as f64,
45 )?;
46 provider.set_property(PropertyName::FrameRate, requested_fps)?;
47
48 // Open and start camera
49 provider.open()?;
50 provider.start()?;
51
52 if !provider.is_started() {
53 eprintln!("Failed to start camera!");
54 return Ok(());
55 }
56
57 // Get real camera properties
58 let real_width = provider.get_property(PropertyName::Width)? as i32;
59 let real_height = provider.get_property(PropertyName::Height)? as i32;
60 let real_fps = provider.get_property(PropertyName::FrameRate)?;
61
62 println!("Camera started successfully, requested resolution: {}x{}, real resolution: {}x{}, requested fps {}, real fps: {}",
63 requested_width, requested_height, real_width, real_height, requested_fps, real_fps);
64
65 // Create directory for captures (using std::fs)
66 std::fs::create_dir_all("./image_capture")
67 .map_err(|e| ccap::CcapError::FileOperationFailed(e.to_string()))?;
68
69 // Statistics tracking
70 let frame_count = Arc::new(Mutex::new(0u32));
71 let frame_count_clone = frame_count.clone();
72
73 // Set frame callback
74 provider.set_new_frame_callback(move |frame| {
75 let mut count = frame_count_clone.lock().unwrap();
76 *count += 1;
77
78 println!(
79 "VideoFrame {} grabbed: width = {}, height = {}, bytes: {}",
80 frame.index(),
81 frame.width(),
82 frame.height(),
83 frame.data_size()
84 );
85
86 // Try to save frame to directory
87 if let Ok(filename) = Utils::dump_frame_to_directory(frame, "./image_capture") {
88 println!("VideoFrame saved to: {}", filename);
89 } else {
90 eprintln!("Failed to save frame!");
91 }
92
93 true // no need to retain the frame
94 })?;
95
96 // Wait for 5 seconds to capture frames
97 println!("Capturing frames for 5 seconds...");
98 thread::sleep(Duration::from_secs(5));
99
100 // Get final count
101 let final_count = *frame_count.lock().unwrap();
102 println!("Captured {} frames, stopping...", final_count);
103
104 // Remove callback before dropping
105 let _ = provider.remove_new_frame_callback();
106
107 Ok(())
108}Sourcepub fn with_device_name<S: AsRef<str>>(device_name: S) -> Result<Self>
pub fn with_device_name<S: AsRef<str>>(device_name: S) -> Result<Self>
Create a provider with a specific device name
Examples found in repository?
20fn print_camera_info(device_name: &str) -> Result<()> {
21 Utils::set_log_level(LogLevel::Verbose);
22
23 // Create provider with specific device name
24 let provider = match Provider::with_device_name(device_name) {
25 Ok(p) => p,
26 Err(e) => {
27 eprintln!(
28 "### Failed to create provider for device: {}, error: {}",
29 device_name, e
30 );
31 return Ok(());
32 }
33 };
34
35 match provider.device_info() {
36 Ok(device_info) => {
37 println!("===== Info for device: {} =======", device_name);
38
39 println!(" Supported resolutions:");
40 for resolution in &device_info.supported_resolutions {
41 println!(" {}x{}", resolution.width, resolution.height);
42 }
43
44 println!(" Supported pixel formats:");
45 for format in &device_info.supported_pixel_formats {
46 println!(" {}", format.as_str());
47 }
48
49 println!("===== Info end =======\n");
50 }
51 Err(e) => {
52 eprintln!(
53 "Failed to get device info for: {}, error: {}",
54 device_name, e
55 );
56 }
57 }
58
59 Ok(())
60}Sourcepub fn get_devices() -> Result<Vec<DeviceInfo>>
pub fn get_devices() -> Result<Vec<DeviceInfo>>
Get available camera devices
Sourcepub fn open(&mut self) -> Result<()>
pub fn open(&mut self) -> Result<()>
Open the camera device
Examples found in repository?
3fn main() -> Result<()> {
4 // Set error callback to receive error notifications
5 Provider::set_error_callback(|error_code, description| {
6 eprintln!(
7 "Error occurred - Code: {}, Description: {}",
8 error_code, description
9 );
10 });
11
12 let temp_provider = Provider::new()?;
13 let devices = temp_provider.list_devices()?;
14 let camera_index = Utils::select_camera(&devices)?;
15
16 // Use device index instead of name to avoid issues
17 let camera_index_i32 = i32::try_from(camera_index).map_err(|_| {
18 CcapError::InvalidParameter(format!(
19 "camera index {} does not fit into i32",
20 camera_index
21 ))
22 })?;
23 let mut provider = Provider::with_device(camera_index_i32)?;
24 provider.open()?;
25 provider.start()?;
26
27 if !provider.is_started() {
28 eprintln!("Failed to start camera!");
29 return Ok(());
30 }
31
32 println!("Camera started successfully.");
33
34 // Capture frames
35 for i in 0..10 {
36 match provider.grab_frame(3000) {
37 Ok(Some(frame)) => {
38 println!(
39 "VideoFrame {} grabbed: width = {}, height = {}, bytes: {}, format: {:?}",
40 frame.index(),
41 frame.width(),
42 frame.height(),
43 frame.data_size(),
44 frame.pixel_format()
45 );
46 }
47 Ok(None) => {
48 eprintln!("Failed to grab frame {}!", i);
49 return Ok(());
50 }
51 Err(e) => {
52 eprintln!("Error grabbing frame {}: {}", i, e);
53 return Ok(());
54 }
55 }
56 }
57
58 println!("Captured 10 frames, stopping...");
59 let _ = provider.stop();
60 Ok(())
61}More examples
4fn main() -> Result<()> {
5 // Enable verbose log to see debug information
6 Utils::set_log_level(LogLevel::Verbose);
7
8 // Set error callback to receive error notifications
9 Provider::set_error_callback(|error_code, description| {
10 eprintln!(
11 "Camera Error - Code: {}, Description: {}",
12 error_code, description
13 );
14 });
15
16 // Create a camera provider
17 let mut provider = Provider::new()?;
18
19 // Open default device
20 provider.open()?;
21 provider.start_capture()?;
22
23 if !provider.is_started() {
24 eprintln!("Failed to start camera!");
25 return Ok(());
26 }
27
28 // Print the real resolution and fps after camera started
29 let real_width = provider.get_property(PropertyName::Width)? as u32;
30 let real_height = provider.get_property(PropertyName::Height)? as u32;
31 let real_fps = provider.get_property(PropertyName::FrameRate)?;
32
33 println!(
34 "Camera started successfully, real resolution: {}x{}, real fps: {}",
35 real_width, real_height, real_fps
36 );
37
38 // Create capture directory
39 let capture_dir = "./image_capture";
40 if !std::path::Path::new(capture_dir).exists() {
41 fs::create_dir_all(capture_dir).map_err(|e| {
42 ccap::CcapError::InvalidParameter(format!("Failed to create directory: {}", e))
43 })?;
44 }
45
46 // Capture frames (3000 ms timeout when grabbing frames)
47 let mut frame_count = 0;
48 while let Some(frame) = provider.grab_frame(3000)? {
49 let frame_info = frame.info()?;
50 println!(
51 "VideoFrame {} grabbed: width = {}, height = {}, bytes: {}",
52 frame_info.frame_index, frame_info.width, frame_info.height, frame_info.size_in_bytes
53 );
54
55 // Save frame to directory
56 match Utils::dump_frame_to_directory(&frame, capture_dir) {
57 Ok(dump_file) => {
58 println!("VideoFrame saved to: {}", dump_file);
59 }
60 Err(e) => {
61 eprintln!("Failed to save frame: {}", e);
62 }
63 }
64
65 frame_count += 1;
66 if frame_count >= 10 {
67 println!("Captured 10 frames, stopping...");
68 break;
69 }
70 }
71
72 Ok(())
73}6fn main() -> Result<()> {
7 // Enable verbose log to see debug information
8 Utils::set_log_level(LogLevel::Verbose);
9
10 // Set error callback to receive error notifications
11 Provider::set_error_callback(|error_code, description| {
12 eprintln!(
13 "Camera Error - Code: {}, Description: {}",
14 error_code, description
15 );
16 });
17
18 let temp_provider = Provider::new()?;
19 let devices = temp_provider.list_devices()?;
20 if devices.is_empty() {
21 eprintln!("No camera devices found!");
22 return Ok(());
23 }
24
25 for (i, device) in devices.iter().enumerate() {
26 println!("## Found video capture device: {}: {}", i, device);
27 }
28
29 // Select camera device (automatically use first device for testing)
30 let device_index = 0;
31
32 // Create provider with selected device
33 let mut provider = Provider::with_device(device_index)?;
34
35 // Set camera properties
36 let requested_width = 1920;
37 let requested_height = 1080;
38 let requested_fps = 60.0;
39
40 provider.set_property(PropertyName::Width, requested_width as f64)?;
41 provider.set_property(PropertyName::Height, requested_height as f64)?;
42 provider.set_property(
43 PropertyName::PixelFormatOutput,
44 PixelFormat::Bgra32 as u32 as f64,
45 )?;
46 provider.set_property(PropertyName::FrameRate, requested_fps)?;
47
48 // Open and start camera
49 provider.open()?;
50 provider.start()?;
51
52 if !provider.is_started() {
53 eprintln!("Failed to start camera!");
54 return Ok(());
55 }
56
57 // Get real camera properties
58 let real_width = provider.get_property(PropertyName::Width)? as i32;
59 let real_height = provider.get_property(PropertyName::Height)? as i32;
60 let real_fps = provider.get_property(PropertyName::FrameRate)?;
61
62 println!("Camera started successfully, requested resolution: {}x{}, real resolution: {}x{}, requested fps {}, real fps: {}",
63 requested_width, requested_height, real_width, real_height, requested_fps, real_fps);
64
65 // Create directory for captures (using std::fs)
66 std::fs::create_dir_all("./image_capture")
67 .map_err(|e| ccap::CcapError::FileOperationFailed(e.to_string()))?;
68
69 // Statistics tracking
70 let frame_count = Arc::new(Mutex::new(0u32));
71 let frame_count_clone = frame_count.clone();
72
73 // Set frame callback
74 provider.set_new_frame_callback(move |frame| {
75 let mut count = frame_count_clone.lock().unwrap();
76 *count += 1;
77
78 println!(
79 "VideoFrame {} grabbed: width = {}, height = {}, bytes: {}",
80 frame.index(),
81 frame.width(),
82 frame.height(),
83 frame.data_size()
84 );
85
86 // Try to save frame to directory
87 if let Ok(filename) = Utils::dump_frame_to_directory(frame, "./image_capture") {
88 println!("VideoFrame saved to: {}", filename);
89 } else {
90 eprintln!("Failed to save frame!");
91 }
92
93 true // no need to retain the frame
94 })?;
95
96 // Wait for 5 seconds to capture frames
97 println!("Capturing frames for 5 seconds...");
98 thread::sleep(Duration::from_secs(5));
99
100 // Get final count
101 let final_count = *frame_count.lock().unwrap();
102 println!("Captured {} frames, stopping...", final_count);
103
104 // Remove callback before dropping
105 let _ = provider.remove_new_frame_callback();
106
107 Ok(())
108}Sourcepub fn open_device(
&mut self,
device_name: Option<&str>,
auto_start: bool,
) -> Result<()>
pub fn open_device( &mut self, device_name: Option<&str>, auto_start: bool, ) -> Result<()>
Open device with optional device name and auto start
Sourcepub fn device_info(&self) -> Result<DeviceInfo>
pub fn device_info(&self) -> Result<DeviceInfo>
Get device info for the current provider
Examples found in repository?
20fn print_camera_info(device_name: &str) -> Result<()> {
21 Utils::set_log_level(LogLevel::Verbose);
22
23 // Create provider with specific device name
24 let provider = match Provider::with_device_name(device_name) {
25 Ok(p) => p,
26 Err(e) => {
27 eprintln!(
28 "### Failed to create provider for device: {}, error: {}",
29 device_name, e
30 );
31 return Ok(());
32 }
33 };
34
35 match provider.device_info() {
36 Ok(device_info) => {
37 println!("===== Info for device: {} =======", device_name);
38
39 println!(" Supported resolutions:");
40 for resolution in &device_info.supported_resolutions {
41 println!(" {}x{}", resolution.width, resolution.height);
42 }
43
44 println!(" Supported pixel formats:");
45 for format in &device_info.supported_pixel_formats {
46 println!(" {}", format.as_str());
47 }
48
49 println!("===== Info end =======\n");
50 }
51 Err(e) => {
52 eprintln!(
53 "Failed to get device info for: {}, error: {}",
54 device_name, e
55 );
56 }
57 }
58
59 Ok(())
60}Sourcepub fn is_started(&self) -> bool
pub fn is_started(&self) -> bool
Check if capture is started
Examples found in repository?
3fn main() -> Result<()> {
4 // Set error callback to receive error notifications
5 Provider::set_error_callback(|error_code, description| {
6 eprintln!(
7 "Error occurred - Code: {}, Description: {}",
8 error_code, description
9 );
10 });
11
12 let temp_provider = Provider::new()?;
13 let devices = temp_provider.list_devices()?;
14 let camera_index = Utils::select_camera(&devices)?;
15
16 // Use device index instead of name to avoid issues
17 let camera_index_i32 = i32::try_from(camera_index).map_err(|_| {
18 CcapError::InvalidParameter(format!(
19 "camera index {} does not fit into i32",
20 camera_index
21 ))
22 })?;
23 let mut provider = Provider::with_device(camera_index_i32)?;
24 provider.open()?;
25 provider.start()?;
26
27 if !provider.is_started() {
28 eprintln!("Failed to start camera!");
29 return Ok(());
30 }
31
32 println!("Camera started successfully.");
33
34 // Capture frames
35 for i in 0..10 {
36 match provider.grab_frame(3000) {
37 Ok(Some(frame)) => {
38 println!(
39 "VideoFrame {} grabbed: width = {}, height = {}, bytes: {}, format: {:?}",
40 frame.index(),
41 frame.width(),
42 frame.height(),
43 frame.data_size(),
44 frame.pixel_format()
45 );
46 }
47 Ok(None) => {
48 eprintln!("Failed to grab frame {}!", i);
49 return Ok(());
50 }
51 Err(e) => {
52 eprintln!("Error grabbing frame {}: {}", i, e);
53 return Ok(());
54 }
55 }
56 }
57
58 println!("Captured 10 frames, stopping...");
59 let _ = provider.stop();
60 Ok(())
61}More examples
4fn main() -> Result<()> {
5 // Enable verbose log to see debug information
6 Utils::set_log_level(LogLevel::Verbose);
7
8 // Set error callback to receive error notifications
9 Provider::set_error_callback(|error_code, description| {
10 eprintln!(
11 "Camera Error - Code: {}, Description: {}",
12 error_code, description
13 );
14 });
15
16 // Create a camera provider
17 let mut provider = Provider::new()?;
18
19 // Open default device
20 provider.open()?;
21 provider.start_capture()?;
22
23 if !provider.is_started() {
24 eprintln!("Failed to start camera!");
25 return Ok(());
26 }
27
28 // Print the real resolution and fps after camera started
29 let real_width = provider.get_property(PropertyName::Width)? as u32;
30 let real_height = provider.get_property(PropertyName::Height)? as u32;
31 let real_fps = provider.get_property(PropertyName::FrameRate)?;
32
33 println!(
34 "Camera started successfully, real resolution: {}x{}, real fps: {}",
35 real_width, real_height, real_fps
36 );
37
38 // Create capture directory
39 let capture_dir = "./image_capture";
40 if !std::path::Path::new(capture_dir).exists() {
41 fs::create_dir_all(capture_dir).map_err(|e| {
42 ccap::CcapError::InvalidParameter(format!("Failed to create directory: {}", e))
43 })?;
44 }
45
46 // Capture frames (3000 ms timeout when grabbing frames)
47 let mut frame_count = 0;
48 while let Some(frame) = provider.grab_frame(3000)? {
49 let frame_info = frame.info()?;
50 println!(
51 "VideoFrame {} grabbed: width = {}, height = {}, bytes: {}",
52 frame_info.frame_index, frame_info.width, frame_info.height, frame_info.size_in_bytes
53 );
54
55 // Save frame to directory
56 match Utils::dump_frame_to_directory(&frame, capture_dir) {
57 Ok(dump_file) => {
58 println!("VideoFrame saved to: {}", dump_file);
59 }
60 Err(e) => {
61 eprintln!("Failed to save frame: {}", e);
62 }
63 }
64
65 frame_count += 1;
66 if frame_count >= 10 {
67 println!("Captured 10 frames, stopping...");
68 break;
69 }
70 }
71
72 Ok(())
73}6fn main() -> Result<()> {
7 // Enable verbose log to see debug information
8 Utils::set_log_level(LogLevel::Verbose);
9
10 // Set error callback to receive error notifications
11 Provider::set_error_callback(|error_code, description| {
12 eprintln!(
13 "Camera Error - Code: {}, Description: {}",
14 error_code, description
15 );
16 });
17
18 let temp_provider = Provider::new()?;
19 let devices = temp_provider.list_devices()?;
20 if devices.is_empty() {
21 eprintln!("No camera devices found!");
22 return Ok(());
23 }
24
25 for (i, device) in devices.iter().enumerate() {
26 println!("## Found video capture device: {}: {}", i, device);
27 }
28
29 // Select camera device (automatically use first device for testing)
30 let device_index = 0;
31
32 // Create provider with selected device
33 let mut provider = Provider::with_device(device_index)?;
34
35 // Set camera properties
36 let requested_width = 1920;
37 let requested_height = 1080;
38 let requested_fps = 60.0;
39
40 provider.set_property(PropertyName::Width, requested_width as f64)?;
41 provider.set_property(PropertyName::Height, requested_height as f64)?;
42 provider.set_property(
43 PropertyName::PixelFormatOutput,
44 PixelFormat::Bgra32 as u32 as f64,
45 )?;
46 provider.set_property(PropertyName::FrameRate, requested_fps)?;
47
48 // Open and start camera
49 provider.open()?;
50 provider.start()?;
51
52 if !provider.is_started() {
53 eprintln!("Failed to start camera!");
54 return Ok(());
55 }
56
57 // Get real camera properties
58 let real_width = provider.get_property(PropertyName::Width)? as i32;
59 let real_height = provider.get_property(PropertyName::Height)? as i32;
60 let real_fps = provider.get_property(PropertyName::FrameRate)?;
61
62 println!("Camera started successfully, requested resolution: {}x{}, real resolution: {}x{}, requested fps {}, real fps: {}",
63 requested_width, requested_height, real_width, real_height, requested_fps, real_fps);
64
65 // Create directory for captures (using std::fs)
66 std::fs::create_dir_all("./image_capture")
67 .map_err(|e| ccap::CcapError::FileOperationFailed(e.to_string()))?;
68
69 // Statistics tracking
70 let frame_count = Arc::new(Mutex::new(0u32));
71 let frame_count_clone = frame_count.clone();
72
73 // Set frame callback
74 provider.set_new_frame_callback(move |frame| {
75 let mut count = frame_count_clone.lock().unwrap();
76 *count += 1;
77
78 println!(
79 "VideoFrame {} grabbed: width = {}, height = {}, bytes: {}",
80 frame.index(),
81 frame.width(),
82 frame.height(),
83 frame.data_size()
84 );
85
86 // Try to save frame to directory
87 if let Ok(filename) = Utils::dump_frame_to_directory(frame, "./image_capture") {
88 println!("VideoFrame saved to: {}", filename);
89 } else {
90 eprintln!("Failed to save frame!");
91 }
92
93 true // no need to retain the frame
94 })?;
95
96 // Wait for 5 seconds to capture frames
97 println!("Capturing frames for 5 seconds...");
98 thread::sleep(Duration::from_secs(5));
99
100 // Get final count
101 let final_count = *frame_count.lock().unwrap();
102 println!("Captured {} frames, stopping...", final_count);
103
104 // Remove callback before dropping
105 let _ = provider.remove_new_frame_callback();
106
107 Ok(())
108}Sourcepub fn start(&mut self) -> Result<()>
pub fn start(&mut self) -> Result<()>
Start capture (alias for start_capture)
Examples found in repository?
3fn main() -> Result<()> {
4 // Set error callback to receive error notifications
5 Provider::set_error_callback(|error_code, description| {
6 eprintln!(
7 "Error occurred - Code: {}, Description: {}",
8 error_code, description
9 );
10 });
11
12 let temp_provider = Provider::new()?;
13 let devices = temp_provider.list_devices()?;
14 let camera_index = Utils::select_camera(&devices)?;
15
16 // Use device index instead of name to avoid issues
17 let camera_index_i32 = i32::try_from(camera_index).map_err(|_| {
18 CcapError::InvalidParameter(format!(
19 "camera index {} does not fit into i32",
20 camera_index
21 ))
22 })?;
23 let mut provider = Provider::with_device(camera_index_i32)?;
24 provider.open()?;
25 provider.start()?;
26
27 if !provider.is_started() {
28 eprintln!("Failed to start camera!");
29 return Ok(());
30 }
31
32 println!("Camera started successfully.");
33
34 // Capture frames
35 for i in 0..10 {
36 match provider.grab_frame(3000) {
37 Ok(Some(frame)) => {
38 println!(
39 "VideoFrame {} grabbed: width = {}, height = {}, bytes: {}, format: {:?}",
40 frame.index(),
41 frame.width(),
42 frame.height(),
43 frame.data_size(),
44 frame.pixel_format()
45 );
46 }
47 Ok(None) => {
48 eprintln!("Failed to grab frame {}!", i);
49 return Ok(());
50 }
51 Err(e) => {
52 eprintln!("Error grabbing frame {}: {}", i, e);
53 return Ok(());
54 }
55 }
56 }
57
58 println!("Captured 10 frames, stopping...");
59 let _ = provider.stop();
60 Ok(())
61}More examples
6fn main() -> Result<()> {
7 // Enable verbose log to see debug information
8 Utils::set_log_level(LogLevel::Verbose);
9
10 // Set error callback to receive error notifications
11 Provider::set_error_callback(|error_code, description| {
12 eprintln!(
13 "Camera Error - Code: {}, Description: {}",
14 error_code, description
15 );
16 });
17
18 let temp_provider = Provider::new()?;
19 let devices = temp_provider.list_devices()?;
20 if devices.is_empty() {
21 eprintln!("No camera devices found!");
22 return Ok(());
23 }
24
25 for (i, device) in devices.iter().enumerate() {
26 println!("## Found video capture device: {}: {}", i, device);
27 }
28
29 // Select camera device (automatically use first device for testing)
30 let device_index = 0;
31
32 // Create provider with selected device
33 let mut provider = Provider::with_device(device_index)?;
34
35 // Set camera properties
36 let requested_width = 1920;
37 let requested_height = 1080;
38 let requested_fps = 60.0;
39
40 provider.set_property(PropertyName::Width, requested_width as f64)?;
41 provider.set_property(PropertyName::Height, requested_height as f64)?;
42 provider.set_property(
43 PropertyName::PixelFormatOutput,
44 PixelFormat::Bgra32 as u32 as f64,
45 )?;
46 provider.set_property(PropertyName::FrameRate, requested_fps)?;
47
48 // Open and start camera
49 provider.open()?;
50 provider.start()?;
51
52 if !provider.is_started() {
53 eprintln!("Failed to start camera!");
54 return Ok(());
55 }
56
57 // Get real camera properties
58 let real_width = provider.get_property(PropertyName::Width)? as i32;
59 let real_height = provider.get_property(PropertyName::Height)? as i32;
60 let real_fps = provider.get_property(PropertyName::FrameRate)?;
61
62 println!("Camera started successfully, requested resolution: {}x{}, real resolution: {}x{}, requested fps {}, real fps: {}",
63 requested_width, requested_height, real_width, real_height, requested_fps, real_fps);
64
65 // Create directory for captures (using std::fs)
66 std::fs::create_dir_all("./image_capture")
67 .map_err(|e| ccap::CcapError::FileOperationFailed(e.to_string()))?;
68
69 // Statistics tracking
70 let frame_count = Arc::new(Mutex::new(0u32));
71 let frame_count_clone = frame_count.clone();
72
73 // Set frame callback
74 provider.set_new_frame_callback(move |frame| {
75 let mut count = frame_count_clone.lock().unwrap();
76 *count += 1;
77
78 println!(
79 "VideoFrame {} grabbed: width = {}, height = {}, bytes: {}",
80 frame.index(),
81 frame.width(),
82 frame.height(),
83 frame.data_size()
84 );
85
86 // Try to save frame to directory
87 if let Ok(filename) = Utils::dump_frame_to_directory(frame, "./image_capture") {
88 println!("VideoFrame saved to: {}", filename);
89 } else {
90 eprintln!("Failed to save frame!");
91 }
92
93 true // no need to retain the frame
94 })?;
95
96 // Wait for 5 seconds to capture frames
97 println!("Capturing frames for 5 seconds...");
98 thread::sleep(Duration::from_secs(5));
99
100 // Get final count
101 let final_count = *frame_count.lock().unwrap();
102 println!("Captured {} frames, stopping...", final_count);
103
104 // Remove callback before dropping
105 let _ = provider.remove_new_frame_callback();
106
107 Ok(())
108}Sourcepub fn stop(&mut self) -> Result<()>
pub fn stop(&mut self) -> Result<()>
Stop capture (alias for stop_capture)
Examples found in repository?
3fn main() -> Result<()> {
4 // Set error callback to receive error notifications
5 Provider::set_error_callback(|error_code, description| {
6 eprintln!(
7 "Error occurred - Code: {}, Description: {}",
8 error_code, description
9 );
10 });
11
12 let temp_provider = Provider::new()?;
13 let devices = temp_provider.list_devices()?;
14 let camera_index = Utils::select_camera(&devices)?;
15
16 // Use device index instead of name to avoid issues
17 let camera_index_i32 = i32::try_from(camera_index).map_err(|_| {
18 CcapError::InvalidParameter(format!(
19 "camera index {} does not fit into i32",
20 camera_index
21 ))
22 })?;
23 let mut provider = Provider::with_device(camera_index_i32)?;
24 provider.open()?;
25 provider.start()?;
26
27 if !provider.is_started() {
28 eprintln!("Failed to start camera!");
29 return Ok(());
30 }
31
32 println!("Camera started successfully.");
33
34 // Capture frames
35 for i in 0..10 {
36 match provider.grab_frame(3000) {
37 Ok(Some(frame)) => {
38 println!(
39 "VideoFrame {} grabbed: width = {}, height = {}, bytes: {}, format: {:?}",
40 frame.index(),
41 frame.width(),
42 frame.height(),
43 frame.data_size(),
44 frame.pixel_format()
45 );
46 }
47 Ok(None) => {
48 eprintln!("Failed to grab frame {}!", i);
49 return Ok(());
50 }
51 Err(e) => {
52 eprintln!("Error grabbing frame {}: {}", i, e);
53 return Ok(());
54 }
55 }
56 }
57
58 println!("Captured 10 frames, stopping...");
59 let _ = provider.stop();
60 Ok(())
61}Sourcepub fn set_property(&mut self, property: PropertyName, value: f64) -> Result<()>
pub fn set_property(&mut self, property: PropertyName, value: f64) -> Result<()>
Set camera property
Examples found in repository?
6fn main() -> Result<()> {
7 // Enable verbose log to see debug information
8 Utils::set_log_level(LogLevel::Verbose);
9
10 // Set error callback to receive error notifications
11 Provider::set_error_callback(|error_code, description| {
12 eprintln!(
13 "Camera Error - Code: {}, Description: {}",
14 error_code, description
15 );
16 });
17
18 let temp_provider = Provider::new()?;
19 let devices = temp_provider.list_devices()?;
20 if devices.is_empty() {
21 eprintln!("No camera devices found!");
22 return Ok(());
23 }
24
25 for (i, device) in devices.iter().enumerate() {
26 println!("## Found video capture device: {}: {}", i, device);
27 }
28
29 // Select camera device (automatically use first device for testing)
30 let device_index = 0;
31
32 // Create provider with selected device
33 let mut provider = Provider::with_device(device_index)?;
34
35 // Set camera properties
36 let requested_width = 1920;
37 let requested_height = 1080;
38 let requested_fps = 60.0;
39
40 provider.set_property(PropertyName::Width, requested_width as f64)?;
41 provider.set_property(PropertyName::Height, requested_height as f64)?;
42 provider.set_property(
43 PropertyName::PixelFormatOutput,
44 PixelFormat::Bgra32 as u32 as f64,
45 )?;
46 provider.set_property(PropertyName::FrameRate, requested_fps)?;
47
48 // Open and start camera
49 provider.open()?;
50 provider.start()?;
51
52 if !provider.is_started() {
53 eprintln!("Failed to start camera!");
54 return Ok(());
55 }
56
57 // Get real camera properties
58 let real_width = provider.get_property(PropertyName::Width)? as i32;
59 let real_height = provider.get_property(PropertyName::Height)? as i32;
60 let real_fps = provider.get_property(PropertyName::FrameRate)?;
61
62 println!("Camera started successfully, requested resolution: {}x{}, real resolution: {}x{}, requested fps {}, real fps: {}",
63 requested_width, requested_height, real_width, real_height, requested_fps, real_fps);
64
65 // Create directory for captures (using std::fs)
66 std::fs::create_dir_all("./image_capture")
67 .map_err(|e| ccap::CcapError::FileOperationFailed(e.to_string()))?;
68
69 // Statistics tracking
70 let frame_count = Arc::new(Mutex::new(0u32));
71 let frame_count_clone = frame_count.clone();
72
73 // Set frame callback
74 provider.set_new_frame_callback(move |frame| {
75 let mut count = frame_count_clone.lock().unwrap();
76 *count += 1;
77
78 println!(
79 "VideoFrame {} grabbed: width = {}, height = {}, bytes: {}",
80 frame.index(),
81 frame.width(),
82 frame.height(),
83 frame.data_size()
84 );
85
86 // Try to save frame to directory
87 if let Ok(filename) = Utils::dump_frame_to_directory(frame, "./image_capture") {
88 println!("VideoFrame saved to: {}", filename);
89 } else {
90 eprintln!("Failed to save frame!");
91 }
92
93 true // no need to retain the frame
94 })?;
95
96 // Wait for 5 seconds to capture frames
97 println!("Capturing frames for 5 seconds...");
98 thread::sleep(Duration::from_secs(5));
99
100 // Get final count
101 let final_count = *frame_count.lock().unwrap();
102 println!("Captured {} frames, stopping...", final_count);
103
104 // Remove callback before dropping
105 let _ = provider.remove_new_frame_callback();
106
107 Ok(())
108}Sourcepub fn get_property(&self, property: PropertyName) -> Result<f64>
pub fn get_property(&self, property: PropertyName) -> Result<f64>
Get camera property
Examples found in repository?
4fn main() -> Result<()> {
5 // Enable verbose log to see debug information
6 Utils::set_log_level(LogLevel::Verbose);
7
8 // Set error callback to receive error notifications
9 Provider::set_error_callback(|error_code, description| {
10 eprintln!(
11 "Camera Error - Code: {}, Description: {}",
12 error_code, description
13 );
14 });
15
16 // Create a camera provider
17 let mut provider = Provider::new()?;
18
19 // Open default device
20 provider.open()?;
21 provider.start_capture()?;
22
23 if !provider.is_started() {
24 eprintln!("Failed to start camera!");
25 return Ok(());
26 }
27
28 // Print the real resolution and fps after camera started
29 let real_width = provider.get_property(PropertyName::Width)? as u32;
30 let real_height = provider.get_property(PropertyName::Height)? as u32;
31 let real_fps = provider.get_property(PropertyName::FrameRate)?;
32
33 println!(
34 "Camera started successfully, real resolution: {}x{}, real fps: {}",
35 real_width, real_height, real_fps
36 );
37
38 // Create capture directory
39 let capture_dir = "./image_capture";
40 if !std::path::Path::new(capture_dir).exists() {
41 fs::create_dir_all(capture_dir).map_err(|e| {
42 ccap::CcapError::InvalidParameter(format!("Failed to create directory: {}", e))
43 })?;
44 }
45
46 // Capture frames (3000 ms timeout when grabbing frames)
47 let mut frame_count = 0;
48 while let Some(frame) = provider.grab_frame(3000)? {
49 let frame_info = frame.info()?;
50 println!(
51 "VideoFrame {} grabbed: width = {}, height = {}, bytes: {}",
52 frame_info.frame_index, frame_info.width, frame_info.height, frame_info.size_in_bytes
53 );
54
55 // Save frame to directory
56 match Utils::dump_frame_to_directory(&frame, capture_dir) {
57 Ok(dump_file) => {
58 println!("VideoFrame saved to: {}", dump_file);
59 }
60 Err(e) => {
61 eprintln!("Failed to save frame: {}", e);
62 }
63 }
64
65 frame_count += 1;
66 if frame_count >= 10 {
67 println!("Captured 10 frames, stopping...");
68 break;
69 }
70 }
71
72 Ok(())
73}More examples
6fn main() -> Result<()> {
7 // Enable verbose log to see debug information
8 Utils::set_log_level(LogLevel::Verbose);
9
10 // Set error callback to receive error notifications
11 Provider::set_error_callback(|error_code, description| {
12 eprintln!(
13 "Camera Error - Code: {}, Description: {}",
14 error_code, description
15 );
16 });
17
18 let temp_provider = Provider::new()?;
19 let devices = temp_provider.list_devices()?;
20 if devices.is_empty() {
21 eprintln!("No camera devices found!");
22 return Ok(());
23 }
24
25 for (i, device) in devices.iter().enumerate() {
26 println!("## Found video capture device: {}: {}", i, device);
27 }
28
29 // Select camera device (automatically use first device for testing)
30 let device_index = 0;
31
32 // Create provider with selected device
33 let mut provider = Provider::with_device(device_index)?;
34
35 // Set camera properties
36 let requested_width = 1920;
37 let requested_height = 1080;
38 let requested_fps = 60.0;
39
40 provider.set_property(PropertyName::Width, requested_width as f64)?;
41 provider.set_property(PropertyName::Height, requested_height as f64)?;
42 provider.set_property(
43 PropertyName::PixelFormatOutput,
44 PixelFormat::Bgra32 as u32 as f64,
45 )?;
46 provider.set_property(PropertyName::FrameRate, requested_fps)?;
47
48 // Open and start camera
49 provider.open()?;
50 provider.start()?;
51
52 if !provider.is_started() {
53 eprintln!("Failed to start camera!");
54 return Ok(());
55 }
56
57 // Get real camera properties
58 let real_width = provider.get_property(PropertyName::Width)? as i32;
59 let real_height = provider.get_property(PropertyName::Height)? as i32;
60 let real_fps = provider.get_property(PropertyName::FrameRate)?;
61
62 println!("Camera started successfully, requested resolution: {}x{}, real resolution: {}x{}, requested fps {}, real fps: {}",
63 requested_width, requested_height, real_width, real_height, requested_fps, real_fps);
64
65 // Create directory for captures (using std::fs)
66 std::fs::create_dir_all("./image_capture")
67 .map_err(|e| ccap::CcapError::FileOperationFailed(e.to_string()))?;
68
69 // Statistics tracking
70 let frame_count = Arc::new(Mutex::new(0u32));
71 let frame_count_clone = frame_count.clone();
72
73 // Set frame callback
74 provider.set_new_frame_callback(move |frame| {
75 let mut count = frame_count_clone.lock().unwrap();
76 *count += 1;
77
78 println!(
79 "VideoFrame {} grabbed: width = {}, height = {}, bytes: {}",
80 frame.index(),
81 frame.width(),
82 frame.height(),
83 frame.data_size()
84 );
85
86 // Try to save frame to directory
87 if let Ok(filename) = Utils::dump_frame_to_directory(frame, "./image_capture") {
88 println!("VideoFrame saved to: {}", filename);
89 } else {
90 eprintln!("Failed to save frame!");
91 }
92
93 true // no need to retain the frame
94 })?;
95
96 // Wait for 5 seconds to capture frames
97 println!("Capturing frames for 5 seconds...");
98 thread::sleep(Duration::from_secs(5));
99
100 // Get final count
101 let final_count = *frame_count.lock().unwrap();
102 println!("Captured {} frames, stopping...", final_count);
103
104 // Remove callback before dropping
105 let _ = provider.remove_new_frame_callback();
106
107 Ok(())
108}Sourcepub fn set_frame_rate(&mut self, fps: f64) -> Result<()>
pub fn set_frame_rate(&mut self, fps: f64) -> Result<()>
Set camera frame rate
Sourcepub fn set_pixel_format(&mut self, format: PixelFormat) -> Result<()>
pub fn set_pixel_format(&mut self, format: PixelFormat) -> Result<()>
Set pixel format
Sourcepub fn grab_frame(&mut self, timeout_ms: u32) -> Result<Option<VideoFrame>>
pub fn grab_frame(&mut self, timeout_ms: u32) -> Result<Option<VideoFrame>>
Grab a single frame with timeout
Examples found in repository?
3fn main() -> Result<()> {
4 // Set error callback to receive error notifications
5 Provider::set_error_callback(|error_code, description| {
6 eprintln!(
7 "Error occurred - Code: {}, Description: {}",
8 error_code, description
9 );
10 });
11
12 let temp_provider = Provider::new()?;
13 let devices = temp_provider.list_devices()?;
14 let camera_index = Utils::select_camera(&devices)?;
15
16 // Use device index instead of name to avoid issues
17 let camera_index_i32 = i32::try_from(camera_index).map_err(|_| {
18 CcapError::InvalidParameter(format!(
19 "camera index {} does not fit into i32",
20 camera_index
21 ))
22 })?;
23 let mut provider = Provider::with_device(camera_index_i32)?;
24 provider.open()?;
25 provider.start()?;
26
27 if !provider.is_started() {
28 eprintln!("Failed to start camera!");
29 return Ok(());
30 }
31
32 println!("Camera started successfully.");
33
34 // Capture frames
35 for i in 0..10 {
36 match provider.grab_frame(3000) {
37 Ok(Some(frame)) => {
38 println!(
39 "VideoFrame {} grabbed: width = {}, height = {}, bytes: {}, format: {:?}",
40 frame.index(),
41 frame.width(),
42 frame.height(),
43 frame.data_size(),
44 frame.pixel_format()
45 );
46 }
47 Ok(None) => {
48 eprintln!("Failed to grab frame {}!", i);
49 return Ok(());
50 }
51 Err(e) => {
52 eprintln!("Error grabbing frame {}: {}", i, e);
53 return Ok(());
54 }
55 }
56 }
57
58 println!("Captured 10 frames, stopping...");
59 let _ = provider.stop();
60 Ok(())
61}More examples
4fn main() -> Result<()> {
5 // Enable verbose log to see debug information
6 Utils::set_log_level(LogLevel::Verbose);
7
8 // Set error callback to receive error notifications
9 Provider::set_error_callback(|error_code, description| {
10 eprintln!(
11 "Camera Error - Code: {}, Description: {}",
12 error_code, description
13 );
14 });
15
16 // Create a camera provider
17 let mut provider = Provider::new()?;
18
19 // Open default device
20 provider.open()?;
21 provider.start_capture()?;
22
23 if !provider.is_started() {
24 eprintln!("Failed to start camera!");
25 return Ok(());
26 }
27
28 // Print the real resolution and fps after camera started
29 let real_width = provider.get_property(PropertyName::Width)? as u32;
30 let real_height = provider.get_property(PropertyName::Height)? as u32;
31 let real_fps = provider.get_property(PropertyName::FrameRate)?;
32
33 println!(
34 "Camera started successfully, real resolution: {}x{}, real fps: {}",
35 real_width, real_height, real_fps
36 );
37
38 // Create capture directory
39 let capture_dir = "./image_capture";
40 if !std::path::Path::new(capture_dir).exists() {
41 fs::create_dir_all(capture_dir).map_err(|e| {
42 ccap::CcapError::InvalidParameter(format!("Failed to create directory: {}", e))
43 })?;
44 }
45
46 // Capture frames (3000 ms timeout when grabbing frames)
47 let mut frame_count = 0;
48 while let Some(frame) = provider.grab_frame(3000)? {
49 let frame_info = frame.info()?;
50 println!(
51 "VideoFrame {} grabbed: width = {}, height = {}, bytes: {}",
52 frame_info.frame_index, frame_info.width, frame_info.height, frame_info.size_in_bytes
53 );
54
55 // Save frame to directory
56 match Utils::dump_frame_to_directory(&frame, capture_dir) {
57 Ok(dump_file) => {
58 println!("VideoFrame saved to: {}", dump_file);
59 }
60 Err(e) => {
61 eprintln!("Failed to save frame: {}", e);
62 }
63 }
64
65 frame_count += 1;
66 if frame_count >= 10 {
67 println!("Captured 10 frames, stopping...");
68 break;
69 }
70 }
71
72 Ok(())
73}Sourcepub fn start_capture(&mut self) -> Result<()>
pub fn start_capture(&mut self) -> Result<()>
Start continuous capture
Examples found in repository?
4fn main() -> Result<()> {
5 // Enable verbose log to see debug information
6 Utils::set_log_level(LogLevel::Verbose);
7
8 // Set error callback to receive error notifications
9 Provider::set_error_callback(|error_code, description| {
10 eprintln!(
11 "Camera Error - Code: {}, Description: {}",
12 error_code, description
13 );
14 });
15
16 // Create a camera provider
17 let mut provider = Provider::new()?;
18
19 // Open default device
20 provider.open()?;
21 provider.start_capture()?;
22
23 if !provider.is_started() {
24 eprintln!("Failed to start camera!");
25 return Ok(());
26 }
27
28 // Print the real resolution and fps after camera started
29 let real_width = provider.get_property(PropertyName::Width)? as u32;
30 let real_height = provider.get_property(PropertyName::Height)? as u32;
31 let real_fps = provider.get_property(PropertyName::FrameRate)?;
32
33 println!(
34 "Camera started successfully, real resolution: {}x{}, real fps: {}",
35 real_width, real_height, real_fps
36 );
37
38 // Create capture directory
39 let capture_dir = "./image_capture";
40 if !std::path::Path::new(capture_dir).exists() {
41 fs::create_dir_all(capture_dir).map_err(|e| {
42 ccap::CcapError::InvalidParameter(format!("Failed to create directory: {}", e))
43 })?;
44 }
45
46 // Capture frames (3000 ms timeout when grabbing frames)
47 let mut frame_count = 0;
48 while let Some(frame) = provider.grab_frame(3000)? {
49 let frame_info = frame.info()?;
50 println!(
51 "VideoFrame {} grabbed: width = {}, height = {}, bytes: {}",
52 frame_info.frame_index, frame_info.width, frame_info.height, frame_info.size_in_bytes
53 );
54
55 // Save frame to directory
56 match Utils::dump_frame_to_directory(&frame, capture_dir) {
57 Ok(dump_file) => {
58 println!("VideoFrame saved to: {}", dump_file);
59 }
60 Err(e) => {
61 eprintln!("Failed to save frame: {}", e);
62 }
63 }
64
65 frame_count += 1;
66 if frame_count >= 10 {
67 println!("Captured 10 frames, stopping...");
68 break;
69 }
70 }
71
72 Ok(())
73}Sourcepub fn stop_capture(&mut self) -> Result<()>
pub fn stop_capture(&mut self) -> Result<()>
Stop continuous capture
Sourcepub fn list_devices(&self) -> Result<Vec<String>>
pub fn list_devices(&self) -> Result<Vec<String>>
List device names (simple string list)
Examples found in repository?
3fn find_camera_names() -> Result<Vec<String>> {
4 // Create a temporary provider to query devices
5 let provider = Provider::new()?;
6 let devices = provider.list_devices()?;
7
8 if !devices.is_empty() {
9 println!("## Found {} video capture device:", devices.len());
10 for (index, name) in devices.iter().enumerate() {
11 println!(" {}: {}", index, name);
12 }
13 } else {
14 eprintln!("Failed to find any video capture device.");
15 }
16
17 Ok(devices)
18}More examples
3fn main() -> Result<()> {
4 // Set error callback to receive error notifications
5 Provider::set_error_callback(|error_code, description| {
6 eprintln!(
7 "Error occurred - Code: {}, Description: {}",
8 error_code, description
9 );
10 });
11
12 let temp_provider = Provider::new()?;
13 let devices = temp_provider.list_devices()?;
14 let camera_index = Utils::select_camera(&devices)?;
15
16 // Use device index instead of name to avoid issues
17 let camera_index_i32 = i32::try_from(camera_index).map_err(|_| {
18 CcapError::InvalidParameter(format!(
19 "camera index {} does not fit into i32",
20 camera_index
21 ))
22 })?;
23 let mut provider = Provider::with_device(camera_index_i32)?;
24 provider.open()?;
25 provider.start()?;
26
27 if !provider.is_started() {
28 eprintln!("Failed to start camera!");
29 return Ok(());
30 }
31
32 println!("Camera started successfully.");
33
34 // Capture frames
35 for i in 0..10 {
36 match provider.grab_frame(3000) {
37 Ok(Some(frame)) => {
38 println!(
39 "VideoFrame {} grabbed: width = {}, height = {}, bytes: {}, format: {:?}",
40 frame.index(),
41 frame.width(),
42 frame.height(),
43 frame.data_size(),
44 frame.pixel_format()
45 );
46 }
47 Ok(None) => {
48 eprintln!("Failed to grab frame {}!", i);
49 return Ok(());
50 }
51 Err(e) => {
52 eprintln!("Error grabbing frame {}: {}", i, e);
53 return Ok(());
54 }
55 }
56 }
57
58 println!("Captured 10 frames, stopping...");
59 let _ = provider.stop();
60 Ok(())
61}6fn main() -> Result<()> {
7 // Enable verbose log to see debug information
8 Utils::set_log_level(LogLevel::Verbose);
9
10 // Set error callback to receive error notifications
11 Provider::set_error_callback(|error_code, description| {
12 eprintln!(
13 "Camera Error - Code: {}, Description: {}",
14 error_code, description
15 );
16 });
17
18 let temp_provider = Provider::new()?;
19 let devices = temp_provider.list_devices()?;
20 if devices.is_empty() {
21 eprintln!("No camera devices found!");
22 return Ok(());
23 }
24
25 for (i, device) in devices.iter().enumerate() {
26 println!("## Found video capture device: {}: {}", i, device);
27 }
28
29 // Select camera device (automatically use first device for testing)
30 let device_index = 0;
31
32 // Create provider with selected device
33 let mut provider = Provider::with_device(device_index)?;
34
35 // Set camera properties
36 let requested_width = 1920;
37 let requested_height = 1080;
38 let requested_fps = 60.0;
39
40 provider.set_property(PropertyName::Width, requested_width as f64)?;
41 provider.set_property(PropertyName::Height, requested_height as f64)?;
42 provider.set_property(
43 PropertyName::PixelFormatOutput,
44 PixelFormat::Bgra32 as u32 as f64,
45 )?;
46 provider.set_property(PropertyName::FrameRate, requested_fps)?;
47
48 // Open and start camera
49 provider.open()?;
50 provider.start()?;
51
52 if !provider.is_started() {
53 eprintln!("Failed to start camera!");
54 return Ok(());
55 }
56
57 // Get real camera properties
58 let real_width = provider.get_property(PropertyName::Width)? as i32;
59 let real_height = provider.get_property(PropertyName::Height)? as i32;
60 let real_fps = provider.get_property(PropertyName::FrameRate)?;
61
62 println!("Camera started successfully, requested resolution: {}x{}, real resolution: {}x{}, requested fps {}, real fps: {}",
63 requested_width, requested_height, real_width, real_height, requested_fps, real_fps);
64
65 // Create directory for captures (using std::fs)
66 std::fs::create_dir_all("./image_capture")
67 .map_err(|e| ccap::CcapError::FileOperationFailed(e.to_string()))?;
68
69 // Statistics tracking
70 let frame_count = Arc::new(Mutex::new(0u32));
71 let frame_count_clone = frame_count.clone();
72
73 // Set frame callback
74 provider.set_new_frame_callback(move |frame| {
75 let mut count = frame_count_clone.lock().unwrap();
76 *count += 1;
77
78 println!(
79 "VideoFrame {} grabbed: width = {}, height = {}, bytes: {}",
80 frame.index(),
81 frame.width(),
82 frame.height(),
83 frame.data_size()
84 );
85
86 // Try to save frame to directory
87 if let Ok(filename) = Utils::dump_frame_to_directory(frame, "./image_capture") {
88 println!("VideoFrame saved to: {}", filename);
89 } else {
90 eprintln!("Failed to save frame!");
91 }
92
93 true // no need to retain the frame
94 })?;
95
96 // Wait for 5 seconds to capture frames
97 println!("Capturing frames for 5 seconds...");
98 thread::sleep(Duration::from_secs(5));
99
100 // Get final count
101 let final_count = *frame_count.lock().unwrap();
102 println!("Captured {} frames, stopping...", final_count);
103
104 // Remove callback before dropping
105 let _ = provider.remove_new_frame_callback();
106
107 Ok(())
108}Sourcepub fn find_device_names(&self) -> Result<Vec<String>>
pub fn find_device_names(&self) -> Result<Vec<String>>
Find device names (alias for list_devices)
Sourcepub fn resolution(&self) -> Result<(u32, u32)>
pub fn resolution(&self) -> Result<(u32, u32)>
Get current resolution (convenience getter)
Sourcepub fn pixel_format(&self) -> Result<PixelFormat>
pub fn pixel_format(&self) -> Result<PixelFormat>
Get current pixel format (convenience getter)
Sourcepub fn frame_rate(&self) -> Result<f64>
pub fn frame_rate(&self) -> Result<f64>
Get current frame rate (convenience getter)
Sourcepub fn set_error_callback<F>(callback: F)
pub fn set_error_callback<F>(callback: F)
Set error callback for camera errors
§Memory Safety
This is a global callback that persists until replaced or cleared. Calling this function multiple times will properly clean up the previous callback.
Important: this callback is process-global (shared by all Provider instances).
The last one set wins.
§Thread Safety
The callback will be invoked from the camera capture thread. Ensure your
callback is thread-safe (Send + Sync).
§Example
Provider::set_error_callback(|code, desc| {
eprintln!("Camera error {}: {}", code, desc);
});Examples found in repository?
62fn main() -> Result<()> {
63 // Set error callback to receive error notifications
64 Provider::set_error_callback(|error_code, description| {
65 eprintln!(
66 "Camera Error - Code: {}, Description: {}",
67 error_code, description
68 );
69 });
70
71 let device_names = find_camera_names()?;
72 if device_names.is_empty() {
73 return Ok(());
74 }
75
76 for name in &device_names {
77 if let Err(e) = print_camera_info(name) {
78 eprintln!("Error processing device {}: {}", name, e);
79 }
80 }
81
82 Ok(())
83}More examples
3fn main() -> Result<()> {
4 // Set error callback to receive error notifications
5 Provider::set_error_callback(|error_code, description| {
6 eprintln!(
7 "Error occurred - Code: {}, Description: {}",
8 error_code, description
9 );
10 });
11
12 let temp_provider = Provider::new()?;
13 let devices = temp_provider.list_devices()?;
14 let camera_index = Utils::select_camera(&devices)?;
15
16 // Use device index instead of name to avoid issues
17 let camera_index_i32 = i32::try_from(camera_index).map_err(|_| {
18 CcapError::InvalidParameter(format!(
19 "camera index {} does not fit into i32",
20 camera_index
21 ))
22 })?;
23 let mut provider = Provider::with_device(camera_index_i32)?;
24 provider.open()?;
25 provider.start()?;
26
27 if !provider.is_started() {
28 eprintln!("Failed to start camera!");
29 return Ok(());
30 }
31
32 println!("Camera started successfully.");
33
34 // Capture frames
35 for i in 0..10 {
36 match provider.grab_frame(3000) {
37 Ok(Some(frame)) => {
38 println!(
39 "VideoFrame {} grabbed: width = {}, height = {}, bytes: {}, format: {:?}",
40 frame.index(),
41 frame.width(),
42 frame.height(),
43 frame.data_size(),
44 frame.pixel_format()
45 );
46 }
47 Ok(None) => {
48 eprintln!("Failed to grab frame {}!", i);
49 return Ok(());
50 }
51 Err(e) => {
52 eprintln!("Error grabbing frame {}: {}", i, e);
53 return Ok(());
54 }
55 }
56 }
57
58 println!("Captured 10 frames, stopping...");
59 let _ = provider.stop();
60 Ok(())
61}4fn main() -> Result<()> {
5 // Enable verbose log to see debug information
6 Utils::set_log_level(LogLevel::Verbose);
7
8 // Set error callback to receive error notifications
9 Provider::set_error_callback(|error_code, description| {
10 eprintln!(
11 "Camera Error - Code: {}, Description: {}",
12 error_code, description
13 );
14 });
15
16 // Create a camera provider
17 let mut provider = Provider::new()?;
18
19 // Open default device
20 provider.open()?;
21 provider.start_capture()?;
22
23 if !provider.is_started() {
24 eprintln!("Failed to start camera!");
25 return Ok(());
26 }
27
28 // Print the real resolution and fps after camera started
29 let real_width = provider.get_property(PropertyName::Width)? as u32;
30 let real_height = provider.get_property(PropertyName::Height)? as u32;
31 let real_fps = provider.get_property(PropertyName::FrameRate)?;
32
33 println!(
34 "Camera started successfully, real resolution: {}x{}, real fps: {}",
35 real_width, real_height, real_fps
36 );
37
38 // Create capture directory
39 let capture_dir = "./image_capture";
40 if !std::path::Path::new(capture_dir).exists() {
41 fs::create_dir_all(capture_dir).map_err(|e| {
42 ccap::CcapError::InvalidParameter(format!("Failed to create directory: {}", e))
43 })?;
44 }
45
46 // Capture frames (3000 ms timeout when grabbing frames)
47 let mut frame_count = 0;
48 while let Some(frame) = provider.grab_frame(3000)? {
49 let frame_info = frame.info()?;
50 println!(
51 "VideoFrame {} grabbed: width = {}, height = {}, bytes: {}",
52 frame_info.frame_index, frame_info.width, frame_info.height, frame_info.size_in_bytes
53 );
54
55 // Save frame to directory
56 match Utils::dump_frame_to_directory(&frame, capture_dir) {
57 Ok(dump_file) => {
58 println!("VideoFrame saved to: {}", dump_file);
59 }
60 Err(e) => {
61 eprintln!("Failed to save frame: {}", e);
62 }
63 }
64
65 frame_count += 1;
66 if frame_count >= 10 {
67 println!("Captured 10 frames, stopping...");
68 break;
69 }
70 }
71
72 Ok(())
73}6fn main() -> Result<()> {
7 // Enable verbose log to see debug information
8 Utils::set_log_level(LogLevel::Verbose);
9
10 // Set error callback to receive error notifications
11 Provider::set_error_callback(|error_code, description| {
12 eprintln!(
13 "Camera Error - Code: {}, Description: {}",
14 error_code, description
15 );
16 });
17
18 let temp_provider = Provider::new()?;
19 let devices = temp_provider.list_devices()?;
20 if devices.is_empty() {
21 eprintln!("No camera devices found!");
22 return Ok(());
23 }
24
25 for (i, device) in devices.iter().enumerate() {
26 println!("## Found video capture device: {}: {}", i, device);
27 }
28
29 // Select camera device (automatically use first device for testing)
30 let device_index = 0;
31
32 // Create provider with selected device
33 let mut provider = Provider::with_device(device_index)?;
34
35 // Set camera properties
36 let requested_width = 1920;
37 let requested_height = 1080;
38 let requested_fps = 60.0;
39
40 provider.set_property(PropertyName::Width, requested_width as f64)?;
41 provider.set_property(PropertyName::Height, requested_height as f64)?;
42 provider.set_property(
43 PropertyName::PixelFormatOutput,
44 PixelFormat::Bgra32 as u32 as f64,
45 )?;
46 provider.set_property(PropertyName::FrameRate, requested_fps)?;
47
48 // Open and start camera
49 provider.open()?;
50 provider.start()?;
51
52 if !provider.is_started() {
53 eprintln!("Failed to start camera!");
54 return Ok(());
55 }
56
57 // Get real camera properties
58 let real_width = provider.get_property(PropertyName::Width)? as i32;
59 let real_height = provider.get_property(PropertyName::Height)? as i32;
60 let real_fps = provider.get_property(PropertyName::FrameRate)?;
61
62 println!("Camera started successfully, requested resolution: {}x{}, real resolution: {}x{}, requested fps {}, real fps: {}",
63 requested_width, requested_height, real_width, real_height, requested_fps, real_fps);
64
65 // Create directory for captures (using std::fs)
66 std::fs::create_dir_all("./image_capture")
67 .map_err(|e| ccap::CcapError::FileOperationFailed(e.to_string()))?;
68
69 // Statistics tracking
70 let frame_count = Arc::new(Mutex::new(0u32));
71 let frame_count_clone = frame_count.clone();
72
73 // Set frame callback
74 provider.set_new_frame_callback(move |frame| {
75 let mut count = frame_count_clone.lock().unwrap();
76 *count += 1;
77
78 println!(
79 "VideoFrame {} grabbed: width = {}, height = {}, bytes: {}",
80 frame.index(),
81 frame.width(),
82 frame.height(),
83 frame.data_size()
84 );
85
86 // Try to save frame to directory
87 if let Ok(filename) = Utils::dump_frame_to_directory(frame, "./image_capture") {
88 println!("VideoFrame saved to: {}", filename);
89 } else {
90 eprintln!("Failed to save frame!");
91 }
92
93 true // no need to retain the frame
94 })?;
95
96 // Wait for 5 seconds to capture frames
97 println!("Capturing frames for 5 seconds...");
98 thread::sleep(Duration::from_secs(5));
99
100 // Get final count
101 let final_count = *frame_count.lock().unwrap();
102 println!("Captured {} frames, stopping...", final_count);
103
104 // Remove callback before dropping
105 let _ = provider.remove_new_frame_callback();
106
107 Ok(())
108}Sourcepub fn set_global_error_callback<F>(callback: F)
pub fn set_global_error_callback<F>(callback: F)
Set the global error callback.
This is an alias for Provider::set_error_callback to make the global scope explicit.
Sourcepub fn clear_error_callback()
pub fn clear_error_callback()
Clear the global error callback
This removes the error callback and frees associated memory.
Sourcepub fn clear_global_error_callback()
pub fn clear_global_error_callback()
Clear the global error callback.
This is an alias for Provider::clear_error_callback to make the global scope explicit.
Sourcepub fn open_with_index(
&mut self,
device_index: i32,
auto_start: bool,
) -> Result<()>
pub fn open_with_index( &mut self, device_index: i32, auto_start: bool, ) -> Result<()>
Open device with index and auto start
Sourcepub fn set_new_frame_callback<F>(&mut self, callback: F) -> Result<()>
pub fn set_new_frame_callback<F>(&mut self, callback: F) -> Result<()>
Set a callback for new frame notifications
The callback receives a reference to the captured frame and returns true
to continue capturing or false to stop.
§Thread Safety
The callback will be invoked from the camera capture thread. Ensure your
callback is thread-safe (Send + Sync).
§Example
provider.set_new_frame_callback(|frame| {
println!("Got frame: {}x{}", frame.width(), frame.height());
true // continue capturing
})?;Examples found in repository?
6fn main() -> Result<()> {
7 // Enable verbose log to see debug information
8 Utils::set_log_level(LogLevel::Verbose);
9
10 // Set error callback to receive error notifications
11 Provider::set_error_callback(|error_code, description| {
12 eprintln!(
13 "Camera Error - Code: {}, Description: {}",
14 error_code, description
15 );
16 });
17
18 let temp_provider = Provider::new()?;
19 let devices = temp_provider.list_devices()?;
20 if devices.is_empty() {
21 eprintln!("No camera devices found!");
22 return Ok(());
23 }
24
25 for (i, device) in devices.iter().enumerate() {
26 println!("## Found video capture device: {}: {}", i, device);
27 }
28
29 // Select camera device (automatically use first device for testing)
30 let device_index = 0;
31
32 // Create provider with selected device
33 let mut provider = Provider::with_device(device_index)?;
34
35 // Set camera properties
36 let requested_width = 1920;
37 let requested_height = 1080;
38 let requested_fps = 60.0;
39
40 provider.set_property(PropertyName::Width, requested_width as f64)?;
41 provider.set_property(PropertyName::Height, requested_height as f64)?;
42 provider.set_property(
43 PropertyName::PixelFormatOutput,
44 PixelFormat::Bgra32 as u32 as f64,
45 )?;
46 provider.set_property(PropertyName::FrameRate, requested_fps)?;
47
48 // Open and start camera
49 provider.open()?;
50 provider.start()?;
51
52 if !provider.is_started() {
53 eprintln!("Failed to start camera!");
54 return Ok(());
55 }
56
57 // Get real camera properties
58 let real_width = provider.get_property(PropertyName::Width)? as i32;
59 let real_height = provider.get_property(PropertyName::Height)? as i32;
60 let real_fps = provider.get_property(PropertyName::FrameRate)?;
61
62 println!("Camera started successfully, requested resolution: {}x{}, real resolution: {}x{}, requested fps {}, real fps: {}",
63 requested_width, requested_height, real_width, real_height, requested_fps, real_fps);
64
65 // Create directory for captures (using std::fs)
66 std::fs::create_dir_all("./image_capture")
67 .map_err(|e| ccap::CcapError::FileOperationFailed(e.to_string()))?;
68
69 // Statistics tracking
70 let frame_count = Arc::new(Mutex::new(0u32));
71 let frame_count_clone = frame_count.clone();
72
73 // Set frame callback
74 provider.set_new_frame_callback(move |frame| {
75 let mut count = frame_count_clone.lock().unwrap();
76 *count += 1;
77
78 println!(
79 "VideoFrame {} grabbed: width = {}, height = {}, bytes: {}",
80 frame.index(),
81 frame.width(),
82 frame.height(),
83 frame.data_size()
84 );
85
86 // Try to save frame to directory
87 if let Ok(filename) = Utils::dump_frame_to_directory(frame, "./image_capture") {
88 println!("VideoFrame saved to: {}", filename);
89 } else {
90 eprintln!("Failed to save frame!");
91 }
92
93 true // no need to retain the frame
94 })?;
95
96 // Wait for 5 seconds to capture frames
97 println!("Capturing frames for 5 seconds...");
98 thread::sleep(Duration::from_secs(5));
99
100 // Get final count
101 let final_count = *frame_count.lock().unwrap();
102 println!("Captured {} frames, stopping...", final_count);
103
104 // Remove callback before dropping
105 let _ = provider.remove_new_frame_callback();
106
107 Ok(())
108}Sourcepub fn remove_new_frame_callback(&mut self) -> Result<()>
pub fn remove_new_frame_callback(&mut self) -> Result<()>
Remove frame callback
Examples found in repository?
6fn main() -> Result<()> {
7 // Enable verbose log to see debug information
8 Utils::set_log_level(LogLevel::Verbose);
9
10 // Set error callback to receive error notifications
11 Provider::set_error_callback(|error_code, description| {
12 eprintln!(
13 "Camera Error - Code: {}, Description: {}",
14 error_code, description
15 );
16 });
17
18 let temp_provider = Provider::new()?;
19 let devices = temp_provider.list_devices()?;
20 if devices.is_empty() {
21 eprintln!("No camera devices found!");
22 return Ok(());
23 }
24
25 for (i, device) in devices.iter().enumerate() {
26 println!("## Found video capture device: {}: {}", i, device);
27 }
28
29 // Select camera device (automatically use first device for testing)
30 let device_index = 0;
31
32 // Create provider with selected device
33 let mut provider = Provider::with_device(device_index)?;
34
35 // Set camera properties
36 let requested_width = 1920;
37 let requested_height = 1080;
38 let requested_fps = 60.0;
39
40 provider.set_property(PropertyName::Width, requested_width as f64)?;
41 provider.set_property(PropertyName::Height, requested_height as f64)?;
42 provider.set_property(
43 PropertyName::PixelFormatOutput,
44 PixelFormat::Bgra32 as u32 as f64,
45 )?;
46 provider.set_property(PropertyName::FrameRate, requested_fps)?;
47
48 // Open and start camera
49 provider.open()?;
50 provider.start()?;
51
52 if !provider.is_started() {
53 eprintln!("Failed to start camera!");
54 return Ok(());
55 }
56
57 // Get real camera properties
58 let real_width = provider.get_property(PropertyName::Width)? as i32;
59 let real_height = provider.get_property(PropertyName::Height)? as i32;
60 let real_fps = provider.get_property(PropertyName::FrameRate)?;
61
62 println!("Camera started successfully, requested resolution: {}x{}, real resolution: {}x{}, requested fps {}, real fps: {}",
63 requested_width, requested_height, real_width, real_height, requested_fps, real_fps);
64
65 // Create directory for captures (using std::fs)
66 std::fs::create_dir_all("./image_capture")
67 .map_err(|e| ccap::CcapError::FileOperationFailed(e.to_string()))?;
68
69 // Statistics tracking
70 let frame_count = Arc::new(Mutex::new(0u32));
71 let frame_count_clone = frame_count.clone();
72
73 // Set frame callback
74 provider.set_new_frame_callback(move |frame| {
75 let mut count = frame_count_clone.lock().unwrap();
76 *count += 1;
77
78 println!(
79 "VideoFrame {} grabbed: width = {}, height = {}, bytes: {}",
80 frame.index(),
81 frame.width(),
82 frame.height(),
83 frame.data_size()
84 );
85
86 // Try to save frame to directory
87 if let Ok(filename) = Utils::dump_frame_to_directory(frame, "./image_capture") {
88 println!("VideoFrame saved to: {}", filename);
89 } else {
90 eprintln!("Failed to save frame!");
91 }
92
93 true // no need to retain the frame
94 })?;
95
96 // Wait for 5 seconds to capture frames
97 println!("Capturing frames for 5 seconds...");
98 thread::sleep(Duration::from_secs(5));
99
100 // Get final count
101 let final_count = *frame_count.lock().unwrap();
102 println!("Captured {} frames, stopping...", final_count);
103
104 // Remove callback before dropping
105 let _ = provider.remove_new_frame_callback();
106
107 Ok(())
108}