1 |
- var t={};t.HandleFileReader=function(t){return new Promise(((e,a)=>{let i=new FileReader;i.readAsDataURL(t),i.onload=t=>{e(t.target.result)}}))};t.getPixelRatio=function(t){var e=t.backingStorePixelRatio||t.webkitBackingStorePixelRatio||t.mozBackingStorePixelRatio||t.msBackingStorePixelRatio||t.oBackingStorePixelRatio||t.backingStorePixelRatio||1;return(window.devicePixelRatio||1)/e};t.HandleImageThumb=function(t,e={},a="image/jpeg"){return new Promise(((i,n)=>{let r=new Image;r.src=t,r.onload=()=>{try{let t=r.width,n=r.height,h=document.createElement("canvas"),o=h.getContext("2d");t<=e.width&&n<=e.height?(h.height=n,h.width=t):(h.width=e.width,h.height=e.fixed?e.height:e.width/(t/n)),o.drawImage(r,0,0,h.width,h.height);let l=h.toDataURL(a,e.quality);i({content:l,handle:!0})}catch(t){n(t)}}}))};t.Base64TOFile=function(t,e){let a=function(t){let e=t.split(","),a=e[0].match(/:(.*?);/)[1],i=atob(e[1]),n=i.length,r=new Uint8Array(n);for(;n--;)r[n]=i.charCodeAt(n);return new Blob([r],{type:a})}(t);return a.lastModifiedDate=new Date,a.name=e,a};const e=function(t,e=0){let a=0,i=0,n=0,r=null,h=null;for(let o=0;o<t.height;o++)for(let l=0;l<t.width;l++){r=4*(t.width*o+l),a=r,i=r+1,n=r+2,h=(t.data[a]+t.data[i]+t.data[n])/3;let g=e?h>e?255:0:h;t.data[a]=g,t.data[i]=g,t.data[n]=g}return t};t.ImageGray=function(t,a=0){return new Promise(((i,n)=>{let r,h=new Image;h.src=t,h.onload=()=>{try{let t=document.createElement("canvas"),n=t.getContext("2d");t.width=h.width,t.height=h.height,n.drawImage(h,0,0,h.width,h.height),r=n.getImageData(0,0,h.width,h.height);let o=e(r,a);n.putImageData(o,0,0),i(t.toDataURL("image/jpeg",.9))}catch(t){n(t)}}}))};t.ImageGetPixel=function(t){return new Promise(((e,a)=>{let i=new Image;i.src=t,i.onload=()=>{try{let t=document.createElement("canvas"),a=t.getContext("2d");t.width=i.width,t.height=i.height,a.drawImage(i,0,0,i.width,i.height),e(a.getImageData(0,0,i.width,i.height))}catch(t){a(t)}}}))};const a=function(t){let e=[],a=null,i=0,n=0,r=0;for(let h=0;h<t.height;h++){let o=[];for(let e=0;e<t.width;e++)a=4*(t.width*h+e),i=a,n=a+1,r=a+2,o.push({r:t.data[i],g:t.data[n],b:t.data[r],a:t.data[a+3]});e.push(o)}return{width:t.width,height:t.height,data:e}};t.ImageOriginPixelToMatrix=a;t.ImageOriginPixelToMatrixAvg=function(t){let e=a(t),i=0;return e.data.forEach((e=>{let a=0;e.forEach((t=>{a+=(t.r+t.g+t.b)/3})),i+=a/t.width})),i/t.height};const i=function(t,e,a){let i,n,r,h,o,l,g,d,c,u=t.data,w=[],s=0;return u[e-1]&&(i=u[e-1][a],u[e-1][a-1]&&(r=u[e-1][a-1]),u[e-1][a+1]&&(g=u[e-1][a+1])),u[e+1]&&(n=u[e+1][a],u[e+1][a+1]&&(d=u[e+1][a+1]),u[e+1][a-1]&&(h=u[e+1][a-1])),u[e][a-1]&&(o=u[e][a-1]),u[e][a+1]&&(l=u[e][a+1]),i&&w.push(i.r),n&&w.push(n.r),o&&w.push(o.r),l&&w.push(l.r),r&&w.push(r.r),h&&w.push(h.r),g&&w.push(g.r),d&&w.push(d.r),w.forEach((t=>{t==u[e][a]?s+=1:c=t})),s>w.length/2?u[e][a]:c};t.Image2Binary=function(t,n="auto",r=128,h=5){return new Promise(((o,l)=>{let g,d=new Image;d.src=t,d.onload=()=>{try{let t=document.createElement("canvas"),l=t.getContext("2d");t.width=d.width,t.height=d.height,l.drawImage(d,0,0,d.width,d.height),g=l.getImageData(0,0,d.width,d.height);let c=e(g,r);if("auto"==n){let t=function(t,e=5){let a={width:t.width,height:t.height,data:[]},n=1-e/100;try{for(let e=0;e<t.height;e++)for(let r=0;r<t.width;r++){let h=i(t,e,r),o=t.data[e][r],l=o.r>h*n?255:0;a.data[e]||(a.data[e]=[]),o.r=l,o.g=l,o.b=l,a.data[e][r]=o}}catch(r){console.log(r)}return a}(a(c),h),e=function(t){let e=[];for(let a=0;a<t.height;a++)for(let i=0;i<t.width;i++){let n=t.data[a][i];e.push(n.r),e.push(n.b),e.push(n.b),e.push(n.a)}return e}(t);c.data.forEach(((t,a)=>{c.data[a]=e[a]}))}l.putImageData(c,0,0),o(t.toDataURL("image/jpeg",.9))}catch(t){l(t)}}}))};const n={compress:function(e,a={}){let i=this,n=Object.assign({width:1e3,height:1e3,quality:.9},a);return new Promise((async(r,h)=>{try{let h=await i.file2Base64(e),o=await t.HandleImageThumb(h,n,a.type||e.type);if(!1===o.handle)r({content:o.content,file:e});else{let t=i.base642File(o.content,e.name);r({content:o.content,file:t})}}catch(o){h(o)}}))},file2Base64:async function(e){return await t.HandleFileReader(e)},base642File:function(e,a){return t.Base64TOFile(e,a)},image2Gray:async function(e,a=0){return await t.ImageGray(e,a)},image2Matrix:async function(e){let a=await t.ImageGetPixel(e);return t.ImageOriginPixelToMatrix(a)},image2Pixel:async function(e){return await t.ImageGetPixel(e)},image2PixelAvg:async function(e){let a=await t.ImageGetPixel(e);return t.ImageOriginPixelToMatrixAvg(a)},image2Binary:async function(e,a="auto",i=128,n=5){return await t.Image2Binary(e,a,i,n)}};export{n as c};
|