@openhealthnz-credentials/pdf-image-qr-scanner
Version:
Library to scan user-uploaded PDF's or images locally and reliably for QR codes using jsQR.
3 lines (2 loc) • 1.34 kB
JavaScript
import t from"jsqr";import e from"pdfjs-dist";const{getDocument:n,GlobalWorkerOptions:a,version:o}=e;async function r(t){const e=new Response(t);if("application/pdf"===t.type){const t=new Uint8Array(await e.arrayBuffer()),a=await n(t).promise;return await async function(t){let e=null;for(let n=1;n<=t.numPages;n++){const a=await t.getPage(n),o=document.createElement("canvas");o.height=3508,o.width=2480;const r=a.getViewport({scale:1}),s=Math.min(o.height/r.height,o.width/r.width),c=a.getViewport({scale:s}),h={canvasContext:o.getContext("2d"),viewport:c};if(await a.render(h).promise,e=i(o),null!==e)return e}return null}(a)}{const t=await e.blob();return await async function(t){const e=await async function(t){return new Promise((e,n)=>{const a=new Image;a.onload=()=>e(a),a.onerror=n,a.src=URL.createObjectURL(t)})}(t),n=document.createElement("canvas");for(const t of[.5,1,.25]){n.height=e.height*t,n.width=e.width*t,n.getContext("2d").drawImage(e,0,0,n.width,n.height);const a=i(n);if(a)return a}return null}(t)}}function i(e){const n=e.getContext("2d").getImageData(0,0,e.width,e.height),a=t(n.data,e.width,e.height,{inversionAttempts:"attemptBoth"});return(null==a?void 0:a.data)||null}a.workerSrc=`https://cdnjs.cloudflare.com/ajax/libs/pdf.js/${o}/pdf.worker.min.js`;export{r as scanFile};
//# sourceMappingURL=index.modern.mjs.map