fix: prefer native BarcodeDetector, fall back to WASM ponyfill
All checks were successful
CI / update (push) Successful in 4m37s
All checks were successful
CI / update (push) Successful in 4m37s
Native BarcodeDetector works in Chrome/Android WebView over HTTPS. Only load the ZXing WASM ponyfill when native API is unavailable or doesn't support the needed formats.
This commit is contained in:
@@ -209,30 +209,40 @@
|
|||||||
await videoEl.play();
|
await videoEl.play();
|
||||||
scanDebug += ` | video: ${videoEl.videoWidth}x${videoEl.videoHeight}`;
|
scanDebug += ` | video: ${videoEl.videoWidth}x${videoEl.videoHeight}`;
|
||||||
|
|
||||||
// Import barcode-detector ponyfill with self-hosted WASM
|
// Use native BarcodeDetector if available, else ponyfill with self-hosted WASM
|
||||||
scanDebug += ' | importing detector…';
|
scanDebug += ' | loading detector…';
|
||||||
let BarcodeDetector;
|
let detector;
|
||||||
|
const formats = ['ean_13', 'ean_8', 'upc_a', 'upc_e', 'code_128'];
|
||||||
try {
|
try {
|
||||||
const mod = await import('barcode-detector/ponyfill');
|
if ('BarcodeDetector' in globalThis) {
|
||||||
BarcodeDetector = mod.BarcodeDetector;
|
const supported = await globalThis.BarcodeDetector.getSupportedFormats();
|
||||||
|
if (supported.includes('ean_13')) {
|
||||||
// Point ZXing WASM to our self-hosted copy in /static
|
detector = new globalThis.BarcodeDetector({ formats });
|
||||||
mod.prepareZXingModule({
|
scanDebug += ' native';
|
||||||
overrides: {
|
}
|
||||||
locateFile: (path, prefix) => {
|
}
|
||||||
if (path.endsWith('.wasm')) return '/fitness/zxing_reader.wasm';
|
} catch {
|
||||||
return prefix + path;
|
// native not usable, fall through to ponyfill
|
||||||
},
|
}
|
||||||
},
|
if (!detector) {
|
||||||
});
|
try {
|
||||||
scanDebug += ' OK';
|
const mod = await import('barcode-detector/ponyfill');
|
||||||
} catch (importErr) {
|
mod.prepareZXingModule({
|
||||||
scanDebug = `IMPORT ERROR: ${importErr?.message ?? importErr}`;
|
overrides: {
|
||||||
stopScan();
|
locateFile: (path, prefix) => {
|
||||||
return;
|
if (path.endsWith('.wasm')) return '/fitness/zxing_reader.wasm';
|
||||||
|
return prefix + path;
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
detector = new mod.BarcodeDetector({ formats });
|
||||||
|
scanDebug += ' ponyfill';
|
||||||
|
} catch (importErr) {
|
||||||
|
scanDebug = `IMPORT ERROR: ${importErr?.message ?? importErr}`;
|
||||||
|
stopScan();
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const detector = new BarcodeDetector({ formats: ['ean_13', 'ean_8', 'upc_a', 'upc_e', 'code_128'] });
|
|
||||||
scanDebug += ' | detector created';
|
scanDebug += ' | detector created';
|
||||||
|
|
||||||
let scanCount = 0;
|
let scanCount = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user