From 4d58f3d6ba1caaa331ae61471a6a0d6778d6baab Mon Sep 17 00:00:00 2001 From: gennyble Date: Thu, 23 Nov 2023 14:28:36 -0600 Subject: oh, i should commit :) --- src/main.rs | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 src/main.rs (limited to 'src') diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..1f8cd70 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,100 @@ +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() + ); + } +} -- cgit 1.4.1-3-g733a5