minimal_example/
minimal_example.rs

1use ccap::{CcapError, Provider, Result, Utils};
2
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}