UNPKG

barcode-detection

Version:

Polyfill for the Barcode Detection API based on Dynamsoft Barcode Reader or ZXing.

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