image-capture
Version:
MediaStream ImageCapture polyfill: takePhoto(), grabFrame() and more
19 lines • 3.53 kB
JavaScript
!function(e,t){if("function"==typeof define&&define.amd)define(["exports"],t);else if("undefined"!=typeof exports)t(exports);else{var n={};t(n),e.imagecapture=n}}(this,function(e){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var t=function(){function e(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}return function(t,n,i){return n&&e(t.prototype,n),i&&e(t,i),t}}(),n=e.ImageCapture=window.ImageCapture;
/**
* MediaStream ImageCapture polyfill
*
* @license
* Copyright 2018 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/void 0===n&&(e.ImageCapture=n=function(){function e(t){var n=this;if(function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),"video"!==t.kind)throw new DOMException("NotSupportedError");this._videoStreamTrack=t,"readyState"in this._videoStreamTrack||(this._videoStreamTrack.readyState="live"),this._previewStream=new MediaStream([t]),this.videoElement=document.createElement("video"),this.videoElementPlaying=new Promise(function(e){n.videoElement.addEventListener("playing",e)}),HTMLMediaElement?this.videoElement.srcObject=this._previewStream:this.videoElement.src=URL.createObjectURL(this._previewStream),this.videoElement.muted=!0,this.videoElement.setAttribute("playsinline",""),this.videoElement.play(),this.canvasElement=document.createElement("canvas"),this.canvas2dContext=this.canvasElement.getContext("2d")}return t(e,[{key:"getPhotoCapabilities",value:function(){return new Promise(function(e,t){var n={current:0,min:0,max:0};e({exposureCompensation:n,exposureMode:"none",fillLightMode:"none",focusMode:"none",imageHeight:n,imageWidth:n,iso:n,redEyeReduction:!1,whiteBalanceMode:"none",zoom:n}),t(new DOMException("OperationError"))})}},{key:"setOptions",value:function(){arguments.length>0&&void 0!==arguments[0]&&arguments[0];return new Promise(function(e,t){})}},{key:"takePhoto",value:function(){var e=this;return new Promise(function(t,n){if("live"!==e._videoStreamTrack.readyState)return n(new DOMException("InvalidStateError"));e.videoElementPlaying.then(function(){try{e.canvasElement.width=e.videoElement.videoWidth,e.canvasElement.height=e.videoElement.videoHeight,e.canvas2dContext.drawImage(e.videoElement,0,0),e.canvasElement.toBlob(t)}catch(e){n(new DOMException("UnknownError"))}})})}},{key:"grabFrame",value:function(){var e=this;return new Promise(function(t,n){if("live"!==e._videoStreamTrack.readyState)return n(new DOMException("InvalidStateError"));e.videoElementPlaying.then(function(){try{e.canvasElement.width=e.videoElement.videoWidth,e.canvasElement.height=e.videoElement.videoHeight,e.canvas2dContext.drawImage(e.videoElement,0,0),t(window.createImageBitmap(e.canvasElement))}catch(e){n(new DOMException("UnknownError"))}})})}},{key:"videoStreamTrack",get:function(){return this._videoStreamTrack}}]),e}()),window.ImageCapture=n});