Multi-printers #32
+53
-40
@@ -189,17 +189,26 @@ function setup_events(){
|
||||
flip_cameras();
|
||||
}, false );
|
||||
|
||||
printButton.addEventListener('click', async function(ev) {
|
||||
printButton.addEventListener("click", async function (ev) {
|
||||
ev.preventDefault();
|
||||
try {
|
||||
await countDownToPicture(); // wait for countdown to finish
|
||||
var data = await take_picture(); // take_picture can be async
|
||||
print_picture(data);
|
||||
} catch (e) {
|
||||
alert("Failed to print a picture because : " + e);
|
||||
}
|
||||
}, false);
|
||||
printButton.disabled = true;
|
||||
|
||||
try {
|
||||
await countDownToPicture();
|
||||
const data = await take_picture();
|
||||
|
||||
if (!data) {
|
||||
throw new Error("No image was captured");
|
||||
}
|
||||
|
||||
await print_picture(data);
|
||||
} catch (e) {
|
||||
console.error("Print failed:", e);
|
||||
alert(`Failed to print: ${e.message || e}`);
|
||||
} finally {
|
||||
printButton.disabled = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function clear_canvas() {
|
||||
@@ -224,45 +233,49 @@ function dataURLtoFile(dataurl, filename) {
|
||||
return new File([u8arr], filename, {type:mime});
|
||||
}
|
||||
|
||||
async function take_picture() {
|
||||
const context = canvas.getContext('2d');
|
||||
let data = null;
|
||||
if (width && height) {
|
||||
canvas.width = width;
|
||||
canvas.height = height;
|
||||
context.drawImage(video, 0, 0, width, height);
|
||||
data = canvas.toDataURL('image/png');
|
||||
photo.setAttribute('src', data);
|
||||
} else {
|
||||
clear_canvas();
|
||||
function take_picture() {
|
||||
if (video.readyState < HTMLMediaElement.HAVE_CURRENT_DATA) {
|
||||
throw new Error("Camera frame isn't ready yet");
|
||||
}
|
||||
|
||||
const w = video.videoWidth;
|
||||
const h = video.videoHeight;
|
||||
if (!w || !h) {
|
||||
throw new Error(`Invalid video dimensions: ${w} × ${h}`);
|
||||
}
|
||||
|
||||
canvas.width = w;
|
||||
canvas.height = h;
|
||||
canvas.getContext("2d").drawImage(video, 0, 0, w, h);
|
||||
|
||||
const data = canvas.toDataURL("image/png");
|
||||
console.log("Captured image:", data.slice(0, 30), "length:", data.length);
|
||||
|
||||
if (data.length < 100) {
|
||||
throw new Error("Captured image is empty");
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
function print_picture(data){
|
||||
var url = "/api/print/img"
|
||||
var picture = dataURLtoFile(data);
|
||||
async function print_picture(data) {
|
||||
const picture = dataURLtoFile(data, "picture.png");
|
||||
|
||||
const formData = new FormData();
|
||||
let currentDate = new Date();
|
||||
let time = currentDate.getHours() + ":" + currentDate.getMinutes() + ":" + currentDate.getSeconds();
|
||||
console.log("Image file size:", picture.size, "type:", picture.type);
|
||||
|
||||
if (!picture.size) {
|
||||
throw new Error("Image file is empty");
|
||||
}
|
||||
|
||||
formData.set("img", picture, "picture.png");
|
||||
formData.set("signature", "Webcam")
|
||||
|
||||
fetch(url, {
|
||||
method: 'POST', // or 'PUT'
|
||||
body: formData,
|
||||
// headers:{
|
||||
// 'Content-Type': 'multipart/form-data'
|
||||
// }
|
||||
}).then(function(response) {
|
||||
console.log('Reponse:', response);
|
||||
if(response.status != 200 ){
|
||||
alert("The picture could not be printed be : " + response.statusText)
|
||||
}
|
||||
} , true)
|
||||
.catch(error => console.error('Error:', error), false);
|
||||
formData.set("signature", "Webcam");
|
||||
formData.set("cut", true);
|
||||
formData.set("process", false);
|
||||
|
||||
fetch("/api/print/img", {
|
||||
method: "POST",
|
||||
body: formData
|
||||
});
|
||||
}
|
||||
|
||||
function flip_cameras(){
|
||||
|
||||
Reference in new issue
Block a user