qrcode-parser
Version:
A pure javascript QR code decoding library, accept File object, image url, image base64.
3 lines (2 loc) • 1.64 kB
JavaScript
import t from"jsqr";var e=function(e){return new Promise(function(r,n){var o=new Image;o.src=URL.createObjectURL(e),o.addEventListener("load",function(){try{var e=document.createElement("canvas");e.width=o.width,e.height=o.height;var i=e.getContext("2d");if(!i)return n(new Error("decode failed"));i.imageSmoothingEnabled=!1,i.drawImage(o,0,0);var a=i.getImageData(0,0,o.width,o.height),c=t(a.data,o.width,o.height);return null!==c?r(c.data):n(new Error("decode failed"))}catch(t){if("string"==typeof t)return n(new Error(t));if(t instanceof Error)return n(new Error(t.message))}})})},r=function(t){var r;return t instanceof File&&"[object File]"===Object.prototype.toString.call(t)?(r=t.slice(),e(r)):"string"==typeof t&&function(t){try{return btoa(atob(t))===t}catch(t){return!1}}(t)?(r=function(t,e,r){void 0===e&&(e=""),void 0===r&&(r=512);for(var n=atob(t),o=[],i=0;i<n.length;i+=r){for(var a=n.slice(i,i+r),c=new Array(a.length),u=0;u<a.length;u++)c[u]=a.charCodeAt(u);var s=new Uint8Array(c);o.push(s)}return new Blob(o,{type:e})}(t),e(r)):"string"==typeof t&&function(t){if("string"!=typeof t)throw new TypeError("Expected a string");if((t=t.trim()).includes(" "))return!1;try{return new URL(t),!0}catch(t){return!1}}(t)?new Promise(function(n,o){var i=new XMLHttpRequest;i.open("GET",t),i.responseType="blob",i.onload=function(){i.status>=200&&i.status<300?e(r=i.response).then(function(t){return n(t)}).catch(function(t){return o(t)}):o(i.statusText)},i.onerror=function(){return o(i.statusText)},i.send()}):Promise.reject(new Error("The input type is invalid"))};export{r as default};
//# sourceMappingURL=qrcodeParser.es.js.map