UNPKG

careplix-web-algo

Version:

CarePlix Web NPM Package to process Raw-Data from Camera Input

2 lines (1 loc) 4.96 kB
var fingerScan=(()=>{var C=Object.defineProperty;var P=Object.getOwnPropertyDescriptor;var S=Object.getOwnPropertyNames;var V=Object.prototype.hasOwnProperty;var L=(l,c)=>{for(var u in c)C(l,u,{get:c[u],enumerable:!0})},O=(l,c,u,f)=>{if(c&&typeof c=="object"||typeof c=="function")for(let d of S(c))!V.call(l,d)&&d!==u&&C(l,d,{get:()=>c[d],enumerable:!(f=P(c,d))||f.enumerable});return l};var k=l=>O(C({},"__esModule",{value:!0}),l);var j={};L(j,{default:()=>H});var G=(()=>{let l=1e4,c=6e4,u=!1,f=!1,d=0,b=0,m=0,y,a,h,g,E=[],v=[],M=({timeElapsed:t=0,type:e="",percentage:n=0,isFingerInView:r=!1})=>{},q=({raw_intensity:t=[],ppg_time:e=[],average_fps:n=0})=>{},w=(t=new Error("Fingerscan Error."))=>{},B=()=>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 R;await((R=a.play)==null?void 0:R.call(a));let F=()=>{a.readyState>=2&&a.videoWidth>0&&a.videoHeight>0?t():d=requestAnimationFrame(F)};d=requestAnimationFrame(F)}}catch(o){e(new Error("We are not able to access the Camera. Please try again.",{cause:o}))}}),p=(t=!1)=>{var e,n,r,i;cancelAnimationFrame(d),(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)}),f=!1,window.onblur=void 0,y==null||y.release().then(()=>console.log("WakeLock Released.")).catch(o=>{console.log("WakeLock Error."),console.error(o)}),t||q({raw_intensity:E,ppg_time:v,average_fps:Math.round(E.length*1e3/c)})},x=(t={r:0,g:0,b:0})=>{let e;if(t.r<(u?25:5))e=0;else{let n=t.g+t.b,r=u?35:60,i=u?125:150;n<=r?e=1:n>=i?e=0:e=Math.min(Math.max((i-n)/r,0),1)}return e},W=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}},A=()=>{let t=h.width||0,e=h.height||0;g.save(),g.clearRect(0,0,t,e),g.drawImage(a,0,0,t,e);let n=W(g.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 g.restore(),n},I=async t=>{let e=t-b;try{if(e<=l){let n=A(),r=x(n);M({timeElapsed:e,type:"calibration",percentage:Math.round(e/l*100),isFingerInView:r>.6}),d=requestAnimationFrame(I)}else if(e<=l+c){let n=A(),r=x(n);if(r>.6?m=0:u?m=101:m++,m>100){p(!0),w(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-l)/c*100),isFingerInView:r>.6}),d=requestAnimationFrame(I)}else p()}catch(n){p(!0),w(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=[],f=!1,m=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(l=t*1e3,c=e*1e3,typeof n!="boolean"?(console.error("Provided Strict Mode value is Invalid!\nSwitching to default..."),u=!1):u=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 B();else throw new Error("Cannot get the video element.");if(h=typeof i>"u"?document.getElementById("canvasOutput"):i,h)h.width=a.videoWidth,h.height=a.videoHeight,g=h.getContext("2d",{alpha:!1,willReadFrequently:!0}),g.drawImage(a,0,0,h.width,h.height);else throw new Error("Cannot get the canvas element.");b=performance.now(),d=requestAnimationFrame(I),f=!0,window.onblur=()=>{p(!0),w(new Error("App functionality disabled in the Background. Keep it in the Foreground for proper operation."))}}catch(s){throw p(!0),w(s),new Error("Fingerscan Initialization Error.",{cause:s})}},stopScan:p,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"&&(q=t)},onError:(t=(e=new Error("Fingerscan Error."))=>{})=>{typeof t=="function"&&(w=t)},get isInitializing(){return()=>!f},get isScanning(){return()=>!!f}}})(),H=G;return k(j);})();