careplix-web-algo
Version:
CarePlix Web NPM Package to process Raw-Data from Camera Input
2 lines (1 loc) • 4.5 kB
JavaScript
var W=(()=>{let f=1e4,w=6e4,d=!1,g=!1,u=0,b=0,p=0,y,a,c,l,E=[],v=[],M=({timeElapsed:t=0,type:e="",percentage:n=0,isFingerInView:r=!1})=>{},C=({raw_intensity:t=[],ppg_time:e=[],average_fps:n=0})=>{},m=(t=new Error("Fingerscan Error."))=>{},R=()=>new Promise(async(t,e)=>{var n,r,i;try{let o=/iPhone|iPad|iPod|Macintosh|Mac/i.test(navigator.userAgent),s=await navigator.mediaDevices.getUserMedia({audio:!1,video:{facingMode:"environment",aspectRatio:16/9,frameRate:{ideal:60},height:o?{exact:720}:void 0}});(i=(r=(n=s.getVideoTracks())==null?void 0:n[0])==null?void 0:r.applyConstraints)==null||i.call(r,{advanced:[{torch:!0}]}).catch(F=>console.error(new Error("Flash could not be acquired.",{cause:F}))),a.srcObject=s,a.onloadedmetadata=async()=>{var A;await((A=a.play)==null?void 0:A.call(a));let F=()=>{a.readyState>=2&&a.videoWidth>0&&a.videoHeight>0?t():u=requestAnimationFrame(F)};u=requestAnimationFrame(F)}}catch(o){e(new Error("We are not able to access the Camera. Please try again.",{cause:o}))}}),h=(t=!1)=>{var e,n,r,i;cancelAnimationFrame(u),(i=(r=(n=(e=a==null?void 0:a.srcObject)==null?void 0:e.getTracks)==null?void 0:n.call(e))==null?void 0:r.forEach)==null||i.call(r,o=>{var s;(s=o==null?void 0:o.stop)==null||s.call(o)}),g=!1,window.onblur=void 0,y==null||y.release().then(()=>console.log("WakeLock Released.")).catch(o=>{console.log("WakeLock Error."),console.error(o)}),t||C({raw_intensity:E,ppg_time:v,average_fps:Math.round(E.length*1e3/w)})},q=(t={r:0,g:0,b:0})=>{let e;if(t.r<(d?25:5))e=0;else{let n=t.g+t.b,r=d?35:60,i=d?125:150;n<=r?e=1:n>=i?e=0:e=Math.min(Math.max((i-n)/r,0),1)}return e},B=t=>{let e=0,n={r:0,g:0,b:0};for(let r=0;r<t.data.length;r+=4){let i=t.data[r],o=t.data[r+1],s=t.data[r+2];i===0&&o===0&&s===0||(e++,n.r+=i,n.g+=o,n.b+=s)}return{r:n.r/e,g:n.g/e,b:n.b/e}},x=()=>{let t=c.width||0,e=c.height||0;l.save(),l.clearRect(0,0,t,e),l.drawImage(a,0,0,t,e);let n=B(l.getImageData(Math.max(Math.round(t/5),0),Math.max(Math.round(e/5),0),Math.max(Math.round(t*.6),1),Math.max(Math.round(e*.6),1)));return l.restore(),n},I=async t=>{let e=t-b;try{if(e<=f){let n=x(),r=q(n);M({timeElapsed:e,type:"calibration",percentage:Math.round(e/f*100),isFingerInView:r>.6}),u=requestAnimationFrame(I)}else if(e<=f+w){let n=x(),r=q(n);if(r>.6?p=0:d?p=101:p++,p>100){h(!0),m(new Error("Ensure your finger is correctly positioned on the camera and there is enough light if the flashlight isn't on."));return}E.push(n),v.push(performance.now()-b),M({timeElapsed:e,type:"scan",percentage:Math.round((e-f)/w*100),isFingerInView:r>.6}),u=requestAnimationFrame(I)}else h()}catch(n){h(!0),m(new Error("Sorry we're unable to compute the signal. Please try again.",{cause:n}))}};return{startScan:async({calibrationTime_inSec:t=10,scanTime_inSec:e=60,strict_mode:n=!1,videoElement:r,canvasElement:i})=>{var o;E=[],v=[],g=!1,p=0;try{if(typeof t!="number"||t<3||t>20)throw new Error("Calibration duration can be between 3-20 seconds.");if(typeof e!="number"||e<10||e>120||e%5!==0)throw new Error("Scan duration can be between 10-120 seconds (multiple of 5).");if(t>e)throw new Error("Scan duration cannot be smaller than Calibration duration.");if(f=t*1e3,w=e*1e3,typeof n!="boolean"?(console.error("Provided Strict Mode value is Invalid!\nSwitching to default..."),d=!1):d=n,(o=navigator.wakeLock)==null||o.request("screen").then(s=>{y=s,console.log("WakeLock Active.")}).catch(s=>{console.log("WakeLock Error."),console.error(s)}),a=typeof r>"u"?document.getElementById("videoInput"):r,a)await R();else throw new Error("Cannot get the video element.");if(c=typeof i>"u"?document.getElementById("canvasOutput"):i,c)c.width=a.videoWidth,c.height=a.videoHeight,l=c.getContext("2d",{alpha:!1,willReadFrequently:!0}),l.drawImage(a,0,0,c.width,c.height);else throw new Error("Cannot get the canvas element.");b=performance.now(),u=requestAnimationFrame(I),g=!0,window.onblur=()=>{h(!0),m(new Error("App functionality disabled in the Background. Keep it in the Foreground for proper operation."))}}catch(s){throw h(!0),m(s),new Error("Fingerscan Initialization Error.",{cause:s})}},stopScan:h,onFrame:(t=({timeElapsed:e=0,type:n="",percentage:r=0,isFingerInView:i=!1})=>{})=>{typeof t=="function"&&(M=t)},onScanFinish:(t=({raw_intensity:e=[],ppg_time:n=[],average_fps:r=0})=>{})=>{typeof t=="function"&&(C=t)},onError:(t=(e=new Error("Fingerscan Error."))=>{})=>{typeof t=="function"&&(m=t)},get isInitializing(){return()=>!g},get isScanning(){return()=>!!g}}})(),S=W;export{S as default};