license declare global { interface Window { BarcodeDetector : typeof BarcodeDetector ; } } import { BarcodeDetector } from '../../../defs/barcode.js' ; import { Marker } from '../../../defs/marker.js' ; import { injectScript } from '../../utils/inject-script.js' ; import { DEBUG_LEVEL , log } from '../../utils/logger.js' ; let detector ; /**
 * Detects barcodes from image sources.
 */ export async function detectBarcodes ( data , { context = window , forceNewDetector = false , polyfillRequired = false , root = '' } = { } ) { const loadPolyfill = polyfillRequired || ( context === window && ! ( 'BarcodeDetector' in context ) ) ; if ( loadPolyfill ) { log ( 'Using barcode detection polyfill' , DEBUG_LEVEL . INFO , 'BarcodeDetector' ) ; await injectScript ( `${ root }/lib/polyfills/barcode-detector.js` ) ; } if ( ! detector || forceNewDetector ) { detector = new context . BarcodeDetector ( ) ; } if ( 'isReady' in detector ) { await detector . isReady ; } try { const barcodes = await detector . detect ( data ) ; return barcodes . map ( ( barcode ) => { return { type : barcode . format , value : barcode . rawValue } ; } ) ; } catch ( e ) { if ( polyfillRequired ) { return [ ] ; } log ( `Detection failed: ${ e . message }` , DEBUG_LEVEL . WARNING ) ; return await detectBarcodes ( data , { context , forceNewDetector , polyfillRequired : true , root , } ) ; } }