diff --git a/html5-qrcode.js b/html5-qrcode.js index aabf23c..ec81372 100644 --- a/html5-qrcode.js +++ b/html5-qrcode.js @@ -513,7 +513,19 @@ class Html5Qrcode { && navigator.mediaDevices.getUserMedia) { this._log("navigator.mediaDevices used"); navigator.mediaDevices.getUserMedia({ audio: false, video: true }) - .then(_ => { + .then(stream => { + // hacky approach to close any active stream if they are active. + stream.oninactive = _ => this._log("All streams closed"); + const closeActiveStreams = stream => { + const tracks = stream.getVideoTracks(); + for (var i = 0; i < tracks.length; i++) { + const track = tracks[i]; + track.enabled = false; + track.stop(); + stream.removeTrack(track); + } + } + navigator.mediaDevices.enumerateDevices() .then(devices => { const results = []; @@ -527,6 +539,7 @@ class Html5Qrcode { } } this._log(`${results.length} results found`); + closeActiveStreams(stream); resolve(results); }) .catch(err => { diff --git a/minified/html5-qrcode.min.js b/minified/html5-qrcode.min.js index 3b87ae6..45733d4 100644 --- a/minified/html5-qrcode.min.js +++ b/minified/html5-qrcode.min.js @@ -2,4 +2,4 @@ function ECB(e,t){this.count=e,this.dataCodewords=t,this.__defineGetter__("Count s[0]=0,s[1]=0,s[2]=0;for(var a=e;a>=0&&r[t+a*qrcode.width]&&s[1]<=n;)s[1]++,a--;if(0>a||s[1]>n)return 0/0;for(;a>=0&&!r[t+a*qrcode.width]&&s[0]<=n;)s[0]++,a--;if(s[0]>n)return 0/0;for(a=e+1;o>a&&r[t+a*qrcode.width]&&s[1]<=n;)s[1]++,a++;if(a==o||s[1]>n)return 0/0;for(;o>a&&!r[t+a*qrcode.width]&&s[2]<=n;)s[2]++,a++;return s[2]>n||5*Math.abs(s[0]+s[1]+s[2]-i)>=2*i?0/0:this.foundPatternCross(s)?this.centerFromEnd(s,a):0/0},this.handlePossibleCenter=function(e,t,n){var i=e[0]+e[1]+e[2];if(n=this.centerFromEnd(e,n),t=this.crossCheckVertical(t,Math.floor(n),2*e[1],i),!isNaN(t)){e=(e[0]+e[1]+e[2])/3;for(var i=this.possibleCenters.length,r=0;i>r;r++)if(this.possibleCenters[r].aboutEquals(e,t,n))return new AlignmentPattern(n,t,e);n=new AlignmentPattern(n,t,e),this.possibleCenters.push(n),null!=this.resultPointCallback&&this.resultPointCallback.foundPossibleResultPoint(n)}return null},this.find=function(){for(var t=this.startX,r=this.height,o=t+i,s=n+(r>>1),a=[0,0,0],h=0;r>h;h++){var c=s+(0==(1&h)?h+1>>1:-(h+1>>1));a[0]=0,a[1]=0,a[2]=0;for(var l=t;o>l&&!e[l+qrcode.width*c];)l++;for(var f=0;o>l;){if(e[l+c*qrcode.width])if(1==f)a[f]++;else if(2==f){if(this.foundPatternCross(a)&&(f=this.handlePossibleCenter(a,c,l),null!=f))return f;a[0]=a[2],a[1]=1,a[2]=0,f=1}else a[++f]++;else 1==f&&f++,a[f]++;l++}if(this.foundPatternCross(a)&&(f=this.handlePossibleCenter(a,c,o),null!=f))return f}if(0!=this.possibleCenters.length)return this.possibleCenters[0];throw"Couldn't find enough alignment patterns"}}function QRCodeDataBlockReader(e,t,n){this.blockPointer=0,this.bitPointer=7,this.dataLength=0,this.blocks=e,this.numErrorCorrectionCode=n,9>=t?this.dataLengthMode=0:t>=10&&26>=t?this.dataLengthMode=1:t>=27&&40>=t&&(this.dataLengthMode=2),this.getNextBits=function(e){var t=0;if(et;t++)n+=1<>this.bitPointer-e+1,this.bitPointer-=e,t}if(e>8-(e-(this.bitPointer+1)),this.bitPointer-=e%8,0>this.bitPointer&&(this.bitPointer=8+this.bitPointer),t}if(e>8-(e-(this.bitPointer+1+8))),this.bitPointer-=(e-8)%8,0>this.bitPointer&&(this.bitPointer=8+this.bitPointer),t}return 0},this.NextMode=function(){return this.blockPointer>this.blocks.length-this.numErrorCorrectionCode-2?0:this.getNextBits(4)},this.getDataLength=function(e){for(var t=0;1!=e>>t;)t++;return this.getNextBits(qrcode.sizeOfDataLengthInfo[this.dataLengthMode][t])},this.getRomanAndFigureString=function(e){var t=0,n="",i="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:".split("");do if(e>1){var t=this.getNextBits(11),r=t%45,n=n+i[Math.floor(t/45)],n=n+i[r];e-=2}else 1==e&&(t=this.getNextBits(6),n+=i[t],e-=1);while(e>0);return n},this.getFigureString=function(e){var t=0,n="";do e>=3?(t=this.getNextBits(10),100>t&&(n+="0"),10>t&&(n+="0"),e-=3):2==e?(t=this.getNextBits(7),10>t&&(n+="0"),e-=2):1==e&&(t=this.getNextBits(4),e-=1),n+=t;while(e>0);return n},this.get8bitByteArray=function(e){var t=0,n=[];do t=this.getNextBits(8),n.push(t),e--;while(e>0);return n},this.getKanjiString=function(e){var t=0,n="";do{var t=getNextBits(13),t=(t/192<<8)+t%192,i=0,i=40956>=t+33088?t+33088:t+49472,n=n+String.fromCharCode(i);e--}while(e>0);return n},this.__defineGetter__("DataByte",function(){for(var e=[];;){var t=this.NextMode();if(0==t){if(0dataLength)throw"Invalid data length: "+dataLength;switch(t){case 1:for(var t=this.getFigureString(dataLength),n=Array(t.length),i=0;is||s>n||-1>a||a>i)throw"Error.checkAndNudgePoints ";r=!1,-1==s?(t[o]=0,r=!0):s==n&&(t[o]=n-1,r=!0),-1==a?(t[o+1]=0,r=!0):a==i&&(t[o+1]=i-1,r=!0)}for(r=!0,o=t.Length-2;o>=0&&r;o-=2){if(s=Math.floor(t[o]),a=Math.floor(t[o+1]),-1>s||s>n||-1>a||a>i)throw"Error.checkAndNudgePoints ";r=!1,-1==s?(t[o]=0,r=!0):s==n&&(t[o]=n-1,r=!0),-1==a?(t[o+1]=0,r=!0):a==i&&(t[o+1]=i-1,r=!0)}},sampleGrid3:function(e,t,n){for(var i=new BitMatrix(t),r=Array(t<<1),o=0;t>o;o++){for(var s=r.length,a=o+.5,h=0;s>h;h+=2)r[h]=(h>>1)+.5,r[h+1]=a;n.transformPoints1(r),GridSampler.checkAndNudgePoints(e,r);try{for(h=0;s>h;h+=2){var c=4*Math.floor(r[h])+4*Math.floor(r[h+1])*qrcode.width,l=e[Math.floor(r[h])+qrcode.width*Math.floor(r[h+1])];qrcode.imagedata.data[c]=l?255:0,qrcode.imagedata.data[c+1]=l?255:0,qrcode.imagedata.data[c+2]=0,qrcode.imagedata.data[c+3]=255,l&&i.set_Renamed(h>>1,o)}}catch(f){throw"Error.checkAndNudgePoints"}}return i},sampleGridx:function(e,t,n,i,r,o,s,a,h,c,l,f,d,w,u,C,E,B){return n=PerspectiveTransform.quadrilateralToQuadrilateral(n,i,r,o,s,a,h,c,l,f,d,w,u,C,E,B),GridSampler.sampleGrid3(e,t,n)}},Version.VERSION_DECODE_INFO=[31892,34236,39577,42195,48118,51042,55367,58893,63784,68472,70749,76311,79154,84390,87683,92361,96236,102084,102881,110507,110734,117786,119615,126325,127568,133589,136944,141498,145311,150283,152622,158308,161089,167017],Version.VERSIONS=buildVersions(),Version.getVersionForNumber=function(e){if(1>e||e>40)throw"ArgumentException";return Version.VERSIONS[e-1]},Version.getProvisionalVersionForDimension=function(e){if(1!=e%4)throw"Error getProvisionalVersionForDimension";try{return Version.getVersionForNumber(e-17>>2)}catch(t){throw"Error getVersionForNumber"}},Version.decodeVersionInformation=function(e){for(var t=4294967295,n=0,i=0;ir&&(n=i+7,t=r)}return 3>=t?this.getVersionForNumber(n):null},PerspectiveTransform.quadrilateralToQuadrilateral=function(e,t,n,i,r,o,s,a,h,c,l,f,d,w,u,C){return e=this.quadrilateralToSquare(e,t,n,i,r,o,s,a),this.squareToQuadrilateral(h,c,l,f,d,w,u,C).times(e)},PerspectiveTransform.squareToQuadrilateral=function(e,t,n,i,r,o,s,a){return dy2=a-o,dy3=t-i+o-a,0==dy2&&0==dy3?new PerspectiveTransform(n-e,r-n,e,i-t,o-i,t,0,0,1):(dx1=n-r,dx2=s-r,dx3=e-n+r-s,dy1=i-o,denominator=dx1*dy2-dx2*dy1,a13=(dx3*dy2-dx2*dy3)/denominator,a23=(dx1*dy3-dx3*dy1)/denominator,new PerspectiveTransform(n-e+a13*n,s-e+a23*s,e,i-t+a13*i,a-t+a23*a,t,a13,a23,1))},PerspectiveTransform.quadrilateralToSquare=function(e,t,n,i,r,o,s,a){return this.squareToQuadrilateral(e,t,n,i,r,o,s,a).buildAdjoint()};var FORMAT_INFO_MASK_QR=21522,FORMAT_INFO_DECODE_LOOKUP=[[21522,0],[20773,1],[24188,2],[23371,3],[17913,4],[16590,5],[20375,6],[19104,7],[30660,8],[29427,9],[32170,10],[30877,11],[26159,12],[25368,13],[27713,14],[26998,15],[5769,16],[5054,17],[7399,18],[6608,19],[1890,20],[597,21],[3340,22],[2107,23],[13663,24],[12392,25],[16177,26],[14854,27],[9396,28],[8579,29],[11994,30],[11245,31]],BITS_SET_IN_HALF_BYTE=[0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4];FormatInformation.numBitsDiffering=function(e,t){return e^=t,BITS_SET_IN_HALF_BYTE[15&e]+BITS_SET_IN_HALF_BYTE[15&URShift(e,4)]+BITS_SET_IN_HALF_BYTE[15&URShift(e,8)]+BITS_SET_IN_HALF_BYTE[15&URShift(e,12)]+BITS_SET_IN_HALF_BYTE[15&URShift(e,16)]+BITS_SET_IN_HALF_BYTE[15&URShift(e,20)]+BITS_SET_IN_HALF_BYTE[15&URShift(e,24)]+BITS_SET_IN_HALF_BYTE[15&URShift(e,28)]},FormatInformation.decodeFormatInformation=function(e){var t=FormatInformation.doDecodeFormatInformation(e);return null!=t?t:FormatInformation.doDecodeFormatInformation(e^FORMAT_INFO_MASK_QR)},FormatInformation.doDecodeFormatInformation=function(e){for(var t=4294967295,n=0,i=0;io&&(n=r[1],t=o)}return 3>=t?new FormatInformation(n):null},ErrorCorrectionLevel.forBits=function(e){if(0>e||e>=FOR_BITS.Length)throw"ArgumentException";return FOR_BITS[e]};var L=new ErrorCorrectionLevel(0,1,"L"),M=new ErrorCorrectionLevel(1,0,"M"),Q=new ErrorCorrectionLevel(2,3,"Q"),H=new ErrorCorrectionLevel(3,2,"H"),FOR_BITS=[M,L,H,Q];DataBlock.getDataBlocks=function(e,t,n){if(e.length!=t.TotalCodewords)throw"ArgumentException";var i=t.getECBlocksForLevel(n);n=0;var r=i.getECBlocks();for(t=0;t=0&&n[r].codewords.length!=t;)r--;for(r++,i=t-i.ECCodewordsPerBlock,t=a=0;i>t;t++)for(s=0;o>s;s++)n[s].codewords[t]=e[a++];for(s=r;o>s;s++)n[s].codewords[i]=e[a++];for(h=n[0].codewords.length,t=i;h>t;t++)for(s=0;o>s;s++)n[s].codewords[r>s?t:t+1]=e[a++];return n},DataMask={forReference:function(e){if(0>e||e>7)throw"System.ArgumentException";return DataMask.DATA_MASKS[e]}},DataMask.DATA_MASKS=[new DataMask000,new DataMask001,new DataMask010,new DataMask011,new DataMask100,new DataMask101,new DataMask110,new DataMask111],GF256.QR_CODE_FIELD=new GF256(285),GF256.DATA_MATRIX_FIELD=new GF256(301),GF256.addOrSubtract=function(e,t){return e^t},Decoder={},Decoder.rsDecoder=new ReedSolomonDecoder(GF256.QR_CODE_FIELD),Decoder.correctErrors=function(e,t){for(var n=e.length,i=Array(n),r=0;n>r;r++)i[r]=255&e[r];n=e.length-t;try{Decoder.rsDecoder.decode(i,n)}catch(o){throw o}for(r=0;t>r;r++)e[r]=i[r]},Decoder.decode=function(e){var t=new BitMatrixParser(e);e=t.readVersion();for(var n=t.readFormatInformation().ErrorCorrectionLevel,t=t.readCodewords(),t=DataBlock.getDataBlocks(t,e,n),i=0,r=0;rr;r++)i[o++]=a[r]}return new QRCodeDataBlockReader(i,e.VersionNumber,n.Bits)},qrcode={imagedata:null,width:0,height:0,qrCodeSymbol:null,debug:!1,sizeOfDataLengthInfo:[[10,9,8,8],[12,11,16,10],[14,13,16,12]],callback:null,decode:function(e){if(0==arguments.length){var t=document.getElementById("qr-canvas"),n=t.getContext("2d");return qrcode.width=t.width,qrcode.height=t.height,qrcode.imagedata=n.getImageData(0,0,qrcode.width,qrcode.height),qrcode.result=qrcode.process(n),null!=qrcode.callback&&qrcode.callback(qrcode.result),qrcode.result}var i=new Image;i.onload=function(){var e=document.createElement("canvas"),t=e.getContext("2d"),n=document.getElementById("out-canvas");null!=n&&(n=n.getContext("2d"),n.clearRect(0,0,320,240),n.drawImage(i,0,0,320,240)),e.width=i.width,e.height=i.height,t.drawImage(i,0,0),qrcode.width=i.width,qrcode.height=i.height;try{qrcode.imagedata=t.getImageData(0,0,i.width,i.height)}catch(r){return qrcode.result="Cross domain image reading not supported in your browser! Save it to your computer then drag and drop the file!",void(null!=qrcode.callback&&qrcode.callback(qrcode.result))}try{qrcode.result=qrcode.process(t)}catch(o){console.log(o),qrcode.result="error decoding QR Code"}null!=qrcode.callback&&qrcode.callback(qrcode.result)},i.src=e},decode_utf8:function(e){return decodeURIComponent(escape(e))},process:function(e){var t=(new Date).getTime(),n=qrcode.grayScaleToBitmap(qrcode.grayscale());if(qrcode.debug){for(var i=0;i=r?!0:!1}return t},getMiddleBrightnessPerArea:function(e){for(var t=Math.floor(qrcode.width/4),n=Math.floor(qrcode.height/4),i=Array(4),r=0;4>r;r++){i[r]=Array(4);for(var o=0;4>o;o++)i[r][o]=[0,0]}for(r=0;4>r;r++)for(o=0;4>o;o++){i[o][r][0]=255;for(var s=0;n>s;s++)for(var a=0;t>a;a++){var h=e[t*o+a+(n*r+s)*qrcode.width];hi[o][r][1]&&(i[o][r][1]=h)}}for(e=Array(4),t=0;4>t;t++)e[t]=Array(4);for(r=0;4>r;r++)for(o=0;4>o;o++)e[o][r]=Math.floor((i[o][r][0]+i[o][r][1])/2);return e},grayScaleToBitmap:function(e){for(var t=qrcode.getMiddleBrightnessPerArea(e),n=t.length,i=Math.floor(qrcode.width/n),r=Math.floor(qrcode.height/n),o=Array(qrcode.height*qrcode.width),s=0;n>s;s++)for(var a=0;n>a;a++)for(var h=0;r>h;h++)for(var c=0;i>c;c++)o[i*a+c+(r*s+h)*qrcode.width]=e[i*a+c+(r*s+h)*qrcode.width]e?this.length+e:e,this.push.apply(this,n)};var MIN_SKIP=3,MAX_MODULES=57,INTEGER_MATH_SHIFT=8,CENTER_QUORUM=2;qrcode.orderBestPatterns=function(e){function t(e,t){return xDiff=e.X-t.X,yDiff=e.Y-t.Y,Math.sqrt(xDiff*xDiff+yDiff*yDiff)}var n=t(e[0],e[1]),i=t(e[1],e[2]),r=t(e[0],e[2]);i>=n&&i>=r?(i=e[0],n=e[1],r=e[2]):r>=i&&r>=n?(i=e[1],n=e[0],r=e[2]):(i=e[2],n=e[0],r=e[1]);var o=i.x,s=i.y;0>(r.x-o)*(n.y-s)-(r.y-s)*(n.x-o)&&(o=n,n=r,r=o),e[0]=n,e[1]=i,e[2]=r}; /** html5qrcode js below */ -"use strict";function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function _defineProperties(a,b){for(var c,d=0;dk)throw"'config.qrbox' should not be greater than the width of the HTML element."}var m=function(a,b){var c=h.qrbox;c>b&&console.warn("[Html5Qrcode] config.qrboxsize is greater than video height. Shading will be ignored");var d=i&&c<=b,e=d?f._getShadedRegionBounds(a,b,c):{x:0,y:0,width:a,height:b},k=f._createCanvasElement(e.width,e.height),l=k.getContext("2d");l.canvas.width=e.width,l.canvas.height=e.height,j.append(k),d&&f._possiblyInsertShadingElement(j,b,e),g._qrRegion=e,g._context=l,g._canvasElement=k},n=function b(){if(g._shouldScan){if(g._localMediaStream){var c=g._videoElement,d=c.videoWidth/c.clientWidth,i=c.videoHeight/c.clientHeight,j=g._qrRegion.width*d,k=g._qrRegion.height*i;g._context.drawImage(g._videoElement,g._qrRegion.x,g._qrRegion.y,j,k,0,0,g._qrRegion.width,g._qrRegion.height);try{qrcode.decode(),f._possiblyUpdateShaders(!0)}catch(a){f._possiblyUpdateShaders(!1),e("QR code parse error, error = ".concat(a))}}g._foreverScanTimeout=setTimeout(b,a._getTimeoutFps(h.fps))}},o=function(a){return new Promise(function(b,c){g._localMediaStream=a,function(){var d=f._createVideoElement(k);g._element.append(d),d.onabort=c,d.onerror=c,d.onplaying=function(){var a=d.clientWidth,c=d.clientHeight;m(a,c),n(),b()},d.srcObject=a,d.play(),g._videoElement=d}()})};return new Promise(function(a,c){if(navigator.mediaDevices&&navigator.mediaDevices.getUserMedia){navigator.mediaDevices.getUserMedia({audio:!1,video:{deviceId:{exact:b}}}).then(function(b){o(b).then(function(){g._isScanning=!0,a()})["catch"](c)})["catch"](function(a){c("Error getting userMedia, error = ".concat(a))})}else if(navigator.getUserMedia){navigator.getUserMedia({video:{optional:[{sourceId:b}]}},function(b){o(b).then(function(){g._isScanning=!0,a()})["catch"](c)},function(a){c("Error getting userMedia, error = ".concat(a))})}else c("Web camera streaming not supported by the browser.")})}},{key:"stop",value:function(){this._shouldScan=!1,clearTimeout(this._foreverScanTimeout);var b=this;return new Promise(function(c){qrcode.callback=null;var d=b._localMediaStream.getVideoTracks().length,e=0,f=function(){for(;b._element.getElementsByClassName(a.SHADED_REGION_CLASSNAME).length;){var c=b._element.getElementsByClassName(a.SHADED_REGION_CLASSNAME)[0];b._element.removeChild(c)}},g=function(){b._localMediaStream=null,b._element.removeChild(b._videoElement),b._element.removeChild(b._canvasElement),f(),b._isScanning=!1,b._qrRegion&&(b._qrRegion=null),b._context&&(b._context=null),c(!0)};b._localMediaStream.getVideoTracks().forEach(function(a){a.stop(),++e,e>=d&&g()})})}},{key:"scanFile",value:function(b,c){var d=this;if(!b||!(b instanceof File))throw"imageFile argument is mandatory and should be instance of File. Use 'event.target.files[0]'";if(c=void 0===c||c,d._isScanning)throw"Close ongoing scan before scanning a file.";var e=function b(c,d,e,f){if(c<=e&&d<=f){var g=(e-c)/2,h=(f-d)/2;return{x:g,y:h,width:c,height:d}}var i=c,j=d;return c>e&&(d=e/c*d,c=e),d>f&&(c=f/d*c,d=f),a._log("Image downsampled from ".concat(i,"X").concat(j)+" to ".concat(c,"X").concat(d,".")),b(c,d,e,f)};return new Promise(function(f,g){d._possiblyCloseLastScanImageFile(),d._clearElement(),d._lastScanImageFile=b;var h=new Image;h.onload=function(){var b=h.width,i=h.height,j=document.getElementById(d._elementId),k=j.clientWidth?j.clientWidth:a.DEFAULT_WIDTH,l=j.clientHeight?j.clientHeight:i,m=e(b,i,k,l);if(c){var n=d._createCanvasElement(k,l,"qr-canvas-visible");n.style.display="inline-block",j.appendChild(n);var o=n.getContext("2d");o.canvas.width=k,o.canvas.height=l,o.drawImage(h,0,0,b,i,m.x,m.y,m.width,m.height)}var p=d._createCanvasElement(m.width,m.height);j.appendChild(p);var q=p.getContext("2d");q.canvas.width=m.width,q.canvas.height=m.height,q.drawImage(h,0,0,b,i,0,0,m.width,m.height);try{f(qrcode.decode())}catch(a){g("QR code parse error, error = ".concat(a))}},h.onerror=g,h.onabort=g,h.onstalled=g,h.onsuspend=g,h.src=URL.createObjectURL(b)})}},{key:"clear",value:function(){this._clearElement()}},{key:"_clearElement",value:function(){if(this._isScanning)throw"Cannot clear while scan is ongoing, close it first.";var a=document.getElementById(this._elementId);a.innerHTML=""}},{key:"_createCanvasElement",value:function(a,b,c){var d=document.createElement("canvas");return d.style.width="".concat(a,"px"),d.style.height="".concat(b,"px"),d.style.display="none",d.id=null==c?"qr-canvas":c,d}},{key:"_createVideoElement",value:function(a){var b=document.createElement("video");return b.style.width="".concat(a,"px"),b.muted=!0,b.playsInline=!0,b}},{key:"_getShadedRegionBounds",value:function(a,b,c){if(c>a||c>b)throw"'config.qrbox' should not be greater than the width and height of the HTML element.";return{x:(a-c)/2,y:(b-c)/2,width:c,height:c}}},{key:"_possiblyInsertShadingElement",value:function(b,c,d){var e=this;if(0!=d.x||0!=d.y){var f={};f[a.SHADED_LEFT]=this._createShadedElement(c,d,a.SHADED_LEFT),f[a.SHADED_RIGHT]=this._createShadedElement(c,d,a.SHADED_RIGHT),f[a.SHADED_TOP]=this._createShadedElement(c,d,a.SHADED_TOP),f[a.SHADED_BOTTOM]=this._createShadedElement(c,d,a.SHADED_BOTTOM),Object.keys(f).forEach(function(a){return b.append(f[a])}),10>d.x||10>d.y?this.hasBorderShaders=!1:(Object.keys(f).forEach(function(a){return e._insertShaderBorders(f[a],d,a)}),this.hasBorderShaders=!0)}}},{key:"_createShadedElement",value:function(b,c,d){var e=document.createElement("div");switch(e.style.position="absolute",e.style.height="".concat(b,"px"),e.className=a.SHADED_REGION_CLASSNAME,e.id="".concat(a.SHADED_REGION_CLASSNAME,"_").concat(d),e.style.background="#0000007a",d){case a.SHADED_LEFT:e.style.top="0px",e.style.left="0px",e.style.width="".concat(c.x,"px"),e.style.height="".concat(b,"px");break;case a.SHADED_RIGHT:e.style.top="0px",e.style.right="0px",e.style.width="".concat(c.x,"px"),e.style.height="".concat(b,"px");break;case a.SHADED_TOP:e.style.top="0px",e.style.left="".concat(c.x,"px"),e.style.width="".concat(c.width,"px"),e.style.height="".concat(c.y,"px");break;case a.SHADED_BOTTOM:var f=c.y+c.height;e.style.top="".concat(f,"px"),e.style.left="".concat(c.x,"px"),e.style.width="".concat(c.width,"px"),e.style.height="".concat(c.y,"px");break;default:throw"Unsupported shadingPosition";}return e}},{key:"_insertShaderBorders",value:function(b,c,d){d=parseInt(d);var e=this,f=5,g=5,h=40,i=function(){var b=document.createElement("div");switch(b.style.position="absolute",b.style.backgroundColor=a.BORDER_SHADER_DEFAULT_COLOR,d){case a.SHADED_LEFT:case a.SHADED_RIGHT:b.style.width="".concat(g,"px"),b.style.height="".concat(h+f,"px");break;case a.SHADED_TOP:case a.SHADED_BOTTOM:b.style.width="".concat(h+f,"px"),b.style.height="".concat(g,"px");break;default:throw"Unsupported shadingPosition";}return b},j=function(a,c){if(null===a||null===c)throw"Shaders should have defined positions";var d=i();d.style.top="".concat(a,"px"),d.style.left="".concat(c,"px"),b.appendChild(d),e.borderShaders||(e.borderShaders=[]),e.borderShaders.push(d)},k=null,l=null,m=null,n=null;switch(d){case a.SHADED_LEFT:k=c.y-f,l=c.x-g,m=c.y+c.height-h,n=l;break;case a.SHADED_RIGHT:k=c.y-f,l=0,m=c.y+c.height-h,n=l;break;case a.SHADED_TOP:k=c.y-f,l=-g,m=k,n=c.width-h;break;case a.SHADED_BOTTOM:k=0,l=-g,m=k,n=c.width-h;break;default:throw"Unsupported shadingPosition";}j(k,l),j(m,n)}},{key:"_possiblyUpdateShaders",value:function(b){this.qrMatch===b||(this.hasBorderShaders&&this.borderShaders&&this.borderShaders.length&&this.borderShaders.forEach(function(c){c.style.backgroundColor=b?a.BORDER_SHADER_MATCH_COLOR:a.BORDER_SHADER_DEFAULT_COLOR}),this.qrMatch=b)}},{key:"_possiblyCloseLastScanImageFile",value:function(){this._lastScanImageFile&&(URL.revokeObjectURL(this._lastScanImageFile),this._lastScanImageFile=null)}}],[{key:"getCameras",value:function(){var a=this;return new Promise(function(b,c){if(navigator.mediaDevices&&navigator.mediaDevices.enumerateDevices&&navigator.mediaDevices.getUserMedia)a._log("navigator.mediaDevices used"),navigator.mediaDevices.getUserMedia({audio:!1,video:!0}).then(function(){navigator.mediaDevices.enumerateDevices().then(function(c){for(var d,e=[],f=0;fd)throw"'config.qrbox' should not be greater than the width of the HTML element."}var u=function(t){return new Promise(function(n,i){var h;o._localMediaStream=t,h=r._createVideoElement(d),o._element.append(h),h.onabort=i,h.onerror=i,h.onplaying=function(){!function(e,t){var n=c.qrbox;n>t&&console.warn("[Html5Qrcode] config.qrboxsize is greater than video height. Shading will be ignored");var i=s&&n<=t,a={x:0,y:0,width:e,height:t},d=i?r._getShadedRegionBounds(e,t,n):a,h=r._createCanvasElement(d.width,d.height),u=h.getContext("2d");u.canvas.width=d.width,u.canvas.height=d.height,l.append(h),i&&r._possiblyInsertShadingElement(l,t,d),o._qrRegion=d,o._context=u,o._canvasElement=h}(h.clientWidth,h.clientHeight),function t(){if(o._shouldScan){if(o._localMediaStream){var n=o._videoElement,i=n.videoWidth/n.clientWidth,s=n.videoHeight/n.clientHeight,l=o._qrRegion.width*i,d=o._qrRegion.height*s;o._context.drawImage(o._videoElement,o._qrRegion.x,o._qrRegion.y,l,d,0,0,o._qrRegion.width,o._qrRegion.height);try{qrcode.decode(),r._possiblyUpdateShaders(!0)}catch(e){r._possiblyUpdateShaders(!1),a("QR code parse error, error = ".concat(e))}}o._foreverScanTimeout=setTimeout(t,e._getTimeoutFps(c.fps))}}(),n()},h.srcObject=t,h.play(),o._videoElement=h})};return new Promise(function(e,n){if(navigator.mediaDevices&&navigator.mediaDevices.getUserMedia){var i={deviceId:{exact:t}};navigator.mediaDevices.getUserMedia({audio:!1,video:i}).then(function(t){u(t).then(function(t){o._isScanning=!0,e()}).catch(n)}).catch(function(e){n("Error getting userMedia, error = ".concat(e))})}else if(navigator.getUserMedia){var a={video:{optional:[{sourceId:t}]}};navigator.getUserMedia(a,function(t){u(t).then(function(t){o._isScanning=!0,e()}).catch(n)},function(e){n("Error getting userMedia, error = ".concat(e))})}else n("Web camera streaming not supported by the browser.")})}},{key:"stop",value:function(){this._shouldScan=!1,clearTimeout(this._foreverScanTimeout);var t=this;return new Promise(function(n,i){qrcode.callback=null;var a=t._localMediaStream.getVideoTracks().length,r=0,o=function(){t._localMediaStream=null,t._element.removeChild(t._videoElement),t._element.removeChild(t._canvasElement),function(){for(;t._element.getElementsByClassName(e.SHADED_REGION_CLASSNAME).length;){var n=t._element.getElementsByClassName(e.SHADED_REGION_CLASSNAME)[0];t._element.removeChild(n)}}(),t._isScanning=!1,t._qrRegion&&(t._qrRegion=null),t._context&&(t._context=null),n(!0)};t._localMediaStream.getVideoTracks().forEach(function(e){e.stop(),++r>=a&&o()})})}},{key:"scanFile",value:function(t,n){var i=this;if(!(t&&t instanceof File))throw"imageFile argument is mandatory and should be instance of File. Use 'event.target.files[0]'";if(n=void 0===n||n,i._isScanning)throw"Close ongoing scan before scanning a file.";return new Promise(function(a,r){i._possiblyCloseLastScanImageFile(),i._clearElement(),i._lastScanImageFile=t;var o=new Image;o.onload=function(){var t=o.width,c=o.height,s=document.getElementById(i._elementId),l=s.clientWidth?s.clientWidth:e.DEFAULT_WIDTH,d=s.clientHeight?s.clientHeight:c,h=function t(n,i,a,r){if(n<=a&&i<=r)return{x:(a-n)/2,y:(r-i)/2,width:n,height:i};var o=n,c=i;return n>a&&(i*=a/n,n=a),i>r&&(n*=r/i,i=r),e._log("Image downsampled from ".concat(o,"X").concat(c)+" to ".concat(n,"X").concat(i,".")),t(n,i,a,r)}(t,c,l,d);if(n){var u=i._createCanvasElement(l,d,"qr-canvas-visible");u.style.display="inline-block",s.appendChild(u);var _=u.getContext("2d");_.canvas.width=l,_.canvas.height=d,_.drawImage(o,0,0,t,c,h.x,h.y,h.width,h.height)}var g=i._createCanvasElement(h.width,h.height);s.appendChild(g);var f=g.getContext("2d");f.canvas.width=h.width,f.canvas.height=h.height,f.drawImage(o,0,0,t,c,0,0,h.width,h.height);try{a(qrcode.decode())}catch(e){r("QR code parse error, error = ".concat(e))}},o.onerror=r,o.onabort=r,o.onstalled=r,o.onsuspend=r,o.src=URL.createObjectURL(t)})}},{key:"clear",value:function(){this._clearElement()}},{key:"_clearElement",value:function(){if(this._isScanning)throw"Cannot clear while scan is ongoing, close it first.";document.getElementById(this._elementId).innerHTML=""}},{key:"_createCanvasElement",value:function(e,t,n){var i=e,a=t,r=document.createElement("canvas");return r.style.width="".concat(i,"px"),r.style.height="".concat(a,"px"),r.style.display="none",r.id=null==n?"qr-canvas":n,r}},{key:"_createVideoElement",value:function(e){var t=document.createElement("video");return t.style.width="".concat(e,"px"),t.muted=!0,t.playsInline=!0,t}},{key:"_getShadedRegionBounds",value:function(e,t,n){if(n>e||n>t)throw"'config.qrbox' should not be greater than the width and height of the HTML element.";return{x:(e-n)/2,y:(t-n)/2,width:n,height:n}}},{key:"_possiblyInsertShadingElement",value:function(t,n,i){var a=this;if(0!=i.x||0!=i.y){var r={};r[e.SHADED_LEFT]=this._createShadedElement(n,i,e.SHADED_LEFT),r[e.SHADED_RIGHT]=this._createShadedElement(n,i,e.SHADED_RIGHT),r[e.SHADED_TOP]=this._createShadedElement(n,i,e.SHADED_TOP),r[e.SHADED_BOTTOM]=this._createShadedElement(n,i,e.SHADED_BOTTOM),Object.keys(r).forEach(function(e){return t.append(r[e])}),i.x<10||i.y<10?this.hasBorderShaders=!1:(Object.keys(r).forEach(function(e){return a._insertShaderBorders(r[e],i,e)}),this.hasBorderShaders=!0)}}},{key:"_createShadedElement",value:function(t,n,i){var a=document.createElement("div");switch(a.style.position="absolute",a.style.height="".concat(t,"px"),a.className=e.SHADED_REGION_CLASSNAME,a.id="".concat(e.SHADED_REGION_CLASSNAME,"_").concat(i),a.style.background="#0000007a",i){case e.SHADED_LEFT:a.style.top="0px",a.style.left="0px",a.style.width="".concat(n.x,"px"),a.style.height="".concat(t,"px");break;case e.SHADED_RIGHT:a.style.top="0px",a.style.right="0px",a.style.width="".concat(n.x,"px"),a.style.height="".concat(t,"px");break;case e.SHADED_TOP:a.style.top="0px",a.style.left="".concat(n.x,"px"),a.style.width="".concat(n.width,"px"),a.style.height="".concat(n.y,"px");break;case e.SHADED_BOTTOM:var r=n.y+n.height;a.style.top="".concat(r,"px"),a.style.left="".concat(n.x,"px"),a.style.width="".concat(n.width,"px"),a.style.height="".concat(n.y,"px");break;default:throw"Unsupported shadingPosition"}return a}},{key:"_insertShaderBorders",value:function(t,n,i){i=parseInt(i);var a=this,r=function(n,r){if(null===n||null===r)throw"Shaders should have defined positions";var o=function(){var t=document.createElement("div");switch(t.style.position="absolute",t.style.backgroundColor=e.BORDER_SHADER_DEFAULT_COLOR,i){case e.SHADED_LEFT:case e.SHADED_RIGHT:t.style.width="".concat(5,"px"),t.style.height="".concat(45,"px");break;case e.SHADED_TOP:case e.SHADED_BOTTOM:t.style.width="".concat(45,"px"),t.style.height="".concat(5,"px");break;default:throw"Unsupported shadingPosition"}return t}();o.style.top="".concat(n,"px"),o.style.left="".concat(r,"px"),t.appendChild(o),a.borderShaders||(a.borderShaders=[]),a.borderShaders.push(o)},o=null,c=null,s=null,l=null;switch(i){case e.SHADED_LEFT:o=n.y-5,c=n.x-5,s=n.y+n.height-40,l=c;break;case e.SHADED_RIGHT:o=n.y-5,c=0,s=n.y+n.height-40,l=c;break;case e.SHADED_TOP:c=-5,s=o=n.y-5,l=n.width-40;break;case e.SHADED_BOTTOM:c=-5,s=o=0,l=n.width-40;break;default:throw"Unsupported shadingPosition"}r(o,c),r(s,l)}},{key:"_possiblyUpdateShaders",value:function(t){this.qrMatch!==t&&(this.hasBorderShaders&&this.borderShaders&&this.borderShaders.length&&this.borderShaders.forEach(function(n){n.style.backgroundColor=t?e.BORDER_SHADER_MATCH_COLOR:e.BORDER_SHADER_DEFAULT_COLOR}),this.qrMatch=t)}},{key:"_possiblyCloseLastScanImageFile",value:function(){this._lastScanImageFile&&(URL.revokeObjectURL(this._lastScanImageFile),this._lastScanImageFile=null)}}],[{key:"getCameras",value:function(){var e=this;return new Promise(function(t,n){if(navigator.mediaDevices&&navigator.mediaDevices.enumerateDevices&&navigator.mediaDevices.getUserMedia)e._log("navigator.mediaDevices used"),navigator.mediaDevices.getUserMedia({audio:!1,video:!0}).then(function(i){i.oninactive=function(t){return e._log("All streams closed")};navigator.mediaDevices.enumerateDevices().then(function(n){for(var a=[],r=0;r