UNPKG

code-craft-studio

Version:

A comprehensive QR code and barcode scanning/generation library for React. Works with or without Capacitor. Supports 22+ QR data types and 14+ barcode formats (EAN, UPC, Code 128, etc.), with customizable designs, analytics, and React components. Provider

1,117 lines (998 loc) 1.9 MB
var CodeCraftStudio = (function (exports, jsxRuntime, React) { 'use strict'; class PlatformDetectorImpl { constructor() { this._isCapacitor = null; } isCapacitor() { if (this._isCapacitor === null) { this._isCapacitor = this.detectCapacitor(); } return this._isCapacitor; } isWeb() { return typeof window !== 'undefined' && typeof document !== 'undefined'; } isNative() { return this.isCapacitor() && (this.isIOS() || this.isAndroid()); } getPlatformName() { if (this.isCapacitor()) { if (this.isIOS()) return 'ios'; if (this.isAndroid()) return 'android'; return 'capacitor-web'; } return 'web'; } detectCapacitor() { if (typeof window === 'undefined') return false; // Check for Capacitor global if ('Capacitor' in window && window.Capacitor) { return true; } // Check for @capacitor/core module dynamically try { // Use dynamic import check if (typeof window !== 'undefined' && window.__capacitor_loaded__) { return true; } } catch (_a) { // Ignore errors } return false; } isIOS() { if (!this.isCapacitor()) return false; // Check if we have Capacitor global if (typeof window !== 'undefined' && window.Capacitor) { return window.Capacitor.getPlatform() === 'ios'; } return false; } isAndroid() { if (!this.isCapacitor()) return false; // Check if we have Capacitor global if (typeof window !== 'undefined' && window.Capacitor) { return window.Capacitor.getPlatform() === 'android'; } return false; } } const platformDetector = new PlatformDetectorImpl(); /** * Barcode formats */ exports.BarcodeFormat = void 0; (function (BarcodeFormat) { // 2D Codes BarcodeFormat["QR_CODE"] = "QR_CODE"; BarcodeFormat["DATA_MATRIX"] = "DATA_MATRIX"; BarcodeFormat["AZTEC"] = "AZTEC"; BarcodeFormat["PDF_417"] = "PDF_417"; BarcodeFormat["MAXICODE"] = "MAXICODE"; // 1D Product Codes BarcodeFormat["EAN_13"] = "EAN_13"; BarcodeFormat["EAN_8"] = "EAN_8"; BarcodeFormat["UPC_A"] = "UPC_A"; BarcodeFormat["UPC_E"] = "UPC_E"; // 1D Industrial Codes BarcodeFormat["CODE_128"] = "CODE_128"; BarcodeFormat["CODE_39"] = "CODE_39"; BarcodeFormat["CODE_93"] = "CODE_93"; BarcodeFormat["CODABAR"] = "CODABAR"; BarcodeFormat["ITF"] = "ITF"; BarcodeFormat["ITF_14"] = "ITF_14"; // Specialty Codes BarcodeFormat["MSI"] = "MSI"; BarcodeFormat["MSI_PLESSEY"] = "MSI_PLESSEY"; BarcodeFormat["PHARMACODE"] = "PHARMACODE"; BarcodeFormat["RSS_14"] = "RSS_14"; BarcodeFormat["RSS_EXPANDED"] = "RSS_EXPANDED"; })(exports.BarcodeFormat || (exports.BarcodeFormat = {})); /** * QR code types */ exports.QRType = void 0; (function (QRType) { QRType["WEBSITE"] = "website"; QRType["PDF"] = "pdf"; QRType["IMAGES"] = "images"; QRType["VIDEO"] = "video"; QRType["WIFI"] = "wifi"; QRType["MENU"] = "menu"; QRType["BUSINESS"] = "business"; QRType["VCARD"] = "vcard"; QRType["MP3"] = "mp3"; QRType["APPS"] = "apps"; QRType["LINKS_LIST"] = "links_list"; QRType["COUPON"] = "coupon"; QRType["FACEBOOK"] = "facebook"; QRType["INSTAGRAM"] = "instagram"; QRType["SOCIAL_MEDIA"] = "social_media"; QRType["WHATSAPP"] = "whatsapp"; QRType["TEXT"] = "text"; QRType["EMAIL"] = "email"; QRType["SMS"] = "sms"; QRType["PHONE"] = "phone"; QRType["LOCATION"] = "location"; QRType["EVENT"] = "event"; })(exports.QRType || (exports.QRType = {})); exports.Directory = void 0; (function (Directory) { Directory["Documents"] = "DOCUMENTS"; Directory["Downloads"] = "DOWNLOADS"; Directory["External"] = "EXTERNAL"; Directory["ExternalStorage"] = "EXTERNAL_STORAGE"; })(exports.Directory || (exports.Directory = {})); /** * Logger utility for Code Craft Studio * Provides consistent logging with configurable levels */ class Logger { constructor() { this.config = { level: 'warn', prefix: '[CodeCraftStudio]', enabled: true, }; this.levels = { debug: 0, info: 1, warn: 2, error: 3, none: 4, }; } configure(config) { this.config = Object.assign(Object.assign({}, this.config), config); } shouldLog(level) { if (!this.config.enabled || this.config.level === 'none') { return false; } return this.levels[level] >= this.levels[this.config.level]; } formatMessage(level, message) { const timestamp = new Date().toISOString(); const prefix = this.config.prefix || ''; return `${timestamp} ${prefix} [${level.toUpperCase()}] ${message}`; } debug(message, ...args) { if (this.shouldLog('debug')) { console.debug(this.formatMessage('debug', message), ...args); } } info(message, ...args) { if (this.shouldLog('info')) { console.info(this.formatMessage('info', message), ...args); } } warn(message, ...args) { if (this.shouldLog('warn')) { console.warn(this.formatMessage('warn', message), ...args); } } error(message, error, ...args) { if (this.shouldLog('error')) { if (error instanceof Error) { console.error(this.formatMessage('error', message), error.message, error.stack, ...args); } else if (error) { console.error(this.formatMessage('error', message), error, ...args); } else { console.error(this.formatMessage('error', message), ...args); } } } /** * Sets the log level based on environment * In production, defaults to 'error' only * In development, defaults to 'warn' */ setEnvironmentLevel() { const isProduction = process.env.NODE_ENV === 'production'; this.configure({ level: isProduction ? 'error' : 'warn', }); } } // Create singleton instance const logger = new Logger(); // Configure based on environment on import if (typeof process !== 'undefined' && process.env) { logger.setEnvironmentLevel(); } class WebStorageAdapter { constructor(useSessionStorage = false, prefix = 'code-craft-studio-') { this.storage = useSessionStorage ? sessionStorage : localStorage; this.prefix = prefix; } async get(key) { try { return this.storage.getItem(this.prefix + key); } catch (error) { logger.error('WebStorageAdapter: Error getting item', error); return null; } } async set(key, value) { try { this.storage.setItem(this.prefix + key, value); } catch (error) { logger.error('WebStorageAdapter: Error setting item', error); throw error; } } async remove(key) { try { this.storage.removeItem(this.prefix + key); } catch (error) { logger.error('WebStorageAdapter: Error removing item', error); throw error; } } async clear() { try { const keysToRemove = []; for (let i = 0; i < this.storage.length; i++) { const key = this.storage.key(i); if (key && key.startsWith(this.prefix)) { keysToRemove.push(key); } } keysToRemove.forEach(key => this.storage.removeItem(key)); } catch (error) { logger.error('WebStorageAdapter: Error clearing storage', error); throw error; } } } class e{constructor(a,b,c,d,f){this._legacyCanvasSize=e.DEFAULT_CANVAS_SIZE;this._preferredCamera="environment";this._maxScansPerSecond=25;this._lastScanTimestamp=-1;this._destroyed=this._flashOn=this._paused=this._active=false;this.$video=a;this.$canvas=document.createElement("canvas");c&&"object"===typeof c?this._onDecode=b:(c||d||f?console.warn("You're using a deprecated version of the QrScanner constructor which will be removed in the future"):console.warn("Note that the type of the scan result passed to onDecode will change in the future. To already switch to the new api today, you can pass returnDetailedScanResult: true."), this._legacyOnDecode=b);b="object"===typeof c?c:{};this._onDecodeError=b.onDecodeError||("function"===typeof c?c:this._onDecodeError);this._calculateScanRegion=b.calculateScanRegion||("function"===typeof d?d:this._calculateScanRegion);this._preferredCamera=b.preferredCamera||f||this._preferredCamera;this._legacyCanvasSize="number"===typeof c?c:"number"===typeof d?d:this._legacyCanvasSize;this._maxScansPerSecond=b.maxScansPerSecond||this._maxScansPerSecond;this._onPlay=this._onPlay.bind(this);this._onLoadedMetaData= this._onLoadedMetaData.bind(this);this._onVisibilityChange=this._onVisibilityChange.bind(this);this._updateOverlay=this._updateOverlay.bind(this);a.disablePictureInPicture=true;a.playsInline=true;a.muted=true;let h=false;a.hidden&&(a.hidden=false,h=true);document.body.contains(a)||(document.body.appendChild(a),h=true);c=a.parentElement;if(b.highlightScanRegion||b.highlightCodeOutline){d=!!b.overlay;this.$overlay=b.overlay||document.createElement("div");f=this.$overlay.style;f.position="absolute";f.display="none"; f.pointerEvents="none";this.$overlay.classList.add("scan-region-highlight");if(!d&&b.highlightScanRegion){this.$overlay.innerHTML='<svg class="scan-region-highlight-svg" viewBox="0 0 238 238" preserveAspectRatio="none" style="position:absolute;width:100%;height:100%;left:0;top:0;fill:none;stroke:#e9b213;stroke-width:4;stroke-linecap:round;stroke-linejoin:round"><path d="M31 2H10a8 8 0 0 0-8 8v21M207 2h21a8 8 0 0 1 8 8v21m0 176v21a8 8 0 0 1-8 8h-21m-176 0H10a8 8 0 0 1-8-8v-21"/></svg>';try{this.$overlay.firstElementChild.animate({transform:["scale(.98)", "scale(1.01)"]},{duration:400,iterations:Infinity,direction:"alternate",easing:"ease-in-out"});}catch(m){}c.insertBefore(this.$overlay,this.$video.nextSibling);}b.highlightCodeOutline&&(this.$overlay.insertAdjacentHTML("beforeend",'<svg class="code-outline-highlight" preserveAspectRatio="none" style="display:none;width:100%;height:100%;fill:none;stroke:#e9b213;stroke-width:5;stroke-dasharray:25;stroke-linecap:round;stroke-linejoin:round"><polygon/></svg>'),this.$codeOutlineHighlight=this.$overlay.lastElementChild);}this._scanRegion= this._calculateScanRegion(a);requestAnimationFrame(()=>{let m=window.getComputedStyle(a);"none"===m.display&&(a.style.setProperty("display","block","important"),h=true);"visible"!==m.visibility&&(a.style.setProperty("visibility","visible","important"),h=true);h&&(console.warn("QrScanner has overwritten the video hiding style to avoid Safari stopping the playback."),a.style.opacity="0",a.style.width="0",a.style.height="0",this.$overlay&&this.$overlay.parentElement&&this.$overlay.parentElement.removeChild(this.$overlay), delete this.$overlay,delete this.$codeOutlineHighlight);this.$overlay&&this._updateOverlay();});a.addEventListener("play",this._onPlay);a.addEventListener("loadedmetadata",this._onLoadedMetaData);document.addEventListener("visibilitychange",this._onVisibilityChange);window.addEventListener("resize",this._updateOverlay);this._qrEnginePromise=e.createQrEngine();}static set WORKER_PATH(a){console.warn("Setting QrScanner.WORKER_PATH is not required and not supported anymore. Have a look at the README for new setup instructions.");}static async hasCamera(){try{return !!(await e.listCameras(!1)).length}catch(a){return false}}static async listCameras(a= false){if(!navigator.mediaDevices)return [];let b=async()=>(await navigator.mediaDevices.enumerateDevices()).filter(d=>"videoinput"===d.kind),c;try{a&&(await b()).every(d=>!d.label)&&(c=await navigator.mediaDevices.getUserMedia({audio:!1,video:!0}));}catch(d){}try{return (await b()).map((d,f)=>({id:d.deviceId,label:d.label||(0===f?"Default Camera":`Camera ${f+1}`)}))}finally{c&&(console.warn("Call listCameras after successfully starting a QR scanner to avoid creating a temporary video stream"),e._stopVideoStream(c));}}async hasFlash(){let a; try{if(this.$video.srcObject){if(!(this.$video.srcObject instanceof MediaStream))return !1;a=this.$video.srcObject;}else a=(await this._getCameraStream()).stream;return "torch"in a.getVideoTracks()[0].getSettings()}catch(b){return false}finally{a&&a!==this.$video.srcObject&&(console.warn("Call hasFlash after successfully starting the scanner to avoid creating a temporary video stream"),e._stopVideoStream(a));}}isFlashOn(){return this._flashOn}async toggleFlash(){this._flashOn?await this.turnFlashOff():await this.turnFlashOn();}async turnFlashOn(){if(!this._flashOn&& !this._destroyed&&(this._flashOn=true,this._active&&!this._paused))try{if(!await this.hasFlash())throw "No flash available";await this.$video.srcObject.getVideoTracks()[0].applyConstraints({advanced:[{torch:!0}]});}catch(a){throw this._flashOn=false,a;}}async turnFlashOff(){this._flashOn&&(this._flashOn=false,await this._restartVideoStream());}destroy(){this.$video.removeEventListener("loadedmetadata",this._onLoadedMetaData);this.$video.removeEventListener("play",this._onPlay);document.removeEventListener("visibilitychange", this._onVisibilityChange);window.removeEventListener("resize",this._updateOverlay);this._destroyed=true;this._flashOn=false;this.stop();e._postWorkerMessage(this._qrEnginePromise,"close");}async start(){if(this._destroyed)throw Error("The QR scanner can not be started as it had been destroyed.");if(!this._active||this._paused)if("https:"!==window.location.protocol&&console.warn("The camera stream is only accessible if the page is transferred via https."),this._active=true,!document.hidden)if(this._paused= false,this.$video.srcObject)await this.$video.play();else try{let {stream:a,facingMode:b}=await this._getCameraStream();!this._active||this._paused?e._stopVideoStream(a):(this._setVideoMirror(b),this.$video.srcObject=a,await this.$video.play(),this._flashOn&&(this._flashOn=!1,this.turnFlashOn().catch(()=>{})));}catch(a){if(!this._paused)throw this._active=false,a;}}stop(){this.pause();this._active=false;}async pause(a=false){this._paused=true;if(!this._active)return true;this.$video.pause();this.$overlay&&(this.$overlay.style.display= "none");let b=()=>{this.$video.srcObject instanceof MediaStream&&(e._stopVideoStream(this.$video.srcObject),this.$video.srcObject=null);};if(a)return b(),true;await new Promise(c=>setTimeout(c,300));if(!this._paused)return false;b();return true}async setCamera(a){a!==this._preferredCamera&&(this._preferredCamera=a,await this._restartVideoStream());}static async scanImage(a,b,c,d,f=false,h=false){let m,n=false;b&&("scanRegion"in b||"qrEngine"in b||"canvas"in b||"disallowCanvasResizing"in b||"alsoTryWithoutScanRegion"in b||"returnDetailedScanResult"in b)?(m=b.scanRegion,c=b.qrEngine,d=b.canvas,f=b.disallowCanvasResizing||false,h=b.alsoTryWithoutScanRegion||false,n=true):b||c||d||f||h?console.warn("You're using a deprecated api for scanImage which will be removed in the future."):console.warn("Note that the return type of scanImage will change in the future. To already switch to the new api today, you can pass returnDetailedScanResult: true.");b=!!c;try{let p,k;[c,p]=await Promise.all([c||e.createQrEngine(),e._loadImage(a)]); [d,k]=e._drawToCanvas(p,m,d,f);let q;if(c instanceof Worker){let g=c;b||e._postWorkerMessageSync(g,"inversionMode","both");q=await new Promise((l,v)=>{let w,u,r,y=-1;u=t=>{t.data.id===y&&(g.removeEventListener("message",u),g.removeEventListener("error",r),clearTimeout(w),null!==t.data.data?l({data:t.data.data,cornerPoints:e._convertPoints(t.data.cornerPoints,m)}):v(e.NO_QR_CODE_FOUND));};r=t=>{g.removeEventListener("message",u);g.removeEventListener("error",r);clearTimeout(w);v("Scanner error: "+(t? t.message||t:"Unknown Error"));};g.addEventListener("message",u);g.addEventListener("error",r);w=setTimeout(()=>r("timeout"),1E4);let x=k.getImageData(0,0,d.width,d.height);y=e._postWorkerMessageSync(g,"decode",x,[x.data.buffer]);});}else q=await Promise.race([new Promise((g,l)=>window.setTimeout(()=>l("Scanner error: timeout"),1E4)),(async()=>{try{var [g]=await c.detect(d);if(!g)throw e.NO_QR_CODE_FOUND;return {data:g.rawValue,cornerPoints:e._convertPoints(g.cornerPoints,m)}}catch(l){g=l.message||l; if(/not implemented|service unavailable/.test(g))return e._disableBarcodeDetector=!0,e.scanImage(a,{scanRegion:m,canvas:d,disallowCanvasResizing:f,alsoTryWithoutScanRegion:h});throw `Scanner error: ${g}`;}})()]);return n?q:q.data}catch(p){if(!m||!h)throw p;let k=await e.scanImage(a,{qrEngine:c,canvas:d,disallowCanvasResizing:f});return n?k:k.data}finally{b||e._postWorkerMessage(c,"close");}}setGrayscaleWeights(a,b,c,d=true){e._postWorkerMessage(this._qrEnginePromise,"grayscaleWeights",{red:a,green:b, blue:c,useIntegerApproximation:d});}setInversionMode(a){e._postWorkerMessage(this._qrEnginePromise,"inversionMode",a);}static async createQrEngine(a){a&&console.warn("Specifying a worker path is not required and not supported anymore.");a=()=>Promise.resolve().then(function () { return qrScannerWorker_min; }).then(c=>c.createWorker());if(!(!e._disableBarcodeDetector&&"BarcodeDetector"in window&&BarcodeDetector.getSupportedFormats&&(await BarcodeDetector.getSupportedFormats()).includes("qr_code")))return a();let b=navigator.userAgentData; return b&&b.brands.some(({brand:c})=>/Chromium/i.test(c))&&/mac ?OS/i.test(b.platform)&&await b.getHighEntropyValues(["architecture","platformVersion"]).then(({architecture:c,platformVersion:d})=>/arm/i.test(c||"arm")&&13<=parseInt(d||"13")).catch(()=>true)?a():new BarcodeDetector({formats:["qr_code"]})}_onPlay(){this._scanRegion=this._calculateScanRegion(this.$video);this._updateOverlay();this.$overlay&&(this.$overlay.style.display="");this._scanFrame();}_onLoadedMetaData(){this._scanRegion=this._calculateScanRegion(this.$video); this._updateOverlay();}_onVisibilityChange(){document.hidden?this.pause():this._active&&this.start();}_calculateScanRegion(a){let b=Math.round(2/3*Math.min(a.videoWidth,a.videoHeight));return {x:Math.round((a.videoWidth-b)/2),y:Math.round((a.videoHeight-b)/2),width:b,height:b,downScaledWidth:this._legacyCanvasSize,downScaledHeight:this._legacyCanvasSize}}_updateOverlay(){requestAnimationFrame(()=>{if(this.$overlay){var a=this.$video,b=a.videoWidth,c=a.videoHeight,d=a.offsetWidth,f=a.offsetHeight,h=a.offsetLeft, m=a.offsetTop,n=window.getComputedStyle(a),p=n.objectFit,k=b/c,q=d/f;switch(p){case "none":var g=b;var l=c;break;case "fill":g=d;l=f;break;default:("cover"===p?k>q:k<q)?(l=f,g=l*k):(g=d,l=g/k),"scale-down"===p&&(g=Math.min(g,b),l=Math.min(l,c));}var [v,w]=n.objectPosition.split(" ").map((r,y)=>{const x=parseFloat(r);return r.endsWith("%")?(y?f-l:d-g)*x/100:x});n=this._scanRegion.width||b;q=this._scanRegion.height||c;p=this._scanRegion.x||0;var u=this._scanRegion.y||0;k=this.$overlay.style;k.width= `${n/b*g}px` ;k.height=`${q/c*l}px`;k.top=`${m+w+u/c*l}px`;c=/scaleX\(-1\)/.test(a.style.transform);k.left=`${h+(c?d-v-g:v)+(c?b-p-n:p)/b*g}px`;k.transform=a.style.transform;}});}static _convertPoints(a,b){if(!b)return a;let c=b.x||0,d=b.y||0,f=b.width&&b.downScaledWidth?b.width/b.downScaledWidth:1;b=b.height&&b.downScaledHeight?b.height/b.downScaledHeight:1;for(let h of a)h.x=h.x*f+c,h.y=h.y*b+d;return a}_scanFrame(){!this._active||this.$video.paused||this.$video.ended||("requestVideoFrameCallback"in this.$video?this.$video.requestVideoFrameCallback.bind(this.$video):requestAnimationFrame)(async()=>{if(!(1>=this.$video.readyState)){var a=Date.now()-this._lastScanTimestamp,b=1E3/this._maxScansPerSecond;a<b&&await new Promise(d=>setTimeout(d,b-a));this._lastScanTimestamp=Date.now();try{var c=await e.scanImage(this.$video,{scanRegion:this._scanRegion,qrEngine:this._qrEnginePromise,canvas:this.$canvas});}catch(d){if(!this._active)return;this._onDecodeError(d);}!e._disableBarcodeDetector||await this._qrEnginePromise instanceof Worker||(this._qrEnginePromise=e.createQrEngine());c?(this._onDecode?this._onDecode(c):this._legacyOnDecode&&this._legacyOnDecode(c.data),this.$codeOutlineHighlight&&(clearTimeout(this._codeOutlineHighlightRemovalTimeout),this._codeOutlineHighlightRemovalTimeout=void 0,this.$codeOutlineHighlight.setAttribute("viewBox",`${this._scanRegion.x||0} `+`${this._scanRegion.y||0} `+`${this._scanRegion.width||this.$video.videoWidth} `+`${this._scanRegion.height||this.$video.videoHeight}`),this.$codeOutlineHighlight.firstElementChild.setAttribute("points", c.cornerPoints.map(({x:d,y:f})=>`${d},${f}`).join(" ")),this.$codeOutlineHighlight.style.display="")):this.$codeOutlineHighlight&&!this._codeOutlineHighlightRemovalTimeout&&(this._codeOutlineHighlightRemovalTimeout=setTimeout(()=>this.$codeOutlineHighlight.style.display="none",100));}this._scanFrame();});}_onDecodeError(a){a!==e.NO_QR_CODE_FOUND&&console.log(a);}async _getCameraStream(){if(!navigator.mediaDevices)throw "Camera not found.";let a=/^(environment|user)$/.test(this._preferredCamera)?"facingMode": "deviceId",b=[{width:{min:1024}},{width:{min:768}},{}],c=b.map(d=>Object.assign({},d,{[a]:{exact:this._preferredCamera}}));for(let d of [...c,...b])try{let f=await navigator.mediaDevices.getUserMedia({video:d,audio:!1}),h=this._getFacingMode(f)||(d.facingMode?this._preferredCamera:"environment"===this._preferredCamera?"user":"environment");return {stream:f,facingMode:h}}catch(f){}throw "Camera not found.";}async _restartVideoStream(){let a=this._paused;await this.pause(true)&&!a&&this._active&&await this.start();}static _stopVideoStream(a){for(let b of a.getTracks())b.stop(), a.removeTrack(b);}_setVideoMirror(a){this.$video.style.transform="scaleX("+("user"===a?-1:1)+")";}_getFacingMode(a){return (a=a.getVideoTracks()[0])?/rear|back|environment/i.test(a.label)?"environment":/front|user|face/i.test(a.label)?"user":null:null}static _drawToCanvas(a,b,c,d=false){c=c||document.createElement("canvas");let f=b&&b.x?b.x:0,h=b&&b.y?b.y:0,m=b&&b.width?b.width:a.videoWidth||a.width,n=b&&b.height?b.height:a.videoHeight||a.height;d||(d=b&&b.downScaledWidth?b.downScaledWidth:m,b=b&&b.downScaledHeight? b.downScaledHeight:n,c.width!==d&&(c.width=d),c.height!==b&&(c.height=b));b=c.getContext("2d",{alpha:false});b.imageSmoothingEnabled=false;b.drawImage(a,f,h,m,n,0,0,c.width,c.height);return [c,b]}static async _loadImage(a){if(a instanceof Image)return await e._awaitImageLoad(a),a;if(a instanceof HTMLVideoElement||a instanceof HTMLCanvasElement||a instanceof SVGImageElement||"OffscreenCanvas"in window&&a instanceof OffscreenCanvas||"ImageBitmap"in window&&a instanceof ImageBitmap)return a;if(a instanceof File||a instanceof Blob||a instanceof URL||"string"===typeof a){let b=new Image;b.src=a instanceof File||a instanceof Blob?URL.createObjectURL(a):a.toString();try{return await e._awaitImageLoad(b),b}finally{(a instanceof File||a instanceof Blob)&&URL.revokeObjectURL(b.src);}}else throw "Unsupported image type.";}static async _awaitImageLoad(a){a.complete&&0!==a.naturalWidth||await new Promise((b,c)=>{let d=f=>{a.removeEventListener("load",d);a.removeEventListener("error",d);f instanceof ErrorEvent? c("Image load error"):b();};a.addEventListener("load",d);a.addEventListener("error",d);});}static async _postWorkerMessage(a,b,c,d){return e._postWorkerMessageSync(await a,b,c,d)}static _postWorkerMessageSync(a,b,c,d){if(!(a instanceof Worker))return -1;let f=e._workerMessageId++;a.postMessage({id:f,type:b,data:c},d);return f}}e.DEFAULT_CANVAS_SIZE=400;e.NO_QR_CODE_FOUND="No QR code found";e._disableBarcodeDetector=false;e._workerMessageId=0; function getDefaultExportFromCjs (x) { return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x; } var browser = {}; var canPromise; var hasRequiredCanPromise; function requireCanPromise () { if (hasRequiredCanPromise) return canPromise; hasRequiredCanPromise = 1; // can-promise has a crash in some versions of react native that dont have // standard global objects // https://github.com/soldair/node-qrcode/issues/157 canPromise = function () { return typeof Promise === 'function' && Promise.prototype && Promise.prototype.then }; return canPromise; } var qrcode = {}; var utils$1 = {}; var hasRequiredUtils$1; function requireUtils$1 () { if (hasRequiredUtils$1) return utils$1; hasRequiredUtils$1 = 1; let toSJISFunction; const CODEWORDS_COUNT = [ 0, // Not used 26, 44, 70, 100, 134, 172, 196, 242, 292, 346, 404, 466, 532, 581, 655, 733, 815, 901, 991, 1085, 1156, 1258, 1364, 1474, 1588, 1706, 1828, 1921, 2051, 2185, 2323, 2465, 2611, 2761, 2876, 3034, 3196, 3362, 3532, 3706 ]; /** * Returns the QR Code size for the specified version * * @param {Number} version QR Code version * @return {Number} size of QR code */ utils$1.getSymbolSize = function getSymbolSize (version) { if (!version) throw new Error('"version" cannot be null or undefined') if (version < 1 || version > 40) throw new Error('"version" should be in range from 1 to 40') return version * 4 + 17 }; /** * Returns the total number of codewords used to store data and EC information. * * @param {Number} version QR Code version * @return {Number} Data length in bits */ utils$1.getSymbolTotalCodewords = function getSymbolTotalCodewords (version) { return CODEWORDS_COUNT[version] }; /** * Encode data with Bose-Chaudhuri-Hocquenghem * * @param {Number} data Value to encode * @return {Number} Encoded value */ utils$1.getBCHDigit = function (data) { let digit = 0; while (data !== 0) { digit++; data >>>= 1; } return digit }; utils$1.setToSJISFunction = function setToSJISFunction (f) { if (typeof f !== 'function') { throw new Error('"toSJISFunc" is not a valid function.') } toSJISFunction = f; }; utils$1.isKanjiModeEnabled = function () { return typeof toSJISFunction !== 'undefined' }; utils$1.toSJIS = function toSJIS (kanji) { return toSJISFunction(kanji) }; return utils$1; } var errorCorrectionLevel = {}; var hasRequiredErrorCorrectionLevel; function requireErrorCorrectionLevel () { if (hasRequiredErrorCorrectionLevel) return errorCorrectionLevel; hasRequiredErrorCorrectionLevel = 1; (function (exports) { exports.L = { bit: 1 }; exports.M = { bit: 0 }; exports.Q = { bit: 3 }; exports.H = { bit: 2 }; function fromString (string) { if (typeof string !== 'string') { throw new Error('Param is not a string') } const lcStr = string.toLowerCase(); switch (lcStr) { case 'l': case 'low': return exports.L case 'm': case 'medium': return exports.M case 'q': case 'quartile': return exports.Q case 'h': case 'high': return exports.H default: throw new Error('Unknown EC Level: ' + string) } } exports.isValid = function isValid (level) { return level && typeof level.bit !== 'undefined' && level.bit >= 0 && level.bit < 4 }; exports.from = function from (value, defaultValue) { if (exports.isValid(value)) { return value } try { return fromString(value) } catch (e) { return defaultValue } }; } (errorCorrectionLevel)); return errorCorrectionLevel; } var bitBuffer; var hasRequiredBitBuffer; function requireBitBuffer () { if (hasRequiredBitBuffer) return bitBuffer; hasRequiredBitBuffer = 1; function BitBuffer () { this.buffer = []; this.length = 0; } BitBuffer.prototype = { get: function (index) { const bufIndex = Math.floor(index / 8); return ((this.buffer[bufIndex] >>> (7 - index % 8)) & 1) === 1 }, put: function (num, length) { for (let i = 0; i < length; i++) { this.putBit(((num >>> (length - i - 1)) & 1) === 1); } }, getLengthInBits: function () { return this.length }, putBit: function (bit) { const bufIndex = Math.floor(this.length / 8); if (this.buffer.length <= bufIndex) { this.buffer.push(0); } if (bit) { this.buffer[bufIndex] |= (0x80 >>> (this.length % 8)); } this.length++; } }; bitBuffer = BitBuffer; return bitBuffer; } /** * Helper class to handle QR Code symbol modules * * @param {Number} size Symbol size */ var bitMatrix; var hasRequiredBitMatrix; function requireBitMatrix () { if (hasRequiredBitMatrix) return bitMatrix; hasRequiredBitMatrix = 1; function BitMatrix (size) { if (!size || size < 1) { throw new Error('BitMatrix size must be defined and greater than 0') } this.size = size; this.data = new Uint8Array(size * size); this.reservedBit = new Uint8Array(size * size); } /** * Set bit value at specified location * If reserved flag is set, this bit will be ignored during masking process * * @param {Number} row * @param {Number} col * @param {Boolean} value * @param {Boolean} reserved */ BitMatrix.prototype.set = function (row, col, value, reserved) { const index = row * this.size + col; this.data[index] = value; if (reserved) this.reservedBit[index] = true; }; /** * Returns bit value at specified location * * @param {Number} row * @param {Number} col * @return {Boolean} */ BitMatrix.prototype.get = function (row, col) { return this.data[row * this.size + col] }; /** * Applies xor operator at specified location * (used during masking process) * * @param {Number} row * @param {Number} col * @param {Boolean} value */ BitMatrix.prototype.xor = function (row, col, value) { this.data[row * this.size + col] ^= value; }; /** * Check if bit at specified location is reserved * * @param {Number} row * @param {Number} col * @return {Boolean} */ BitMatrix.prototype.isReserved = function (row, col) { return this.reservedBit[row * this.size + col] }; bitMatrix = BitMatrix; return bitMatrix; } var alignmentPattern = {}; /** * Alignment pattern are fixed reference pattern in defined positions * in a matrix symbology, which enables the decode software to re-synchronise * the coordinate mapping of the image modules in the event of moderate amounts * of distortion of the image. * * Alignment patterns are present only in QR Code symbols of version 2 or larger * and their number depends on the symbol version. */ var hasRequiredAlignmentPattern; function requireAlignmentPattern () { if (hasRequiredAlignmentPattern) return alignmentPattern; hasRequiredAlignmentPattern = 1; (function (exports) { const getSymbolSize = requireUtils$1().getSymbolSize; /** * Calculate the row/column coordinates of the center module of each alignment pattern * for the specified QR Code version. * * The alignment patterns are positioned symmetrically on either side of the diagonal * running from the top left corner of the symbol to the bottom right corner. * * Since positions are simmetrical only half of the coordinates are returned. * Each item of the array will represent in turn the x and y coordinate. * @see {@link getPositions} * * @param {Number} version QR Code version * @return {Array} Array of coordinate */ exports.getRowColCoords = function getRowColCoords (version) { if (version === 1) return [] const posCount = Math.floor(version / 7) + 2; const size = getSymbolSize(version); const intervals = size === 145 ? 26 : Math.ceil((size - 13) / (2 * posCount - 2)) * 2; const positions = [size - 7]; // Last coord is always (size - 7) for (let i = 1; i < posCount - 1; i++) { positions[i] = positions[i - 1] - intervals; } positions.push(6); // First coord is always 6 return positions.reverse() }; /** * Returns an array containing the positions of each alignment pattern. * Each array's element represent the center point of the pattern as (x, y) coordinates * * Coordinates are calculated expanding the row/column coordinates returned by {@link getRowColCoords} * and filtering out the items that overlaps with finder pattern * * @example * For a Version 7 symbol {@link getRowColCoords} returns values 6, 22 and 38. * The alignment patterns, therefore, are to be centered on (row, column) * positions (6,22), (22,6), (22,22), (22,38), (38,22), (38,38). * Note that the coordinates (6,6), (6,38), (38,6) are occupied by finder patterns * and are not therefore used for alignment patterns. * * let pos = getPositions(7) * // [[6,22], [22,6], [22,22], [22,38], [38,22], [38,38]] * * @param {Number} version QR Code version * @return {Array} Array of coordinates */ exports.getPositions = function getPositions (version) { const coords = []; const pos = exports.getRowColCoords(version); const posLength = pos.length; for (let i = 0; i < posLength; i++) { for (let j = 0; j < posLength; j++) { // Skip if position is occupied by finder patterns if ((i === 0 && j === 0) || // top-left (i === 0 && j === posLength - 1) || // bottom-left (i === posLength - 1 && j === 0)) { // top-right continue } coords.push([pos[i], pos[j]]); } } return coords }; } (alignmentPattern)); return alignmentPattern; } var finderPattern = {}; var hasRequiredFinderPattern; function requireFinderPattern () { if (hasRequiredFinderPattern) return finderPattern; hasRequiredFinderPattern = 1; const getSymbolSize = requireUtils$1().getSymbolSize; const FINDER_PATTERN_SIZE = 7; /** * Returns an array containing the positions of each finder pattern. * Each array's element represent the top-left point of the pattern as (x, y) coordinates * * @param {Number} version QR Code version * @return {Array} Array of coordinates */ finderPattern.getPositions = function getPositions (version) { const size = getSymbolSize(version); return [ // top-left [0, 0], // top-right [size - FINDER_PATTERN_SIZE, 0], // bottom-left [0, size - FINDER_PATTERN_SIZE] ] }; return finderPattern; } var maskPattern = {}; /** * Data mask pattern reference * @type {Object} */ var hasRequiredMaskPattern; function requireMaskPattern () { if (hasRequiredMaskPattern) return maskPattern; hasRequiredMaskPattern = 1; (function (exports) { exports.Patterns = { PATTERN000: 0, PATTERN001: 1, PATTERN010: 2, PATTERN011: 3, PATTERN100: 4, PATTERN101: 5, PATTERN110: 6, PATTERN111: 7 }; /** * Weighted penalty scores for the undesirable features * @type {Object} */ const PenaltyScores = { N1: 3, N2: 3, N3: 40, N4: 10 }; /** * Check if mask pattern value is valid * * @param {Number} mask Mask pattern * @return {Boolean} true if valid, false otherwise */ exports.isValid = function isValid (mask) { return mask != null && mask !== '' && !isNaN(mask) && mask >= 0 && mask <= 7 }; /** * Returns mask pattern from a value. * If value is not valid, returns undefined * * @param {Number|String} value Mask pattern value * @return {Number} Valid mask pattern or undefined */ exports.from = function from (value) { return exports.isValid(value) ? parseInt(value, 10) : undefined }; /** * Find adjacent modules in row/column with the same color * and assign a penalty value. * * Points: N1 + i * i is the amount by which the number of adjacent modules of the same color exceeds 5 */ exports.getPenaltyN1 = function getPenaltyN1 (data) { const size = data.size; let points = 0; let sameCountCol = 0; let sameCountRow = 0; let lastCol = null; let lastRow = null; for (let row = 0; row < size; row++) { sameCountCol = sameCountRow = 0; lastCol = lastRow = null; for (let col = 0; col < size; col++) { let module = data.get(row, col); if (module === lastCol) { sameCountCol++; } else { if (sameCountCol >= 5) points += PenaltyScores.N1 + (sameCountCol - 5); lastCol = module; sameCountCol = 1; } module = data.get(col, row); if (module === lastRow) { sameCountRow++; } else { if (sameCountRow >= 5) points += PenaltyScores.N1 + (sameCountRow - 5); lastRow = module; sameCountRow = 1; } } if (sameCountCol >= 5) points += PenaltyScores.N1 + (sameCountCol - 5); if (sameCountRow >= 5) points += PenaltyScores.N1 + (sameCountRow - 5); } return points }; /** * Find 2x2 blocks with the same color and assign a penalty value * * Points: N2 * (m - 1) * (n - 1) */ exports.getPenaltyN2 = function getPenaltyN2 (data) { const size = data.size; let points = 0; for (let row = 0; row < size - 1; row++) { for (let col = 0; col < size - 1; col++) { const last = data.get(row, col) + data.get(row, col + 1) + data.get(row + 1, col) + data.get(row + 1, col + 1); if (last === 4 || last === 0) points++; } } return points * PenaltyScores.N2 }; /** * Find 1:1:3:1:1 ratio (dark:light:dark:light:dark) pattern in row/column, * preceded or followed by light area 4 modules wide * * Points: N3 * number of pattern found */ exports.getPenaltyN3 = function getPenaltyN3 (data) { const size = data.size; let points = 0; let bitsCol = 0; let bitsRow = 0; for (let row = 0; row < size; row++) { bitsCol = bitsRow = 0; for (let col = 0; col < size; col++) { bitsCol = ((bitsCol << 1) & 0x7FF) | data.get(row, col); if (col >= 10 && (bitsCol === 0x5D0 || bitsCol === 0x05D)) points++; bitsRow = ((bitsRow << 1) & 0x7FF) | data.get(col, row); if (col >= 10 && (bitsRow === 0x5D0 || bitsRow === 0x05D)) points++; } } return points * PenaltyScores.N3 }; /** * Calculate proportion of dark modules in entire symbol * * Points: N4 * k * * k is the rating of the deviation of the proportion of dark modules * in the symbol from 50% in steps of 5% */ exports.getPenaltyN4 = function getPenaltyN4 (data) { let darkCount = 0; const modulesCount = data.data.length; for (let i = 0; i < modulesCount; i++) darkCount += data.data[i]; const k = Math.abs(Math.ceil((darkCount * 100 / modulesCount) / 5) - 10); return k * PenaltyScores.N4 }; /** * Return mask value at given position * * @param {Number} maskPattern Pattern reference value * @param {Number} i Row * @param {Number} j Column * @return {Boolean} Mask value */ function getMaskAt (maskPattern, i, j) { switch (maskPattern) { case exports.Patterns.PATTERN000: return (i + j) % 2 === 0 case exports.Patterns.PATTERN001: return i % 2 === 0 case exports.Patterns.PATTERN010: return j % 3 === 0 case exports.Patterns.PATTERN011: return (i + j) % 3 === 0 case exports.Patterns.PATTERN100: return (Math.floor(i / 2) + Math.floor(j / 3)) % 2 === 0 case exports.Patterns.PATTERN101: return (i * j) % 2 + (i * j) % 3 === 0 case exports.Patterns.PATTERN110: return ((i * j) % 2 + (i * j) % 3) % 2 === 0 case exports.Patterns.PATTERN111: return ((i * j) % 3 + (i + j) % 2) % 2 === 0 default: throw new Error('bad maskPattern:' + maskPattern) } } /** * Apply a mask pattern to a BitMatrix * * @param {Number} pattern Pattern reference number * @param {BitMatrix} data BitMatrix data */ exports.applyMask = function applyMask (pattern, data) { const size = data.size; for (let col = 0; col < size; col++) { for (let row = 0; row < size; row++) { if (data.isReserved(row, col)) continue data.xor(row, col, getMaskAt(pattern, row, col)); } } }; /** * Returns the best mask pattern for data * * @param {BitMatrix} data * @return {Number} Mask pattern reference number */ exports.getBestMask = function getBestMask (data, setupFormatFunc) { const numPatterns = Object.keys(exports.Patterns).length; let bestPattern = 0; let lowerPenalty = Infinity; for (let p = 0; p < numPatterns; p++) { setupFormatFunc(p); exports.applyMask(p, data); // Calculate penalty const penalty = exports.getPenaltyN1(data) + exports.getPenaltyN2(data) + exports.getPenaltyN3(data) + exports.getPenaltyN4(data); // Undo previously applied mask exports.applyMask(p, data); if (penalty < lowerPenalty) { lowerPenalty = penalty; bestPattern = p; } } return bestPattern }; } (maskPattern)); return maskPattern; } var errorCorrectionCode = {}; var hasRequiredErrorCorrectionCode; function requireErrorCorrectionCode () { if (hasRequiredErrorCorrectionCode) return errorCorrectionCode; hasRequiredErrorCorrectionCode = 1; const ECLevel = requireErrorCorrectionLevel(); const EC_BLOCKS_TABLE = [ // L M Q H 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 2, 2, 4, 1, 2, 4, 4, 2, 4, 4, 4, 2, 4, 6, 5, 2, 4, 6, 6, 2, 5, 8, 8, 4, 5, 8, 8, 4, 5, 8, 11, 4, 8, 10, 11, 4, 9, 12, 16, 4, 9, 16, 16, 6, 10, 12, 18, 6, 10, 17, 16, 6, 11, 16, 19, 6, 13, 18, 21, 7, 14, 21, 25, 8, 16, 20, 25, 8, 17, 23, 25, 9, 17, 23, 34, 9, 18, 25, 30, 10, 20, 27, 32, 12, 21, 29, 35, 12, 23, 34, 37, 12, 25, 34, 40, 13, 26, 35, 42, 14, 28, 38, 45, 15, 29, 40, 48, 16, 31, 43, 51, 17, 33, 45, 54, 18, 35, 48, 57, 19, 37, 51, 60, 19, 38, 53, 63, 20, 40, 56, 66, 21, 43, 59, 70, 22, 45, 62, 74, 24, 47, 65, 77, 25, 49, 68, 81 ]; const EC_CODEWORDS_TABLE = [ // L M Q H 7, 10, 13, 17, 10, 16, 22, 28, 15, 26, 36, 44, 20, 36, 52, 64, 26, 48, 72, 88, 36, 64, 96, 112, 40, 72, 108, 130, 48, 88, 132, 156, 60, 110, 160, 192, 72, 130, 192, 224, 80, 150, 224, 264, 96, 176, 260, 308, 104, 198, 288, 352, 120, 216, 320, 384, 132, 240, 360, 432, 144, 280, 408, 480, 168, 308, 448, 532, 180, 338, 504, 588, 196, 364, 546, 650, 224, 416, 600, 700, 224, 442, 644, 750, 252, 476, 690, 816, 270, 504, 750, 900, 300, 560, 810, 960, 312, 588, 870, 1050, 336, 644, 952, 1110, 360, 700, 1020, 1200, 390, 728, 1050, 1260, 420, 784, 1140, 1350, 450, 812, 1200, 1440, 480, 868, 1290, 1530, 510, 924, 1350, 1620, 540, 980, 1440, 1710, 570, 1036, 1530, 1800, 570, 1064, 1590, 1890, 600, 1120, 1680, 1980, 630, 1204, 1770, 2100, 660, 1260, 1860, 2220, 720, 1316, 1950, 2310, 750, 1372, 2040, 2430 ]; /** * Returns the number of error correction block that the QR Code should contain * for the specified version and error correction level. * * @param {Number} version QR Code version * @param {Number} errorCorrectionLevel Error correction level * @return {Number} Number of error correction blocks */ errorCorrectionCode.getBlocksCount = function getBlocksCount (version, errorCorrectionLevel) { switch (errorCorrectionLevel) { case ECLevel.L: return EC_BLOCKS_TABLE[(version - 1) * 4 + 0] case ECLevel.M: return EC_BLOCKS_TABLE[(version - 1) * 4 + 1] case ECLevel.Q: return EC_BLOCKS_TABLE[(version - 1) * 4 + 2] case ECLevel.H: return EC_BLOCKS_TABLE[(version - 1) * 4 + 3] default: return undefined } }; /** * Returns the number of error correction codewords to use for the specified * version and error correction level. * * @param {Number} version QR Code version * @param {Number} errorCorrectionLevel Error correction level * @return {Number} Number of error correction codewords */ errorCorrectionCode.getTotalCodewordsCount = function getTotalCodewordsCount (version, errorCorrectionLevel) { switch (errorCorrectionLevel) { case ECLevel.L: return EC_CODEWORDS_TABLE[(version - 1) * 4 + 0] case ECLevel.M: return EC_CODEWORDS_TABLE[(version - 1) * 4 + 1] case ECLevel.Q: return EC_CODEWORDS_TABLE[(version - 1) * 4 + 2] case ECLevel.H: return EC_CODEWORDS_TABLE[(version - 1) * 4 + 3] default: return undefined } }; return errorCorrectionCode; } var polynomial = {}; var galoisField = {}; var hasRequiredGaloisField; function requireGaloisField () { if (hasRequiredGaloisField) return galoisField; hasRequiredGaloisField = 1; const EXP_TABLE = new Uint8Array(512); const LOG_TABLE = new Uint8Array(256) /** * Precompute the log and anti-log tables for faster computation later * * For each possible value in the galois field 2^8, we will pre-compute * the logarithm and anti-logarithm (exponential) of this value * * ref {@link https://en.wikiversity.org/wiki/Reed%E2%80%93Solomon_codes_for_coders#Introduction_to_mathematical_fields} */ ;(function initTables () { let x = 1; for (le