可以正常运行

This commit is contained in:
conglinyizhi 2024-12-07 01:59:04 +08:00
commit eea6cb428c
6 changed files with 1160 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

1079
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

9
Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "rust_v4l2"
version = "0.1.0"
edition = "2021"
[dependencies]
fltk = "1.4.36"
image = "0.25.5"
rscam = "0.5.5"

BIN
frame.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

58
src/main.rs Normal file
View File

@ -0,0 +1,58 @@
extern crate rscam;
use fltk::{
app::App,
enums::ColorDepth,
frame::Frame,
prelude::{GroupExt, WidgetBase, WidgetExt},
utils,
window::Window,
};
use image::buffer::ConvertBuffer;
use rscam::{Camera, Config};
use std::io;
fn main() -> io::Result<()> {
let app = App::default();
let mut wind = Window::new(100, 100, 400, 300, "Hello from rust");
let mut frame = Frame::default_fill();
wind.end();
wind.show();
// app.run().unwrap();
// 打开摄像头设备
let mut camera = Camera::new("/dev/video0").unwrap();
// 配置摄像头参数
camera
.start(&Config {
interval: (1, 30), // 设置帧率为 30fps
resolution: (640, 480), // 设置分辨率为 640x480
format: b"MJPG", // 设置图像格式为 MJPG
..Default::default()
})
.unwrap();
loop {
// 捕获一帧图像
match camera.capture() {
Ok(data) => {
let img = image::load_from_memory(&data).unwrap();
let rgb_data = img.to_rgb8();
let fltk_image = fltk::image::RgbImage::new(
&rgb_data.as_raw(),
rgb_data.width() as i32,
rgb_data.height() as i32,
ColorDepth::Rgb8,
)
.unwrap();
frame.set_image(Some(fltk_image));
frame.redraw();
}
Err(e) => {
eprintln!("捕获错误: {}", e);
continue;
}
}
app.wait();
}
}

13
src/window.glade Normal file
View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk+" version="3.0"/>
<object class="GtkWindow" id="window">
<property name="title">摄像头视频流</property>
<property name="default-width">640</property>
<property name="default-height">480</property>
<child>
<object class="GtkImage" id="image"/>
</child>
</object>
</interface>