mirror of
https://github.com/FliegendeWurst/v4l-save-image.git
synced 2024-11-23 13:24:58 +00:00
Clean up code
This commit is contained in:
parent
9de58a4c75
commit
289bb754c6
57
src/main.rs
57
src/main.rs
@ -2,17 +2,9 @@ use chrono::{Datelike, Local, Timelike};
|
|||||||
use image::{ImageBuffer, Rgb};
|
use image::{ImageBuffer, Rgb};
|
||||||
use v4l::{Buffer, CaptureDevice, CaptureFormat, FourCC, MappedBufferStream};
|
use v4l::{Buffer, CaptureDevice, CaptureFormat, FourCC, MappedBufferStream};
|
||||||
|
|
||||||
use std::time::Instant;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// Determine which device to use
|
let path = "/dev/video0".to_owned();
|
||||||
let path: String = "/dev/video0".to_owned();
|
println!("Using device: {}", path);
|
||||||
println!("Using device: {}\n", path);
|
|
||||||
|
|
||||||
let count = 1;
|
|
||||||
|
|
||||||
// Allocate 4 buffers by default
|
|
||||||
let buffers = 4;
|
|
||||||
|
|
||||||
let mut dev = CaptureDevice::with_path(path).expect("Failed to open device");
|
let mut dev = CaptureDevice::with_path(path).expect("Failed to open device");
|
||||||
println!("{:?}", dev.enumerate_formats());
|
println!("{:?}", dev.enumerate_formats());
|
||||||
@ -21,41 +13,16 @@ fn main() {
|
|||||||
println!("Active format:\n{}", dev.get_format().unwrap());
|
println!("Active format:\n{}", dev.get_format().unwrap());
|
||||||
println!("Active parameters:\n{}", params);
|
println!("Active parameters:\n{}", params);
|
||||||
|
|
||||||
// Setup a buffer stream and grab a frame, then print its data
|
let mut stream = MappedBufferStream::with_buffers(&mut dev, 1)
|
||||||
let mut stream = MappedBufferStream::with_buffers(&mut dev, buffers)
|
|
||||||
.expect("Failed to create buffer stream");
|
.expect("Failed to create buffer stream");
|
||||||
|
|
||||||
// warmup
|
let buf = stream.next().expect("Failed to capture buffer");
|
||||||
stream.next().expect("Failed to capture buffer");
|
println!("Buffer");
|
||||||
|
println!(" sequence : {}", buf.seq());
|
||||||
let start = Instant::now();
|
println!(" timestamp : {}", buf.timestamp());
|
||||||
let mut megabytes_ps: f64 = 0.0;
|
println!(" flags : {}", buf.flags());
|
||||||
for i in 0..count {
|
println!(" length : {}", buf.len());
|
||||||
let t0 = Instant::now();
|
let now = Local::now();
|
||||||
let buf = stream.next().expect("Failed to capture buffer");
|
let img: ImageBuffer<Rgb<u8>, _> = ImageBuffer::from_vec(640, 480, buf.data().to_vec()).unwrap();
|
||||||
let duration_us = t0.elapsed().as_micros();
|
img.save(format!("image_{}-{:02}-{:02}_{:02}-{:02}-{:02}.jpg", now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second())).unwrap();
|
||||||
|
|
||||||
let cur = buf.len() as f64 / 1_048_576.0 * 1_000_000.0 / duration_us as f64;
|
|
||||||
if i == 0 {
|
|
||||||
megabytes_ps = cur;
|
|
||||||
} else {
|
|
||||||
// ignore the first measurement
|
|
||||||
let prev = megabytes_ps * (i as f64 / (i + 1) as f64);
|
|
||||||
let now = cur * (1.0 / (i + 1) as f64);
|
|
||||||
megabytes_ps = prev + now;
|
|
||||||
}
|
|
||||||
|
|
||||||
println!("Buffer");
|
|
||||||
println!(" sequence : {}", buf.seq());
|
|
||||||
println!(" timestamp : {}", buf.timestamp());
|
|
||||||
println!(" flags : {}", buf.flags());
|
|
||||||
println!(" length : {}", buf.len());
|
|
||||||
let now = Local::now();
|
|
||||||
let img: ImageBuffer<Rgb<u8>, _> = ImageBuffer::from_vec(640, 480, buf.data().to_vec()).unwrap();
|
|
||||||
img.save(format!("image_{}-{:02}-{:02}_{:02}-{:02}-{:02}.jpg", now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second())).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
println!();
|
|
||||||
println!("FPS: {}", count as f64 / start.elapsed().as_secs_f64());
|
|
||||||
println!("MB/s: {}", megabytes_ps);
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user