fix: restore empty photo printing hook
This commit is contained in:
@@ -60,92 +60,26 @@ impl Sequence {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Submit the clean, mirrored camera frame to the default CUPS printer.
|
/// Count the captured, clean selfie frame and pass the intended copies to the
|
||||||
/// JPEG travels over stdin, so captured photos are never left in temporary files.
|
/// print hook. Printer integration is intentionally left unimplemented.
|
||||||
pub fn take_picture(
|
pub fn take_picture(
|
||||||
frame: &opencv::core::Mat,
|
frame: &opencv::core::Mat,
|
||||||
detector: &mut crate::faces::Detector,
|
detector: &mut crate::faces::Detector,
|
||||||
) -> Result<(), Box<dyn std::error::Error>> {
|
) -> opencv::Result<()> {
|
||||||
use opencv::{core::Vector, imgcodecs};
|
|
||||||
let copies = detector.count(frame)?;
|
let copies = detector.count(frame)?;
|
||||||
if copies == 0 {
|
eprintln!("[capture DEBUG] faces={copies} requested_copies={copies} printing=unimplemented");
|
||||||
eprintln!("[capture DEBUG] No frontal faces; skipping print job");
|
print_picture(frame, copies);
|
||||||
return Ok(());
|
Ok(())
|
||||||
}
|
|
||||||
let mut jpeg = Vector::<u8>::new();
|
|
||||||
if !imgcodecs::imencode(".jpg", frame, &mut jpeg, &Vector::new())? {
|
|
||||||
return Err("Could not encode captured image".into());
|
|
||||||
}
|
|
||||||
submit_print(
|
|
||||||
jpeg.as_slice(),
|
|
||||||
copies,
|
|
||||||
&mut std::process::Command::new("lp"),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn submit_print(
|
fn print_picture(_frame: &opencv::core::Mat, _copies: usize) {
|
||||||
jpeg: &[u8],
|
// TODO: Implement printing.
|
||||||
copies: usize,
|
|
||||||
command: &mut std::process::Command,
|
|
||||||
) -> Result<(), Box<dyn std::error::Error>> {
|
|
||||||
use std::{io::Write, process::Stdio};
|
|
||||||
let mut child = command
|
|
||||||
.args([
|
|
||||||
"-n",
|
|
||||||
&copies.to_string(),
|
|
||||||
"-t",
|
|
||||||
"winf photobooth",
|
|
||||||
"-o",
|
|
||||||
"fit-to-page",
|
|
||||||
])
|
|
||||||
.stdin(Stdio::piped())
|
|
||||||
.stdout(Stdio::piped())
|
|
||||||
.stderr(Stdio::piped())
|
|
||||||
.spawn()
|
|
||||||
.map_err(|error| format!("Could not start CUPS lp print command: {error}. Install the CUPS client and ensure lp is on PATH."))?;
|
|
||||||
let written = child
|
|
||||||
.stdin
|
|
||||||
.take()
|
|
||||||
.ok_or("Printer stdin unavailable")?
|
|
||||||
.write_all(jpeg);
|
|
||||||
// Reap lp even if writing fails; never retry an ambiguously submitted job.
|
|
||||||
let output = child.wait_with_output()?;
|
|
||||||
if !output.status.success() {
|
|
||||||
return Err(format!(
|
|
||||||
"lp failed ({}): {}",
|
|
||||||
output.status,
|
|
||||||
String::from_utf8_lossy(&output.stderr)
|
|
||||||
)
|
|
||||||
.into());
|
|
||||||
}
|
|
||||||
written?;
|
|
||||||
eprintln!(
|
|
||||||
"[capture DEBUG] Submitted copies={copies}: {}",
|
|
||||||
String::from_utf8_lossy(&output.stdout).trim()
|
|
||||||
);
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn printer_receives_copy_count_and_image_and_reports_failure() {
|
|
||||||
use std::process::Command;
|
|
||||||
let mut printer = Command::new("sh");
|
|
||||||
printer.args(["-c", r#"test "$1" = -n && test "$2" = 3 && test "$4" = 'winf photobooth' && test "$6" = fit-to-page && test "$(cat)" = photo"#, "mock-lp"]);
|
|
||||||
submit_print(b"photo", 3, &mut printer).unwrap();
|
|
||||||
let mut failing = Command::new("sh");
|
|
||||||
failing.args(["-c", "cat >/dev/null; echo unavailable >&2; exit 1"]);
|
|
||||||
assert!(
|
|
||||||
submit_print(b"photo", 2, &mut failing)
|
|
||||||
.unwrap_err()
|
|
||||||
.to_string()
|
|
||||||
.contains("unavailable")
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn flash_countdown_and_single_capture_then_release() {
|
fn flash_countdown_and_single_capture_then_release() {
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
|
|||||||
Reference in New Issue
Block a user