import { PayloadBody } from "./payload" ; import * as PdfJS from 'pdfjs-dist' ; import jsQR , { QRCode } from "jsqr" ; import { decodeData } from "./decode" ; import { Result } from "@zxing/library" ; import { COLORS } from "./colors" ; PdfJS . GlobalWorkerOptions . workerSrc = `https://cdnjs.cloudflare.com/ajax/libs/pdf.js/${ PdfJS . version }/pdf.worker.js` ; export async function getPayloadBodyFromFile ( file , color ) { let imageData ; switch ( file . type ) { case 'application/pdf' : const fileBuffer = await file . arrayBuffer ( ) ; imageData = await getImageDataFromPdf ( fileBuffer ) ; break ; case 'image/png' : case 'image/jpeg' : case 'image/webp' : case 'image/gif' : imageData = await getImageDataFromImage ( file ) ; break ; default : throw Error ( 'invalidFileType' ) ; } let code ; try { code = jsQR ( imageData . data , imageData . width , imageData . height , { inversionAttempts : "dontInvert" , } ) ; } catch ( e ) { throw Error ( 'couldNotDecode' ) ; } if ( code == undefined ) { throw Error ( 'couldNotFindQrCode' ) ; } let rawData = code . data ; let decodedData ; try { decodedData = decodeData ( rawData ) ; } catch ( e ) { throw Error ( 'invalidQrCode' ) ; } return { rawData : rawData , decodedData : decodedData , color : color , } ; } export async function getPayloadBodyFromQR ( qrCodeResult , color ) { let rawData = qrCodeResult . getText ( ) ; let decodedData ; try { decodedData = decodeData ( rawData ) ; } catch ( e ) { throw Error ( "invalidQrCode" ) ; } return { rawData : rawData , decodedData : decodedData , color : color , } ; } function getImageDataFromImage ( file ) { return new Promise ( ( resolve , reject ) => { const canvas = < HTMLCanvasElement > document . getElementById ( 'canvas' ) ; const canvasContext = canvas . getContext ( '2d' ) ; const img = new Image ( ) ; img . onload = ( ) => { const maxPx = 2000000 ; let width ; let height ; if ( img . naturalWidth * img . naturalHeight > maxPx ) { const ratio = img . naturalHeight / img . naturalWidth ; width = Math . sqrt ( maxPx / ratio ) ; height = Math . floor ( width * ratio ) ; width = Math . floor ( width ) ; } else { width = img . naturalWidth ; height = img . naturalHeight ; } canvas . width = width ; canvas . height = height ; canvasContext . clearRect ( 0 , 0 , width , height ) ; canvasContext . drawImage ( img , 0 , 0 , width , height ) ; resolve ( canvasContext . getImageData ( 0 , 0 , width , height ) ) ; } ; img . onerror = ( e ) => { reject ( e ) ; } ; img . src = URL . createObjectURL ( file ) ; } ) ; } async function getImageDataFromPdf ( fileBuffer ) { const typedArray = new Uint8Array ( fileBuffer ) ; const pdfScale = 2 ; const canvas = < HTMLCanvasElement > document . getElementById ( 'canvas' ) ; const canvasContext = canvas . getContext ( '2d' ) ; let loadingTask = PdfJS . getDocument ( typedArray ) ; await loadingTask . promise . then ( async function ( pdfDocument ) { const pageNumber = pdfDocument . numPages ; const pdfPage = await pdfDocument . getPage ( pageNumber ) ; const viewport = pdfPage . getViewport ( { scale : pdfScale } ) ; canvas . width = viewport . width ; canvas . height = viewport . height ; const renderTask = pdfPage . render ( { canvasContext : canvasContext , viewport , } ) ; return await renderTask . promise ; } ) ; return canvasContext . getImageData ( 0 , 0 , canvas . width , canvas . height ) ; }