Skip to content

Instantly share code, notes, and snippets.

@legokichi
Last active January 22, 2021 10:01
Show Gist options
  • Save legokichi/0f5b01af904e9cf3410f132cf4230e36 to your computer and use it in GitHub Desktop.
Save legokichi/0f5b01af904e9cf3410f132cf4230e36 to your computer and use it in GitHub Desktop.
[package]
name = "a"
version = "0.1.0"
authors = ["pi"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
v4l = "0.12"
bardecoder = "0.2"
image = "0.22"
static MJPG_FOURCC: &[u8; 4] = &[0x4d, 0x4a, 0x50, 0x47];
fn main() -> Result<(), Box<dyn std::error::Error>> {
let fourcc = v4l::format::fourcc::FourCC::new(MJPG_FOURCC);
let mut device = v4l::device::Device::with_path("/dev/video0")?;
use v4l::video::Capture;
let formats = device.enum_formats()?;
dbg!(&formats);
let fourcc_supported = formats.iter().any(|format| format.fourcc == fourcc);
assert!(fourcc_supported);
let sizes = device.enum_framesizes(fourcc)?;
dbg!(sizes);
let expect_format = v4l::format::Format::new(1200,1200, fourcc);
let actual_format = device.set_format(&expect_format)?;
dbg!(actual_format);
let mut stream = v4l::io::mmap::stream::Stream::with_buffers(
&mut device,
v4l::buffer::Type::VideoCapture,
2
)?;
let decoder = bardecoder::default_decoder();
loop {
use v4l::io::traits::CaptureStream;
let (buf, _) = stream.next()?;
let reader = image::io::Reader::with_format(std::io::Cursor::new(buf), image::ImageFormat::JPEG);
let img = reader.decode()?;
let results = decoder.decode(&img);
dbg!(results);
if results.len() > 0 {
img.save("./a.jpg")?;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment