qrcode-parser
Version:
A pure javascript QR code decoding library, accept File object, image url, image base64.
3 lines (2 loc) • 1.72 kB
JavaScript
function e(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var t=/*#__PURE__*/e(require("jsqr")),r=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),u=t.default(a.data,o.width,o.height);return null!==u?r(u.data):n(new Error("decode failed"))}catch(e){if("string"==typeof e)return n(new Error(e));if(e instanceof Error)return n(new Error(e.message))}})})};module.exports=function(e){var t;return e instanceof File&&"[object File]"===Object.prototype.toString.call(e)?(t=e.slice(),r(t)):"string"==typeof e&&function(e){try{return btoa(atob(e))===e}catch(e){return!1}}(e)?(t=function(e,t,r){void 0===t&&(t=""),void 0===r&&(r=512);for(var n=atob(e),o=[],i=0;i<n.length;i+=r){for(var a=n.slice(i,i+r),u=new Array(a.length),c=0;c<a.length;c++)u[c]=a.charCodeAt(c);var s=new Uint8Array(u);o.push(s)}return new Blob(o,{type:t})}(e),r(t)):"string"==typeof e&&function(e){if("string"!=typeof e)throw new TypeError("Expected a string");if((e=e.trim()).includes(" "))return!1;try{return new URL(e),!0}catch(e){return!1}}(e)?new Promise(function(n,o){var i=new XMLHttpRequest;i.open("GET",e),i.responseType="blob",i.onload=function(){i.status>=200&&i.status<300?r(t=i.response).then(function(e){return n(e)}).catch(function(e){return o(e)}):o(i.statusText)},i.onerror=function(){return o(i.statusText)},i.send()}):Promise.reject(new Error("The input type is invalid"))};
//# sourceMappingURL=qrcodeParser.cjs.map