@@ -55,16 +55,23 @@ const scannerConfig = {
5555
5656
5757async function findAvailableCameras ( ) {
58- Log . info ( 'Enumerating devices...' ) ;
59- const devices = await navigator . mediaDevices . enumerateDevices ( ) ;
60- availableCameras = devices . filter ( device => device . kind === 'videoinput' ) ;
61- Log . info ( 'Found ' + availableCameras . length + ' cameras' ) ;
62-
63- // Show switch button if multiple cameras exist
64- if ( availableCameras . length > 1 ) {
65- switchButton . removeAttribute ( 'hidden' ) ;
66- switchButton . addEventListener ( 'click' , switchCamera ) ;
67- }
58+ closeExistingStreams ( )
59+
60+ Log . info ( 'Enumerating devices...' ) ;
61+ const devices = await navigator . mediaDevices . enumerateDevices ( ) ;
62+ availableCameras = devices . filter ( device => device . kind === 'videoinput' ) ;
63+
64+ Log . info ( 'Found cameras:' , availableCameras . map ( cam => ( {
65+ label : cam . label ,
66+ deviceId : cam . deviceId ? cam . deviceId . substring ( 0 , 20 ) + '...' : 'empty'
67+ } ) ) ) ;
68+
69+ // Show switch button if multiple cameras exist
70+ if ( availableCameras . length > 1 ) {
71+ switchButton . removeAttribute ( 'hidden' ) ;
72+ } else {
73+ switchButton . setAttribute ( 'hidden' , 'hidden' ) ;
74+ }
6875}
6976
7077// Update button text with current camera name
@@ -87,12 +94,17 @@ async function startScanner() {
8794 try {
8895 closeExistingStreams ( ) ;
8996
90- // Get the preferred camera (usually the rear camera )
97+ // Get the preferred camera (usually the last in the list )
9198 if ( availableCameras . length > 0 ) {
9299 if ( ! currentCamera ) {
93100 currentCamera = availableCameras [ availableCameras . length - 1 ] ;
94101 }
95- scannerConfig . inputStream . constraints . deviceId = { exact : currentCamera . deviceId } ; // Use deviceId property of the camera object
102+ if ( ! currentCamera . deviceId ) {
103+ Log . error ( 'no deviceId' ) ;
104+ isScanning = false ;
105+ return ;
106+ }
107+ scannerConfig . inputStream . constraints . deviceId = { exact : currentCamera . deviceId }
96108 } else {
97109 Log . error ( 'No cameras found' ) ;
98110 isScanning = false ;
@@ -119,8 +131,6 @@ async function startScanner() {
119131 // Start Quagga
120132 Quagga . start ( ) ;
121133 isScanning = true ;
122-
123- Log . info ( 'Quagga scanner started' ) ;
124134 } catch ( startError ) {
125135 Log . error ( 'Error starting Quagga:' , startError ) ;
126136 isScanning = false ;
@@ -220,12 +230,6 @@ async function switchCamera() {
220230 const nextIndex = ( currentIndex + 1 ) % availableCameras . length ;
221231 currentCamera = availableCameras [ nextIndex ] ; // Fix: Set to the actual camera object
222232
223- Log . info ( {
224- currentCamera : currentCamera . label ,
225- availableCameras : availableCameras
226- . map ( camera => camera . label )
227- } ) ;
228-
229233 // Restart scanner with new camera
230234 setTimeout ( startScanner , 100 ) ;
231235 } catch ( error ) {
@@ -249,7 +253,11 @@ window.addEventListener('beforeunload', () => {
249253// Initialize scanner on page load
250254async function initializeApp ( ) {
251255 try {
256+ Log . info ( 'Requesting camera permissions...' ) ;
257+ currentStream = await navigator . mediaDevices . getUserMedia ( { video : { } } ) ;
258+
252259 await findAvailableCameras ( ) ;
260+ switchButton . addEventListener ( 'click' , switchCamera ) ;
253261
254262 // Start the scanner
255263 await startScanner ( ) ;
0 commit comments