Spaces:
Build error
Build error
Update script.js
Browse files
script.js
CHANGED
|
@@ -1,39 +1,25 @@
|
|
| 1 |
const video = document.getElementById("video");
|
| 2 |
-
const canvas = document.getElementById("canvas");
|
| 3 |
-
const output = document.getElementById("output");
|
| 4 |
let currentFacingMode = "environment"; // Default to back-facing camera
|
| 5 |
-
let stream = null;
|
| 6 |
|
| 7 |
-
// Start the camera
|
| 8 |
async function startCamera() {
|
| 9 |
try {
|
| 10 |
-
// Stop any existing camera stream
|
| 11 |
-
if (stream) {
|
| 12 |
-
stream.getTracks().forEach((track) => track.stop());
|
| 13 |
-
}
|
| 14 |
-
|
| 15 |
-
// Set up camera constraints
|
| 16 |
const constraints = {
|
| 17 |
-
video: {
|
| 18 |
-
facingMode: currentFacingMode,
|
| 19 |
-
width: { ideal: 1280 },
|
| 20 |
-
height: { ideal: 720 },
|
| 21 |
-
},
|
| 22 |
};
|
| 23 |
|
| 24 |
-
|
| 25 |
-
stream = await navigator.mediaDevices.getUserMedia(constraints);
|
| 26 |
video.srcObject = stream;
|
| 27 |
video.play();
|
| 28 |
console.log("Camera started successfully.");
|
| 29 |
} catch (err) {
|
| 30 |
console.error("Error accessing the camera:", err);
|
|
|
|
| 31 |
if (err.name === "NotAllowedError") {
|
| 32 |
alert("Camera access denied. Please allow camera permissions.");
|
| 33 |
} else if (err.name === "NotFoundError") {
|
| 34 |
alert("No camera found. Please connect a camera.");
|
| 35 |
} else {
|
| 36 |
-
alert("An error occurred while accessing the camera.");
|
| 37 |
}
|
| 38 |
}
|
| 39 |
}
|
|
@@ -44,24 +30,5 @@ document.getElementById("flipCamera").addEventListener("click", () => {
|
|
| 44 |
startCamera();
|
| 45 |
});
|
| 46 |
|
| 47 |
-
// Capture the image
|
| 48 |
-
document.getElementById("capture").addEventListener("click", () => {
|
| 49 |
-
if (!video.srcObject) {
|
| 50 |
-
alert("Camera is not active. Please check your permissions or reload the page.");
|
| 51 |
-
console.error("Camera feed not available.");
|
| 52 |
-
return;
|
| 53 |
-
}
|
| 54 |
-
|
| 55 |
-
const context = canvas.getContext("2d");
|
| 56 |
-
canvas.width = video.videoWidth;
|
| 57 |
-
canvas.height = video.videoHeight;
|
| 58 |
-
context.drawImage(video, 0, 0, canvas.width, canvas.height);
|
| 59 |
-
|
| 60 |
-
canvas.toBlob((blob) => {
|
| 61 |
-
output.textContent = "Image captured. Ready to extract text.";
|
| 62 |
-
console.log("Image captured successfully.");
|
| 63 |
-
}, "image/png");
|
| 64 |
-
});
|
| 65 |
-
|
| 66 |
// Initialize the camera on page load
|
| 67 |
startCamera();
|
|
|
|
| 1 |
const video = document.getElementById("video");
|
|
|
|
|
|
|
| 2 |
let currentFacingMode = "environment"; // Default to back-facing camera
|
|
|
|
| 3 |
|
|
|
|
| 4 |
async function startCamera() {
|
| 5 |
try {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
const constraints = {
|
| 7 |
+
video: { facingMode: currentFacingMode },
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
};
|
| 9 |
|
| 10 |
+
const stream = await navigator.mediaDevices.getUserMedia(constraints);
|
|
|
|
| 11 |
video.srcObject = stream;
|
| 12 |
video.play();
|
| 13 |
console.log("Camera started successfully.");
|
| 14 |
} catch (err) {
|
| 15 |
console.error("Error accessing the camera:", err);
|
| 16 |
+
|
| 17 |
if (err.name === "NotAllowedError") {
|
| 18 |
alert("Camera access denied. Please allow camera permissions.");
|
| 19 |
} else if (err.name === "NotFoundError") {
|
| 20 |
alert("No camera found. Please connect a camera.");
|
| 21 |
} else {
|
| 22 |
+
alert("An unexpected error occurred while accessing the camera.");
|
| 23 |
}
|
| 24 |
}
|
| 25 |
}
|
|
|
|
| 30 |
startCamera();
|
| 31 |
});
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
// Initialize the camera on page load
|
| 34 |
startCamera();
|