Update webcam code
This commit is contained in:
1 parent
35d14965b3
commit
96b169c017
1 file changed
+53
-40
+53
-40
@@ -189,17 +189,26 @@ function setup_events(){
|
|||||||
flip_cameras();
|
flip_cameras();
|
||||||
}, false );
|
}, false );
|
||||||
|
|
||||||
printButton.addEventListener('click', async function(ev) {
|
printButton.addEventListener("click", async function (ev) {
|
||||||
ev.preventDefault();
|
ev.preventDefault();
|
||||||
try {
|
printButton.disabled = true;
|
||||||
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);
|
|
||||||
|
|
||||||
|
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() {
|
function clear_canvas() {
|
||||||
@@ -224,45 +233,49 @@ function dataURLtoFile(dataurl, filename) {
|
|||||||
return new File([u8arr], filename, {type:mime});
|
return new File([u8arr], filename, {type:mime});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function take_picture() {
|
function take_picture() {
|
||||||
const context = canvas.getContext('2d');
|
if (video.readyState < HTMLMediaElement.HAVE_CURRENT_DATA) {
|
||||||
let data = null;
|
throw new Error("Camera frame isn't ready yet");
|
||||||
if (width && height) {
|
}
|
||||||
canvas.width = width;
|
|
||||||
canvas.height = height;
|
const w = video.videoWidth;
|
||||||
context.drawImage(video, 0, 0, width, height);
|
const h = video.videoHeight;
|
||||||
data = canvas.toDataURL('image/png');
|
if (!w || !h) {
|
||||||
photo.setAttribute('src', data);
|
throw new Error(`Invalid video dimensions: ${w} × ${h}`);
|
||||||
} else {
|
}
|
||||||
clear_canvas();
|
|
||||||
|
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;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
function print_picture(data){
|
async function print_picture(data) {
|
||||||
var url = "/api/print/img"
|
const picture = dataURLtoFile(data, "picture.png");
|
||||||
var picture = dataURLtoFile(data);
|
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
let currentDate = new Date();
|
console.log("Image file size:", picture.size, "type:", picture.type);
|
||||||
let time = currentDate.getHours() + ":" + currentDate.getMinutes() + ":" + currentDate.getSeconds();
|
|
||||||
|
if (!picture.size) {
|
||||||
|
throw new Error("Image file is empty");
|
||||||
|
}
|
||||||
|
|
||||||
formData.set("img", picture, "picture.png");
|
formData.set("img", picture, "picture.png");
|
||||||
formData.set("signature", "Webcam")
|
formData.set("signature", "Webcam");
|
||||||
|
formData.set("cut", true);
|
||||||
fetch(url, {
|
formData.set("process", false);
|
||||||
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);
|
|
||||||
|
|
||||||
|
fetch("/api/print/img", {
|
||||||
|
method: "POST",
|
||||||
|
body: formData
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function flip_cameras(){
|
function flip_cameras(){
|
||||||
|
|||||||
Reference in new issue
Block a user