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:
+15
-10
@@ -2613,20 +2613,25 @@
|
|||||||
// Create reader with 1D-only hints for fast, accurate scanning
|
// Create reader with 1D-only hints for fast, accurate scanning
|
||||||
codeReader = new ZXing.BrowserMultiFormatReader(_barcodeHints(), _barcodeOptions());
|
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';
|
document.getElementById('cameraPlaceholder').style.display = 'none';
|
||||||
video.style.display = 'block';
|
video.style.display = 'block';
|
||||||
scanningActive = true;
|
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');
|
setScanStatus('Point camera at a barcode', 'success');
|
||||||
|
|
||||||
// decodeFromVideoDevice handles camera + scanning in one call
|
// decodeFromVideoDevice handles camera + scanning in one call
|
||||||
|
|||||||
Reference in New Issue
Block a user