Spaces:
Runtime error
Runtime error
Update camera.js
Browse files123454 unmodified lines1011121314151617181920212223242526272813 unmodified lines42434445464748495051525354// Get the video elementconst videoElement = document.querySelector("#video");// Store the current facing mode (front or rear)let currentFacingMode = "user"; // "user" for front camera, "environment" for rear camera4 unmodified linesstopCamera();// Access the media devicesnavigator.mediaDevices.getUserMedia({video: { facingMode: facingMode } // Use facingMode to select the desired camera}).then((stream) => {// Attach the video stream to the video elementvideoElement.srcObject = stream;// Reset mirroring (no mirroring for any camera)videoElement.style.transform = "scaleX(1)";}).catch((err) => {console.error("Error accessing camera: ", err);});}// Function to stop the camera13 unmodified linesstartCamera(currentFacingMode);}// Initialize the front camera on page loaddocument.addEventListener("DOMContentLoaded", () => {startCamera(currentFacingMode);// Attach event listener to button to toggle cameraconst switchButton = document.querySelector("#switchCameraButton");if (switchButton) {switchButton.addEventListener("click", toggleCamera);}});12345674 unmodified lines12131415161718192021222324252627282930313233343513 unmodified lines495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108// Get the video, canvas, and buttonsconst videoElement = document.querySelector("#video");const canvasElement = document.querySelector("#canvas");const captureButton = document.querySelector("#captureButton");// Store the current facing mode (front or rear)let currentFacingMode = "user"; // "user" for front camera, "environment" for rear camera4 unmodified linesstopCamera();// Access the media devicesnavigator.mediaDevices.getUserMedia({video: { facingMode: facingMode }, // Select camera based on facingMode}).then((stream) => {// Attach the video stream to the video elementvideoElement.srcObject = stream;// Adjust mirroring based on cameraif (facingMode === "user") {videoElement.style.transform = "scaleX(-1)"; // Mirror front camera} else {videoElement.style.transform = "scaleX(1)"; // Normal view for rear camera}}).catch((err) => {console.error("Error accessing camera: ", err);});}// Function to stop the camera13 unmodified linesstartCamera(currentFacingMode);}// Function to capture an imagefunction captureImage() {if (!videoElement.srcObject) {console.error("Camera is not active.");return;}// Set canvas dimensions to match the videocanvasElement.width = videoElement.videoWidth;canvasElement.height = videoElement.videoHeight;// Draw the current frame from the video onto the canvasconst context = canvasElement.getContext("2d");context.drawImage(videoElement, 0, 0, canvasElement.width, canvasElement.height);// Convert the canvas to a Base64 imageconst imageData = canvasElement.toDataURL("image/png");console.log("Captured Image:", imageData);// Pass the image data to the server or OCR functionalitysendImageForOCR(imageData);}// Function to send the captured image for OCR or processingfunction sendImageForOCR(imageData) {fetch("/process-image", {method: "POST",headers: {"Content-Type": "application/json",},body: JSON.stringify({ image: imageData }),}).then((response) => response.json()).then((data) => {console.log("OCR Result:", data);alert("Text Extracted: " + data.text);}).catch((err) => {console.error("Error processing image:", err);});}// Initialize the front camera on page loaddocument.addEventListener("DOMContentLoaded", () => {startCamera(currentFacingMode);// Attach event listener to toggle buttonconst switchButton = document.querySelector("#switchCameraButton");if (switchButton) {switchButton.addEventListener("click", toggleCamera);}// Attach event listener to capture buttonif (captureButton) {captureButton.addEventListener("click", captureImage);}});
camera.js+68-14