barcode-detection
Version:
Polyfill for the Barcode Detection API based on Dynamsoft Barcode Reader or ZXing.
3 lines (2 loc) • 5.28 kB
JavaScript
import{BarcodeReader as t,EnumBarcodeFormat as e}from"dynamsoft-javascript-barcode";import*as a from"@zxing/library";import{MultiFormatReader as r,HTMLCanvasElementLuminanceSource as n,HybridBinarizer as o,BinaryBitmap as i}from"@zxing/library";t.engineResourcePath="https://unpkg.com/dynamsoft-javascript-barcode@9.6.20/dist/";const s=new Map([["aztec",e.BF_AZTEC],["codabar",e.BF_CODABAR],["code_11",e.BF_CODE_11],["code_39",e.BF_CODE_39],["code_93",e.BF_CODE_93],["code_128",e.BF_CODE_128],["data_matrix",e.BF_DATAMATRIX],["ean_8",e.BF_EAN_8],["ean_13",e.BF_EAN_13],["itf",e.BF_ITF],["pdf417",e.BF_PDF417],["qr_code",e.BF_QR_CODE],["micro_qr_code",e.BF_MICRO_QR],["gs1_composite",e.BF_GS1_COMPOSITE],["gs1_databar",e.BF_GS1_DATABAR],["gs1_databar_expanded",e.BF_GS1_DATABAR_EXPANDED],["gs1_databar_expanded_stacked",e.BF_GS1_DATABAR_EXPANDED_STACKED],["gs1_databar_limited",e.BF_GS1_DATABAR_LIMITED],["gs1_databar_omnidirectional",e.BF_GS1_DATABAR_OMNIDIRECTIONAL],["gs1_databar_stacked",e.BF_GS1_DATABAR_STACKED],["gs1_databar_stacked_omnidirectional",e.BF_GS1_DATABAR_STACKED_OMNIDIRECTIONAL],["gs1_databar_truncated",e.BF_GS1_DATABAR_TRUNCATED],["maxi_code",e.BF_MAXICODE],["upc_a",e.BF_UPC_A],["upc_e",e.BF_UPC_E]]),c=new Map(Array.from(s).map(([t,e])=>[e,t])),d=Array.from(s.keys());let _;class m{constructor(t){var e;if(this.formats=void 0,this.formats=null!=(e=null==t?void 0:t.formats)?e:d,0===this.formats.length)throw new TypeError("");if(this.formats.includes("unknown"))throw new TypeError("");this.updateRuntimeSettings()}async updateRuntimeSettings(){if(this.formats.length!=d.length){console.log("update runtime settings for formats...");let t,e=await _.getRuntimeSettings();for(let e=0;e<this.formats.length;e++){const a=s.get(this.formats[e]);a&&(0===e?t=a:t|=a)}t&&(e.barcodeFormatIds=t,await _.updateRuntimeSettings(e))}}static setEngineResourcePath(e){t.engineResourcePath=e}static setLicense(e){t.license=e}static getLicense(){return t.license}static async init(){return _=await t.createInstance(),_}static async getSupportedFormats(){return d}async detect(t){if(!_)throw new Error("Dynamsoft Barcode Reader has not been initialized.");let e=await _.decode(t),a=[];return e.forEach(t=>{let e=this.wrapResult(t);a.push(e)}),a}wrapResult(t){const e=[];let a,r,n,o;a=t.localizationResult.x1,r=t.localizationResult.y1,n=t.localizationResult.x1,o=t.localizationResult.y1;for(let i=1;i<5;i++){const s=t.localizationResult["x"+i],c=t.localizationResult["y"+i];a=Math.min(s,a),r=Math.min(c,r),n=Math.max(s,n),o=Math.max(c,o),e.push({x:s,y:c})}let i=new DOMRectReadOnly(a,r,n-a,o-r),s=c.get(t.barcodeFormat);return s||(s="unknown"),{boundingBox:i,rawValue:t.barcodeText,format:s,cornerPoints:e}}}const l=new Map([["aztec",a.BarcodeFormat.AZTEC],["code_39",a.BarcodeFormat.CODE_39],["code_128",a.BarcodeFormat.CODE_128],["data_matrix",a.BarcodeFormat.DATA_MATRIX],["ean_8",a.BarcodeFormat.EAN_8],["ean_13",a.BarcodeFormat.EAN_13],["itf",a.BarcodeFormat.ITF],["pdf417",a.BarcodeFormat.PDF_417],["qr_code",a.BarcodeFormat.QR_CODE],["upc_a",a.BarcodeFormat.UPC_A],["upc_e",a.BarcodeFormat.UPC_E]]),u=new Map(Array.from(l).map(([t,e])=>[e,t])),B=Array.from(l.keys());class h{constructor(t){var e;this.reader=void 0,this.canvas=void 0;const n=null!=(e=null==t?void 0:t.formats)?e:B;if(0===n.length)throw new TypeError("");if(n.includes("unknown"))throw new TypeError("");if("ZXingBrowser"in window){let t=window.ZXingBrowser;this.reader=new t.BrowserMultiFormatReader}else{const t=new Map([[a.DecodeHintType.POSSIBLE_FORMATS,n.map(t=>l.get(t))]]);this.reader=new r,this.reader.setHints(t)}this.canvas=document.createElement("canvas")}static async getSupportedFormats(){return B}async detect(t){let e,a,r=[];a="ZXingBrowser"in window?t:t instanceof HTMLCanvasElement?this.createBinaryBitmapFromCanvas(t):this.createBinaryBitmap(t);try{e=this.reader.decode(a)}catch(t){return r}let n=this.wrapResult(e);return r.push(n),r}createBinaryBitmap(t){const e=this.canvas.getContext("2d");let a,r;return t instanceof HTMLVideoElement?(a=t.videoWidth,r=t.videoHeight):(a=t.naturalWidth,r=t.naturalHeight),this.canvas.width=a,this.canvas.height=r,e.drawImage(t,0,0,a,r),this.createBinaryBitmapFromCanvas(this.canvas)}createBinaryBitmapFromCanvas(t){const e=new n(t),a=new o(e);return new i(a)}wrapResult(t){let e,a,r,n,o=t.getResultPoints();e=o[0].getX(),a=o[0].getY(),r=o[0].getX(),n=o[0].getY(),o.forEach(t=>{const o=t.getX(),i=t.getY();e=Math.min(o,e),a=Math.min(i,a),r=Math.max(o,r),n=Math.max(i,n)});let i=new DOMRectReadOnly(e,a,r-e,n-a);const s=[{x:i.left,y:i.top},{x:i.right,y:i.top},{x:i.right,y:i.bottom},{x:i.left,y:i.bottom}];let c=u.get(t.getBarcodeFormat());return c||(c="unknown"),{boundingBox:i,rawValue:t.getText(),format:c,cornerPoints:s}}}class g{constructor(t){this.reader=void 0,this.reader="DBR"===g.engine?new m(t):new h(t)}static async getSupportedFormats(){return"DBR"===g.engine?m.getSupportedFormats():h.getSupportedFormats()}async detect(t){return await this.reader.detect(t)}static setDBREngineResourcePath(t){m.setEngineResourcePath(t)}static setDBRLicense(t){m.setLicense(t)}static getDBRLicense(t){return m.getLicense()}static async initDBR(){return await m.init()}}g.engine="DBR";export{g as default};
//# sourceMappingURL=barcode-detector.modern.js.map