feat(main): 重构图像捕获和处理逻辑,提升代码可读性和维护性

This commit is contained in:
conglinyizhi 2024-12-07 13:52:25 +08:00
parent 8a15ee2c7c
commit a23b117752
1 changed files with 23 additions and 24 deletions

View File

@ -51,19 +51,20 @@ fn main() {
let mut w = 0; let mut w = 0;
let mut h = 0; let mut h = 0;
if camera_open { if camera_open {
loop { crossbeam::scope(|_| {
// 捕获一帧图像 loop {
match camera.capture() { // 捕获一帧图像
Ok(data) => { match camera.capture() {
let start = Instant::now(); Ok(data) => {
let img = image::load_from_memory(&data).unwrap(); let start = Instant::now();
let l_data = img.to_luma8(); let img = image::load_from_memory(&data).unwrap();
let mut rgb_data = img.to_rgb8(); let l_data = img.to_luma8();
if w == 0 { let mut rgb_data = img.to_rgb8();
w = l_data.width() as i32; if w == 0 {
h = l_data.height() as i32; w = l_data.width() as i32;
} h = l_data.height() as i32;
crossbeam::scope(|_| { }
for tmp_x in 0..30 { for tmp_x in 0..30 {
for tmp_y in 0..60 { for tmp_y in 0..60 {
rgb_data.put_pixel(tmp_x, tmp_y, Rgb([102, 204, 255])); rgb_data.put_pixel(tmp_x, tmp_y, Rgb([102, 204, 255]));
@ -77,9 +78,7 @@ fn main() {
color_frame.redraw(); color_frame.redraw();
let duration = start.elapsed(); let duration = start.elapsed();
println!("L8 Frame Draw use time: {:?}", duration); println!("L8 Frame Draw use time: {:?}", duration);
})
.unwrap();
crossbeam::scope(|_| {
let fltk_l_image = let fltk_l_image =
RgbImage::new(l_data.as_raw(), w, h, ColorDepth::L8).unwrap(); RgbImage::new(l_data.as_raw(), w, h, ColorDepth::L8).unwrap();
camera_frame.set_image(Some(fltk_l_image)); camera_frame.set_image(Some(fltk_l_image));
@ -87,16 +86,16 @@ fn main() {
camera_frame.redraw(); camera_frame.redraw();
let duration = start.elapsed(); let duration = start.elapsed();
println!("RGB8 Frame Draw use time: {:?}", duration); println!("RGB8 Frame Draw use time: {:?}", duration);
}) }
.unwrap(); Err(e) => {
} eprintln!("捕获错误: {}", e);
Err(e) => { continue;
eprintln!("捕获错误: {}", e); }
continue;
} }
app.wait();
} }
app.wait(); })
} .unwrap();
} }
} }