keyfork/crates/qrcode/keyfork-zbar/examples/v4l-scan.rs

46 lines
1.1 KiB
Rust
Raw Permalink Normal View History

2024-01-16 02:44:48 +00:00
//! Scan for a barcode or QR code from the default camera.
2024-01-13 07:52:43 +00:00
use std::{
io::Cursor,
time::{Duration, SystemTime},
};
use keyfork_zbar::{image::Image, image_scanner::ImageScanner};
use image::io::Reader as ImageReader;
use v4l::{
buffer::Type,
io::{traits::CaptureStream, userptr::Stream},
Device,
};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let device = Device::new(0)?;
let mut stream = Stream::with_buffers(&device, Type::VideoCapture, 4)?;
let start = SystemTime::now();
let mut scanner = ImageScanner::new();
while SystemTime::now()
.duration_since(start)
.unwrap_or(Duration::from_secs(0))
< Duration::from_secs(30)
{
let (buffer, _) = stream.next()?;
let image = Image::from(
ImageReader::new(Cursor::new(buffer))
.with_guessed_format()?
.decode()?,
);
2024-08-11 23:25:25 +00:00
if let Some(symbol) = scanner.scan_image(&image).first() {
2024-01-13 07:52:43 +00:00
println!("{}", String::from_utf8_lossy(symbol.data()));
return Ok(());
}
}
println!("Could not find a QR code");
Ok(())
}