diff --git a/client/components/forms/BarcodeInput.vue b/client/components/forms/BarcodeInput.vue index e652af87..f283459e 100644 --- a/client/components/forms/BarcodeInput.vue +++ b/client/components/forms/BarcodeInput.vue @@ -61,33 +61,15 @@
{{ $t('forms.barcodeInput.clickToOpenCamera') }}
-@@ -80,7 +110,8 @@
@@ -195,16 +227,11 @@ export default { async switchCamera() { try { - // Stop current camera - if (this.quaggaInitialized) { - Quagga.stop() - this.quaggaInitialized = false - } - this.webcam.stop() + // Stop current camera and clean up resources + this.cleanupCurrentStream() - // Toggle facing mode considering barcode mode - this.currentFacingMode = this.isBarcodeMode ? 'environment' : - (this.currentFacingMode === 'user' ? 'environment' : 'user') + // Toggle facing mode + this.currentFacingMode = this.currentFacingMode === 'user' ? 'environment' : 'user' // Restart camera await this.openCameraUpload() @@ -222,22 +249,49 @@ export default { const webcamElement = document.getElementById("webcam") const canvasElement = document.getElementById("canvas") - const constraints = { + // Determine the facing mode to use + let facingMode = this.currentFacingMode + if (this.isBarcodeMode && this.currentFacingMode === 'user') { + // Force environment mode for barcode scanning + facingMode = 'environment' + } + + // Create constraints based on device capabilities + let constraints = { audio: false, video: { - facingMode: this.isBarcodeMode ? 'environment' : this.currentFacingMode, width: { ideal: 1280 }, height: { ideal: 720 } } } - const stream = await navigator.mediaDevices.getUserMedia(constraints) + // Use exact constraints for mobile devices to ensure proper camera selection + if (this.isMobileDevice) { + constraints.video.facingMode = { exact: facingMode } + } else { + constraints.video.facingMode = facingMode + } + + // Try to get the stream with the specified constraints + let stream + try { + stream = await navigator.mediaDevices.getUserMedia(constraints) + } catch (err) { + // If exact constraint fails, fall back to preference + if (this.isMobileDevice && err.name === 'OverconstrainedError') { + constraints.video.facingMode = facingMode + stream = await navigator.mediaDevices.getUserMedia(constraints) + } else { + throw err + } + } + this.mediaStream = stream // Store the stream reference webcamElement.srcObject = stream this.webcam = new Webcam( webcamElement, - this.isBarcodeMode ? 'environment' : this.currentFacingMode, + facingMode, canvasElement ) @@ -269,9 +323,18 @@ export default { type: "LiveStream", target: document.getElementById("webcam"), constraints: { - facingMode: "environment" + facingMode: this.currentFacingMode, + width: { min: 640 }, + height: { min: 480 }, + aspectRatio: { min: 1, max: 2 } }, }, + locator: { + patchSize: "medium", + halfSample: true + }, + numOfWorkers: navigator.hardwareConcurrency || 4, + frequency: 10, decoder: { readers: this.decoders || [] }, @@ -326,3 +389,4 @@ export default { }, } +