ng-open-cv
Version:
Angular 6+ library integration for OpenCV.js
2 lines • 5.74 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@angular/core"),require("rxjs")):"function"==typeof define&&define.amd?define("ng-open-cv",["exports","@angular/core","rxjs"],t):t(e["ng-open-cv"]={},e.ng.core,e.rxjs)}(this,function(e,n,s){"use strict";var o=function(){return(o=Object.assign||function(e){for(var t,n=1,r=arguments.length;n<r;n++)for(var o in t=arguments[n])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e}).apply(this,arguments)},r=new n.InjectionToken("Angular OpenCV Configuration Object"),i=function(){function e(e){this.src=null,this.dstC1=null,this.dstC3=null,this.dstC4=null,this.isReady=new s.BehaviorSubject({ready:!1,error:!1,loading:!0}),this.isReady$=this.isReady.asObservable(),this.OPENCV_URL="opencv.js",this.DEFAULT_OPTIONS={scriptUrl:"assets/opencv/asm/3.4/opencv.js",wasmBinaryFile:"wasm/3.4/opencv_js.wasm",usingWasm:!1,locateFile:this.locateFile.bind(this),onRuntimeInitialized:function(){}},this.setScriptUrl(e.scriptUrl);var t=o({},this.DEFAULT_OPTIONS,{options:e});this.loadOpenCv(t)}return e.prototype.locateFile=function(e,t){return"opencv_js.wasm"===e?t+"/wasm/"+e:t+e},e.prototype.setScriptUrl=function(e){this.OPENCV_URL=e},e.prototype.loadOpenCv=function(e){var t=this;this.isReady.next({ready:!1,error:!1,loading:!0}),window.Module=o({},e);var n=document.createElement("script");n.setAttribute("async",""),n.setAttribute("type","text/javascript"),n.addEventListener("load",function(){cv.onRuntimeInitialized=function(){e.onRuntimeInitialized&&e.onRuntimeInitialized(),t.isReady.next({ready:!0,error:!1,loading:!1})}}),n.addEventListener("error",function(){var e=t.printError("Failed to load "+t.OPENCV_URL);t.isReady.next({ready:!1,error:!0,loading:!1}),t.isReady.error(e)}),n.src=this.OPENCV_URL;var r=document.getElementsByTagName("script")[0];r?r.parentNode.insertBefore(n,r):document.head.appendChild(n)},e.prototype.createFileFromUrl=function(r,o){var i=this,a=new XMLHttpRequest;return a.open("GET",o,!0),a.responseType="arraybuffer",new s.Observable(function(n){n.next,n.error,n.complete;a.onload=function(e){if(4===a.readyState)if(200===a.status){var t=new Uint8Array(a.response);cv.FS_createDataFile("/",r,t,!0,!1,!1),n.next(),n.complete()}else i.printError("Failed to load "+o+" status: "+a.status),n.error()},a.send()})},e.prototype.loadImageToCanvas=function(o,i){return s.Observable.create(function(e){var t=document.getElementById(i),n=t.getContext("2d"),r=new Image;r.crossOrigin="anonymous",r.onload=function(){t.width=r.width,t.height=r.height,n.drawImage(r,0,0,r.width,r.height),e.next(),e.complete()},r.src=o})},e.prototype.loadImageToHTMLCanvas=function(r,o){return s.Observable.create(function(e){var t=o.getContext("2d"),n=new Image;n.crossOrigin="anonymous",n.onload=function(){o.width=n.width,o.height=n.height,t.drawImage(n,0,0,n.width,n.height),e.next(),e.complete()},n.src=r})},e.prototype.clearError=function(){this.errorOutput.innerHTML=""},e.prototype.printError=function(e){if(void 0===e)e="";else if("number"==typeof e)isNaN(e)||"undefined"!=typeof cv&&(e="Exception: "+cv.exceptionFromPtr(e).msg);else if("string"==typeof e){var t=Number(e.split(" ")[0]);isNaN(t)||"undefined"!=typeof cv&&(e="Exception: "+cv.exceptionFromPtr(t).msg)}else e instanceof Error&&(e=e.stack.replace(/\n/g,"<br>"));throw new Error(e)},e.prototype.loadCode=function(e,t){var n=document.getElementById(e),r=document.getElementById(t);if("text/code-snippet"!==n.type)throw Error("Unknown code snippet type");r.value=n.text.replace(/^\n/,"")},e.prototype.addFileInputHandler=function(e,r){var o=this;document.getElementById(e).addEventListener("change",function(e){var t=e.target.files;if(0<t.length){var n=URL.createObjectURL(t[0]);o.loadImageToCanvas(n,r)}},!1)},e.prototype.onVideoCanPlay=function(){this.onCameraStartedCallback&&this.onCameraStartedCallback(this.stream,this.video)},e.prototype.startCamera=function(e,t,n){var r=this,o=document.getElementById(n);o||(o=document.createElement("video"));var i={qvga:{width:{exact:320},height:{exact:240}},vga:{width:{exact:640},height:{exact:480}}}[e];i||(i=!0),navigator.mediaDevices.getUserMedia({video:i,audio:!1}).then(function(e){o.srcObject=e,o.play(),r.video=o,r.stream=e,r.onCameraStartedCallback=t,o.addEventListener("canplay",r.onVideoCanPlay.bind(r),!1)})["catch"](function(e){r.printError("Camera Error: "+e.name+" "+e.message)})},e.prototype.stopCamera=function(){this.video&&(this.video.pause(),this.video.srcObject=null,this.video.removeEventListener("canplay",this.onVideoCanPlay.bind(this))),this.stream&&this.stream.getVideoTracks()[0].stop()},e.prototype.getContours=function(e,t,n){cv.cvtColor(e,this.dstC1,cv.COLOR_RGBA2GRAY),cv.threshold(this.dstC1,this.dstC4,120,200,cv.THRESH_BINARY);var r=new cv.MatVector,o=new cv.Mat;cv.findContours(this.dstC4,r,o,cv.RETR_CCOMP,cv.CHAIN_APPROX_SIMPLE,{x:0,y:0}),this.dstC3["delete"](),this.dstC3=cv.Mat.ones(n,t,cv.CV_8UC3);for(var i=0;i<r.size();++i){var a=new cv.Scalar(0,255,0);cv.drawContours(this.dstC3,r,i,a,1,cv.LINE_8,o)}return r["delete"](),o["delete"](),this.dstC3},e.decorators=[{type:n.Injectable,args:[{providedIn:"root"}]}],e.ctorParameters=function(){return[{type:undefined,decorators:[{type:n.Inject,args:[r]}]}]},e.ngInjectableDef=n.defineInjectable({factory:function(){return new e(n.inject(r))},token:e,providedIn:"root"}),e}(),t=function(){function t(){}return t.forRoot=function(e){return{ngModule:t,providers:[{provide:r,useValue:e}]}},t.decorators=[{type:n.NgModule,args:[{imports:[],declarations:[],exports:[],providers:[i]}]}],t}();e.OPEN_CV_CONFIGURATION=r,e.NgOpenCVService=i,e.NgOpenCVModule=t,Object.defineProperty(e,"__esModule",{value:!0})});
//# sourceMappingURL=ng-open-cv.umd.min.js.map