forked from nimiq/qr-scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qr-scanner.min.js
17 lines (17 loc) · 6.34 KB
/
qr-scanner.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function d(a,b,c){c=void 0===c?d.DEFAULT_CANVAS_SIZE:c;this.$video=a;this.$canvas=document.createElement("canvas");this._onDecode=b;this._paused=this._active=!1;this.$canvas.width=c;this.$canvas.height=c;this._sourceRect={x:0,y:0,width:c,height:c};this._onCanPlay=this._onCanPlay.bind(this);this._onPlay=this._onPlay.bind(this);this._onVisibilityChange=this._onVisibilityChange.bind(this);this._scanFrame=this._scanFrame.bind(this);this.$video.addEventListener("canplay",this._onCanPlay);this.$video.addEventListener("play",
this._onPlay);document.addEventListener("visibilitychange",this._onVisibilityChange);this._qrWorker=new Worker(d.WORKER_PATH)}d.hasCamera=function(){return navigator.mediaDevices.enumerateDevices().then(function(a){return a.some(function(a){return"videoinput"===a.kind})}).catch(function(){return!1})};
d.prototype.destroy=function(){this.$video.removeEventListener("canplay",this._onCanPlay);this.$video.removeEventListener("play",this._onPlay);document.removeEventListener("visibilitychange",this._onVisibilityChange);this.stop();this._qrWorker.postMessage({type:"close"})};
d.prototype.start=function(){var a=this;if(this._active&&!this._paused)return Promise.resolve();"https:"!==window.location.protocol&&console.warn("The camera stream is only accessible if the page is transferred via https.");this._active=!0;this._paused=!1;if(document.hidden)return Promise.resolve();clearTimeout(this._offTimeout);this._offTimeout=null;if(this.$video.srcObject)return this.$video.play(),Promise.resolve();var b="environment";return this._getCameraStream("environment",!0).catch(function(){b=
"user";return a._getCameraStream()}).then(function(c){a.$video.srcObject=c;a._setVideoMirror(b)}).catch(function(b){a._active=!1;throw b;})};d.prototype.stop=function(){this.pause();this._active=!1};d.prototype.pause=function(){var a=this;this._paused=!0;this._active&&(this.$video.pause(),this._offTimeout||(this._offTimeout=setTimeout(function(){var b=a.$video.srcObject&&a.$video.srcObject.getTracks()[0];b&&(b.stop(),a.$video.srcObject=null,a._offTimeout=null)},300)))};
d.scanImage=function(a,b){var c=Object.assign({sourceRect:null,worker:null,canvas:null,fixedCanvasSize:!1,alsoTryWithoutSourceRect:!1,scanTimeout:5E3},b),e=c.sourceRect,f=c.worker,k=c.canvas,g=c.fixedCanvasSize;b=c.alsoTryWithoutSourceRect;var p=c.scanTimeout,m=!1;c=new Promise(function(b,c){f||(f=new Worker(d.WORKER_PATH),m=!0,f.postMessage({type:"inversionMode",data:"both"}));var l=function(a){"qrResult"===a.data.type&&(f.removeEventListener("message",l),f.removeEventListener("error",h),clearTimeout(n),
null!==a.data.data?b(a.data.data):c("QR code not found."))};var h=function(a){f.removeEventListener("message",l);f.removeEventListener("error",h);clearTimeout(n);c("Scanner error: "+(a?a.message||a:"Unknown Error"))};f.addEventListener("message",l);f.addEventListener("error",h);var n=setTimeout(function(){return h("timeout")},p);d._loadImage(a).then(function(a){a=d._getImageData(a,e,k,g);f.postMessage({type:"decode",data:a},[a.data.buffer])}).catch(h)});e&&b&&(c=c.catch(function(){return d.scanImage(a,
{sourceRect:null,worker:f,canvas:k,fixedCanvasSize:g})}));return c=c.finally(function(){m&&f.postMessage({type:"close"})})};d.prototype.setGrayscaleWeights=function(a,b,c,e){this._qrWorker.postMessage({type:"grayscaleWeights",data:{red:a,green:b,blue:c,useIntegerApproximation:void 0===e?!0:e}})};d.prototype.setInversionMode=function(a){this._qrWorker.postMessage({type:"inversionMode",data:a})};d.prototype._onCanPlay=function(){this._updateSourceRect();this.$video.play()};
d.prototype._onPlay=function(){this._updateSourceRect();this._scanFrame()};d.prototype._onVisibilityChange=function(){document.hidden?this.pause():this._active&&this.start()};d.prototype._updateSourceRect=function(){var a=Math.round(2/3*Math.min(this.$video.videoWidth,this.$video.videoHeight));this._sourceRect.width=this._sourceRect.height=a;this._sourceRect.x=(this.$video.videoWidth-a)/2;this._sourceRect.y=(this.$video.videoHeight-a)/2};
d.prototype._scanFrame=function(){var a=this;if(!this._active||this.$video.paused||this.$video.ended)return!1;requestAnimationFrame(function(){d.scanImage(a.$video,{sourceRect:a._sourceRect,worker:a._qrWorker,canvas:a.$canvas,fixedCanvasSize:!0}).then(a._onDecode,function(b){a._active&&"QR code not found."!==b&&console.error(b)}).then(function(){return setTimeout(a._scanFrame,100)})})};
d.prototype._getCameraStream=function(a,b){b=void 0===b?!1:b;var c=[{width:{min:1024}},{width:{min:768}},{}];a&&(b&&(a={exact:a}),c.forEach(function(b){return b.facingMode=a}));return this._getMatchingCameraStream(c)};d.prototype._getMatchingCameraStream=function(a){var b=this;return 0===a.length?Promise.reject("Camera not found."):navigator.mediaDevices.getUserMedia({video:a.shift()}).catch(function(){return b._getMatchingCameraStream(a)})};
d.prototype._setVideoMirror=function(a){this.$video.style.transform="scaleX("+("user"===a?-1:1)+")"};
d._getImageData=function(a,b,c,e){b=void 0===b?null:b;e=void 0===e?!1:e;c=(void 0===c?null:c)||document.createElement("canvas");var f=b&&b.x?b.x:0,k=b&&b.y?b.y:0,g=b&&b.width?b.width:a.width||a.videoWidth;b=b&&b.height?b.height:a.height||a.videoHeight;e||c.width===g&&c.height===b||(c.width=g,c.height=b);e=c.getContext("2d",{alpha:!1});e.imageSmoothingEnabled=!1;e.drawImage(a,f,k,g,b,0,0,c.width,c.height);return e.getImageData(0,0,c.width,c.height)};
d._loadImage=function(a){if(a instanceof HTMLCanvasElement||a instanceof HTMLVideoElement||window.ImageBitmap&&a instanceof window.ImageBitmap||window.OffscreenCanvas&&a instanceof window.OffscreenCanvas)return Promise.resolve(a);if(a instanceof Image)return d._awaitImageLoad(a).then(function(){return a});if(a instanceof File||a instanceof URL||"string"===typeof a){var b=new Image;b.src=a instanceof File?URL.createObjectURL(a):a;return d._awaitImageLoad(b).then(function(){a instanceof File&&URL.revokeObjectURL(b.src);
return b})}return Promise.reject("Unsupported image type.")};d._awaitImageLoad=function(a){return new Promise(function(b,c){if(a.complete&&0!==a.naturalWidth)b();else{var e=function(){a.removeEventListener("load",e);a.removeEventListener("error",f);b()};var f=function(){a.removeEventListener("load",e);a.removeEventListener("error",f);c("Image load error")};a.addEventListener("load",e);a.addEventListener("error",f)}})};d.DEFAULT_CANVAS_SIZE=400;d.WORKER_PATH="qr-scanner-worker.min.js";
export default d;
//# sourceMappingURL=qr-scanner.min.js.map