forked from jhuckaby/webcamjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webcam.min.js
2 lines (2 loc) · 7.18 KB
/
webcam.min.js
1
2
// WebcamJS v1.0 - http://github.com/jhuckaby/webcamjs - MIT Licensed
var Webcam={version:"1.0.0",protocol:location.protocol.match(/https/i)?"https":"http",swfURL:"",loaded:false,live:false,userMedia:true,params:{width:0,height:0,dest_width:0,dest_height:0,image_format:"jpeg",jpeg_quality:90,force_flash:false},hooks:{load:null,live:null,uploadcomplete:null,uploadprogress:null,error:function(e){alert("Webcam.js Error: "+e)}},init:function(){navigator.getUserMedia=navigator.getUserMedia||navigator.webkitGetUserMedia||navigator.mozGetUserMedia||navigator.msGetUserMedia;window.URL=window.URL||window.webkitURL||window.mozURL||window.msURL;this.userMedia=this.userMedia&&!!navigator.getUserMedia&&!!window.URL;if(navigator.userAgent.match(/Firefox\D+(\d+)/)){if(parseInt(RegExp.$1,10)<21)this.userMedia=null}},attach:function(e){if(typeof e=="string"){e=document.getElementById(e)||document.querySelector(e)}if(!e){return this.dispatch("error","Could not locate DOM element to attach to.")}this.container=e;if(!this.params.width)this.params.width=e.offsetWidth;if(!this.params.height)this.params.height=e.offsetHeight;if(!this.params.dest_width)this.params.dest_width=this.params.width;if(!this.params.dest_height)this.params.dest_height=this.params.height;if(this.params.force_flash)this.userMedia=null;if(this.userMedia){var t=document.createElement("video");t.setAttribute("autoplay","autoplay");t.style.width=""+this.params.dest_width+"px";t.style.height=""+this.params.dest_height+"px";var a=this.params.width/this.params.dest_width;var s=this.params.height/this.params.dest_height;if(a!=1||s!=1){e.style.overflow="visible";t.style.webkitTransformOrigin="0px 0px";t.style.mozTransformOrigin="0px 0px";t.style.msTransformOrigin="0px 0px";t.style.oTransformOrigin="0px 0px";t.style.transformOrigin="0px 0px";t.style.webkitTransform="scaleX("+a+") scaleY("+s+")";t.style.mozTransform="scaleX("+a+") scaleY("+s+")";t.style.msTransform="scaleX("+a+") scaleY("+s+")";t.style.oTransform="scaleX("+a+") scaleY("+s+")";t.style.transform="scaleX("+a+") scaleY("+s+")"}e.appendChild(t);this.video=t;var i=document.createElement("canvas");i.width=this.params.dest_width;i.height=this.params.dest_height;var r=i.getContext("2d");this.context=r;this.canvas=i;var o=this;navigator.getUserMedia({audio:false,video:true},function(e){t.src=window.URL.createObjectURL(e)||e;Webcam.stream=e;Webcam.loaded=true;Webcam.live=true;Webcam.dispatch("load");Webcam.dispatch("live")},function(e){return o.dispatch("error","Could not access webcam.")})}else{e.innerHTML=this.getSWFHTML()}},reset:function(){if(this.userMedia){try{this.stream.stop()}catch(e){}delete this.stream;delete this.canvas;delete this.context;delete this.video}this.container.innerHTML="";delete this.container;this.loaded=false;this.live=false},set:function(){if(arguments.length==1){for(var e in arguments[0]){this.params[e]=arguments[0][e]}}else{this.params[arguments[0]]=arguments[1]}},on:function(e,t){e=e.replace(/^on/i,"").toLowerCase();if(typeof this.hooks[e]=="undefined")throw"Event type not supported: "+e;this.hooks[e]=t},dispatch:function(){var e=arguments[0].replace(/^on/i,"").toLowerCase();var t=Array.prototype.slice.call(arguments,1);if(this.hooks[e]){if(typeof this.hooks[e]=="function"){this.hooks[e].apply(this,t)}else if(typeof this.hooks[e]=="array"){this.hooks[e][0][this.hooks[e][1]].apply(this.hooks[e][0],t)}else if(window[this.hooks[e]]){window[this.hooks[e]].apply(window,t)}return true}return false},setSWFLocation:function(e){this.swfURL=e},getSWFHTML:function(){var e="";if(location.protocol.match(/file/)){return'<h1 style="color:red">Sorry, the Webcam.js Flash fallback does not work from local disk. Please upload it to a web server first.</h1>'}if(!this.swfURL){var t="";var a=document.getElementsByTagName("script");for(var s=0,i=a.length;s<i;s++){var r=a[s].getAttribute("src");if(r&&r.match(/\/webcam(\.min)?\.js/)){t=r.replace(/\/webcam(\.min)?\.js.*$/,"");s=i}}if(t)this.swfURL=t+"/webcam.swf";else this.swfURL="webcam.swf"}if(window.localStorage&&!localStorage.getItem("visited")){this.params.new_user=1;localStorage.setItem("visited",1)}var o="";for(var h in this.params){if(o)o+="&";o+=h+"="+escape(this.params[h])}e+='<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="'+this.protocol+'://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="'+this.params.width+'" height="'+this.params.height+'" id="webcam_movie_obj" align="middle"><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="false" /><param name="movie" value="'+this.swfURL+'" /><param name="loop" value="false" /><param name="menu" value="false" /><param name="quality" value="best" /><param name="bgcolor" value="#ffffff" /><param name="flashvars" value="'+o+'"/><embed id="webcam_movie_embed" src="'+this.swfURL+'" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="'+this.params.width+'" height="'+this.params.height+'" name="webcam_movie_embed" align="middle" allowScriptAccess="always" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="'+o+'"></embed></object>';return e},getMovie:function(){if(!this.loaded)return this.dispatch("error","Flash Movie is not loaded yet");var e=document.getElementById("webcam_movie_obj");if(!e||!e._snap)e=document.getElementById("webcam_movie_embed");if(!e)this.dispatch("error","Cannot locate Flash movie in DOM");return e},snap:function(){if(!this.loaded)return this.dispatch("error","Webcam is not loaded yet");if(!this.live)return this.dispatch("error","Webcam is not live yet");if(this.userMedia){this.context.drawImage(this.video,0,0,this.params.dest_width,this.params.dest_height);return this.canvas.toDataURL("image/"+this.params.image_format,this.params.jpeg_quality/100)}else{var e=this.getMovie()._snap();return"data:image/"+this.params.image_format+";base64,"+e}},configure:function(e){if(!e)e="camera";this.getMovie()._configure(e)},flashNotify:function(e,t){switch(e){case"flashLoadComplete":this.loaded=true;this.dispatch("load");break;case"cameraLive":this.live=true;this.dispatch("live");break;case"error":this.dispatch("error",t);break;default:break}},b64ToUint6:function(e){return e>64&&e<91?e-65:e>96&&e<123?e-71:e>47&&e<58?e+4:e===43?62:e===47?63:0},base64DecToArr:function(e,t){var a=e.replace(/[^A-Za-z0-9\+\/]/g,""),s=a.length,i=t?Math.ceil((s*3+1>>2)/t)*t:s*3+1>>2,r=new Uint8Array(i);for(var o,h,n=0,l=0,d=0;d<s;d++){h=d&3;n|=this.b64ToUint6(a.charCodeAt(d))<<18-6*h;if(h===3||s-d===1){for(o=0;o<3&&l<i;o++,l++){r[l]=n>>>(16>>>o&24)&255}n=0}}return r},upload:function(e,t,a){if(a)Webcam.on("uploadComplete",a);var s="webcam";var i="";if(e.match(/^data\:image\/(\w+)/))i=RegExp.$1;else throw"Cannot locate image format in Data URI";var r=e.replace(/^data\:image\/\w+\;base64\,/,"");var o=new XMLHttpRequest;o.open("POST",t,true);if(o.upload&&o.upload.addEventListener){o.upload.addEventListener("progress",function(e){if(e.lengthComputable){var t=e.loaded/e.total;Webcam.dispatch("uploadProgress",t,e)}},false)}o.onload=function(){Webcam.dispatch("uploadComplete",o.status,o.responseText,o.statusText)};var h=new Blob([this.base64DecToArr(r)],{type:"image/"+i});var n=new FormData;n.append(s,h,s+"."+i.replace(/e/,""));o.send(n)}};Webcam.init();