!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("jimp")):"function"==typeof define&&define.amd?define(["jimp"],e):(n="undefined"!=typeof globalThis?globalThis:n||self).javascriptBarcodeReader=e(n.Jimp)}(this,function(n){"use strict";function e(n){var e=Object.create(null);return n&&Object.keys(n).forEach(function(t){if("default"!==t){var o=Object.getOwnPropertyDescriptor(n,t);Object.defineProperty(e,t,o.get?o:{enumerable:!0,get:function(){return n[t]}})}}),e.default=n,Object.freeze(e)}var t=e(n);function o(n,e,t,o){return new(t||(t=Promise))(function(r,a){function w(n){try{s(o.next(n))}catch(n){a(n)}}function c(n){try{s(o.throw(n))}catch(n){a(n)}}function s(n){var e;n.done?r(n.value):(e=n.value,e instanceof t?e:new t(function(n){n(e)})).then(w,c)}s((o=o.apply(n,e||[])).next())})}"function"==typeof SuppressedError&&SuppressedError;const r={nnnnnww:"0",nnnnwwn:"1",nnnwnnw:"2",wwnnnnn:"3",nnwnnwn:"4",wnnnnwn:"5",nwnnnnw:"6",nwnnwnn:"7",nwwnnnn:"8",wnnwnnn:"9",nnnwwnn:"-",nnwwnnn:"$",wnnnwnw:":",wnwnnnw:"/",wnwnwnn:".",nnwwwww:"+",nnwwnwn:"A",nnnwnww:"B",nwnwnnw:"C",nnnwwwn:"D"};function a(n){const e=[],t=Math.ceil(n.reduce((n,e)=>n+e,0)/n.length);for(;n.length>0;){const o=n.splice(0,8).splice(0,7).map(n=>n<t?"n":"w").join("");e.push(r[o])}return e.join("")}const w=["212222","222122","222221","121223","121322","131222","122213","122312","132212","221213","221312","231212","112232","122132","122231","113222","123122","123221","223211","221132","221231","213212","223112","312131","311222","321122","321221","312212","322112","322211","212123","212321","232121","111323","131123","131321","112313","132113","132311","211313","231113","231311","112133","112331","132131","113123","113321","133121","313121","211331","231131","213113","213311","213131","311123","311321","331121","312113","312311","332111","314111","221411","431111","111224","111422","121124","121421","141122","141221","112214","112412","122114","122411","142112","142211","241211","221114","413111","241112","134111","111242","121142","121241","114212","124112","124211","411212","421112","421211","212141","214121","412121","111143","111341","131141","114113","114311","411113","411311","113141","114131","311141","411131","211412","211214","211232","233111","211133","233111"],c=[" ","!",'"',"#","$","%","&","'","(",")","*","+",",","-",".","/","0","1","2","3","4","5","6","7","8","9",":",";","<","=",">","?","@","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","[","\\","]","^","_","NUL","SOH","STX","ETX","EOT","ENQ","ACK","BEL","BS","HT","LF","VT","FF","CR","SO","SI","DLE","DC1","DC2","DC3","DC4","NAK","SYN","ETB","CAN","EM","SUB","ESC","FS","GS","RS","US","FNC 3","FNC 2","Shift B","Code C","Code B","FNC 4","FNC 1","Code A","Code B","Code C","Stop","Reverse Stop"],s=[" ","!",'"',"#","$","%","&","'","(",")","*","+",",","-",".","/","0","1","2","3","4","5","6","7","8","9",":",";","<","=",">","?","@","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","[","\\","]","^","_","`","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","{","|","}","~","DEL","FNC 3","FNC 2","Shift A","Code C","FNC 4","Code A","FNC 1","Code A","Code B","Code C","Stop","Reverse Stop"],i=["00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59","60","61","62","63","64","65","66","67","68","69","70","71","72","73","74","75","76","77","78","79","80","81","82","83","84","85","86","87","88","89","90","91","92","93","94","95","96","97","98","99","Code B","Code A","FNC 1","Code A","Code B","Code C","Stop","Reverse Stop"];function l(n){const e=[];let t,o,r=s;const a=(n=>{const e=n.length-13,t=n.reduce((n,t,o)=>o>=e?n:n+t,0)/(11*Math.ceil(e/6));return n.map(n=>Math.round(n/t)||1)})(n);if(!a)return"";a.pop();for(let n=0;6*n<a.length-13;n+=1){t=a.slice(6*n,6*(n+1)).join("");const l=r[w.indexOf(t)];switch(l){case"Code A":r=c;break;case"Code B":r=s;break;case"Code C":r=i;break;case"FNC 4":break;default:l?("FNC 4"===o?e.push(l.charCodeAt(0)+128):e.push(l),o=l):e.push("?")}}return e.join("")}const f={nnnwwnwnn:"0",wnnwnnnnw:"1",nnwwnnnnw:"2",wnwwnnnnn:"3",nnnwwnnnw:"4",wnnwwnnnn:"5",nnwwwnnnn:"6",nnnwnnwnw:"7",wnnwnnwnn:"8",nnwwnnwnn:"9",wnnnnwnnw:"A",nnwnnwnnw:"B",wnwnnwnnn:"C",nnnnwwnnw:"D",wnnnwwnnn:"E",nnwnwwnnn:"F",nnnnnwwnw:"G",wnnnnwwnn:"H",nnwnnwwnn:"I",nnnnwwwnn:"J",wnnnnnnww:"K",nnwnnnnww:"L",wnwnnnnwn:"M",nnnnwnnww:"N",wnnnwnnwn:"O",nnwnwnnwn:"P",nnnnnnwww:"Q",wnnnnnwwn:"R",nnwnnnwwn:"S",nnnnwnwwn:"T",wwnnnnnnw:"U",nwwnnnnnw:"V",wwwnnnnnn:"W",nwnnwnnnw:"X",wwnnwnnnn:"Y",nwwnwnnnn:"Z",nwnnnnwnw:"-",wwnnnnwnn:".",nwwnnnwnn:" ",nwnwnwnnn:"$",nwnwnnnwn:"/",nwnnnwnwn:"+",nnnwnwnwn:"%",nwnnwnwnn:"*"};function h(n){const e=[],t=Math.ceil(n.reduce((n,e)=>n+e,0)/n.length);for(;n.length>0;){const o=n.splice(0,10).map(n=>n>t?"w":"n").slice(0,9).join("");e.push(f[o])}return"*"!==e.pop()||"*"!==e.shift()?"":e.join("")}const u=[{100010100:"0"},{101001e3:"1"},{101000100:"2"},{101000010:"3"},{100101e3:"4"},{100100100:"5"},{100100010:"6"},{10101e4:"7"},{100010010:"8"},{100001010:"9"},{110101e3:"A"},{110100100:"B"},{110100010:"C"},{110010100:"D"},{110010010:"E"},{110001010:"F"},{101101e3:"G"},{101100100:"H"},{101100010:"I"},{100110100:"J"},{100011010:"K"},{101011e3:"L"},{101001100:"M"},{101000110:"N"},{100101100:"O"},{100010110:"P"},{110110100:"Q"},{110110010:"R"},{110101100:"S"},{110100110:"T"},{110010110:"U"},{110011010:"V"},{101101100:"W"},{101100110:"X"},{100110110:"Y"},{100111010:"Z"},{100101110:"-"},{111010100:"."},{111010010:" "},{111001010:"$"},{101101110:"/"},{101110110:"+"},{110101110:"%"},{100100110:"($)"},{111011010:"(%)"},{111010110:"(/)"},{100110010:"(+)"},{101011110:"*"}];function d(n){const e=[],t=[];n.pop();const o=Math.ceil(n.reduce((n,e)=>n+e,0)/n.length),r=Math.ceil(n.reduce((n,e)=>e<o?(n+e)/2:n,0));for(let e=0;e<n.length;e+=1){let o=n[e];for(;o>0;)e%2==0?t.push(1):t.push(0),o-=r}for(let n=0;n<t.length;n+=9){const o=t.slice(n,n+9).join(""),r=u.filter(n=>Object.keys(n)[0]===o);e.push(r[0][o])}if("*"!==e.shift()||"*"!==e.pop())return"";const a=e.pop();let w,c,s=0;const i=n=>Object.values(n)[0]===w;for(let n=e.length-1;n>=0;n-=1)w=e[n],c=u.indexOf(u.filter(i)[0]),s+=c*(1+(e.length-(n+1))%20);if(Object.values(u[s%47])[0]!==a)return"";const l=e.pop();s=0;for(let n=e.length-1;n>=0;n-=1)w=e[n],c=u.indexOf(u.filter(i)[0]),s+=c*(1+(e.length-(n+1))%20);return Object.values(u[s%47])[0]!==l?"":e.join("")}const p=["nnwwn","wnnnw","nwnnw","wwnnn","nnwnw","wnwnn","nwwnn","nnnww","wnnwn","nwnwn"];function g(n,e){const t=[],o=Math.ceil(n.reduce((n,e)=>n+e,0)/n.length);if("interleaved"===e){const e=n.splice(0,4).map(n=>n>o?"w":"n").join(""),r=n.splice(n.length-3,3).map(n=>n>o?"w":"n").join("");if("nnnn"!==e||"wnn"!==r)return"";for(;n.length>0;){const e=n.splice(0,10),r=e.filter((n,e)=>e%2==0).map(n=>n>o?"w":"n").join("");t.push(p.indexOf(r));const a=e.filter((n,e)=>e%2!=0).map(n=>n>o?"w":"n").join("");t.push(p.indexOf(a))}}else{const e=n.splice(0,6).filter((n,e)=>e%2==0).map(n=>n>o?"w":"n").join(""),r=n.splice(n.length-5,5).filter((n,e)=>e%2==0).map(n=>n>o?"w":"n").join("");if("wwn"!==e||"wnw"!==r)return"";for(;n.length>0;){const e=n.splice(0,10).filter((n,e)=>e%2==0).map(n=>n>o?"w":"n").join("");t.push(p.indexOf(e))}}return t.join("")}const b={3211:"0",2221:"1",2122:"2",1411:"3",1132:"4",1231:"5",1114:"6",1312:"7",1213:"8",3112:"9"};function C(n,e="13"){let t="";const o=(n[0]+n[1]+n[2])/3;n.shift(),n.shift(),n.shift(),n.pop(),n.pop(),n.pop(),"13"===e||"A"===e?n.splice(24,5):"8"===e?n.splice(16,5):"E"===e&&n.splice(20,5);for(let e=0;e<n.length;e+=4){const r=n.slice(e,e+4),a=[r[0]/o,r[1]/o,r[2]/o,r[3]/o].map(n=>1.5===n?1:Math.round(n)),w=b[a.join("")]||b[a.reverse().join("")];t+=w||"?"}if("E"===e){const n=t.charCodeAt(t.length-1)-48,e=t.substring(0,6);let o="";switch(n){case 0:o=e+"0000"+n;break;case 1:o=e+"1000"+n;break;case 2:o=e+"2000"+n;break;case 3:o=e.substring(0,3)+"0000"+e.substring(3);break;case 4:o=e.substring(0,4)+"00000"+e.substring(4,5);break;default:o=e+"00000"+n}t=o}return t}const m={100100100100:"0",100100100110:"1",100100110100:"2",100100110110:"3",100110100100:"4",100110100110:"5",100110110100:"6",100110110110:"7",110100100100:"8",110100100110:"9"};function M(n){if(n.length<20)return"";const e=(n[0]+n[1])/2;let t="";for(let o=0;o<n.length;o++){const r=n[o]/e;t+=r>=.5&&r<1.5?"1":r>=1.5&&r<2.5?"11":r>=2.5?"111":"0"}return function(n){let e="",t=0;for(n.startsWith("110")&&(t+=3);t<n.length-4;){const o=n.substring(t,t+12),r=m[o];if(!r){e+="?";break}e+=r,t+=12}return e}(t)}function j(n){if(n.length<8)return"";const e=(n[0]+n[1])/2;let t="";for(let o=0;o<n.length;o++){const r=n[o]/e;r>=.5&&r<1.5?t+="0":r>=1.5&&(t+="1")}return function(n){if(n.length<3)return"";let e=0,t=0;for(;t<n.length;)if("1"===n[t]){let o=1;for(t++;t<n.length&&"1"===n[t];)o++,t++;o>2&&(e=10*e+(o-2))}else t++;return e.toString()}(t)}var v;function E(n,e){return Math.sqrt((Math.pow(n[e],2)+Math.pow(n[e+1],2)+Math.pow(n[e+2],2))/3)}function k(n,e){return n>=e?255:0}function S(n,e,t,o,r){const a=[];let w=0,c=-1;for(let t=0;t<e;t+=1){const s=k(E(n,(r*e+t)*o),127);a.push(s),s!==c&&-1!==c&&(w+=1),c=s}if(w<10)return null;let s=0,i=e-1;for(let n=0;n<e;n+=1)if(0===a[n]){s=n;break}for(let n=e-1;n>=0;n-=1)if(0===a[n]){i=n;break}return{startX:s,endX:i}}function O(n,e,t,o){let r=null,a=-1;for(let w=0;w<t;w+=2){const t=S(n,e,0,o,w);if(t){r=t,a=w;break}}if(!r)return null;let w=a,c=a;for(let t=a-1;t>=0;t-=1){const a=S(n,e,0,o,t);if(!(a&&Math.abs(a.startX-r.startX)<20))break;w=t}for(let w=a+1;w<t;w+=1){const t=S(n,e,0,o,w);if(!(t&&Math.abs(t.startX-r.startX)<20))break;c=w}const s=function(n,e,t,o,r){const a=r.endX-r.startX,w=r.endY-r.startY;if(w<.1*a)return 0;const c=Math.atan2(w,a)*(180/Math.PI);return c>45&&c<135?90:c>135||c<-135?180:c<-45&&c>-135?270:0}(0,0,0,0,Object.assign(Object.assign({},r),{startY:w,endY:c}));return{startX:r.startX,endX:r.endX,startY:w,endY:c,rotation:s}}function A(n,e){if(""===n||""===e)return e;const t=n.split("");return e.split("").forEach((n,e)=>{t[e]&&"?"!==t[e]||n&&"?"!==n&&(t[e]=n)}),t.join("")}function y(n){const e=document.createElement("canvas"),t=e.getContext("2d");if(!t)throw new Error("Cannot create canvas 2d context");const o=n.naturalWidth,r=n.naturalHeight;return e.width=o,e.height=r,t.drawImage(n,0,0),t.getImageData(0,0,o,r)}!function(n){n["code-128"]="code-128",n["code-2of5"]="code-2of5",n["code-39"]="code-39",n["code-93"]="code-93",n["ean-13"]="ean-13",n["ean-8"]="ean-8",n["upc-a"]="upc-a",n["upc-e"]="upc-e",n.codabar="codabar",n.msi="msi",n.pharmacode="pharmacode"}(v||(v={}));const I="object"==typeof process&&process.release&&"node"===process.release.name;function N(n,e,t){const o=[],r=n.length/(e*t);let a=0,w=0;for(let c=0;c<e;c+=1){let s=0,i=0;for(let o=0;o<t;o+=1){const t=(o*e+c)*r;s+=Math.sqrt((Math.pow(n[t],2)+Math.pow(n[t+1],2)+Math.pow(n[t+2],2))/3)}i=s/t>=127?255:0,255===i&&0===a||(i===w?a+=1:(o.push(a),w=i,a=1),c===e-1&&0===i&&o.push(a))}return o}let F;try{process&&"test"===process.env.NODE_ENV&&(F=!0)}catch(n){F=!1}return function(n){return o(this,arguments,void 0,function*({image:n,barcode:e,barcodeType:r,options:w}){let c;switch(e){case v.codabar:c=a;break;case v["code-128"]:c=l;break;case v["code-39"]:c=h;break;case v["code-93"]:c=d;break;case v["code-2of5"]:c=g;break;case v["ean-13"]:c=C,r="13";break;case v["ean-8"]:c=C,r="8";break;case v["upc-a"]:c=C,r="A";break;case v["upc-e"]:c=C,r="E";break;case v.msi:c=M;break;case v.pharmacode:c=j;break;default:throw new Error(`Invalid barcode specified. Available decoders: ${Object.values(v).join(", ")}.`)}const s=F||w&&w.singlePass||!1,i=w&&w.detectRotation,f=w&&w.locateBarcode;let{data:u,width:p,height:b}=(m=n).data&&m.width&&m.height?n:yield function(n){return o(this,void 0,void 0,function*(){return new Promise((e,o)=>{if("string"==typeof n)if(n.startsWith("#")){const t=document.getElementById(n.substr(1));if(t instanceof HTMLImageElement&&e(y(t)),t instanceof HTMLCanvasElement){const n=t.getContext("2d");if(!n)throw new Error("Cannot create canvas 2d context");e(n.getImageData(0,0,t.width,t.height))}o(new Error("Invalid image source specified!"))}else if(!(r=n).startsWith("#")&&/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-/]))?/.test(r)){const t=new Image;t.onerror=o,t.onload=()=>e(y(t)),t.src=n}else I&&t.read(n,(n,t)=>{if(n)o(n);else{const{data:n,width:o,height:r}=t.bitmap;e({data:Uint8ClampedArray.from(n),width:o,height:r})}});else if(n instanceof HTMLImageElement)e(y(n));else if(n instanceof HTMLCanvasElement){const t=n.getContext("2d");if(!t)throw new Error("Cannot create canvas 2d context");e(t.getImageData(0,0,n.width,n.height))}var r})})}(n);var m;const E=u.length/(p*b);let k="";if(f||i){const n=O(u,p,b,E);if(n&&i&&0!==n.rotation){const e=function(n,e,t,o){const r="undefined"!=typeof document?document.createElement("canvas"):null;if(!r)return{data:n,width:e,height:t};const a=r.getContext("2d");if(!a)return{data:n,width:e,height:t};r.width=e,r.height=t;const w=new ImageData(n,e,t);a.putImageData(w,0,0);const c=document.createElement("canvas");c.width=e,c.height=t;const s=c.getContext("2d");if(!s)return{data:n,width:e,height:t};s.drawImage(r,0,0);const i=o*Math.PI/180,l=Math.abs(Math.sin(i)),f=Math.abs(Math.cos(i)),h=Math.round(e*f+t*l),u=Math.round(e*l+t*f);return r.width=h,r.height=u,a.translate(h/2,u/2),a.rotate(i),a.drawImage(c,-e/2,-t/2),{data:a.getImageData(0,0,h,u).data,width:h,height:u}}(u,p,b,n.rotation);u=e.data,p=e.width,b=e.height}}w&&w.useAdaptiveThreshold&&function(n,e,t){const o=new Array(e*t).fill(0),r=n.length/(e*t),a=Math.floor(t),w=Math.floor(a/2);for(let a=0;a<e;a+=1){let w=0;for(let c=0;c<t;c+=1){const t=c*e+a,s=t*r,i=(n[s]+n[s+1]+n[s+2])/3;n[s]=i,n[s+1]=i,n[s+2]=i,w+=i,o[t]=0===a?w:o[t-1]+w}}for(let a=0;a<e;a+=1)for(let c=0;c<t;c+=1){const s=(c*e+a)*r;let i=a-w,l=a+w,f=c-w,h=c+w;i<0&&(i=0),l>=e&&(l=e-1),f<0&&(f=0),h>=t&&(h=t-1);const u=(l-i)*(h-f),d=o[h*e+l]-o[f*e+l]-o[h*e+i]+o[f*e+i];let p=255;n[s]*u<.85*d&&(p=0),n[s]=p,n[s+1]=p,n[s+2]=p}}(u,p,b);const S=[5,6,4,7,3,8,2,9,1],x=Math.round(b/S.length),B=Math.min(2,b);for(let n=0;n<S.length;n+=1){const e=E*p*Math.floor(x*S[n]),t=e+B*E*p,o=N(u.slice(e,t),p,B);if(0===o.length){if(s||n===S.length-1)throw new Error("Failed to detect lines in the image!");continue}const a=c(o,r);if(a){if(s||!a.includes("?"))return a;if(k=A(k,a),!k.includes("?"))return k}}return k})}});
//# sourceMappingURL=javascript-barcode-reader.umd.min.js.map
