use std::{thread::sleep, time::Duration}; use nokhwa::{ pixel_format::{self, RgbFormat}, query, utils::{ApiBackend, CameraIndex, FrameFormat, RequestedFormat, RequestedFormatType}, Camera, }; fn main() { nokhwa::nokhwa_initialize(callback); let mut args = std::env::args().skip(1); match args.next().as_deref() { Some("tryids") => { let start: u32 = args.next().map(|n| n.parse().unwrap()).unwrap_or(0); let end: u32 = args.next().map(|n| n.parse().unwrap()).unwrap_or(255); tryids(start, end); } Some("trydev") => match args.next() { None => { eprintln!("device path required"); std::process::exit(1); } Some(dev) => trydev(&dev), }, Some("query") => { cmdquery(); } None | Some(_) => { println!("tryids "); println!("\ttry to open video devices with IDs start to end, inclusive on both ends\n"); println!("trydev "); println!("\ttry to open the video device from the full path"); } } } fn callback(success: bool) { println!("callback: os says: {success}"); } fn tryids(start: u32, end: u32) { for idx in start..=end { let fmt = RequestedFormat::new::(RequestedFormatType::None); match Camera::new(CameraIndex::Index(idx), fmt) { Err(e) => { eprintln!("cam [{idx}]: failed to open device: {e}"); } Ok(cam) => { let info = cam.info(); println!( "cam [{idx}]: {} - {}\n\t{}", info.index(), info.human_name(), info.description() ); } } } } fn trydev(device: &str) { let fmt = RequestedFormat::new::(RequestedFormatType::None); match Camera::new(CameraIndex::String(device.to_owned()), fmt) { Err(e) => { eprintln!("cam [{device}]: failed to open device: {e}"); } Ok(cam) => { let info = cam.info(); println!( "cam [{device}]: {} - {}\n\t{}", info.index(), info.human_name(), info.description() ); } } } fn cmdquery() { let query = match query(ApiBackend::Auto) { Err(e) => { eprintln!("failed to query devices: {e}"); std::process::exit(1); } Ok(q) => q, }; for info in query { println!( "cam [{}]: {}\n\t{}", info.index(), info.human_name(), info.description() ); } }