use ccap::{LogLevel, PropertyName, Provider, Result, Utils};
use std::fs;
fn main() -> Result<()> {
Utils::set_log_level(LogLevel::Verbose);
Provider::set_error_callback(|error_code, description| {
eprintln!(
"Camera Error - Code: {}, Description: {}",
error_code, description
);
});
let mut provider = Provider::new()?;
provider.open()?;
provider.start_capture()?;
if !provider.is_started() {
eprintln!("Failed to start camera!");
return Ok(());
}
let real_width = provider.get_property(PropertyName::Width)? as u32;
let real_height = provider.get_property(PropertyName::Height)? as u32;
let real_fps = provider.get_property(PropertyName::FrameRate)?;
println!(
"Camera started successfully, real resolution: {}x{}, real fps: {}",
real_width, real_height, real_fps
);
let capture_dir = "./image_capture";
if !std::path::Path::new(capture_dir).exists() {
fs::create_dir_all(capture_dir).map_err(|e| {
ccap::CcapError::InvalidParameter(format!("Failed to create directory: {}", e))
})?;
}
let mut frame_count = 0;
while let Some(frame) = provider.grab_frame(3000)? {
let frame_info = frame.info()?;
println!(
"VideoFrame {} grabbed: width = {}, height = {}, bytes: {}",
frame_info.frame_index, frame_info.width, frame_info.height, frame_info.size_in_bytes
);
match Utils::dump_frame_to_directory(&frame, capture_dir) {
Ok(dump_file) => {
println!("VideoFrame saved to: {}", dump_file);
}
Err(e) => {
eprintln!("Failed to save frame: {}", e);
}
}
frame_count += 1;
if frame_count >= 10 {
println!("Captured 10 frames, stopping...");
break;
}
}
Ok(())
}