feat: camera window
This commit is contained in:
64
Cargo.lock
generated
64
Cargo.lock
generated
@@ -689,6 +689,16 @@ dependencies = [
|
|||||||
"windows-link",
|
"windows-link",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "clang"
|
||||||
|
version = "2.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b2ed30b30e1e17966b55ddd0b9dd2758fd824f0a6da6875c8abb17e6aba47c28"
|
||||||
|
dependencies = [
|
||||||
|
"clang-sys",
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "clang-sys"
|
name = "clang-sys"
|
||||||
version = "1.9.1"
|
version = "1.9.1"
|
||||||
@@ -1039,6 +1049,12 @@ version = "0.8.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "edf234dd1594d6dd434a8fb8cada51ddbbc593e40e4a01556a0b31c62da2775b"
|
checksum = "edf234dd1594d6dd434a8fb8cada51ddbbc593e40e4a01556a0b31c62da2775b"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "dunce"
|
||||||
|
version = "1.0.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "92773504d58c093f6de2459af4af33faa518c13451eb8f2b5698ed3d36e7c813"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "either"
|
name = "either"
|
||||||
version = "1.19.0"
|
version = "1.19.0"
|
||||||
@@ -3136,6 +3152,41 @@ dependencies = [
|
|||||||
"portable-atomic",
|
"portable-atomic",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "opencv"
|
||||||
|
version = "0.101.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "cac4182ca27a86f18c4f07f4ccf83ae8cc68fe8080734ca75745f617f6658c89"
|
||||||
|
dependencies = [
|
||||||
|
"cc",
|
||||||
|
"dunce",
|
||||||
|
"jobserver",
|
||||||
|
"libc",
|
||||||
|
"num-traits",
|
||||||
|
"opencv-binding-generator",
|
||||||
|
"pkg-config",
|
||||||
|
"semver",
|
||||||
|
"shlex 2.0.1",
|
||||||
|
"vcpkg",
|
||||||
|
"windows",
|
||||||
|
"winsplit",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "opencv-binding-generator"
|
||||||
|
version = "0.107.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "42973794347e82fc07e29700c422f1d2cdda9c4b124247ae4f66c5f08fb22856"
|
||||||
|
dependencies = [
|
||||||
|
"clang",
|
||||||
|
"clang-sys",
|
||||||
|
"dunce",
|
||||||
|
"percent-encoding",
|
||||||
|
"regex",
|
||||||
|
"semver",
|
||||||
|
"shlex 2.0.1",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "orbclient"
|
name = "orbclient"
|
||||||
version = "0.3.55"
|
version = "0.3.55"
|
||||||
@@ -4707,6 +4758,12 @@ dependencies = [
|
|||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "vcpkg"
|
||||||
|
version = "0.2.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "version_check"
|
name = "version_check"
|
||||||
version = "0.9.5"
|
version = "0.9.5"
|
||||||
@@ -5387,6 +5444,7 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
|
|||||||
name = "winf-photobooth-app"
|
name = "winf-photobooth-app"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"opencv",
|
||||||
"slint",
|
"slint",
|
||||||
"slint-build",
|
"slint-build",
|
||||||
]
|
]
|
||||||
@@ -5452,6 +5510,12 @@ dependencies = [
|
|||||||
"memchr",
|
"memchr",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winsplit"
|
||||||
|
version = "0.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3ab703352da6a72f35c39a533526393725640575bb211f61987a2748323ad956"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wit-bindgen"
|
name = "wit-bindgen"
|
||||||
version = "0.57.1"
|
version = "0.57.1"
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
opencv = "0.101.1"
|
||||||
slint = {
|
slint = {
|
||||||
version = "1.18.1",
|
version = "1.18.1",
|
||||||
features = [
|
features = [
|
||||||
|
|||||||
111
src/main.rs
111
src/main.rs
@@ -1,19 +1,122 @@
|
|||||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||||
|
|
||||||
use std::error::Error;
|
use std::{
|
||||||
|
error::Error,
|
||||||
|
sync::{
|
||||||
|
atomic::{AtomicBool, Ordering},
|
||||||
|
Arc, Mutex,
|
||||||
|
},
|
||||||
|
thread,
|
||||||
|
time::Duration,
|
||||||
|
};
|
||||||
|
|
||||||
|
use opencv::{core::Mat, imgproc, prelude::*, videoio};
|
||||||
|
use slint::{Rgb8Pixel, SharedPixelBuffer};
|
||||||
|
|
||||||
slint::include_modules!();
|
slint::include_modules!();
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn Error>> {
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
let ui = AppWindow::new()?;
|
let mut cam = videoio::VideoCapture::new(0, videoio::CAP_V4L2).expect("Failed to get video capture");
|
||||||
|
let mut frame = Mat::default();
|
||||||
|
|
||||||
let ui_handle = ui.as_weak();
|
let codec = videoio::VideoWriter::fourcc('M', 'J', 'P', 'G')?;
|
||||||
|
|
||||||
|
for (property, value) in [
|
||||||
|
(videoio::CAP_PROP_FOURCC, codec as f64),
|
||||||
|
(videoio::CAP_PROP_FRAME_WIDTH, 640.0),
|
||||||
|
(videoio::CAP_PROP_FRAME_HEIGHT, 480.0),
|
||||||
|
] {
|
||||||
|
if !cam.set(property, value)? {
|
||||||
|
eprintln!("Camera rejected property {property}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _ in 0..30 {
|
||||||
|
if !cam.read(&mut frame)? || frame.empty() {
|
||||||
|
return Err("Failed to capture a frame".into());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
let ui = AppWindow::new()?;
|
||||||
|
|
||||||
ui.window().on_close_requested(|| {
|
ui.window().on_close_requested(|| {
|
||||||
slint::CloseRequestResponse::KeepWindowShown
|
slint::CloseRequestResponse::KeepWindowShown
|
||||||
});
|
});
|
||||||
|
|
||||||
ui.run()?;
|
let latest_frame = Arc::new(Mutex::new(Some(frame_to_buffer(&frame)?)));
|
||||||
|
let running = Arc::new(AtomicBool::new(true));
|
||||||
|
|
||||||
|
let worker_frame = Arc::clone(&latest_frame);
|
||||||
|
let worker_running = Arc::clone(&running);
|
||||||
|
|
||||||
|
let worker = thread::spawn(move || -> opencv::Result<()> {
|
||||||
|
while worker_running.load(Ordering::Relaxed) {
|
||||||
|
if !cam.read(&mut frame)? || frame.empty() {
|
||||||
|
eprintln!("Camera stopped providing frames");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
let buffer = frame_to_buffer(&frame)?;
|
||||||
|
|
||||||
|
// Replace any undisplayed frame with the newest one.
|
||||||
|
*worker_frame.lock().unwrap() = Some(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
});
|
||||||
|
|
||||||
|
let ui_handle = ui.as_weak();
|
||||||
|
let timer = slint::Timer::default();
|
||||||
|
|
||||||
|
timer.start(
|
||||||
|
slint::TimerMode::Repeated,
|
||||||
|
Duration::from_millis(33),
|
||||||
|
move || {
|
||||||
|
let buffer = latest_frame.lock().unwrap().take();
|
||||||
|
|
||||||
|
if let (Some(app), Some(buffer)) = (ui_handle.upgrade(), buffer) {
|
||||||
|
app.set_video_frame(slint::Image::from_rgb8(buffer));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
// Keep `timer` alive while the interface is running.
|
||||||
|
let result = ui.run();
|
||||||
|
|
||||||
|
timer.stop();
|
||||||
|
running.store(false, Ordering::Relaxed);
|
||||||
|
|
||||||
|
// Wait for the current camera read to finish and release the camera.
|
||||||
|
match worker.join() {
|
||||||
|
Ok(Ok(())) => {}
|
||||||
|
Ok(Err(error)) => eprintln!("Capture error: {error}"),
|
||||||
|
Err(_) => eprintln!("Capture thread panicked"),
|
||||||
|
}
|
||||||
|
|
||||||
|
result?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn frame_to_buffer(
|
||||||
|
frame: &Mat,
|
||||||
|
) -> opencv::Result<SharedPixelBuffer<Rgb8Pixel>> {
|
||||||
|
let mut rgb = Mat::default();
|
||||||
|
|
||||||
|
imgproc::cvt_color(
|
||||||
|
frame,
|
||||||
|
&mut rgb,
|
||||||
|
imgproc::COLOR_BGR2RGB,
|
||||||
|
0,
|
||||||
|
)?;
|
||||||
|
|
||||||
|
// Ensure the bytes are contiguous.
|
||||||
|
let rgb = rgb.try_clone()?;
|
||||||
|
|
||||||
|
Ok(SharedPixelBuffer::<Rgb8Pixel>::clone_from_slice(
|
||||||
|
rgb.data_bytes()?,
|
||||||
|
rgb.cols() as u32,
|
||||||
|
rgb.rows() as u32,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,10 +1,15 @@
|
|||||||
import { Button, VerticalBox } from "std-widgets.slint";
|
import { Button, VerticalBox } from "std-widgets.slint";
|
||||||
|
|
||||||
export component AppWindow inherits Window {
|
export component AppWindow inherits Window {
|
||||||
|
in property <image> video-frame <=> image.source;
|
||||||
|
|
||||||
VerticalBox {
|
VerticalBox {
|
||||||
Text {
|
x: root.safe-area-insets.left;
|
||||||
text: "Welcome to winf-photobooth";
|
y: root.safe-area-insets.top;
|
||||||
}
|
width: root.width - root.safe-area-insets.left - root.safe-area-insets.right;
|
||||||
|
height: root.height - root.safe-area-insets.top - root.safe-area-insets.bottom;
|
||||||
|
|
||||||
|
image := Image {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user