use crabcamera::commands::{
capture::{capture_single_photo, release_camera, start_camera_preview, stop_camera_preview},
init::{get_available_cameras, initialize_camera_system},
};
use crabcamera::types::CameraFormat;
use tokio::time::{sleep, Duration};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();
println!("đĻ CrabCamera Preview Example");
println!("==============================");
println!("\nđˇ Initializing camera system...");
match initialize_camera_system().await {
Ok(message) => println!("â
{}", message),
Err(e) => {
eprintln!("â Failed to initialize camera system: {}", e);
return Err(e.into());
}
}
println!("\nđ Discovering available cameras...");
let cameras = match get_available_cameras().await {
Ok(cameras) => cameras,
Err(e) => {
eprintln!("â Failed to get cameras: {}", e);
return Err(e.into());
}
};
if cameras.is_empty() {
eprintln!("â No cameras found!");
return Ok(());
}
for (i, camera) in cameras.iter().enumerate() {
println!(" {}. {} ({})", i + 1, camera.name, camera.id);
println!(
" Platform: {:?}, Available: {}",
camera.platform, camera.is_available
);
}
let camera = &cameras[0];
let device_id = camera.id.clone();
println!("\nđ¯ Using camera: {} (ID: {})", camera.name, device_id);
println!("\nâļī¸ Starting camera preview...");
let format = CameraFormat::standard(); match start_camera_preview(device_id.clone(), Some(format)).await {
Ok(message) => println!("â
{}", message),
Err(e) => {
eprintln!("â Failed to start preview: {}", e);
return Err(e.into());
}
}
println!("đš Preview is now running! Camera stream is active.");
println!("â° Waiting 3 seconds before capturing frames...");
sleep(Duration::from_secs(3)).await;
println!("\nđ¸ Capturing frames from active preview stream...");
for i in 1..=5 {
match capture_single_photo(Some(device_id.clone()), None).await {
Ok(frame) => {
println!(
" Frame {}: {}x{} pixels ({} bytes) at {}",
i,
frame.width,
frame.height,
frame.size_bytes,
frame.timestamp.format("%H:%M:%S")
);
}
Err(e) => {
eprintln!(" â Failed to capture frame {}: {}", i, e);
}
}
sleep(Duration::from_millis(500)).await;
}
println!("\nâ° Preview running for 5 more seconds...");
sleep(Duration::from_secs(5)).await;
println!("\nâšī¸ Stopping camera preview...");
match stop_camera_preview(device_id.clone()).await {
Ok(message) => println!("â
{}", message),
Err(e) => {
eprintln!("â Failed to stop preview: {}", e);
}
}
println!("\nđī¸ Releasing camera resources...");
match release_camera(device_id.clone()).await {
Ok(message) => println!("â
{}", message),
Err(e) => {
eprintln!("â Failed to release camera: {}", e);
}
}
println!("\nđ Example completed!");
println!("\nđĄ Key Points:");
println!(" âĸ start_camera_preview() starts the camera stream");
println!(" âĸ Camera remains active for continuous capture");
println!(" âĸ capture_single_photo() gets frames from active stream");
println!(" âĸ stop_camera_preview() stops the stream");
println!(" âĸ release_camera() cleans up all resources");
Ok(())
}