diff --git a/webcam.js b/webcam.js index dd945d5..dfa0e89 100644 --- a/webcam.js +++ b/webcam.js @@ -986,7 +986,7 @@ var Webcam = { return taBytes; }, - upload: function(image_data_uri, target_url, callback) { + upload: function(image_data_uri, target_url, post_data, callback, headers) { // submit image data to server using binary AJAX var form_elem_name = this.params.upload_name || 'webcam'; @@ -1000,9 +1000,27 @@ var Webcam = { // extract raw base64 data from Data URI var raw_image_data = image_data_uri.replace(/^data\:image\/\w+\;base64\,/, ''); + // construct parameters + if ( typeof post_data == 'function' ) { + if ( typeof callback == 'object' ) { + headers = callback; + } + callback = post_data; + post_data = undefined; + } + // contruct use AJAX object var http = new XMLHttpRequest(); http.open("POST", target_url, true); + + // Add headers + if ( typeof headers == 'object' ) { + for( var item_name in headers ){ + if ( headers.hasOwnProperty(item_name) ) { + http.setRequestHeader(item_name, headers[item_name]); + } + } + } // setup progress events if (http.upload && http.upload.addEventListener) { @@ -1026,7 +1044,27 @@ var Webcam = { // stuff into a form, so servers can easily receive it as a standard file upload var form = new FormData(); - form.append( form_elem_name, blob, form_elem_name+"."+image_fmt.replace(/e/, '') ); + if ( typeof form_elem_name == 'object' ) { + /*Customize file input field like this + Webcam.params.upload_name = { + 'input_name' :'[input file name]', + 'file_name' : [file_name] + } + + */ + form.append( form_elem_name['input_name'], blob, form_elem_name['file_name']+"."+image_fmt.replace(/e/, '') ); + } else { + form.append( form_elem_name, blob, form_elem_name+"."+image_fmt.replace(/e/, '') ); + } + + // Add post data to formdata + if ( typeof post_data == 'object' ) { + for( var item_name in post_data ){ + if ( post_data.hasOwnProperty(item_name) ) { + form.append( item_name, post_data[item_name] ); + } + } + } // send data to server http.send(form); diff --git a/webcam.min.js b/webcam.min.js index 03de2f7..476d6f7 100644 --- a/webcam.min.js +++ b/webcam.min.js @@ -1,2 +1 @@ -// WebcamJS v1.0.25 - http://github.com/jhuckaby/webcamjs - MIT Licensed -(function(e){var t;function a(){var e=Error.apply(this,arguments);e.name=this.name="FlashError";this.stack=e.stack;this.message=e.message}function i(){var e=Error.apply(this,arguments);e.name=this.name="WebcamError";this.stack=e.stack;this.message=e.message}var s=function(){};s.prototype=Error.prototype;a.prototype=new s;i.prototype=new s;var Webcam={version:"1.0.26",protocol:location.protocol.match(/https/i)?"https":"http",loaded:false,live:false,userMedia:true,iOS:/iPad|iPhone|iPod/.test(navigator.userAgent)&&!e.MSStream,params:{width:0,height:0,dest_width:0,dest_height:0,image_format:"jpeg",jpeg_quality:90,enable_flash:true,force_flash:false,flip_horiz:false,fps:30,upload_name:"webcam",constraints:null,swfURL:"",flashNotDetectedText:"ERROR: No Adobe Flash Player detected. Webcam.js relies on Flash for browsers that do not support getUserMedia (like yours).",noInterfaceFoundText:"No supported webcam interface found.",unfreeze_snap:true,iosPlaceholderText:"Click here to open camera.",user_callback:null,user_canvas:null},errors:{FlashError:a,WebcamError:i},hooks:{},init:function(){var t=this;this.mediaDevices=navigator.mediaDevices&&navigator.mediaDevices.getUserMedia?navigator.mediaDevices:navigator.mozGetUserMedia||navigator.webkitGetUserMedia?{getUserMedia:function(e){return new Promise(function(t,a){(navigator.mozGetUserMedia||navigator.webkitGetUserMedia).call(navigator,e,t,a)})}}:null;e.URL=e.URL||e.webkitURL||e.mozURL||e.msURL;this.userMedia=this.userMedia&&!!this.mediaDevices&&!!e.URL;if(navigator.userAgent.match(/Firefox\D+(\d+)/)){if(parseInt(RegExp.$1,10)<21)this.userMedia=null}if(this.userMedia){e.addEventListener("beforeunload",function(e){t.reset()})}},exifOrientation:function(e){var t=new DataView(e);if(t.getUint8(0)!=255||t.getUint8(1)!=216){console.log("Not a valid JPEG file");return 0}var a=2;var i=null;while(a8){console.log("Invalid EXIF orientation value ("+p+")");return 0}return p}}}else{a+=2+t.getUint16(a+2)}}return 0},fixOrientation:function(e,t,a){var i=new Image;i.addEventListener("load",function(e){var s=document.createElement("canvas");var r=s.getContext("2d");if(t<5){s.width=i.width;s.height=i.height}else{s.width=i.height;s.height=i.width}switch(t){case 2:r.transform(-1,0,0,1,i.width,0);break;case 3:r.transform(-1,0,0,-1,i.width,i.height);break;case 4:r.transform(1,0,0,-1,0,i.height);break;case 5:r.transform(0,1,1,0,0,0);break;case 6:r.transform(0,1,-1,0,i.height,0);break;case 7:r.transform(0,-1,-1,0,i.height,i.width);break;case 8:r.transform(0,-1,1,0,0,i.width);break}r.drawImage(i,0,0);a.src=s.toDataURL()},false);i.src=e},attach:function(a){if(typeof a=="string"){a=document.getElementById(a)||document.querySelector(a)}if(!a){return this.dispatch("error",new i("Could not locate DOM element to attach to."))}this.container=a;a.innerHTML="";var s=document.createElement("div");a.appendChild(s);this.peg=s;if(!this.params.width)this.params.width=a.offsetWidth;if(!this.params.height)this.params.height=a.offsetHeight;if(!this.params.width||!this.params.height){return this.dispatch("error",new i("No width and/or height for webcam. Please call set() first, or attach to a visible element."))}if(!this.params.dest_width)this.params.dest_width=this.params.width;if(!this.params.dest_height)this.params.dest_height=this.params.height;this.userMedia=t===undefined?this.userMedia:t;if(this.params.force_flash){t=this.userMedia;this.userMedia=null}if(typeof this.params.fps!=="number")this.params.fps=30;var r=this.params.width/this.params.dest_width;var o=this.params.height/this.params.dest_height;if(this.userMedia){var n=document.createElement("video");n.setAttribute("autoplay","autoplay");n.setAttribute("playsinline","playsinline");n.style.width=""+this.params.dest_width+"px";n.style.height=""+this.params.dest_height+"px";if(r!=1||o!=1){a.style.overflow="hidden";n.style.webkitTransformOrigin="0px 0px";n.style.mozTransformOrigin="0px 0px";n.style.msTransformOrigin="0px 0px";n.style.oTransformOrigin="0px 0px";n.style.transformOrigin="0px 0px";n.style.webkitTransform="scaleX("+r+") scaleY("+o+")";n.style.mozTransform="scaleX("+r+") scaleY("+o+")";n.style.msTransform="scaleX("+r+") scaleY("+o+")";n.style.oTransform="scaleX("+r+") scaleY("+o+")";n.style.transform="scaleX("+r+") scaleY("+o+")"}a.appendChild(n);this.video=n;var l=this;this.mediaDevices.getUserMedia({audio:false,video:this.params.constraints||{mandatory:{minWidth:this.params.dest_width,minHeight:this.params.dest_height}}}).then(function(t){n.onloadedmetadata=function(e){l.stream=t;l.loaded=true;l.live=true;l.dispatch("load");l.dispatch("live");l.flip()};if("srcObject"in n){n.srcObject=t}else{n.src=e.URL.createObjectURL(t)}}).catch(function(e){if(l.params.enable_flash&&l.detectFlash()){setTimeout(function(){l.params.force_flash=1;l.attach(a)},1)}else{l.dispatch("error",e)}})}else if(this.iOS){var h=document.createElement("div");h.id=this.container.id+"-ios_div";h.className="webcamjs-ios-placeholder";h.style.width=""+this.params.width+"px";h.style.height=""+this.params.height+"px";h.style.textAlign="center";h.style.display="table-cell";h.style.verticalAlign="middle";h.style.backgroundRepeat="no-repeat";h.style.backgroundSize="contain";h.style.backgroundPosition="center";var c=document.createElement("span");c.className="webcamjs-ios-text";c.innerHTML=this.params.iosPlaceholderText;h.appendChild(c);var d=document.createElement("img");d.id=this.container.id+"-ios_img";d.style.width=""+this.params.dest_width+"px";d.style.height=""+this.params.dest_height+"px";d.style.display="none";h.appendChild(d);var f=document.createElement("input");f.id=this.container.id+"-ios_input";f.setAttribute("type","file");f.setAttribute("accept","image/*");f.setAttribute("capture","camera");var l=this;var m=this.params;f.addEventListener("change",function(e){if(e.target.files.length>0&&e.target.files[0].type.indexOf("image/")==0){var t=URL.createObjectURL(e.target.files[0]);var a=new Image;a.addEventListener("load",function(e){var t=document.createElement("canvas");t.width=m.dest_width;t.height=m.dest_height;var i=t.getContext("2d");ratio=Math.min(a.width/m.dest_width,a.height/m.dest_height);var s=m.dest_width*ratio;var r=m.dest_height*ratio;var o=(a.width-s)/2;var n=(a.height-r)/2;i.drawImage(a,o,n,s,r,0,0,m.dest_width,m.dest_height);var l=t.toDataURL();d.src=l;h.style.backgroundImage="url('"+l+"')"},false);var i=new FileReader;i.addEventListener("load",function(e){var i=l.exifOrientation(e.target.result);if(i>1){l.fixOrientation(t,i,a)}else{a.src=t}},false);var s=new XMLHttpRequest;s.open("GET",t,true);s.responseType="blob";s.onload=function(e){if(this.status==200||this.status===0){i.readAsArrayBuffer(this.response)}};s.send()}},false);f.style.display="none";a.appendChild(f);h.addEventListener("click",function(e){if(m.user_callback){l.snap(m.user_callback,m.user_canvas)}else{f.style.display="block";f.focus();f.click();f.style.display="none"}},false);a.appendChild(h);this.loaded=true;this.live=true}else if(this.params.enable_flash&&this.detectFlash()){e.Webcam=Webcam;var h=document.createElement("div");h.innerHTML=this.getSWFHTML();a.appendChild(h)}else{this.dispatch("error",new i(this.params.noInterfaceFoundText))}if(this.params.crop_width&&this.params.crop_height){var p=Math.floor(this.params.crop_width*r);var u=Math.floor(this.params.crop_height*o);a.style.width=""+p+"px";a.style.height=""+u+"px";a.style.overflow="hidden";a.scrollLeft=Math.floor(this.params.width/2-p/2);a.scrollTop=Math.floor(this.params.height/2-u/2)}else{a.style.width=""+this.params.width+"px";a.style.height=""+this.params.height+"px"}},reset:function(){if(this.preview_active)this.unfreeze();this.unflip();if(this.userMedia){if(this.stream){if(this.stream.getVideoTracks){var e=this.stream.getVideoTracks();if(e&&e[0]&&e[0].stop)e[0].stop()}else if(this.stream.stop){this.stream.stop()}}delete this.stream;delete this.video}if(this.userMedia!==true&&this.loaded&&!this.iOS){var t=this.getMovie();if(t&&t._releaseCamera)t._releaseCamera()}if(this.container){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(!this.hooks[e])this.hooks[e]=[];this.hooks[e].push(t)},off:function(e,t){e=e.replace(/^on/i,"").toLowerCase();if(this.hooks[e]){if(t){var a=this.hooks[e].indexOf(t);if(a>-1)this.hooks[e].splice(a,1)}else{this.hooks[e]=[]}}},dispatch:function(){var t=arguments[0].replace(/^on/i,"").toLowerCase();var s=Array.prototype.slice.call(arguments,1);if(this.hooks[t]&&this.hooks[t].length){for(var r=0,o=this.hooks[t].length;rERROR: the Webcam.js Flash fallback does not work from local disk. Please run it from a web server.'}if(!this.detectFlash()){this.dispatch("error",new a("Adobe Flash Player not found. Please install from get.adobe.com/flashplayer and try again."));return'

'+this.params.flashNotDetectedText+"

"}if(!i){var s="";var r=document.getElementsByTagName("script");for(var o=0,n=r.length;o';return t},getMovie:function(){if(!this.loaded)return this.dispatch("error",new a("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",new a("Cannot locate Flash movie in DOM"));return e},freeze:function(){var e=this;var t=this.params;if(this.preview_active)this.unfreeze();var a=this.params.width/this.params.dest_width;var i=this.params.height/this.params.dest_height;this.unflip();var s=t.crop_width||t.dest_width;var r=t.crop_height||t.dest_height;var o=document.createElement("canvas");o.width=s;o.height=r;var n=o.getContext("2d");this.preview_canvas=o;this.preview_context=n;if(a!=1||i!=1){o.style.webkitTransformOrigin="0px 0px";o.style.mozTransformOrigin="0px 0px";o.style.msTransformOrigin="0px 0px";o.style.oTransformOrigin="0px 0px";o.style.transformOrigin="0px 0px";o.style.webkitTransform="scaleX("+a+") scaleY("+i+")";o.style.mozTransform="scaleX("+a+") scaleY("+i+")";o.style.msTransform="scaleX("+a+") scaleY("+i+")";o.style.oTransform="scaleX("+a+") scaleY("+i+")";o.style.transform="scaleX("+a+") scaleY("+i+")"}this.snap(function(){o.style.position="relative";o.style.left=""+e.container.scrollLeft+"px";o.style.top=""+e.container.scrollTop+"px";e.container.insertBefore(o,e.peg);e.container.style.overflow="hidden";e.preview_active=true},o)},unfreeze:function(){if(this.preview_active){this.container.removeChild(this.preview_canvas);delete this.preview_context;delete this.preview_canvas;this.preview_active=false;this.flip()}},flip:function(){if(this.params.flip_horiz){var e=this.container.style;e.webkitTransform="scaleX(-1)";e.mozTransform="scaleX(-1)";e.msTransform="scaleX(-1)";e.oTransform="scaleX(-1)";e.transform="scaleX(-1)";e.filter="FlipH";e.msFilter="FlipH"}},unflip:function(){if(this.params.flip_horiz){var e=this.container.style;e.webkitTransform="scaleX(1)";e.mozTransform="scaleX(1)";e.msTransform="scaleX(1)";e.oTransform="scaleX(1)";e.transform="scaleX(1)";e.filter="";e.msFilter=""}},savePreview:function(e,t){var a=this.params;var i=this.preview_canvas;var s=this.preview_context;if(t){var r=t.getContext("2d");r.drawImage(i,0,0)}e(t?null:i.toDataURL("image/"+a.image_format,a.jpeg_quality/100),i,s);if(this.params.unfreeze_snap)this.unfreeze()},snap:function(e,t){if(!e)e=this.params.user_callback;if(!t)t=this.params.user_canvas;var a=this;var s=this.params;if(!this.loaded)return this.dispatch("error",new i("Webcam is not loaded yet"));if(!e)return this.dispatch("error",new i("Please provide a callback function or canvas to snap()"));if(this.preview_active){this.savePreview(e,t);return null}var r=document.createElement("canvas");r.width=this.params.dest_width;r.height=this.params.dest_height;var o=r.getContext("2d");if(this.params.flip_horiz){o.translate(s.dest_width,0);o.scale(-1,1)}var n=function(){if(this.src&&this.width&&this.height){o.drawImage(this,0,0,s.dest_width,s.dest_height)}if(s.crop_width&&s.crop_height){var a=document.createElement("canvas");a.width=s.crop_width;a.height=s.crop_height;var i=a.getContext("2d");i.drawImage(r,Math.floor(s.dest_width/2-s.crop_width/2),Math.floor(s.dest_height/2-s.crop_height/2),s.crop_width,s.crop_height,0,0,s.crop_width,s.crop_height);o=i;r=a}if(t){var n=t.getContext("2d");n.drawImage(r,0,0)}e(t?null:r.toDataURL("image/"+s.image_format,s.jpeg_quality/100),r,o)};if(this.userMedia){o.drawImage(this.video,0,0,this.params.dest_width,this.params.dest_height);n()}else if(this.iOS){var l=document.getElementById(this.container.id+"-ios_div");var h=document.getElementById(this.container.id+"-ios_img");var c=document.getElementById(this.container.id+"-ios_input");iFunc=function(e){n.call(h);h.removeEventListener("load",iFunc);l.style.backgroundImage="none";h.removeAttribute("src");c.value=null};if(!c.value){h.addEventListener("load",iFunc);c.style.display="block";c.focus();c.click();c.style.display="none"}else{iFunc(null)}}else{var d=this.getMovie()._snap();var h=new Image;h.onload=n;h.src="data:image/"+this.params.image_format+";base64,"+d}return null},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",new a(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,""),i=a.length,s=t?Math.ceil((i*3+1>>2)/t)*t:i*3+1>>2,r=new Uint8Array(s);for(var o,n,l=0,h=0,c=0;c>>(16>>>o&24)&255}l=0}}return r},upload:function(e,t,a){var i=this.params.upload_name||"webcam";var s="";if(e.match(/^data\:image\/(\w+)/))s=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)}var n=this;o.onload=function(){if(a)a.apply(n,[o.status,o.responseText,o.statusText]);Webcam.dispatch("uploadComplete",o.status,o.responseText,o.statusText)};var l=new Blob([this.base64DecToArr(r)],{type:"image/"+s});var h=new FormData;h.append(i,l,i+"."+s.replace(/e/,""));o.send(h)}};Webcam.init();if(typeof define==="function"&&define.amd){define(function(){return Webcam})}else if(typeof module==="object"&&module.exports){module.exports=Webcam}else{e.Webcam=Webcam}})(window); \ No newline at end of file +(function(p){var m;function h(){var e=Error.apply(this,arguments);e.name=this.name="FlashError";this.stack=e.stack;this.message=e.message}function u(){var e=Error.apply(this,arguments);e.name=this.name="WebcamError";this.stack=e.stack;this.message=e.message}var e=function(){};e.prototype=Error.prototype;h.prototype=new e;u.prototype=new e;var v={version:"1.0.26",protocol:location.protocol.match(/https/i)?"https":"http",loaded:false,live:false,userMedia:true,iOS:/iPad|iPhone|iPod/.test(navigator.userAgent)&&!p.MSStream,params:{width:0,height:0,dest_width:0,dest_height:0,image_format:"jpeg",jpeg_quality:90,enable_flash:true,force_flash:false,flip_horiz:false,fps:30,upload_name:"webcam",constraints:null,swfURL:"",flashNotDetectedText:"ERROR: No Adobe Flash Player detected. Webcam.js relies on Flash for browsers that do not support getUserMedia (like yours).",noInterfaceFoundText:"No supported webcam interface found.",unfreeze_snap:true,iosPlaceholderText:"Click here to open camera.",user_callback:null,user_canvas:null},errors:{FlashError:h,WebcamError:u},hooks:{},init:function(){var t=this;this.mediaDevices=navigator.mediaDevices&&navigator.mediaDevices.getUserMedia?navigator.mediaDevices:navigator.mozGetUserMedia||navigator.webkitGetUserMedia?{getUserMedia:function(a){return new Promise(function(e,t){(navigator.mozGetUserMedia||navigator.webkitGetUserMedia).call(navigator,a,e,t)})}}:null;p.URL=p.URL||p.webkitURL||p.mozURL||p.msURL;this.userMedia=this.userMedia&&!!this.mediaDevices&&!!p.URL;if(navigator.userAgent.match(/Firefox\D+(\d+)/)){if(parseInt(RegExp.$1,10)<21)this.userMedia=null}if(this.userMedia){p.addEventListener("beforeunload",function(e){t.reset()})}},exifOrientation:function(e){var t=new DataView(e);if(t.getUint8(0)!=255||t.getUint8(1)!=216){console.log("Not a valid JPEG file");return 0}var a=2;var i=null;while(a8){console.log("Invalid EXIF orientation value ("+m+")");return 0}return m}}}else{a+=2+t.getUint16(a+2)}}return 0},fixOrientation:function(e,i,s){var r=new Image;r.addEventListener("load",function(e){var t=document.createElement("canvas");var a=t.getContext("2d");if(i<5){t.width=r.width;t.height=r.height}else{t.width=r.height;t.height=r.width}switch(i){case 2:a.transform(-1,0,0,1,r.width,0);break;case 3:a.transform(-1,0,0,-1,r.width,r.height);break;case 4:a.transform(1,0,0,-1,0,r.height);break;case 5:a.transform(0,1,1,0,0,0);break;case 6:a.transform(0,1,-1,0,r.height,0);break;case 7:a.transform(0,-1,-1,0,r.height,r.width);break;case 8:a.transform(0,-1,1,0,0,r.width);break}a.drawImage(r,0,0);s.src=t.toDataURL()},false);r.src=e},attach:function(t){if(typeof t=="string"){t=document.getElementById(t)||document.querySelector(t)}if(!t){return this.dispatch("error",new u("Could not locate DOM element to attach to."))}this.container=t;t.innerHTML="";var e=document.createElement("div");t.appendChild(e);this.peg=e;if(!this.params.width)this.params.width=t.offsetWidth;if(!this.params.height)this.params.height=t.offsetHeight;if(!this.params.width||!this.params.height){return this.dispatch("error",new u("No width and/or height for webcam. Please call set() first, or attach to a visible element."))}if(!this.params.dest_width)this.params.dest_width=this.params.width;if(!this.params.dest_height)this.params.dest_height=this.params.height;this.userMedia=m===undefined?this.userMedia:m;if(this.params.force_flash){m=this.userMedia;this.userMedia=null}if(typeof this.params.fps!=="number")this.params.fps=30;var a=this.params.width/this.params.dest_width;var i=this.params.height/this.params.dest_height;if(this.userMedia){var s=document.createElement("video");s.setAttribute("autoplay","autoplay");s.setAttribute("playsinline","playsinline");s.style.width=""+this.params.dest_width+"px";s.style.height=""+this.params.dest_height+"px";if(a!=1||i!=1){t.style.overflow="hidden";s.style.webkitTransformOrigin="0px 0px";s.style.mozTransformOrigin="0px 0px";s.style.msTransformOrigin="0px 0px";s.style.oTransformOrigin="0px 0px";s.style.transformOrigin="0px 0px";s.style.webkitTransform="scaleX("+a+") scaleY("+i+")";s.style.mozTransform="scaleX("+a+") scaleY("+i+")";s.style.msTransform="scaleX("+a+") scaleY("+i+")";s.style.oTransform="scaleX("+a+") scaleY("+i+")";s.style.transform="scaleX("+a+") scaleY("+i+")"}t.appendChild(s);this.video=s;var r=this;this.mediaDevices.getUserMedia({audio:false,video:this.params.constraints||{mandatory:{minWidth:this.params.dest_width,minHeight:this.params.dest_height}}}).then(function(t){s.onloadedmetadata=function(e){r.stream=t;r.loaded=true;r.live=true;r.dispatch("load");r.dispatch("live");r.flip()};if("srcObject"in s){s.srcObject=t}else{s.src=p.URL.createObjectURL(t)}}).catch(function(e){if(r.params.enable_flash&&r.detectFlash()){setTimeout(function(){r.params.force_flash=1;r.attach(t)},1)}else{r.dispatch("error",e)}})}else if(this.iOS){var h=document.createElement("div");h.id=this.container.id+"-ios_div";h.className="webcamjs-ios-placeholder";h.style.width=""+this.params.width+"px";h.style.height=""+this.params.height+"px";h.style.textAlign="center";h.style.display="table-cell";h.style.verticalAlign="middle";h.style.backgroundRepeat="no-repeat";h.style.backgroundSize="contain";h.style.backgroundPosition="center";var o=document.createElement("span");o.className="webcamjs-ios-text";o.innerHTML=this.params.iosPlaceholderText;h.appendChild(o);var c=document.createElement("img");c.id=this.container.id+"-ios_img";c.style.width=""+this.params.dest_width+"px";c.style.height=""+this.params.dest_height+"px";c.style.display="none";h.appendChild(c);var n=document.createElement("input");n.id=this.container.id+"-ios_input";n.setAttribute("type","file");n.setAttribute("accept","image/*");n.setAttribute("capture","camera");var r=this;var d=this.params;n.addEventListener("change",function(e){if(e.target.files.length>0&&e.target.files[0].type.indexOf("image/")==0){var a=URL.createObjectURL(e.target.files[0]);var l=new Image;l.addEventListener("load",function(e){var t=document.createElement("canvas");t.width=d.dest_width;t.height=d.dest_height;var a=t.getContext("2d");ratio=Math.min(l.width/d.dest_width,l.height/d.dest_height);var i=d.dest_width*ratio;var s=d.dest_height*ratio;var r=(l.width-i)/2;var o=(l.height-s)/2;a.drawImage(l,r,o,i,s,0,0,d.dest_width,d.dest_height);var n=t.toDataURL();c.src=n;h.style.backgroundImage="url('"+n+"')"},false);var t=new FileReader;t.addEventListener("load",function(e){var t=r.exifOrientation(e.target.result);if(t>1){r.fixOrientation(a,t,l)}else{l.src=a}},false);var i=new XMLHttpRequest;i.open("GET",a,true);i.responseType="blob";i.onload=function(e){if(this.status==200||this.status===0){t.readAsArrayBuffer(this.response)}};i.send()}},false);n.style.display="none";t.appendChild(n);h.addEventListener("click",function(e){if(d.user_callback){r.snap(d.user_callback,d.user_canvas)}else{n.style.display="block";n.focus();n.click();n.style.display="none"}},false);t.appendChild(h);this.loaded=true;this.live=true}else if(this.params.enable_flash&&this.detectFlash()){p.Webcam=v;var h=document.createElement("div");h.innerHTML=this.getSWFHTML();t.appendChild(h)}else{this.dispatch("error",new u(this.params.noInterfaceFoundText))}if(this.params.crop_width&&this.params.crop_height){var l=Math.floor(this.params.crop_width*a);var f=Math.floor(this.params.crop_height*i);t.style.width=""+l+"px";t.style.height=""+f+"px";t.style.overflow="hidden";t.scrollLeft=Math.floor(this.params.width/2-l/2);t.scrollTop=Math.floor(this.params.height/2-f/2)}else{t.style.width=""+this.params.width+"px";t.style.height=""+this.params.height+"px"}},reset:function(){if(this.preview_active)this.unfreeze();this.unflip();if(this.userMedia){if(this.stream){if(this.stream.getVideoTracks){var e=this.stream.getVideoTracks();if(e&&e[0]&&e[0].stop)e[0].stop()}else if(this.stream.stop){this.stream.stop()}}delete this.stream;delete this.video}if(this.userMedia!==true&&this.loaded&&!this.iOS){var t=this.getMovie();if(t&&t._releaseCamera)t._releaseCamera()}if(this.container){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(!this.hooks[e])this.hooks[e]=[];this.hooks[e].push(t)},off:function(e,t){e=e.replace(/^on/i,"").toLowerCase();if(this.hooks[e]){if(t){var a=this.hooks[e].indexOf(t);if(a>-1)this.hooks[e].splice(a,1)}else{this.hooks[e]=[]}}},dispatch:function(){var e=arguments[0].replace(/^on/i,"").toLowerCase();var t=Array.prototype.slice.call(arguments,1);if(this.hooks[e]&&this.hooks[e].length){for(var a=0,i=this.hooks[e].length;aERROR: the Webcam.js Flash fallback does not work from local disk. Please run it from a web server.'}if(!this.detectFlash()){this.dispatch("error",new h("Adobe Flash Player not found. Please install from get.adobe.com/flashplayer and try again."));return'

'+this.params.flashNotDetectedText+"

"}if(!t){var a="";var i=document.getElementsByTagName("script");for(var s=0,r=i.length;s';return e},getMovie:function(){if(!this.loaded)return this.dispatch("error",new h("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",new h("Cannot locate Flash movie in DOM"));return e},freeze:function(){var e=this;var t=this.params;if(this.preview_active)this.unfreeze();var a=this.params.width/this.params.dest_width;var i=this.params.height/this.params.dest_height;this.unflip();var s=t.crop_width||t.dest_width;var r=t.crop_height||t.dest_height;var o=document.createElement("canvas");o.width=s;o.height=r;var n=o.getContext("2d");this.preview_canvas=o;this.preview_context=n;if(a!=1||i!=1){o.style.webkitTransformOrigin="0px 0px";o.style.mozTransformOrigin="0px 0px";o.style.msTransformOrigin="0px 0px";o.style.oTransformOrigin="0px 0px";o.style.transformOrigin="0px 0px";o.style.webkitTransform="scaleX("+a+") scaleY("+i+")";o.style.mozTransform="scaleX("+a+") scaleY("+i+")";o.style.msTransform="scaleX("+a+") scaleY("+i+")";o.style.oTransform="scaleX("+a+") scaleY("+i+")";o.style.transform="scaleX("+a+") scaleY("+i+")"}this.snap(function(){o.style.position="relative";o.style.left=""+e.container.scrollLeft+"px";o.style.top=""+e.container.scrollTop+"px";e.container.insertBefore(o,e.peg);e.container.style.overflow="hidden";e.preview_active=true},o)},unfreeze:function(){if(this.preview_active){this.container.removeChild(this.preview_canvas);delete this.preview_context;delete this.preview_canvas;this.preview_active=false;this.flip()}},flip:function(){if(this.params.flip_horiz){var e=this.container.style;e.webkitTransform="scaleX(-1)";e.mozTransform="scaleX(-1)";e.msTransform="scaleX(-1)";e.oTransform="scaleX(-1)";e.transform="scaleX(-1)";e.filter="FlipH";e.msFilter="FlipH"}},unflip:function(){if(this.params.flip_horiz){var e=this.container.style;e.webkitTransform="scaleX(1)";e.mozTransform="scaleX(1)";e.msTransform="scaleX(1)";e.oTransform="scaleX(1)";e.transform="scaleX(1)";e.filter="";e.msFilter=""}},savePreview:function(e,t){var a=this.params;var i=this.preview_canvas;var s=this.preview_context;if(t){var r=t.getContext("2d");r.drawImage(i,0,0)}e(t?null:i.toDataURL("image/"+a.image_format,a.jpeg_quality/100),i,s);if(this.params.unfreeze_snap)this.unfreeze()},snap:function(i,s){if(!i)i=this.params.user_callback;if(!s)s=this.params.user_canvas;var e=this;var r=this.params;if(!this.loaded)return this.dispatch("error",new u("Webcam is not loaded yet"));if(!i)return this.dispatch("error",new u("Please provide a callback function or canvas to snap()"));if(this.preview_active){this.savePreview(i,s);return null}var o=document.createElement("canvas");o.width=this.params.dest_width;o.height=this.params.dest_height;var n=o.getContext("2d");if(this.params.flip_horiz){n.translate(r.dest_width,0);n.scale(-1,1)}var t=function(){if(this.src&&this.width&&this.height){n.drawImage(this,0,0,r.dest_width,r.dest_height)}if(r.crop_width&&r.crop_height){var e=document.createElement("canvas");e.width=r.crop_width;e.height=r.crop_height;var t=e.getContext("2d");t.drawImage(o,Math.floor(r.dest_width/2-r.crop_width/2),Math.floor(r.dest_height/2-r.crop_height/2),r.crop_width,r.crop_height,0,0,r.crop_width,r.crop_height);n=t;o=e}if(s){var a=s.getContext("2d");a.drawImage(o,0,0)}i(s?null:o.toDataURL("image/"+r.image_format,r.jpeg_quality/100),o,n)};if(this.userMedia){n.drawImage(this.video,0,0,this.params.dest_width,this.params.dest_height);t()}else if(this.iOS){var a=document.getElementById(this.container.id+"-ios_div");var l=document.getElementById(this.container.id+"-ios_img");var h=document.getElementById(this.container.id+"-ios_input");iFunc=function(e){t.call(l);l.removeEventListener("load",iFunc);a.style.backgroundImage="none";l.removeAttribute("src");h.value=null};if(!h.value){l.addEventListener("load",iFunc);h.style.display="block";h.focus();h.click();h.style.display="none"}else{iFunc(null)}}else{var c=this.getMovie()._snap();var l=new Image;l.onload=t;l.src="data:image/"+this.params.image_format+";base64,"+c}return null},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",new h(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,""),i=a.length,s=t?Math.ceil((i*3+1>>2)/t)*t:i*3+1>>2,r=new Uint8Array(s);for(var o,n,l=0,h=0,c=0;c>>(16>>>o&24)&255}l=0}}return r},upload:function(e,t,a,i,s){var r=this.params.upload_name||"webcam";var o="";if(e.match(/^data\:image\/(\w+)/))o=RegExp.$1;else throw"Cannot locate image format in Data URI";var n=e.replace(/^data\:image\/\w+\;base64\,/,"");if(typeof a=="function"){if(typeof i=="object"){s=i}i=a;a=undefined}var l=new XMLHttpRequest;l.open("POST",t,true);if(typeof s=="object"){for(var h in s){if(s.hasOwnProperty(h)){l.setRequestHeader(h,s[h])}}}if(l.upload&&l.upload.addEventListener){l.upload.addEventListener("progress",function(e){if(e.lengthComputable){var t=e.loaded/e.total;v.dispatch("uploadProgress",t,e)}},false)}var c=this;l.onload=function(){if(i)i.apply(c,[l.status,l.responseText,l.statusText]);v.dispatch("uploadComplete",l.status,l.responseText,l.statusText)};var d=new Blob([this.base64DecToArr(n)],{type:"image/"+o});var f=new FormData;if(typeof r=="object"){f.append(r["input_name"],d,r["file_name"]+"."+o.replace(/e/,""))}else{f.append(r,d,r+"."+o.replace(/e/,""))}if(typeof a=="object"){for(var h in a){if(a.hasOwnProperty(h)){f.append(h,a[h])}}}l.send(f)}};v.init();if(typeof define==="function"&&define.amd){define(function(){return v})}else if(typeof module==="object"&&module.exports){module.exports=v}else{p.Webcam=v}})(window); \ No newline at end of file