@autonomdev/ngx-document-scanner
Version:
Angular 2+ component for cropping and enhancing images of documents
16 lines (14 loc) • 27.9 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@angular/core"),require("rxjs"),require("@angular/material/bottom-sheet"),require("ngx-opencv"),require("@angular/flex-layout"),require("angular2-draggable"),require("@angular/common"),require("@angular/material/button"),require("@angular/material/icon"),require("@angular/material/list")):"function"==typeof define&&define.amd?define("@autonomdev/ngx-document-scanner",["exports","@angular/core","rxjs","@angular/material/bottom-sheet","ngx-opencv","@angular/flex-layout","angular2-draggable","@angular/common","@angular/material/button","@angular/material/icon","@angular/material/list"],e):e(((t=t||self).autonomdev=t.autonomdev||{},t.autonomdev["ngx-document-scanner"]={}),t.ng.core,t.rxjs,t.ng.material.bottomSheet,t.ngxOpencv,t.ng.flexLayout,t.angular2Draggable,t.ng.common,t.ng.material.button,t.ng.material.icon,t.ng.material.list)}(this,(function(t,e,i,n,o,r,s,a,c,h,l){"use strict";
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */function p(t,e,i,n){return new(i||(i=Promise))((function(o,r){function s(t){try{c(n.next(t))}catch(t){r(t)}}function a(t){try{c(n.throw(t))}catch(t){r(t)}}function c(t){var e;t.done?o(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(s,a)}c((n=n.apply(t,e||[])).next())}))}function u(t,e){var i,n,o,r,s={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return r={next:a(0),throw:a(1),return:a(2)},"function"==typeof Symbol&&(r[Symbol.iterator]=function(){return this}),r;function a(r){return function(a){return function(r){if(i)throw new TypeError("Generator is already executing.");for(;s;)try{if(i=1,n&&(o=2&r[0]?n.return:r[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,r[1])).done)return o;switch(n=0,o&&(r=[2&r[0],o.value]),r[0]){case 0:case 1:o=r;break;case 4:return s.label++,{value:r[1],done:!1};case 5:s.label++,n=r[1],r=[0];continue;case 7:r=s.ops.pop(),s.trys.pop();continue;default:if(!(o=s.trys,(o=o.length>0&&o[o.length-1])||6!==r[0]&&2!==r[0])){s=0;continue}if(3===r[0]&&(!o||r[1]>o[0]&&r[1]<o[3])){s.label=r[1];break}if(6===r[0]&&s.label<o[1]){s.label=o[1],o=r;break}if(o&&s.label<o[2]){s.label=o[2],s.ops.push(r);break}o[2]&&s.ops.pop(),s.trys.pop();continue}r=e.call(t,s)}catch(t){r=[6,t],n=0}finally{i=o=0}if(5&r[0])throw r[1];return{value:r[0]?r[1]:void 0,done:!0}}([r,a])}}}Object.create;function m(t,e){var i="function"==typeof Symbol&&t[Symbol.iterator];if(!i)return t;var n,o,r=i.call(t),s=[];try{for(;(void 0===e||e-- >0)&&!(n=r.next()).done;)s.push(n.value)}catch(t){o={error:t}}finally{try{n&&!n.done&&(i=r.return)&&i.call(r)}finally{if(o)throw o.error}}return s}function d(){for(var t=[],e=0;e<arguments.length;e++)t=t.concat(m(arguments[e]));return t}Object.create;var g=function(){function t(){this.limitDirections=["left","right","top","bottom"],this._limits={top:0,bottom:0,right:0,left:0},this._points=[],this.positions=new i.BehaviorSubject(Array.from(this._points)),this.repositionEvent=new i.BehaviorSubject([]),this.limits=new i.BehaviorSubject(this._limits),this.paneDimensions=new i.BehaviorSubject({width:0,height:0})}return t.prototype.setPaneDimensions=function(t){var e=this;return new Promise((function(i,n){e._paneDimensions=t,e.paneDimensions.next(t),i()}))},t.prototype.repositionPoints=function(t){var e=this;this._points=t,t.forEach((function(t){e.positionChange(t)})),this.repositionEvent.next(t)},t.prototype.positionChange=function(t){var e=this;this.updatePosition(t),this.limitDirections.forEach((function(t){var i,n=e._points.filter((function(e){return e.roles.includes(t)})).map((function(i){return i[e.getDirectionAxis(t)]}));"top"!==t&&"left"!==t||(i=Math.max.apply(Math,d(n))),"right"!==t&&"bottom"!==t||(i=Math.min.apply(Math,d(n))),e._limits[t]=i})),this.limits.next(this._limits),this.positions.next(Array.from(this._points))},t.prototype.updatePosition=function(t){var e=this,i=this._points.findIndex((function(i){return e.compareArray(t.roles,i.roles)}));-1===i?this._points.push(t):this._points.splice(i,1,t)},t.prototype.exceedsLimit=function(t){var e=this,i=this.limitDirections.filter((function(e){return!t.roles.includes(e)})),n={exceeds:!1,resetCoefficients:{x:0,y:0},resetCoordinates:{x:t.x,y:t.y}};return i.forEach((function(i){var o=e.getDirectionAxis(i);"top"===i||"left"===i?t[o]<e._limits[i]&&(n.resetCoefficients[o]=1,n.resetCoordinates[o]=e._limits[i]):"right"!==i&&"bottom"!==i||t[o]>e._limits[i]&&(n.resetCoefficients[o]=-1,n.resetCoordinates[o]=e._limits[i])})),0===n.resetCoefficients.x&&0===n.resetCoefficients.y||(n.exceeds=!0),n},t.prototype.rotateClockwise=function(t,e,i){var n=this;i=i.map((function(t){return new f({x:t.x/e.width,y:t.y/e.height},t.roles)})),this.repositionPoints(i.map((function(t){return n.rotateCornerClockwise(t)})))},t.prototype.rotateCornerClockwise=function(t){var e=this,i={x:this._paneDimensions.width*(1-t.y),y:this._paneDimensions.height*t.x,roles:[]},n=[["bottom","left"],["top","left"],["top","right"],["bottom","right"],["bottom","left"]];return i.roles=n[n.findIndex((function(i){return e.compareArray(i,t.roles)}))+1],i},t.prototype.compareArray=function(t,e){return t.every((function(t){return e.includes(t)}))&&t.length===e.length},t.prototype.getDirectionAxis=function(t){return{left:"x",right:"x",top:"y",bottom:"y"}[t]},t.decorators=[{type:e.Injectable,args:[{providedIn:"root"}]}],t.ctorParameters=function(){return[]},t.ɵprov=e.ɵɵdefineInjectable({factory:function(){return new t},token:t,providedIn:"root"}),t}();var f=function(t,e){this.x=t.x,this.y=t.y,this.roles=e};var v=function(){function t(t){this.limitsService=t,this.width=10,this.height=10,this.color="#3cabe2",this.shape="rect",this.pointOptions="rect",this.position={x:0,y:0}}return t.prototype.ngAfterViewInit=function(){var t=this;Object.keys(this.pointOptions).forEach((function(e){t[e]=t.pointOptions[e]})),this.limitsService.paneDimensions.subscribe((function(e){e.width>0&&e.width>0&&(t._paneDimensions={width:e.width,height:e.height},t.position=t.getInitialPosition(e),t.limitsService.positionChange(new f(t.position,t.limitRoles)))})),this.limitsService.repositionEvent.subscribe((function(e){e.length>0&&t.externalReposition(e)}))},t.prototype.pointStyle=function(){return{width:this.width+"px",height:this.height+"px","background-color":this.color,"border-radius":"circle"===this.shape?"100%":0,position:"absolute"}},t.prototype.positionChange=function(t){var e=new f(t,this.limitRoles),i=this.limitsService.exceedsLimit(e);i.exceeds?this.resetPosition=i.resetCoordinates:(this.limitsService.positionChange(e),this._currentPosition=t)},t.prototype.adjustPosition=function(t){var e={x:0,y:0};Object.keys(this.startPosition).forEach((function(i){e[i]=t.resetCoordinates[i]+t.resetCoefficients[i]})),this.position=e,this.limitsService.positionChange(new f(this.position,this.limitRoles))},t.prototype.movementEnd=function(t){var e=new f(t,this.limitRoles),i=this.limitsService.exceedsLimit(e);i.exceeds&&(this.resetPosition=i.resetCoordinates,i.exceeds&&(this.adjustPosition(i),e=new f(this.position,this.limitRoles),this.limitsService.updatePosition(e)))},t.prototype.getInitialPosition=function(t){return{x:this.limitRoles.includes("left")?0:t.width-this.width/2,y:this.limitRoles.includes("top")?0:t.height-this.height/2}},t.prototype.externalReposition=function(t){var e=this;t.forEach((function(t){e.limitsService.compareArray(e.limitRoles,t.roles)&&(t=e.enforcePaneLimits(t),e.position={x:t.x,y:t.y})}))},t.prototype.enforcePaneLimits=function(t){return 0===this._paneDimensions.width||0===this._paneDimensions.height||(t.x>this._paneDimensions.width&&(t.x=this._paneDimensions.width),t.x<0&&(t.x=1),t.y>this._paneDimensions.height&&(t.y=this._paneDimensions.height),t.y<0&&(t.y=1)),t},t.decorators=[{type:e.Component,args:[{selector:"ngx-draggable-point",template:'<div #point ngDraggable="draggable"\n (movingOffset)="positionChange($event)"\n [ngStyle]="pointStyle()"\n [position]="position"\n [bounds]="container"\n [inBounds]="true"\n (endOffset)="movementEnd($event)"\n style="z-index: 1000">\n</div>\n'}]}],t.ctorParameters=function(){return[{type:g}]},t.propDecorators={width:[{type:e.Input}],height:[{type:e.Input}],color:[{type:e.Input}],shape:[{type:e.Input}],pointOptions:[{type:e.Input}],limitRoles:[{type:e.Input}],startPosition:[{type:e.Input}],container:[{type:e.Input}],_currentPosition:[{type:e.Input}]},t}();var y=function(){function t(t,i){var n=this;this.bottomSheetRef=t,this.data=i,this.filterOptions=[{name:"bw2",icon:"filter_b_and_w",action:function(t){n.filterSelected.emit(t)},text:"B&W 2"},{name:"bw3",icon:"blur_on",action:function(t){n.filterSelected.emit(t)},text:"B&W 3"},{name:"magic_color",icon:"filter_vintage",action:function(t){n.filterSelected.emit(t)},text:"Magic Color"},{name:"default",icon:"crop_original",action:function(t){n.filterSelected.emit(t)},text:"Original"}],this.filterSelected=new e.EventEmitter}return t.prototype.selectOption=function(t){this.data.filter=t,this.bottomSheetRef.dismiss()},t.decorators=[{type:e.Component,args:[{selector:"ngx-filter-menu",template:'<mat-action-list>\n <button mat-list-item *ngFor="let option of filterOptions" (click)="selectOption(option.name)">\n <mat-icon>{{option.icon}}</mat-icon>\n <span fxFlex="100" style="text-align: start; margin: 5px">{{option.text}}</span>\n <span fxFlex="100"></span>\n <mat-icon *ngIf="option.name === data.filter">done</mat-icon>\n </button>\n</mat-action-list>\n'}]}],t.ctorParameters=function(){return[{type:n.MatBottomSheetRef},{type:void 0,decorators:[{type:e.Inject,args:[n.MAT_BOTTOM_SHEET_DATA]}]}]},t.propDecorators={filterSelected:[{type:e.Output}]},t}();var w=function(){function t(t){this.limitsService=t,this.color="#3cabe2"}return t.prototype.ngAfterViewInit=function(){var t=this;this.canvas.nativeElement.width=this.dimensions.width,this.canvas.nativeElement.height=this.dimensions.height,this.limitsService.positions.subscribe((function(e){4===e.length&&(t._points=e,t.sortPoints(),t.clearCanvas(),t.drawShape())})),this.limitsService.paneDimensions.subscribe((function(e){t.clearCanvas(),t.canvas.nativeElement.width=e.width,t.canvas.nativeElement.height=e.height})),this.limitsService.repositionEvent.subscribe((function(e){4===e.length&&setTimeout((function(){t.clearCanvas(),t.sortPoints(),t.drawShape()}),10)}))},t.prototype.clearCanvas=function(){this.canvas.nativeElement.getContext("2d").clearRect(0,0,this.dimensions.width,this.dimensions.height)},t.prototype.sortPoints=function(){for(var t=this,e=Array.from(this._points),i=[],n={vertical:["top","top","bottom","bottom"],horizontal:["left","right","right","left"]},o=function(o){var r=Array.from([n.vertical[o],n.horizontal[o]]);i.push(e.filter((function(e){return t.limitsService.compareArray(e.roles,r)}))[0])},r=0;r<4;r++)o(r);this._sortedPoints=i},t.prototype.drawShape=function(){var t=this,e=this.canvas.nativeElement.getContext("2d");e.lineWidth=this.weight,e.strokeStyle=this.color,e.beginPath(),this._sortedPoints.forEach((function(i,n){if(0===n&&e.moveTo(i.x,i.y),n!==t._sortedPoints.length-1){var o=t._sortedPoints[n+1];e.lineTo(o.x,o.y)}else e.closePath()})),e.stroke()},t.decorators=[{type:e.Component,args:[{selector:"ngx-shape-outine",template:'<canvas #outline\n style="position: absolute; z-index: 1000"\n [ngStyle]="{width: dimensions.width + \'px\', height: dimensions.height + \'px\'}"\n *ngIf="dimensions">\n</canvas>\n'}]}],t.ctorParameters=function(){return[{type:g}]},t.propDecorators={color:[{type:e.Input}],weight:[{type:e.Input}],dimensions:[{type:e.Input}],canvas:[{type:e.ViewChild,args:["outline"]}]},t}();var b=function(){function t(t,i,n){var o=this;this.ngxOpenCv=t,this.limitsService=i,this.bottomSheet=n,this.editorButtons=[{name:"exit",action:function(){o.exitEditor.emit("canceled")},icon:"arrow_back",type:"fab",mode:"crop"},{name:"rotate",action:this.rotateImage.bind(this),icon:"rotate_right",type:"fab",mode:"crop"},{name:"done_crop",action:function(){return p(o,void 0,void 0,(function(){return u(this,(function(t){switch(t.label){case 0:return this.mode="color",[4,this.transform()];case 1:return t.sent(),[4,this.applyFilter(!0)];case 2:return t.sent(),[2]}}))}))},icon:"done",type:"fab",mode:"crop"},{name:"back",action:function(){o.mode="crop",o.loadFile(o.originalImage)},icon:"arrow_back",type:"fab",mode:"color"},{name:"filter",action:function(){return o.chooseFilters()},icon:"photo_filter",type:"fab",mode:"color"},{name:"upload",action:this.exportImage.bind(this),icon:"cloud_upload",type:"fab",mode:"color"}],this.imageLoaded=!1,this.mode="crop",this.selectedFilter="default",this.imageDimensions={width:0,height:0},this.exitEditor=new e.EventEmitter,this.editResult=new e.EventEmitter,this.error=new e.EventEmitter,this.ready=new e.EventEmitter,this.processing=new e.EventEmitter,this.screenDimensions={width:window.innerWidth,height:window.innerHeight},this.ngxOpenCv.cvState.subscribe((function(t){o.cvState=t.state,o.ready.emit(t.ready),t.error?o.error.emit(new Error("error loading cv")):t.loading?o.processing.emit(!0):t.ready&&o.processing.emit(!1)})),this.limitsService.positions.subscribe((function(t){o.points=t}))}return Object.defineProperty(t.prototype,"displayedButtons",{get:function(){var t=this;return this.editorButtons.filter((function(e){return e.mode===t.mode}))},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"file",{set:function(t){var e=this;t&&(setTimeout((function(){e.processing.emit(!0)}),5),this.imageLoaded=!1,this.originalImage=t,this.ngxOpenCv.cvState.subscribe((function(i){return p(e,void 0,void 0,(function(){return u(this,(function(e){switch(e.label){case 0:return i.ready?[4,this.loadFile(t)]:[3,2];case 1:e.sent(),this.processing.emit(!1),e.label=2;case 2:return[2]}}))}))})))},enumerable:!0,configurable:!0}),t.prototype.ngOnInit=function(){var t=this;this.options=new x(this.config),this.editorButtons.forEach((function(e){"upload"===e.name&&(e.icon=t.options.exportImageIcon)})),this.maxPreviewWidth=this.options.maxPreviewWidth,this.editorStyle=this.options.editorStyle},t.prototype.exit=function(){this.exitEditor.emit("canceled")},t.prototype.exportImage=function(){return p(this,void 0,void 0,(function(){var t=this;return u(this,(function(e){switch(e.label){case 0:return[4,this.applyFilter(!1)];case 1:return e.sent(),this.options.maxImageDimensions?this.resize(this.editedImage).then((function(e){e.toBlob((function(e){t.editResult.emit(e),t.processing.emit(!1)}),t.originalImage.type)})):this.editedImage.toBlob((function(e){t.editResult.emit(e),t.processing.emit(!1)}),this.originalImage.type),[2]}}))}))},t.prototype.chooseFilters=function(){var t=this,e={filter:this.selectedFilter};this.bottomSheet.open(y,{data:e}).afterDismissed().subscribe((function(){t.selectedFilter=e.filter,t.applyFilter(!0)}))},t.prototype.loadFile=function(t){var e=this;return new Promise((function(i,n){return p(e,void 0,void 0,(function(){var e,n,o=this;return u(this,(function(r){switch(r.label){case 0:this.processing.emit(!0),r.label=1;case 1:return r.trys.push([1,3,,4]),[4,this.readImage(t)];case 2:return r.sent(),[3,4];case 3:return e=r.sent(),console.error(e),this.error.emit(new Error(e)),[3,4];case 4:return r.trys.push([4,6,,7]),[4,this.showPreview()];case 5:return r.sent(),[3,7];case 6:return n=r.sent(),console.error(n),this.error.emit(new Error(n)),[3,7];case 7:return this.imageLoaded=!0,[4,this.limitsService.setPaneDimensions({width:this.previewDimensions.width,height:this.previewDimensions.height})];case 8:return r.sent(),setTimeout((function(){return p(o,void 0,void 0,(function(){return u(this,(function(t){switch(t.label){case 0:return[4,this.detectContours()];case 1:return t.sent(),this.processing.emit(!1),i(),[2]}}))}))}),15),[2]}}))}))}))},t.prototype.readImage=function(t){var e=this;return new Promise((function(i,n){return p(e,void 0,void 0,(function(){var e,o,r,s=this;return u(this,(function(a){switch(a.label){case 0:return a.trys.push([0,2,,3]),[4,new Promise((function(e,i){var n=new FileReader;n.onload=function(t){e(n.result)},n.onerror=function(t){i(t)},n.readAsDataURL(t)}))];case 1:return e=a.sent(),[3,3];case 2:return o=a.sent(),n(o),[3,3];case 3:return(r=new Image).onload=function(){return p(s,void 0,void 0,(function(){var t;return u(this,(function(e){switch(e.label){case 0:return this.editedImage=document.createElement("canvas"),this.editedImage.width=r.width,this.editedImage.height=r.height,this.editedImage.getContext("2d").drawImage(r,0,0),(r.width>r.height?r.height:r.width)>this.options.maxImageDimensions.width?(t=this,[4,this.resize(this.editedImage)]):[3,2];case 1:t.editedImage=e.sent(),e.label=2;case 2:return this.imageDimensions.width=this.editedImage.width,this.imageDimensions.height=this.editedImage.height,this.setPreviewPaneDimensions(this.editedImage),i(),[2]}}))}))},r.src=e,[2]}}))}))}))},t.prototype.rotateImage=function(){var t=this;return new Promise((function(e,i){t.processing.emit(!0),setTimeout((function(){var i=cv.imread(t.editedImage);cv.transpose(i,i),cv.flip(i,i,1),cv.imshow(t.editedImage,i),i.delete();var n={width:0,height:0};Object.assign(n,t.previewDimensions);var o=Array.from(t.points);t.setPreviewPaneDimensions(t.editedImage);var r={width:t.previewDimensions.width/n.width,height:t.previewDimensions.height/n.height};t.limitsService.rotateClockwise(r,n,o),t.showPreview().then((function(){t.processing.emit(!1),e()}))}),30)}))},t.prototype.detectContours=function(){var t=this;return new Promise((function(e,i){t.processing.emit(!0),setTimeout((function(){var i=cv.imread(t.editedImage),n=(new cv.Size(.5*i.rows,.5*i.cols),new cv.Size(5,5));cv.cvtColor(i,i,cv.COLOR_RGBA2GRAY,0),cv.GaussianBlur(i,i,n,0,0,cv.BORDER_DEFAULT),cv.Canny(i,i,75,200),cv.threshold(i,i,120,200,cv.THRESH_BINARY);var o=new cv.MatVector,r=new cv.Mat;cv.findContours(i,o,r,cv.RETR_CCOMP,cv.CHAIN_APPROX_SIMPLE);var s=cv.boundingRect(i);i.delete(),r.delete(),o.delete(),Object.keys(s).forEach((function(e){s[e]=s[e]*t.imageResizeRatio}));var a=[new f({x:s.x,y:s.y},["left","top"]),new f({x:s.x+s.width,y:s.y},["right","top"]),new f({x:s.x+s.width,y:s.y+s.height},["right","bottom"]),new f({x:s.x,y:s.y+s.height},["left","bottom"])];t.limitsService.repositionPoints(a),e()}),30)}))},t.prototype.transform=function(){var t=this;return new Promise((function(e,i){t.processing.emit(!0),setTimeout((function(){var i=cv.imread(t.editedImage),n=[t.getPoint(["top","left"]),t.getPoint(["top","right"]),t.getPoint(["bottom","right"]),t.getPoint(["bottom","left"])].map((function(e){return[e.x/t.imageResizeRatio,e.y/t.imageResizeRatio]})),o=t.getPoint(["bottom","right"]).x-t.getPoint(["bottom","left"]).x,r=t.getPoint(["top","right"]).x-t.getPoint(["top","left"]).x,s=Math.max(o,r)/t.imageResizeRatio,a=t.getPoint(["bottom","left"]).y-t.getPoint(["top","left"]).y,c=t.getPoint(["bottom","right"]).y-t.getPoint(["top","right"]).y,h=Math.max(a,c)/t.imageResizeRatio,l=[[0,0],[s-1,0],[s-1,h-1],[0,h-1]],p=cv.matFromArray(4,1,cv.CV_32FC2,[].concat.apply([],d(n))),u=cv.matFromArray(4,1,cv.CV_32FC2,[].concat.apply([],d(l))),m=cv.getPerspectiveTransform(p,u),g=new cv.Size(s,h);cv.warpPerspective(i,i,m,g,cv.INTER_LINEAR,cv.BORDER_CONSTANT,new cv.Scalar),cv.imshow(t.editedImage,i),i.delete(),p.delete(),u.delete(),m.delete(),t.setPreviewPaneDimensions(t.editedImage),t.showPreview().then((function(){t.processing.emit(!1),e()}))}),30)}))},t.prototype.applyFilter=function(t){var e=this;return new Promise((function(i,n){return p(e,void 0,void 0,(function(){var e,n,o=this;return u(this,(function(r){switch(this.processing.emit(!0),e={blur:!1,th:!0,thMode:cv.ADAPTIVE_THRESH_MEAN_C,thMeanCorrection:10,thBlockSize:25,thMax:255,grayScale:!0},n=cv.imread(this.editedImage),this.selectedFilter){case"default":e.th=!1,e.grayScale=!1,e.blur=!1;break;case"magic_color":e.grayScale=!1;break;case"bw2":e.thMode=cv.ADAPTIVE_THRESH_GAUSSIAN_C,e.thMeanCorrection=15,e.thBlockSize=15;break;case"bw3":e.blur=!0,e.thMeanCorrection=15}return setTimeout((function(){return p(o,void 0,void 0,(function(){var o;return u(this,(function(r){switch(r.label){case 0:return e.grayScale&&cv.cvtColor(n,n,cv.COLOR_RGBA2GRAY,0),e.blur&&(o=new cv.Size(5,5),cv.GaussianBlur(n,n,o,0,0,cv.BORDER_DEFAULT)),e.th&&(e.grayScale?cv.adaptiveThreshold(n,n,e.thMax,e.thMode,cv.THRESH_BINARY,e.thBlockSize,e.thMeanCorrection):(n.convertTo(n,-1,1,60),cv.threshold(n,n,170,255,cv.THRESH_BINARY))),t||cv.imshow(this.editedImage,n),[4,this.showPreview(n)];case 1:return r.sent(),this.processing.emit(!1),i(),[2]}}))}))}),30),[2]}))}))}))},t.prototype.resize=function(t){var e=this;return new Promise((function(i,n){e.processing.emit(!0),setTimeout((function(){var n=cv.imread(t),o=n.size().width,r=n.size().height,s={width:0,height:0};if(o>e.options.maxImageDimensions.width){s.width=e.options.maxImageDimensions.width,s.height=e.options.maxImageDimensions.width/o*r,s.height>e.options.maxImageDimensions.height&&(s.height=e.options.maxImageDimensions.height,s.width=e.options.maxImageDimensions.height/r*o);var a=new cv.Size(Math.floor(s.width),Math.floor(s.height));cv.resize(n,n,a,0,0,cv.INTER_AREA);var c=document.createElement("canvas");cv.imshow(c,n),n.delete(),e.processing.emit(!1),i(c)}else e.processing.emit(!1),i(t)}),30)}))},t.prototype.showPreview=function(t){var e=this;return new Promise((function(i,n){var o;o=t||cv.imread(e.editedImage);var r=new cv.Mat,s=new cv.Size(0,0);cv.resize(o,r,s,e.imageResizeRatio,e.imageResizeRatio,cv.INTER_AREA),cv.imshow(e.previewCanvas.nativeElement,r),o.delete(),r.delete(),i()}))},t.prototype.setPreviewPaneDimensions=function(t){this.previewDimensions=this.calculateDimensions(t.width,t.height),this.previewCanvas.nativeElement.width=this.previewDimensions.width,this.previewCanvas.nativeElement.height=this.previewDimensions.height,this.imageResizeRatio=this.previewDimensions.width/t.width,this.imageDivStyle={width:this.previewDimensions.width+this.options.cropToolDimensions.width+"px",height:this.previewDimensions.height+this.options.cropToolDimensions.height+"px","margin-left":"calc((100% - "+(this.previewDimensions.width+10)+"px) / 2 + "+this.options.cropToolDimensions.width/2+"px)","margin-right":"calc((100% - "+(this.previewDimensions.width+10)+"px) / 2 - "+this.options.cropToolDimensions.width/2+"px)"},this.limitsService.setPaneDimensions({width:this.previewDimensions.width,height:this.previewDimensions.height})},t.prototype.calculateDimensions=function(t,e){var i=t/e,n=this.screenDimensions.width>this.maxPreviewWidth?this.maxPreviewWidth:this.screenDimensions.width-40,o=this.screenDimensions.height-240,r={width:n,height:Math.round(n/i),ratio:i};return r.height>o&&(r.height=o,r.width=Math.round(o*i)),r},t.prototype.getPoint=function(t){var e=this;return this.points.find((function(i){return e.limitsService.compareArray(i.roles,t)}))},t.decorators=[{type:e.Component,args:[{selector:"ngx-doc-scanner",template:'<div [ngStyle]="editorStyle" fxLayoutAlign="space-around" style="direction: ltr !important">\n <div #imageContainer [ngStyle]="imageDivStyle" style="margin: auto;" >\n <ng-container *ngIf="imageLoaded && mode === \'crop\'">\n <ngx-shape-outine #shapeOutline [color]="options.cropToolColor" [weight]="options.cropToolLineWeight" [dimensions]="previewDimensions"></ngx-shape-outine>\n <ngx-draggable-point #topLeft [pointOptions]="options.pointOptions" [startPosition]="{x: 0, y: 0}" [limitRoles]="[\'top\', \'left\']" [container]="imageContainer"></ngx-draggable-point>\n <ngx-draggable-point #topRight [pointOptions]="options.pointOptions" [startPosition]="{x: previewDimensions.width, y: 0}" [limitRoles]="[\'top\', \'right\']" [container]="imageContainer"></ngx-draggable-point>\n <ngx-draggable-point #bottomLeft [pointOptions]="options.pointOptions" [startPosition]="{x: 0, y: previewDimensions.height}" [limitRoles]="[\'bottom\', \'left\']" [container]="imageContainer"></ngx-draggable-point>\n <ngx-draggable-point #bottomRight [pointOptions]="options.pointOptions" [startPosition]="{x: previewDimensions.width, y: previewDimensions.height}" [limitRoles]="[\'bottom\', \'right\']" [container]="imageContainer"></ngx-draggable-point>\n </ng-container>\n <canvas #PreviewCanvas [ngStyle]="{\'max-width\': options.maxPreviewWidth}" style="z-index: 5" ></canvas>\n </div>\n <div class="editor-actions" fxLayout="row" fxLayoutAlign="space-around" style="position: absolute; bottom: 0; width: 100vw">\n <ng-container *ngFor="let button of displayedButtons" [ngSwitch]="button.type">\n <button mat-mini-fab *ngSwitchCase="\'fab\'" [name]="button.name" (click)="button.action()" [color]="options.buttonThemeColor">\n <mat-icon>{{button.icon}}</mat-icon>\n </button>\n <button mat-raised-button *ngSwitchCase="\'button\'" [name]="button.name" (click)="button.action()" [color]="options.buttonThemeColor">\n <mat-icon>{{button.icon}}</mat-icon>\n <span>{{button.text}}}</span>\n </button>\n </ng-container>\n </div>\n</div>\n\n\n',styles:[".editor-actions{padding:12px}.editor-actions button{margin:5px}"]}]}],t.ctorParameters=function(){return[{type:o.NgxOpenCVService},{type:g},{type:n.MatBottomSheet}]},t.propDecorators={previewCanvas:[{type:e.ViewChild,args:["PreviewCanvas",{read:e.ElementRef}]}],exitEditor:[{type:e.Output}],editResult:[{type:e.Output}],error:[{type:e.Output}],ready:[{type:e.Output}],processing:[{type:e.Output}],file:[{type:e.Input}],config:[{type:e.Input}]},t}();var x=function(t){var e=this;this.maxImageDimensions={width:800,height:1200},this.editorBackgroundColor="#fefefe",this.editorDimensions={width:"100vw",height:"100vh"},this.extraCss={position:"absolute",top:0,left:0},this.buttonThemeColor="accent",this.exportImageIcon="cloud_upload",this.cropToolColor="#3cabe2",this.cropToolShape="rect",this.cropToolDimensions={width:10,height:10},this.cropToolLineWeight=3,this.maxPreviewWidth=800,t&&Object.keys(t).forEach((function(i){e[i]=t[i]})),this.editorStyle={"background-color":this.editorBackgroundColor},Object.assign(this.editorStyle,this.editorDimensions),Object.assign(this.editorStyle,this.extraCss),this.pointOptions={shape:this.cropToolShape,color:this.cropToolColor,width:0,height:0},Object.assign(this.pointOptions,this.cropToolDimensions)};var S=function(){function t(){}return t.forRoot=function(e){return{ngModule:t,providers:[{provide:o.OpenCvConfigToken,useValue:e}]}},t.decorators=[{type:e.NgModule,args:[{declarations:[v,y,w,b],imports:[r.FlexLayoutModule,c.MatButtonModule,h.MatIconModule,n.MatBottomSheetModule,l.MatListModule,s.AngularDraggableModule,a.CommonModule,o.NgxOpenCVModule],exports:[r.FlexLayoutModule,c.MatButtonModule,h.MatIconModule,n.MatBottomSheetModule,l.MatListModule,s.AngularDraggableModule,b],entryComponents:[y],providers:[o.NgxOpenCVService,g]}]}],t}();t.NgxDocScannerComponent=b,t.NgxDocumentScannerModule=S,t.ɵa=v,t.ɵb=g,t.ɵc=y,t.ɵd=w,Object.defineProperty(t,"__esModule",{value:!0})}));
//# sourceMappingURL=autonomdev-ngx-document-scanner.umd.min.js.map