let selectedDeviceId; let isScanning = false; // Flag to prevent multiple scans at the same time async function testCameraAccess() { try { const stream = await navigator.mediaDevices.getUserMedia({ video: true }); console.log('Camera access granted', stream); } catch (error) { console.error('Error accessing camera:', error); } } // Get available video input devices (cameras) async function getCameras() { try { const cameraSelect = document.getElementById('camera-select'); const stream = await navigator.mediaDevices.getUserMedia({ video: true }); // Prompt for camera access const devices = await navigator.mediaDevices.enumerateDevices(); const videoDevices = devices.filter(device => device.kind === 'videoinput'); // Update selectedDeviceId when user selects a camera cameraSelect.addEventListener('change', function() { selectedDeviceId = this.value; }); if (videoDevices.length > 0) { videoDevices.forEach((device, index) => { const option = document.createElement('option'); option.value = device.deviceId; option.text = device.label || `Camera ${index + 1}`; cameraSelect.appendChild(option); }); } else { console.log("No video devices found."); cameraSelect.innerHTML = ''; } } catch (error) { console.error("Error accessing the camera or enumerating devices:", error); document.getElementById('camera-select').innerHTML = ''; } } function startScanner() { if (!selectedDeviceId) { alert('No camera selected or available.'); return; } Quagga.init({ inputStream: { name: "Live", type: "LiveStream", target: document.querySelector('#interactive'), constraints: { deviceId: selectedDeviceId, facingMode: "environment", // Default to rear camera }, }, decoder: { readers: ["ean_reader"] // EAN is the standard format for ISBN-13 barcodes } }, function (err) { if (err) { console.log(err); return; } console.log("Initialization finished. Ready to start"); Quagga.start(); }); Quagga.onDetected(async function (data) { if (isScanning) return; // Prevent further scans while a scan is being processed isScanning = true; // Set the scanning flag const isbn = data.codeResult.code; console.log("Detected ISBN:", isbn); Quagga.stop(); // Stop the scanner once an ISBN is detected // Fetch book details await fetchBookInfo(isbn); isScanning = false; // Reset the scanning flag once processing is done }); } async function fetchBookInfo(isbn) { try { const response = await fetch(`/book/${isbn}`); const bookData = await response.json(); if (bookData.title) { promptUserWithBook(bookData); } else { console.log("No book data found. Restarting scanner..."); startScanner(); // Restart the scanner if no book information is found } } catch (error) { console.error('Error fetching book data:', error); startScanner(); // Restart the scanner on error as well } } function promptUserWithBook(bookData) { // Display book information or prompt the user for confirmation document.getElementById('book-info').textContent = `Title: ${bookData.title}`; document.getElementById('prompt').style.display = 'block'; // Additional logic for user confirmation can be added here } // Start the scanner when the start button is clicked document.getElementById('start-scanner').addEventListener('click', startScanner); // Get cameras on page load window.onload = getCameras;