UNPKG

@face-auth/face-id-video

Version:

Utility library for capturing photos from webcam video streams in the browser. Handles camera selection, image formatting, and output for face authentication APIs.

2 lines (1 loc) 3.46 kB
var p=function(d,e,t,i){function a(r){return r instanceof t?r:new t(function(o){o(r)})}return new(t||(t=Promise))(function(r,o){function h(n){try{c(i.next(n))}catch(s){o(s)}}function u(n){try{c(i.throw(n))}catch(s){o(s)}}function c(n){n.done?r(n.value):a(n.value).then(h,u)}c((i=i.apply(d,e||[])).next())})},l=class{constructor(e){this.stream=null,this.video=e}getDevices(){return p(this,void 0,void 0,function*(){return(yield navigator.mediaDevices.enumerateDevices()).filter(t=>t.kind==="videoinput")})}start(){return p(this,arguments,void 0,function*(e={},t){let i={video:{}};t?i.video={deviceId:{exact:t}}:e.preferredFacingMode&&(i.video={facingMode:e.preferredFacingMode}),e.idealResolution&&(i.video=Object.assign(Object.assign({},i.video),{width:{ideal:e.idealResolution.width},height:{ideal:e.idealResolution.height}}));try{this.stream=yield navigator.mediaDevices.getUserMedia(i),this.video.srcObject=this.stream;let a=yield new Promise(r=>{this.video.onloadedmetadata=()=>{r(this.video)}});return yield this.video.play(),a}catch(a){let r=new Error((a==null?void 0:a.message)||"Unknown error");throw a.name==="NotAllowedError"?r.code="PERMISSION_DENIED":a.name==="NotFoundError"?r.code="NO_DEVICES":a.name==="OverconstrainedError"?r.code="OVERCONSTRAINED":r.code="UNKNOWN",r}})}stop(){this.stream&&(this.stream.getTracks().forEach(e=>e.stop()),this.stream=null),this.video.srcObject=null}};var w="image/png";var g=function(d,e,t,i){function a(r){return r instanceof t?r:new t(function(o){o(r)})}return new(t||(t=Promise))(function(r,o){function h(n){try{c(i.next(n))}catch(s){o(s)}}function u(n){try{c(i.throw(n))}catch(s){o(s)}}function c(n){n.done?r(n.value):a(n.value).then(h,u)}c((i=i.apply(d,e||[])).next())})},f=class{constructor(e){this.video=e}capture(e){return g(this,void 0,void 0,function*(){let t=e.imageType||w;return this.captureFullFrame(t)})}captureFullFrame(e){return g(this,void 0,void 0,function*(){let t=document.createElement("canvas");t.width=this.video.videoWidth,t.height=this.video.videoHeight;let i=t.getContext("2d");if(!i)throw new Error("No se pudo crear el contexto del canvas.");return i.drawImage(this.video,0,0,t.width,t.height),new Promise((a,r)=>{t.toBlob(o=>{o?a({blob:o,imageType:e}):r(new Error("No se pudo crear el blob del frame completo."))},e)})})}};var m=function(d,e,t,i){function a(r){return r instanceof t?r:new t(function(o){o(r)})}return new(t||(t=Promise))(function(r,o){function h(n){try{c(i.next(n))}catch(s){o(s)}}function u(n){try{c(i.throw(n))}catch(s){o(s)}}function c(n){n.done?r(n.value):a(n.value).then(h,u)}c((i=i.apply(d,e||[])).next())})},v=class{constructor(e){this.onCameraStartedCallbacks=new Set,this.cameraManager=new l(e),this.photoManager=null}getDevices(){return m(this,void 0,void 0,function*(){return this.cameraManager.getDevices()})}start(){return m(this,arguments,void 0,function*(e={},t){return yield this.cameraManager.start(e,t).then(i=>{this.photoManager=new f(i)}).then(()=>{this.emitCameraStarted()})})}stop(){this.cameraManager.stop()}capture(e){return m(this,void 0,void 0,function*(){return this.photoManager?yield this.photoManager.capture(e):new Promise((t,i)=>{i("NO_VIDEO_INITIALIZED")})})}onCameraStarted(e){this.onCameraStartedCallbacks.add(e)}offCameraStarted(e){this.onCameraStartedCallbacks.delete(e)}emitCameraStarted(){for(let e of this.onCameraStartedCallbacks)try{e()}catch(t){console.error("Error in camera started callback",t)}}};export{v as FaceVideo};