scandit-web-datacapture-barcode
Version:
Scandit Data Capture SDK for the Web
7 lines (4 loc) • 2.06 kB
JavaScript
import { a } from './chunk-WMUD7V77.js';
import { Deferred, Size } from 'scandit-web-datacapture-core';
var c=class s{constructor(e,t){var a;this.options={scale:(a=t.scale)!=null?a:1,size:t.size},this.data=e;}static async withHTMLElement(e,t){let a=e.cloneNode(!0);return this.getEncodedImageData(a).then(({data:i,size:r})=>new s(i,{size:r,...t}))}static async withBase64EncodedData(e,t){let a=new Deferred,i=new Image;i.addEventListener("error",a.reject,{once:!0}),i.src=e,await i.decode();let r=this.getSize(i);return a.resolve(new s(e,{size:r,...t})),a.promise}static async getEncodedImageData(e){let t=await this.getSVGDataForElement(e);return this.getBase64DataFromDataURL(t)}static getSize(e){let t=document.body.contains(e);t||document.body.append(e);let a=e.getBoundingClientRect();return t||e.remove(),new Size(a.width,a.height)}static async getSVGDataForElement(e){"customElements"in window&&customElements.get(e.tagName.toLowerCase())!==void 0&&await customElements.whenDefined(e.tagName.toLowerCase());let t=this.getSize(e),a$1="http://www.w3.org/2000/svg",i=document.createElementNS(a$1,"svg"),r=document.createElementNS(a$1,"foreignObject");return i.setAttribute("width",`${t.width}`),i.setAttribute("height",`${t.height}`),i.setAttribute("viewBox",`0 0 ${t.width} ${t.height}`),r.setAttribute("width","100%"),r.setAttribute("height","100%"),r.setAttribute("x","0"),r.setAttribute("y","0"),r.setAttribute("externalResourcesRequired","true"),i.append(r),r.append(e),{data:await a(i),size:t}}static getCanvasWithSize(e){let t=document.createElement("canvas");return t.width=e.width,t.height=e.height,t}static async getBase64DataFromDataURL(e){var r;let t=new Deferred,a=new Image;a.addEventListener("error",t.reject,{once:!0}),a.src=e.data,await a.decode();let i=this.getCanvasWithSize(e.size);return (r=i.getContext("2d"))==null||r.drawImage(a,0,0),t.resolve({data:i.toDataURL("image/png",1),size:e.size}),t.promise}toJSONObject(){return {data:this.data,options:{size:this.options.size.toJSONObject(),scale:this.options.scale}}}};
export { c as a };