Spaces:
Build error
Build error
File size: 2,061 Bytes
41bee8e cf6dd0e 42c83c2 cf6dd0e f548e15 cf6dd0e 789ff70 cf6dd0e 789ff70 cf6dd0e f548e15 8a6e0fa f548e15 42c83c2 789ff70 f8d3a35 789ff70 f548e15 f94dd64 cf6dd0e c003421 cf6dd0e f548e15 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
const video = document.getElementById("video");
const canvas = document.getElementById("canvas");
const output = document.getElementById("output");
let currentFacingMode = "environment"; // Default to back-facing camera
let stream = null;
async function startCamera() {
try {
// Stop any existing stream
if (stream) {
stream.getTracks().forEach((track) => track.stop());
}
const constraints = {
video: {
facingMode: currentFacingMode,
width: { ideal: 1280 },
height: { ideal: 720 },
},
};
stream = await navigator.mediaDevices.getUserMedia(constraints);
video.srcObject = stream;
video.play();
console.log("Camera started successfully.");
} catch (err) {
console.error("Error accessing the camera:", err);
if (err.name === "NotAllowedError") {
alert("Camera access denied. Please allow camera permissions.");
} else if (err.name === "NotFoundError") {
alert("No camera found. Please connect a camera.");
} else {
alert("An unexpected error occurred while accessing the camera.");
}
}
}
document.getElementById("flipCamera").addEventListener("click", () => {
currentFacingMode = currentFacingMode === "user" ? "environment" : "user";
startCamera();
});
document.getElementById("capture").addEventListener("click", () => {
if (!video.srcObject) {
alert("Camera is not active. Please check your permissions or reload the page.");
console.error("Camera feed not available.");
return;
}
const context = canvas.getContext("2d");
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
context.drawImage(video, 0, 0, canvas.width, canvas.height);
canvas.toBlob((blob) => {
output.textContent = "Image captured. Ready for further processing.";
console.log("Image captured successfully.");
}, "image/png");
});
startCamera();
|