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