fix: call listVideoInputDevices on reader instance, not static

ZXing.BrowserMultiFormatReader.listVideoInputDevices is an instance
method, not a static method. Call codeReader.listVideoInputDevices()
after creating the reader. Also wrap in try/catch with null fallback
so decodeFromVideoDevice picks the default camera if enumeration fails.
This commit is contained in:
2026-05-20 15:32:44 -04:00
parent 45af141073
commit 8cc563374d
+15 -10
View File
@@ -2613,20 +2613,25 @@
// Create reader with 1D-only hints for fast, accurate scanning
codeReader = new ZXing.BrowserMultiFormatReader(_barcodeHints(), _barcodeOptions());
// Pick the rear-facing camera (environment-facing on phones)
let deviceId = null;
try {
const devices = await codeReader.listVideoInputDevices();
if (devices && devices.length > 0) {
const rear = devices.find(d =>
d.label && /back|rear|environment/i.test(d.label)
);
deviceId = rear ? rear.deviceId : devices[0].deviceId;
}
} catch (e) {
// Fallback: pass null to let decodeFromVideoDevice pick default
console.warn('listVideoInputDevices failed, using default camera:', e);
}
document.getElementById('cameraPlaceholder').style.display = 'none';
video.style.display = 'block';
scanningActive = true;
// Pick the rear-facing camera (environment-facing on phones)
const devices = await ZXing.BrowserMultiFormatReader.listVideoInputDevices();
let deviceId = null;
if (devices && devices.length > 0) {
const rear = devices.find(d =>
d.label && /back|rear|environment/i.test(d.label)
);
deviceId = rear ? rear.deviceId : devices[0].deviceId;
}
setScanStatus('Point camera at a barcode', 'success');
// decodeFromVideoDevice handles camera + scanning in one call