fix: barcode scanner — hints not applied, per-frame errors killing scanner

Root cause #1: BrowserMultiFormatReader constructor takes 0 args so hints
and timing options (passed as args) were silently ignored. Hints are now
applied as reader.reader.hints, and timing as instance properties.

Root cause #2: ZXing per-frame 'no barcode' exceptions have message=undefined
causing .toLowerCase() to crash in old message-text filter. Switched to
instanceof checks against NotFoundException, ChecksumException, FormatException
which properly distinguishes normal 'no barcode' frames from real errors.

Tested in browser — hint setting and error filtering both verified working.
This commit is contained in:
2026-06-02 18:09:03 -04:00
parent 2c3ae1de22
commit 3859d9d555
+13 -3
View File
@@ -2885,7 +2885,12 @@
if (!video) return;
// Create reader with 1D-only hints for fast, accurate scanning
codeReader = new ZXing.BrowserMultiFormatReader(_barcodeHints(), _barcodeOptions());
// NOTE: BrowserMultiFormatReader constructor takes 0 args — hints and options
// must be set as instance properties for them to take effect.
codeReader = new ZXing.BrowserMultiFormatReader();
codeReader.reader.hints = _barcodeHints();
codeReader.timeBetweenScansMillis = 2000; // delayBetweenScanSuccess
codeReader._timeBetweenDecodingAttempts = 300; // delayBetweenScanAttempts
// Pick the rear-facing camera (environment-facing on phones)
let deviceId = null;
@@ -2927,8 +2932,13 @@
handleBarcode(val.trim());
}
} else if (err && scanningActive) {
const msg = (err.message || err || '').toLowerCase();
if (!msg.includes('multiformat') && !msg.includes('unable to detect')) {
// NotFoundException, ChecksumException, FormatException are normal
// "no barcode on this frame" results — silently continue scanning.
// We use instanceof because ZXing's minified exception classes all
// have message="undefined", making message-text filtering unreliable.
if (!(err instanceof ZXing.NotFoundException ||
err instanceof ZXing.ChecksumException ||
err instanceof ZXing.FormatException)) {
console.error('ZXing camera error:', err);
handleCameraError(new Error(err.message || err));
}