From 5ce7625f6b80565000f437736e46f54099decaac Mon Sep 17 00:00:00 2001 From: Joscha Date: Wed, 26 Mar 2025 23:52:26 +0100 Subject: [PATCH] Add gallery button --- showbits-thermal-printer-ui/src/AppPhoto.vue | 49 +++++++++++-------- .../src/components/CPhotoButtonGallery.vue | 19 +++++++ 2 files changed, 47 insertions(+), 21 deletions(-) diff --git a/showbits-thermal-printer-ui/src/AppPhoto.vue b/showbits-thermal-printer-ui/src/AppPhoto.vue index bce0d70..5e486ff 100644 --- a/showbits-thermal-printer-ui/src/AppPhoto.vue +++ b/showbits-thermal-printer-ui/src/AppPhoto.vue @@ -51,27 +51,9 @@ async function waitAtLeast(duration: number, since: number) { } } -async function onRecord() { - assert(video.value !== null); - - const canvas = document.createElement("canvas"); - - const scale = 384 / video.value.videoWidth; - canvas.width = video.value.videoWidth * scale; - canvas.height = video.value.videoHeight * scale; - - const ctx = canvas.getContext("2d"); - assert(ctx !== null); - - ctx.drawImage(video.value, 0, 0, canvas.width, canvas.height); - - const blob = await new Promise((resolve) => { - canvas.toBlob(resolve); - }); - assert(blob !== null); - +async function postImage(image: Blob | File) { const form = new FormData(); - form.append("image", blob); + form.append("image", image); form.append("caption", new Date().toLocaleString()); const start = Date.now(); @@ -85,6 +67,31 @@ async function onRecord() { covered.value = false; } +async function onGallery(file: File) { + await postImage(file); +} + +async function onRecord() { + assert(video.value !== null); + const video_ = video.value; + + const canvas = document.createElement("canvas"); + const ctx = canvas.getContext("2d"); + assert(ctx !== null); + + const scale = 384 / video_.videoWidth; + canvas.width = video_.videoWidth * scale; // Yes, slightly redundant + canvas.height = video_.videoHeight * scale; + ctx.drawImage(video_, 0, 0, canvas.width, canvas.height); + + const blob = await new Promise((resolve) => { + canvas.toBlob(resolve); + }); + assert(blob !== null); + + await postImage(blob); +} + async function onFlip() { const facingOpposite = facing.value === "user" ? "environment" : "user"; await initStream(facingOpposite); @@ -98,7 +105,7 @@ onMounted(async () => {