jpeg_camera 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 05bc1164940b3d79859cd0c4654a3bfad8c5605b
4
- data.tar.gz: 9261c1d24e3243fc9b91f51ea071c2778236c646
3
+ metadata.gz: b34d1a1ff1a8ef2e17e01a2cded7304a1fc58c6f
4
+ data.tar.gz: 937b143f723694535e65d9467f413e1c58f2f1a9
5
5
  SHA512:
6
- metadata.gz: f7597ee77da4f1f48b7b08377257c7687c23ca1ecbb18c6c159cc76a444f2a11c387ccac8bb02696840713c5af910c291149e7e0dd561d96e9fcca89496e1758
7
- data.tar.gz: e1c30491363130897e80e04ab3a4cb1a9bef23e8a9c6b0f9b798772cfc801533e5d9a2d403241e5615a964c46dae61e1e79623577a3d89440dc72be48c71f009
6
+ metadata.gz: 8cb35009a29f678f51a3bdd2325acd44ca446fa379683629acdf5e9de40089bc191e7998ed78b58c5e02734eaeb84007f66226e870a87357aee6b95b2426a74e
7
+ data.tar.gz: dc32b6a1034e5e07726fb57f3aa1a1414a53ad6f09dcb3a206149b2435574058f62b9ab211ca9d60edaf416223d4503b2bd4f3ee1aea1ed9b1cd767dbb5cab2d
data/README.md CHANGED
@@ -64,7 +64,7 @@ to also load Canvas-to-Blob. You don't need SWFObject for HTML5.
64
64
 
65
65
  Require the gem in your Gemfile.
66
66
 
67
- gem "jpeg_camera", "~> 1.0.0"
67
+ gem "jpeg_camera", "~> 1.0.2"
68
68
 
69
69
  Add appropriate requires to your application.js. SWFObject and Canvas-to-Blob
70
70
  are stored in separate files so that you don't have to load them again if you
@@ -95,8 +95,8 @@ want to include JpegCamera in your application.js file and would rather use
95
95
  snapshot.upload({api_url: "/upload_image"}).done(function(response) {
96
96
  response_container.innerHTML = response;
97
97
  this.discard(); // discard snapshot and show video stream again
98
- }).fail(function() {
99
- alert("Upload failed.");
98
+ }).fail(function(status_code, error_message, response) {
99
+ alert("Upload failed with status " + status_code);
100
100
  });
101
101
 
102
102
  A detailed documentation using in-code comments is maintained for
@@ -1,4 +1,4 @@
1
- /*! JpegCamera 1.0.0 | 2013-07-29
1
+ /*! JpegCamera 1.0.2 | 2013-07-30
2
2
  (c) 2013 Adam Wrobel
3
3
  http://amw.github.io/jpeg_camera */
4
4
  (function() {
@@ -18,7 +18,8 @@
18
18
  quality: 0.9,
19
19
  shutter: true,
20
20
  mirror: false,
21
- timeout: 0
21
+ timeout: 0,
22
+ retry_success: false
22
23
  };
23
24
 
24
25
  function JpegCamera(container, options) {
@@ -626,7 +627,7 @@
626
627
  };
627
628
 
628
629
  Snapshot.prototype.upload = function(options) {
629
- var cache, csrf_token;
630
+ var cache;
630
631
  if (options == null) {
631
632
  options = {};
632
633
  }
@@ -634,27 +635,18 @@
634
635
  raise("discarded snapshot cannot be used");
635
636
  }
636
637
  if (this._uploading) {
637
- this._debug("Upload already in progress");
638
+ this.camera._debug("Upload already in progress");
638
639
  return;
639
640
  }
640
641
  this._uploading = true;
642
+ this._retry = 1;
641
643
  this._upload_options = options;
642
644
  cache = this._options();
643
645
  if (!cache.api_url) {
644
646
  this.camera._debug("Snapshot#upload called without valid api_url");
645
647
  throw "Snapshot#upload called without valid api_url";
646
648
  }
647
- if ("string" === typeof cache.csrf_token && cache.csrf_token.length > 0) {
648
- csrf_token = cache.csrf_token;
649
- } else {
650
- csrf_token = null;
651
- }
652
- this._done = false;
653
- this._response = null;
654
- this._fail = false;
655
- this._status = null;
656
- this._error_message = null;
657
- this.camera._upload(this, cache.api_url, csrf_token, cache.timeout);
649
+ this._start_upload(cache);
658
650
  return this;
659
651
  };
660
652
 
@@ -662,6 +654,8 @@
662
654
 
663
655
  Snapshot.prototype._uploading = false;
664
656
 
657
+ Snapshot.prototype._retry = 1;
658
+
665
659
  Snapshot.prototype.done = function(callback) {
666
660
  var cache;
667
661
  if (this._discarded) {
@@ -707,25 +701,78 @@
707
701
  return this.camera._extend({}, this.camera.options, this.options, this._upload_options);
708
702
  };
709
703
 
704
+ Snapshot.prototype._start_upload = function(cache) {
705
+ var csrf_token;
706
+ if ("string" === typeof cache.csrf_token && cache.csrf_token.length > 0) {
707
+ csrf_token = cache.csrf_token;
708
+ } else {
709
+ csrf_token = null;
710
+ }
711
+ this._done = false;
712
+ this._response = null;
713
+ this._fail = false;
714
+ this._status = null;
715
+ this._error_message = null;
716
+ return this.camera._upload(this, cache.api_url, csrf_token, cache.timeout);
717
+ };
718
+
710
719
  Snapshot.prototype._upload_done = function() {
711
- var cache;
712
- this.camera._debug("Upload completed");
713
- this._uploading = false;
720
+ var cache, delay, retry_decision, that;
721
+ this.camera._debug("Upload completed with status " + this._status);
714
722
  this._done = true;
715
723
  cache = this._options();
716
- if (cache.on_upload_done) {
717
- return cache.on_upload_done.call(this, this._response);
724
+ retry_decision = cache.retry_success && cache.retry_if && cache.retry_if.call(this, this._status, this._error_message, this._response, this._retry);
725
+ if (true === retry_decision) {
726
+ retry_decision = 0;
727
+ }
728
+ if ("number" === typeof retry_decision) {
729
+ this._retry++;
730
+ if (retry_decision > 0) {
731
+ delay = parseInt(retry_decision);
732
+ this.camera._debug("Will retry the upload in " + delay + "ms (attempt #" + this._retry + ")");
733
+ that = this;
734
+ return setTimeout((function() {
735
+ return that._start_upload(cache);
736
+ }), delay);
737
+ } else {
738
+ this.camera._debug("Will retry the upload immediately (attempt #" + this._retry + ")");
739
+ return this._start_upload(cache);
740
+ }
741
+ } else {
742
+ this._uploading = false;
743
+ if (cache.on_upload_done) {
744
+ return cache.on_upload_done.call(this, this._response);
745
+ }
718
746
  }
719
747
  };
720
748
 
721
749
  Snapshot.prototype._upload_fail = function() {
722
- var cache;
750
+ var cache, delay, retry_decision, that;
723
751
  this.camera._debug("Upload failed with status " + this._status);
724
- this._uploading = false;
725
752
  this._fail = true;
726
753
  cache = this._options();
727
- if (cache.on_upload_fail) {
728
- return cache.on_upload_fail.call(this, this._status, this._error_message, this._response);
754
+ retry_decision = cache.retry_if && cache.retry_if.call(this, this._status, this._error_message, this._response, this._retry);
755
+ if (true === retry_decision) {
756
+ retry_decision = 0;
757
+ }
758
+ if ("number" === typeof retry_decision) {
759
+ this._retry++;
760
+ if (retry_decision > 0) {
761
+ delay = parseInt(retry_decision);
762
+ this.camera._debug("Will retry the upload in " + delay + "ms (attempt #" + this._retry + ")");
763
+ that = this;
764
+ return setTimeout((function() {
765
+ return that._start_upload(cache);
766
+ }), delay);
767
+ } else {
768
+ this.camera._debug("Will retry the upload immediately (attempt #" + this._retry + ")");
769
+ return this._start_upload(cache);
770
+ }
771
+ } else {
772
+ this._uploading = false;
773
+ if (cache.on_upload_fail) {
774
+ return cache.on_upload_fail.call(this, this._status, this._error_message, this._response);
775
+ }
729
776
  }
730
777
  };
731
778
 
@@ -1,4 +1,4 @@
1
- /*! JpegCamera 1.0.0 | 2013-07-29
1
+ /*! JpegCamera 1.0.2 | 2013-07-30
2
2
  (c) 2013 Adam Wrobel
3
3
  http://amw.github.io/jpeg_camera */
4
- !function(){var a,b,c,d,e,f,g,h={}.hasOwnProperty,i=function(a,b){function c(){this.constructor=a}for(var d in b)h.call(b,d)&&(a[d]=b[d]);return c.prototype=b.prototype,a.prototype=new c,a.__super__=b.prototype,a};a=function(){function a(a,b){if(this.container="string"==typeof a?document.querySelector(a):a,!this.container||!this.container.offsetWidth)throw"JpegCamera: invalid container";this.container.innerHTML="",this.options=this._extend({},this.constructor.DefaultOptions,b),this._engine_init()}return a.DefaultOptions={shutter_url:"/jpeg_camera/shutter.mp3",swf_url:"/jpeg_camera/jpeg_camera.swf",on_debug:function(a){return console&&console.log?console.log("JpegCamera: "+a):void 0},quality:.9,shutter:!0,mirror:!1,timeout:0},a.prototype.ready=function(a){return this.options.on_ready=a,this.options.on_ready&&this._is_ready&&this.options.on_ready.call(this),this},a.prototype._is_ready=!1,a.prototype.error=function(a){return this.options.on_error=a,this.options.on_error&&this._error_occured&&this.options.on_error.call(this,this._error_occured),this},a.prototype._error_occured=!1,a.prototype.capture=function(a){var b,c;return null==a&&(a={}),b=new d(this,a),this._snapshots[b.id]=b,c=b._options(),c.shutter&&this._engine_play_shutter_sound(),this._engine_capture(b,c.mirror,c.quality),b},a.prototype._snapshots={},a.prototype.show_stream=function(){return this._engine_show_stream(),this._displayed_snapshot=null,this},a.prototype.discard_all=function(){var a,b,c;this._displayed_snapshot&&this.show_stream(),c=this._snapshots;for(a in c)b=c[a],this._engine_discard(b),b._discarded=!0;return this._snapshots={},this},a.prototype._extend=function(a){var b,c,d,e,f,g;for(d=Array.prototype.slice.call(arguments,1),f=0,g=d.length;g>f;f++)if(c=d[f])for(b in c)e=c[b],a[b]=e;return a},a.prototype._debug=function(a){return this.options.on_debug?this.options.on_debug.call(this,a):void 0},a.prototype._view_width=function(){return parseInt(this.container.offsetWidth,10)},a.prototype._view_height=function(){return parseInt(this.container.offsetHeight,10)},a.prototype._display=function(a){return this._engine_display(a),this._displayed_snapshot=a},a.prototype._displayed_snapshot=null,a.prototype._upload=function(a,b,c,d){return this._engine_upload(a,b,c,d)},a.prototype._discard=function(a){return this._displayed_snapshot===a&&this.show_stream(),this._engine_discard(a),a._discarded=!0,delete this._snapshots[a.id]},a.prototype._prepared=function(){return this._is_ready=!0,this.options.on_ready?this.options.on_ready.call(this):void 0},a.prototype._got_error=function(a){return this._debug("Error - "+a),this._error_occured=a,this.options.on_error?this.options.on_error.call(this,this._error_occured):void 0},a}(),navigator.getUserMedia||(navigator.getUserMedia=navigator.webkitGetUserMedia||navigator.mozGetUserMedia||navigator.msGetUserMedia),navigator.getUserMedia&&(c=function(a){function b(){return f=b.__super__.constructor.apply(this,arguments)}return i(b,a),b.prototype._engine_init=function(){var a,b,c,d;return this._debug("Using HTML5 engine"),this.internal_container=document.createElement("div"),this.internal_container.style.width="100%",this.internal_container.style.height="100%",this.internal_container.style.position="relative",this.container.appendChild(this.internal_container),d=Math.floor(.2*this._view_height()),b=Math.floor(.2*this._view_width()),this.message=document.createElement("div"),this.message["class"]="message",this.message.style.width="100%",this.message.style.height="100%",this._add_prefixed_style(this.message,"boxSizing","border-box"),this.message.style.overflow="hidden",this.message.style.textAlign="center",this.message.style.paddingTop=""+d+"px",this.message.style.paddingBottom=""+d+"px",this.message.style.paddingLeft=""+b+"px",this.message.style.paddingRight=""+b+"px",this.message.style.position="absolute",this.message.style.zIndex=3,this.message.innerHTML="Please allow camera access when prompted by the browser.",this.internal_container.appendChild(this.message),this.video_container=document.createElement("div"),this.video_container.style.width=""+this._view_width()+"px",this.video_container.style.height=""+this._view_height()+"px",this.video_container.style.overflow="hidden",this.video_container.style.position="absolute",this.video_container.style.zIndex=1,this.internal_container.appendChild(this.video_container),this.video=document.createElement("video"),this.video.autoplay=!0,this._add_prefixed_style(this.video,"transform","scalex(-1.0)"),window.AudioContext||(window.AudioContext=window.webkitAudioContext),window.AudioContext&&this._load_shutter_sound(),a={video:{optional:[{minWidth:1280},{minWidth:640},{minWidth:480},{minWidth:360}]}},c=this,navigator.getUserMedia(a,function(a){return c._remove_message(),c.video.src=window.URL?URL.createObjectURL(a):a,c._wait_for_video_ready()},function(a){var b,d,e;b=a.code;for(d in a)if(e=a[d],"code"!==d)return c._got_error(d),void 0;return c._got_error("UNKNOWN ERROR")})},b.prototype._engine_play_shutter_sound=function(){var a;if(this.shutter_buffer)return a=this.audio_context.createBufferSource(),a.buffer=this.shutter_buffer,a.connect(this.audio_context.destination),a.start(0)},b.prototype._engine_capture=function(a,b,c){var d,e,f;return f=this._get_capture_crop(),d=document.createElement("canvas"),d.width=f.width,d.height=f.height,e=d.getContext("2d"),e.drawImage(this.video,f.x_offset,f.y_offset,f.width,f.height,0,0,f.width,f.height),a._canvas=d,a._mirror=b,a._quality=c},b.prototype._engine_display=function(a){return this.displayed_canvas&&this.internal_container.removeChild(this.displayed_canvas),this.displayed_canvas=a._canvas,this.displayed_canvas.style.width=""+this._view_width()+"px",this.displayed_canvas.style.height=""+this._view_height()+"px",this.displayed_canvas.style.top=0,this.displayed_canvas.style.left=0,this.displayed_canvas.style.position="absolute",this.displayed_canvas.style.zIndex=2,this._add_prefixed_style(this.displayed_canvas,"transform","scalex(-1.0)"),this.internal_container.appendChild(this.displayed_canvas)},b.prototype._engine_discard=function(a){return a._xhr&&a._xhr.abort(),delete a._xhr,delete a._canvas,delete a._jpeg_blob},b.prototype._engine_show_stream=function(){return this.displayed_canvas&&(this.internal_container.removeChild(this.displayed_canvas),this.displayed_canvas=null),this.video_container.style.display="block"},b.prototype._engine_upload=function(a,b,c,d){var e,f,g,h,i;return h=this,a._jpeg_blob?(this._debug("Uploading the file"),g=function(b){return delete a._xhr,a._status=b.target.status,a._response=b.target.responseText,a._status>=200&&a._status<300?a._upload_done():(a._error_message=b.target.statusText||"Unknown error",a._upload_fail())},i=new XMLHttpRequest,i.open("POST",b),i.timeout=d,c&&i.setRequestHeader("X-CSRF-Token",c),i.onload=g,i.onerror=g,i.onabort=g,i.send(a._jpeg_blob),a._xhr=i):(this._debug("Generating JPEG file"),a._mirror?(e=document.createElement("canvas"),e.width=a._canvas.width,e.height=a._canvas.height,f=e.getContext("2d"),f.setTransform(1,0,0,1,0,0),f.translate(e.width,0),f.scale(-1,1),f.drawImage(a._canvas,0,0)):e=a._canvas,e.toBlob(function(e){return a._jpeg_blob=e,h._engine_upload(a,b,c,d)},"image/jpeg",this.quality))},b.prototype._remove_message=function(){return this.message.style.display="none"},b.prototype._load_shutter_sound=function(){var a,b;if(!this.audio_context)return this.audio_context=new AudioContext,a=new XMLHttpRequest,a.open("GET",this.options.shutter_url,!0),a.responseType="arraybuffer",b=this,a.onload=function(){return b.audio_context.decodeAudioData(a.response,function(a){return b.shutter_buffer=a})},a.send()},b.prototype._wait_for_video_ready=function(){var a,b,c,d;return d=parseInt(this.video.videoWidth),c=parseInt(this.video.videoHeight),d>0&&c>0?(this.video_container.appendChild(this.video),this.video_width=d,this.video_height=c,this._debug("Camera resolution "+this.video_width+"x"+this.video_height+"px"),a=this._get_video_crop(),this.video.style.position="relative",this.video.style.width=""+a.width+"px",this.video.style.height=""+a.height+"px",this.video.style.left=""+a.x_offset+"px",this.video.style.top=""+a.y_offset+"px",this._prepared()):this._status_checks_count>100?this._got_error("Camera failed to initialize in 10 seconds"):(this._status_checks_count++,b=this,setTimeout(function(){return b._wait_for_video_ready()},100))},b.prototype._status_checks_count=0,b.prototype._add_prefixed_style=function(a,b,c){var d;return d=b.charAt(0).toUpperCase()+b.slice(1),a.style[b]=c,a.style["Webkit"+d]=c,a.style["Moz"+d]=c,a.style["ms"+d]=c,a.style["O"+d]=c},b.prototype._get_video_crop=function(){var a,b,c,d,e,f,g;return g=this._view_width(),e=this._view_height(),c=this.video_width/this.video_height,f=g/e,c>=f?(this._debug("Filling height"),d=e/this.video_height,b=Math.round(this.video_width*d),{width:b,height:e,x_offset:-Math.floor((b-g)/2),y_offset:0}):(this._debug("Filling width"),d=g/this.video_width,a=Math.round(this.video_height*d),{width:g,height:a,x_offset:0,y_offset:-Math.floor((a-e)/2)})},b.prototype._get_capture_crop=function(){var a,b,c,d,e,f;return f=this._view_width(),d=this._view_height(),c=this.video_width/this.video_height,e=f/d,c>=e?(b=Math.round(this.video_height*e),{width:b,height:this.video_height,x_offset:Math.floor((this.video_width-b)/2),y_offset:0}):(a=Math.round(this.video_width/e),{width:this.video_width,height:a,x_offset:0,y_offset:Math.floor((this.video_height-a)/2)})},b}(a),window.JpegCamera=c),e="9",!window.JpegCamera&&window.swfobject&&swfobject.hasFlashPlayerVersion(e)&&(b=function(a){function b(){return g=b.__super__.constructor.apply(this,arguments)}return i(b,a),b._send_message=function(a,b){var c,d;return(d=this._instances[parseInt(a)])?(c=Array.prototype.slice.call(arguments,2),this.prototype[b].apply(d,c)):void 0},b._instances={},b._next_id=1,b.prototype._engine_init=function(){var a,b,c,d,e,f,g,h;return this._debug("Using Flash engine"),this._id=this.constructor._next_id++,this.constructor._instances[this._id]=this,h=this._view_width(),e=this._view_height(),215>h||138>e?(this._got_error("camera is too small to display privacy dialog"),void 0):(c="flash_object_"+this._id,f={loop:"false",allowScriptAccess:"always",allowFullScreen:"false",quality:"best",wmode:"opaque",menu:"false"},a={id:c,align:"middle"},d={id:this._id,width:h,height:e,shutter_url:this.options.shutter_url},g=this,b=function(a){return a.success?(g._debug("Flash loaded"),g._flash=document.getElementById(c)):g._got_error("Flash loading failed.")},this.internal_container=document.createElement("div"),this.internal_container.id="jpeg_camera_flash_"+this._id,this.internal_container.style.width="100%",this.internal_container.style.height="100%",this.container.appendChild(this.internal_container),swfobject.embedSWF(this.options.swf_url,this.internal_container.id,h,e,"9",null,d,f,a,b))},b.prototype._engine_play_shutter_sound=function(){return this._flash._play_shutter()},b.prototype._engine_capture=function(a,b,c){return this._flash._capture(a.id,b,c)},b.prototype._engine_display=function(a){return this._flash._display(a.id)},b.prototype._engine_discard=function(a){return this._flash._discard(a.id)},b.prototype._engine_show_stream=function(){return this._flash._show_stream()},b.prototype._engine_upload=function(a,b,c,d){return this._flash._upload(a.id,b,c,d)},b.prototype._flash_prepared=function(){return this._prepared()},b.prototype._flash_upload_complete=function(a,b,c,d){var e;return a=parseInt(a),e=this._snapshots[a],e._status=parseInt(b),e._response=d,e._status>=200&&e._status<300?e._upload_done():(e._error_message=c,e._upload_fail())},b}(a),window.JpegCamera=b),d=function(){function a(a,b){this.camera=a,this.options=b,this.id=this.constructor._next_snapshot_id++}return a._next_snapshot_id=1,a.prototype._discarded=!1,a.prototype.show=function(){return this._discarded&&raise("discarded snapshot cannot be used"),this.camera._display(this),this},a.prototype.hide=function(){return this.camera.displayed_snapshot()===this&&this.camera.show_stream(),this},a.prototype.upload=function(a){var b,c;if(null==a&&(a={}),this._discarded&&raise("discarded snapshot cannot be used"),this._uploading)return this._debug("Upload already in progress"),void 0;if(this._uploading=!0,this._upload_options=a,b=this._options(),!b.api_url)throw this.camera._debug("Snapshot#upload called without valid api_url"),"Snapshot#upload called without valid api_url";return c="string"==typeof b.csrf_token&&b.csrf_token.length>0?b.csrf_token:null,this._done=!1,this._response=null,this._fail=!1,this._status=null,this._error_message=null,this.camera._upload(this,b.api_url,c,b.timeout),this},a.prototype._upload_options={},a.prototype._uploading=!1,a.prototype.done=function(a){var b;return this._discarded&&raise("discarded snapshot cannot be used"),this._upload_options.on_upload_done=a,b=this._options(),b.on_upload_done&&this._done&&b.on_upload_done.call(this,this._response),this},a.prototype._done=!1,a.prototype._response=null,a.prototype.fail=function(a){var b;return this._discarded&&raise("discarded snapshot cannot be used"),this._upload_options.on_upload_fail=a,b=this._options(),b.on_upload_fail&&this._fail&&b.on_upload_fail.call(this,this._status,this._error_message,this._response),this},a.prototype._fail=!1,a.prototype._status=null,a.prototype._error_message=null,a.prototype.discard=function(){return this.camera._discard(this),void 0},a.prototype._options=function(){return this.camera._extend({},this.camera.options,this.options,this._upload_options)},a.prototype._upload_done=function(){var a;return this.camera._debug("Upload completed"),this._uploading=!1,this._done=!0,a=this._options(),a.on_upload_done?a.on_upload_done.call(this,this._response):void 0},a.prototype._upload_fail=function(){var a;return this.camera._debug("Upload failed with status "+this._status),this._uploading=!1,this._fail=!0,a=this._options(),a.on_upload_fail?a.on_upload_fail.call(this,this._status,this._error_message,this._response):void 0},a}()}.call(this);
4
+ !function(){var a,b,c,d,e,f,g,h={}.hasOwnProperty,i=function(a,b){function c(){this.constructor=a}for(var d in b)h.call(b,d)&&(a[d]=b[d]);return c.prototype=b.prototype,a.prototype=new c,a.__super__=b.prototype,a};a=function(){function a(a,b){if(this.container="string"==typeof a?document.querySelector(a):a,!this.container||!this.container.offsetWidth)throw"JpegCamera: invalid container";this.container.innerHTML="",this.options=this._extend({},this.constructor.DefaultOptions,b),this._engine_init()}return a.DefaultOptions={shutter_url:"/jpeg_camera/shutter.mp3",swf_url:"/jpeg_camera/jpeg_camera.swf",on_debug:function(a){return console&&console.log?console.log("JpegCamera: "+a):void 0},quality:.9,shutter:!0,mirror:!1,timeout:0,retry_success:!1},a.prototype.ready=function(a){return this.options.on_ready=a,this.options.on_ready&&this._is_ready&&this.options.on_ready.call(this),this},a.prototype._is_ready=!1,a.prototype.error=function(a){return this.options.on_error=a,this.options.on_error&&this._error_occured&&this.options.on_error.call(this,this._error_occured),this},a.prototype._error_occured=!1,a.prototype.capture=function(a){var b,c;return null==a&&(a={}),b=new d(this,a),this._snapshots[b.id]=b,c=b._options(),c.shutter&&this._engine_play_shutter_sound(),this._engine_capture(b,c.mirror,c.quality),b},a.prototype._snapshots={},a.prototype.show_stream=function(){return this._engine_show_stream(),this._displayed_snapshot=null,this},a.prototype.discard_all=function(){var a,b,c;this._displayed_snapshot&&this.show_stream(),c=this._snapshots;for(a in c)b=c[a],this._engine_discard(b),b._discarded=!0;return this._snapshots={},this},a.prototype._extend=function(a){var b,c,d,e,f,g;for(d=Array.prototype.slice.call(arguments,1),f=0,g=d.length;g>f;f++)if(c=d[f])for(b in c)e=c[b],a[b]=e;return a},a.prototype._debug=function(a){return this.options.on_debug?this.options.on_debug.call(this,a):void 0},a.prototype._view_width=function(){return parseInt(this.container.offsetWidth,10)},a.prototype._view_height=function(){return parseInt(this.container.offsetHeight,10)},a.prototype._display=function(a){return this._engine_display(a),this._displayed_snapshot=a},a.prototype._displayed_snapshot=null,a.prototype._upload=function(a,b,c,d){return this._engine_upload(a,b,c,d)},a.prototype._discard=function(a){return this._displayed_snapshot===a&&this.show_stream(),this._engine_discard(a),a._discarded=!0,delete this._snapshots[a.id]},a.prototype._prepared=function(){return this._is_ready=!0,this.options.on_ready?this.options.on_ready.call(this):void 0},a.prototype._got_error=function(a){return this._debug("Error - "+a),this._error_occured=a,this.options.on_error?this.options.on_error.call(this,this._error_occured):void 0},a}(),navigator.getUserMedia||(navigator.getUserMedia=navigator.webkitGetUserMedia||navigator.mozGetUserMedia||navigator.msGetUserMedia),navigator.getUserMedia&&(c=function(a){function b(){return f=b.__super__.constructor.apply(this,arguments)}return i(b,a),b.prototype._engine_init=function(){var a,b,c,d;return this._debug("Using HTML5 engine"),this.internal_container=document.createElement("div"),this.internal_container.style.width="100%",this.internal_container.style.height="100%",this.internal_container.style.position="relative",this.container.appendChild(this.internal_container),d=Math.floor(.2*this._view_height()),b=Math.floor(.2*this._view_width()),this.message=document.createElement("div"),this.message["class"]="message",this.message.style.width="100%",this.message.style.height="100%",this._add_prefixed_style(this.message,"boxSizing","border-box"),this.message.style.overflow="hidden",this.message.style.textAlign="center",this.message.style.paddingTop=""+d+"px",this.message.style.paddingBottom=""+d+"px",this.message.style.paddingLeft=""+b+"px",this.message.style.paddingRight=""+b+"px",this.message.style.position="absolute",this.message.style.zIndex=3,this.message.innerHTML="Please allow camera access when prompted by the browser.",this.internal_container.appendChild(this.message),this.video_container=document.createElement("div"),this.video_container.style.width=""+this._view_width()+"px",this.video_container.style.height=""+this._view_height()+"px",this.video_container.style.overflow="hidden",this.video_container.style.position="absolute",this.video_container.style.zIndex=1,this.internal_container.appendChild(this.video_container),this.video=document.createElement("video"),this.video.autoplay=!0,this._add_prefixed_style(this.video,"transform","scalex(-1.0)"),window.AudioContext||(window.AudioContext=window.webkitAudioContext),window.AudioContext&&this._load_shutter_sound(),a={video:{optional:[{minWidth:1280},{minWidth:640},{minWidth:480},{minWidth:360}]}},c=this,navigator.getUserMedia(a,function(a){return c._remove_message(),c.video.src=window.URL?URL.createObjectURL(a):a,c._wait_for_video_ready()},function(a){var b,d,e;b=a.code;for(d in a)if(e=a[d],"code"!==d)return c._got_error(d),void 0;return c._got_error("UNKNOWN ERROR")})},b.prototype._engine_play_shutter_sound=function(){var a;if(this.shutter_buffer)return a=this.audio_context.createBufferSource(),a.buffer=this.shutter_buffer,a.connect(this.audio_context.destination),a.start(0)},b.prototype._engine_capture=function(a,b,c){var d,e,f;return f=this._get_capture_crop(),d=document.createElement("canvas"),d.width=f.width,d.height=f.height,e=d.getContext("2d"),e.drawImage(this.video,f.x_offset,f.y_offset,f.width,f.height,0,0,f.width,f.height),a._canvas=d,a._mirror=b,a._quality=c},b.prototype._engine_display=function(a){return this.displayed_canvas&&this.internal_container.removeChild(this.displayed_canvas),this.displayed_canvas=a._canvas,this.displayed_canvas.style.width=""+this._view_width()+"px",this.displayed_canvas.style.height=""+this._view_height()+"px",this.displayed_canvas.style.top=0,this.displayed_canvas.style.left=0,this.displayed_canvas.style.position="absolute",this.displayed_canvas.style.zIndex=2,this._add_prefixed_style(this.displayed_canvas,"transform","scalex(-1.0)"),this.internal_container.appendChild(this.displayed_canvas)},b.prototype._engine_discard=function(a){return a._xhr&&a._xhr.abort(),delete a._xhr,delete a._canvas,delete a._jpeg_blob},b.prototype._engine_show_stream=function(){return this.displayed_canvas&&(this.internal_container.removeChild(this.displayed_canvas),this.displayed_canvas=null),this.video_container.style.display="block"},b.prototype._engine_upload=function(a,b,c,d){var e,f,g,h,i;return h=this,a._jpeg_blob?(this._debug("Uploading the file"),g=function(b){return delete a._xhr,a._status=b.target.status,a._response=b.target.responseText,a._status>=200&&a._status<300?a._upload_done():(a._error_message=b.target.statusText||"Unknown error",a._upload_fail())},i=new XMLHttpRequest,i.open("POST",b),i.timeout=d,c&&i.setRequestHeader("X-CSRF-Token",c),i.onload=g,i.onerror=g,i.onabort=g,i.send(a._jpeg_blob),a._xhr=i):(this._debug("Generating JPEG file"),a._mirror?(e=document.createElement("canvas"),e.width=a._canvas.width,e.height=a._canvas.height,f=e.getContext("2d"),f.setTransform(1,0,0,1,0,0),f.translate(e.width,0),f.scale(-1,1),f.drawImage(a._canvas,0,0)):e=a._canvas,e.toBlob(function(e){return a._jpeg_blob=e,h._engine_upload(a,b,c,d)},"image/jpeg",this.quality))},b.prototype._remove_message=function(){return this.message.style.display="none"},b.prototype._load_shutter_sound=function(){var a,b;if(!this.audio_context)return this.audio_context=new AudioContext,a=new XMLHttpRequest,a.open("GET",this.options.shutter_url,!0),a.responseType="arraybuffer",b=this,a.onload=function(){return b.audio_context.decodeAudioData(a.response,function(a){return b.shutter_buffer=a})},a.send()},b.prototype._wait_for_video_ready=function(){var a,b,c,d;return d=parseInt(this.video.videoWidth),c=parseInt(this.video.videoHeight),d>0&&c>0?(this.video_container.appendChild(this.video),this.video_width=d,this.video_height=c,this._debug("Camera resolution "+this.video_width+"x"+this.video_height+"px"),a=this._get_video_crop(),this.video.style.position="relative",this.video.style.width=""+a.width+"px",this.video.style.height=""+a.height+"px",this.video.style.left=""+a.x_offset+"px",this.video.style.top=""+a.y_offset+"px",this._prepared()):this._status_checks_count>100?this._got_error("Camera failed to initialize in 10 seconds"):(this._status_checks_count++,b=this,setTimeout(function(){return b._wait_for_video_ready()},100))},b.prototype._status_checks_count=0,b.prototype._add_prefixed_style=function(a,b,c){var d;return d=b.charAt(0).toUpperCase()+b.slice(1),a.style[b]=c,a.style["Webkit"+d]=c,a.style["Moz"+d]=c,a.style["ms"+d]=c,a.style["O"+d]=c},b.prototype._get_video_crop=function(){var a,b,c,d,e,f,g;return g=this._view_width(),e=this._view_height(),c=this.video_width/this.video_height,f=g/e,c>=f?(this._debug("Filling height"),d=e/this.video_height,b=Math.round(this.video_width*d),{width:b,height:e,x_offset:-Math.floor((b-g)/2),y_offset:0}):(this._debug("Filling width"),d=g/this.video_width,a=Math.round(this.video_height*d),{width:g,height:a,x_offset:0,y_offset:-Math.floor((a-e)/2)})},b.prototype._get_capture_crop=function(){var a,b,c,d,e,f;return f=this._view_width(),d=this._view_height(),c=this.video_width/this.video_height,e=f/d,c>=e?(b=Math.round(this.video_height*e),{width:b,height:this.video_height,x_offset:Math.floor((this.video_width-b)/2),y_offset:0}):(a=Math.round(this.video_width/e),{width:this.video_width,height:a,x_offset:0,y_offset:Math.floor((this.video_height-a)/2)})},b}(a),window.JpegCamera=c),e="9",!window.JpegCamera&&window.swfobject&&swfobject.hasFlashPlayerVersion(e)&&(b=function(a){function b(){return g=b.__super__.constructor.apply(this,arguments)}return i(b,a),b._send_message=function(a,b){var c,d;return(d=this._instances[parseInt(a)])?(c=Array.prototype.slice.call(arguments,2),this.prototype[b].apply(d,c)):void 0},b._instances={},b._next_id=1,b.prototype._engine_init=function(){var a,b,c,d,e,f,g,h;return this._debug("Using Flash engine"),this._id=this.constructor._next_id++,this.constructor._instances[this._id]=this,h=this._view_width(),e=this._view_height(),215>h||138>e?(this._got_error("camera is too small to display privacy dialog"),void 0):(c="flash_object_"+this._id,f={loop:"false",allowScriptAccess:"always",allowFullScreen:"false",quality:"best",wmode:"opaque",menu:"false"},a={id:c,align:"middle"},d={id:this._id,width:h,height:e,shutter_url:this.options.shutter_url},g=this,b=function(a){return a.success?(g._debug("Flash loaded"),g._flash=document.getElementById(c)):g._got_error("Flash loading failed.")},this.internal_container=document.createElement("div"),this.internal_container.id="jpeg_camera_flash_"+this._id,this.internal_container.style.width="100%",this.internal_container.style.height="100%",this.container.appendChild(this.internal_container),swfobject.embedSWF(this.options.swf_url,this.internal_container.id,h,e,"9",null,d,f,a,b))},b.prototype._engine_play_shutter_sound=function(){return this._flash._play_shutter()},b.prototype._engine_capture=function(a,b,c){return this._flash._capture(a.id,b,c)},b.prototype._engine_display=function(a){return this._flash._display(a.id)},b.prototype._engine_discard=function(a){return this._flash._discard(a.id)},b.prototype._engine_show_stream=function(){return this._flash._show_stream()},b.prototype._engine_upload=function(a,b,c,d){return this._flash._upload(a.id,b,c,d)},b.prototype._flash_prepared=function(){return this._prepared()},b.prototype._flash_upload_complete=function(a,b,c,d){var e;return a=parseInt(a),e=this._snapshots[a],e._status=parseInt(b),e._response=d,e._status>=200&&e._status<300?e._upload_done():(e._error_message=c,e._upload_fail())},b}(a),window.JpegCamera=b),d=function(){function a(a,b){this.camera=a,this.options=b,this.id=this.constructor._next_snapshot_id++}return a._next_snapshot_id=1,a.prototype._discarded=!1,a.prototype.show=function(){return this._discarded&&raise("discarded snapshot cannot be used"),this.camera._display(this),this},a.prototype.hide=function(){return this.camera.displayed_snapshot()===this&&this.camera.show_stream(),this},a.prototype.upload=function(a){var b;if(null==a&&(a={}),this._discarded&&raise("discarded snapshot cannot be used"),this._uploading)return this.camera._debug("Upload already in progress"),void 0;if(this._uploading=!0,this._retry=1,this._upload_options=a,b=this._options(),!b.api_url)throw this.camera._debug("Snapshot#upload called without valid api_url"),"Snapshot#upload called without valid api_url";return this._start_upload(b),this},a.prototype._upload_options={},a.prototype._uploading=!1,a.prototype._retry=1,a.prototype.done=function(a){var b;return this._discarded&&raise("discarded snapshot cannot be used"),this._upload_options.on_upload_done=a,b=this._options(),b.on_upload_done&&this._done&&b.on_upload_done.call(this,this._response),this},a.prototype._done=!1,a.prototype._response=null,a.prototype.fail=function(a){var b;return this._discarded&&raise("discarded snapshot cannot be used"),this._upload_options.on_upload_fail=a,b=this._options(),b.on_upload_fail&&this._fail&&b.on_upload_fail.call(this,this._status,this._error_message,this._response),this},a.prototype._fail=!1,a.prototype._status=null,a.prototype._error_message=null,a.prototype.discard=function(){return this.camera._discard(this),void 0},a.prototype._options=function(){return this.camera._extend({},this.camera.options,this.options,this._upload_options)},a.prototype._start_upload=function(a){var b;return b="string"==typeof a.csrf_token&&a.csrf_token.length>0?a.csrf_token:null,this._done=!1,this._response=null,this._fail=!1,this._status=null,this._error_message=null,this.camera._upload(this,a.api_url,b,a.timeout)},a.prototype._upload_done=function(){var a,b,c,d;return this.camera._debug("Upload completed with status "+this._status),this._done=!0,a=this._options(),c=a.retry_success&&a.retry_if&&a.retry_if.call(this,this._status,this._error_message,this._response,this._retry),!0===c&&(c=0),"number"==typeof c?(this._retry++,c>0?(b=parseInt(c),this.camera._debug("Will retry the upload in "+b+"ms (attempt #"+this._retry+")"),d=this,setTimeout(function(){return d._start_upload(a)},b)):(this.camera._debug("Will retry the upload immediately (attempt #"+this._retry+")"),this._start_upload(a))):(this._uploading=!1,a.on_upload_done?a.on_upload_done.call(this,this._response):void 0)},a.prototype._upload_fail=function(){var a,b,c,d;return this.camera._debug("Upload failed with status "+this._status),this._fail=!0,a=this._options(),c=a.retry_if&&a.retry_if.call(this,this._status,this._error_message,this._response,this._retry),!0===c&&(c=0),"number"==typeof c?(this._retry++,c>0?(b=parseInt(c),this.camera._debug("Will retry the upload in "+b+"ms (attempt #"+this._retry+")"),d=this,setTimeout(function(){return d._start_upload(a)},b)):(this.camera._debug("Will retry the upload immediately (attempt #"+this._retry+")"),this._start_upload(a))):(this._uploading=!1,a.on_upload_fail?a.on_upload_fail.call(this,this._status,this._error_message,this._response):void 0)},a}()}.call(this);
@@ -1,4 +1,4 @@
1
- /*! JpegCamera 1.0.0 | 2013-07-29
1
+ /*! JpegCamera 1.0.2 | 2013-07-30
2
2
  (c) 2013 Adam Wrobel
3
3
  http://amw.github.io/jpeg_camera */
4
4
  (function() {
@@ -18,7 +18,8 @@
18
18
  quality: 0.9,
19
19
  shutter: true,
20
20
  mirror: false,
21
- timeout: 0
21
+ timeout: 0,
22
+ retry_success: false
22
23
  };
23
24
 
24
25
  function JpegCamera(container, options) {
@@ -506,7 +507,7 @@
506
507
  };
507
508
 
508
509
  Snapshot.prototype.upload = function(options) {
509
- var cache, csrf_token;
510
+ var cache;
510
511
  if (options == null) {
511
512
  options = {};
512
513
  }
@@ -514,27 +515,18 @@
514
515
  raise("discarded snapshot cannot be used");
515
516
  }
516
517
  if (this._uploading) {
517
- this._debug("Upload already in progress");
518
+ this.camera._debug("Upload already in progress");
518
519
  return;
519
520
  }
520
521
  this._uploading = true;
522
+ this._retry = 1;
521
523
  this._upload_options = options;
522
524
  cache = this._options();
523
525
  if (!cache.api_url) {
524
526
  this.camera._debug("Snapshot#upload called without valid api_url");
525
527
  throw "Snapshot#upload called without valid api_url";
526
528
  }
527
- if ("string" === typeof cache.csrf_token && cache.csrf_token.length > 0) {
528
- csrf_token = cache.csrf_token;
529
- } else {
530
- csrf_token = null;
531
- }
532
- this._done = false;
533
- this._response = null;
534
- this._fail = false;
535
- this._status = null;
536
- this._error_message = null;
537
- this.camera._upload(this, cache.api_url, csrf_token, cache.timeout);
529
+ this._start_upload(cache);
538
530
  return this;
539
531
  };
540
532
 
@@ -542,6 +534,8 @@
542
534
 
543
535
  Snapshot.prototype._uploading = false;
544
536
 
537
+ Snapshot.prototype._retry = 1;
538
+
545
539
  Snapshot.prototype.done = function(callback) {
546
540
  var cache;
547
541
  if (this._discarded) {
@@ -587,25 +581,78 @@
587
581
  return this.camera._extend({}, this.camera.options, this.options, this._upload_options);
588
582
  };
589
583
 
584
+ Snapshot.prototype._start_upload = function(cache) {
585
+ var csrf_token;
586
+ if ("string" === typeof cache.csrf_token && cache.csrf_token.length > 0) {
587
+ csrf_token = cache.csrf_token;
588
+ } else {
589
+ csrf_token = null;
590
+ }
591
+ this._done = false;
592
+ this._response = null;
593
+ this._fail = false;
594
+ this._status = null;
595
+ this._error_message = null;
596
+ return this.camera._upload(this, cache.api_url, csrf_token, cache.timeout);
597
+ };
598
+
590
599
  Snapshot.prototype._upload_done = function() {
591
- var cache;
592
- this.camera._debug("Upload completed");
593
- this._uploading = false;
600
+ var cache, delay, retry_decision, that;
601
+ this.camera._debug("Upload completed with status " + this._status);
594
602
  this._done = true;
595
603
  cache = this._options();
596
- if (cache.on_upload_done) {
597
- return cache.on_upload_done.call(this, this._response);
604
+ retry_decision = cache.retry_success && cache.retry_if && cache.retry_if.call(this, this._status, this._error_message, this._response, this._retry);
605
+ if (true === retry_decision) {
606
+ retry_decision = 0;
607
+ }
608
+ if ("number" === typeof retry_decision) {
609
+ this._retry++;
610
+ if (retry_decision > 0) {
611
+ delay = parseInt(retry_decision);
612
+ this.camera._debug("Will retry the upload in " + delay + "ms (attempt #" + this._retry + ")");
613
+ that = this;
614
+ return setTimeout((function() {
615
+ return that._start_upload(cache);
616
+ }), delay);
617
+ } else {
618
+ this.camera._debug("Will retry the upload immediately (attempt #" + this._retry + ")");
619
+ return this._start_upload(cache);
620
+ }
621
+ } else {
622
+ this._uploading = false;
623
+ if (cache.on_upload_done) {
624
+ return cache.on_upload_done.call(this, this._response);
625
+ }
598
626
  }
599
627
  };
600
628
 
601
629
  Snapshot.prototype._upload_fail = function() {
602
- var cache;
630
+ var cache, delay, retry_decision, that;
603
631
  this.camera._debug("Upload failed with status " + this._status);
604
- this._uploading = false;
605
632
  this._fail = true;
606
633
  cache = this._options();
607
- if (cache.on_upload_fail) {
608
- return cache.on_upload_fail.call(this, this._status, this._error_message, this._response);
634
+ retry_decision = cache.retry_if && cache.retry_if.call(this, this._status, this._error_message, this._response, this._retry);
635
+ if (true === retry_decision) {
636
+ retry_decision = 0;
637
+ }
638
+ if ("number" === typeof retry_decision) {
639
+ this._retry++;
640
+ if (retry_decision > 0) {
641
+ delay = parseInt(retry_decision);
642
+ this.camera._debug("Will retry the upload in " + delay + "ms (attempt #" + this._retry + ")");
643
+ that = this;
644
+ return setTimeout((function() {
645
+ return that._start_upload(cache);
646
+ }), delay);
647
+ } else {
648
+ this.camera._debug("Will retry the upload immediately (attempt #" + this._retry + ")");
649
+ return this._start_upload(cache);
650
+ }
651
+ } else {
652
+ this._uploading = false;
653
+ if (cache.on_upload_fail) {
654
+ return cache.on_upload_fail.call(this, this._status, this._error_message, this._response);
655
+ }
609
656
  }
610
657
  };
611
658
 
@@ -1,4 +1,4 @@
1
- /*! JpegCamera 1.0.0 | 2013-07-29
1
+ /*! JpegCamera 1.0.2 | 2013-07-30
2
2
  (c) 2013 Adam Wrobel
3
3
  http://amw.github.io/jpeg_camera */
4
- !function(){var a,b,c,d,e={}.hasOwnProperty,f=function(a,b){function c(){this.constructor=a}for(var d in b)e.call(b,d)&&(a[d]=b[d]);return c.prototype=b.prototype,a.prototype=new c,a.__super__=b.prototype,a};a=function(){function a(a,b){if(this.container="string"==typeof a?document.querySelector(a):a,!this.container||!this.container.offsetWidth)throw"JpegCamera: invalid container";this.container.innerHTML="",this.options=this._extend({},this.constructor.DefaultOptions,b),this._engine_init()}return a.DefaultOptions={shutter_url:"/jpeg_camera/shutter.mp3",swf_url:"/jpeg_camera/jpeg_camera.swf",on_debug:function(a){return console&&console.log?console.log("JpegCamera: "+a):void 0},quality:.9,shutter:!0,mirror:!1,timeout:0},a.prototype.ready=function(a){return this.options.on_ready=a,this.options.on_ready&&this._is_ready&&this.options.on_ready.call(this),this},a.prototype._is_ready=!1,a.prototype.error=function(a){return this.options.on_error=a,this.options.on_error&&this._error_occured&&this.options.on_error.call(this,this._error_occured),this},a.prototype._error_occured=!1,a.prototype.capture=function(a){var b,d;return null==a&&(a={}),b=new c(this,a),this._snapshots[b.id]=b,d=b._options(),d.shutter&&this._engine_play_shutter_sound(),this._engine_capture(b,d.mirror,d.quality),b},a.prototype._snapshots={},a.prototype.show_stream=function(){return this._engine_show_stream(),this._displayed_snapshot=null,this},a.prototype.discard_all=function(){var a,b,c;this._displayed_snapshot&&this.show_stream(),c=this._snapshots;for(a in c)b=c[a],this._engine_discard(b),b._discarded=!0;return this._snapshots={},this},a.prototype._extend=function(a){var b,c,d,e,f,g;for(d=Array.prototype.slice.call(arguments,1),f=0,g=d.length;g>f;f++)if(c=d[f])for(b in c)e=c[b],a[b]=e;return a},a.prototype._debug=function(a){return this.options.on_debug?this.options.on_debug.call(this,a):void 0},a.prototype._view_width=function(){return parseInt(this.container.offsetWidth,10)},a.prototype._view_height=function(){return parseInt(this.container.offsetHeight,10)},a.prototype._display=function(a){return this._engine_display(a),this._displayed_snapshot=a},a.prototype._displayed_snapshot=null,a.prototype._upload=function(a,b,c,d){return this._engine_upload(a,b,c,d)},a.prototype._discard=function(a){return this._displayed_snapshot===a&&this.show_stream(),this._engine_discard(a),a._discarded=!0,delete this._snapshots[a.id]},a.prototype._prepared=function(){return this._is_ready=!0,this.options.on_ready?this.options.on_ready.call(this):void 0},a.prototype._got_error=function(a){return this._debug("Error - "+a),this._error_occured=a,this.options.on_error?this.options.on_error.call(this,this._error_occured):void 0},a}(),navigator.getUserMedia||(navigator.getUserMedia=navigator.webkitGetUserMedia||navigator.mozGetUserMedia||navigator.msGetUserMedia),navigator.getUserMedia&&(b=function(a){function b(){return d=b.__super__.constructor.apply(this,arguments)}return f(b,a),b.prototype._engine_init=function(){var a,b,c,d;return this._debug("Using HTML5 engine"),this.internal_container=document.createElement("div"),this.internal_container.style.width="100%",this.internal_container.style.height="100%",this.internal_container.style.position="relative",this.container.appendChild(this.internal_container),d=Math.floor(.2*this._view_height()),b=Math.floor(.2*this._view_width()),this.message=document.createElement("div"),this.message["class"]="message",this.message.style.width="100%",this.message.style.height="100%",this._add_prefixed_style(this.message,"boxSizing","border-box"),this.message.style.overflow="hidden",this.message.style.textAlign="center",this.message.style.paddingTop=""+d+"px",this.message.style.paddingBottom=""+d+"px",this.message.style.paddingLeft=""+b+"px",this.message.style.paddingRight=""+b+"px",this.message.style.position="absolute",this.message.style.zIndex=3,this.message.innerHTML="Please allow camera access when prompted by the browser.",this.internal_container.appendChild(this.message),this.video_container=document.createElement("div"),this.video_container.style.width=""+this._view_width()+"px",this.video_container.style.height=""+this._view_height()+"px",this.video_container.style.overflow="hidden",this.video_container.style.position="absolute",this.video_container.style.zIndex=1,this.internal_container.appendChild(this.video_container),this.video=document.createElement("video"),this.video.autoplay=!0,this._add_prefixed_style(this.video,"transform","scalex(-1.0)"),window.AudioContext||(window.AudioContext=window.webkitAudioContext),window.AudioContext&&this._load_shutter_sound(),a={video:{optional:[{minWidth:1280},{minWidth:640},{minWidth:480},{minWidth:360}]}},c=this,navigator.getUserMedia(a,function(a){return c._remove_message(),c.video.src=window.URL?URL.createObjectURL(a):a,c._wait_for_video_ready()},function(a){var b,d,e;b=a.code;for(d in a)if(e=a[d],"code"!==d)return c._got_error(d),void 0;return c._got_error("UNKNOWN ERROR")})},b.prototype._engine_play_shutter_sound=function(){var a;if(this.shutter_buffer)return a=this.audio_context.createBufferSource(),a.buffer=this.shutter_buffer,a.connect(this.audio_context.destination),a.start(0)},b.prototype._engine_capture=function(a,b,c){var d,e,f;return f=this._get_capture_crop(),d=document.createElement("canvas"),d.width=f.width,d.height=f.height,e=d.getContext("2d"),e.drawImage(this.video,f.x_offset,f.y_offset,f.width,f.height,0,0,f.width,f.height),a._canvas=d,a._mirror=b,a._quality=c},b.prototype._engine_display=function(a){return this.displayed_canvas&&this.internal_container.removeChild(this.displayed_canvas),this.displayed_canvas=a._canvas,this.displayed_canvas.style.width=""+this._view_width()+"px",this.displayed_canvas.style.height=""+this._view_height()+"px",this.displayed_canvas.style.top=0,this.displayed_canvas.style.left=0,this.displayed_canvas.style.position="absolute",this.displayed_canvas.style.zIndex=2,this._add_prefixed_style(this.displayed_canvas,"transform","scalex(-1.0)"),this.internal_container.appendChild(this.displayed_canvas)},b.prototype._engine_discard=function(a){return a._xhr&&a._xhr.abort(),delete a._xhr,delete a._canvas,delete a._jpeg_blob},b.prototype._engine_show_stream=function(){return this.displayed_canvas&&(this.internal_container.removeChild(this.displayed_canvas),this.displayed_canvas=null),this.video_container.style.display="block"},b.prototype._engine_upload=function(a,b,c,d){var e,f,g,h,i;return h=this,a._jpeg_blob?(this._debug("Uploading the file"),g=function(b){return delete a._xhr,a._status=b.target.status,a._response=b.target.responseText,a._status>=200&&a._status<300?a._upload_done():(a._error_message=b.target.statusText||"Unknown error",a._upload_fail())},i=new XMLHttpRequest,i.open("POST",b),i.timeout=d,c&&i.setRequestHeader("X-CSRF-Token",c),i.onload=g,i.onerror=g,i.onabort=g,i.send(a._jpeg_blob),a._xhr=i):(this._debug("Generating JPEG file"),a._mirror?(e=document.createElement("canvas"),e.width=a._canvas.width,e.height=a._canvas.height,f=e.getContext("2d"),f.setTransform(1,0,0,1,0,0),f.translate(e.width,0),f.scale(-1,1),f.drawImage(a._canvas,0,0)):e=a._canvas,e.toBlob(function(e){return a._jpeg_blob=e,h._engine_upload(a,b,c,d)},"image/jpeg",this.quality))},b.prototype._remove_message=function(){return this.message.style.display="none"},b.prototype._load_shutter_sound=function(){var a,b;if(!this.audio_context)return this.audio_context=new AudioContext,a=new XMLHttpRequest,a.open("GET",this.options.shutter_url,!0),a.responseType="arraybuffer",b=this,a.onload=function(){return b.audio_context.decodeAudioData(a.response,function(a){return b.shutter_buffer=a})},a.send()},b.prototype._wait_for_video_ready=function(){var a,b,c,d;return d=parseInt(this.video.videoWidth),c=parseInt(this.video.videoHeight),d>0&&c>0?(this.video_container.appendChild(this.video),this.video_width=d,this.video_height=c,this._debug("Camera resolution "+this.video_width+"x"+this.video_height+"px"),a=this._get_video_crop(),this.video.style.position="relative",this.video.style.width=""+a.width+"px",this.video.style.height=""+a.height+"px",this.video.style.left=""+a.x_offset+"px",this.video.style.top=""+a.y_offset+"px",this._prepared()):this._status_checks_count>100?this._got_error("Camera failed to initialize in 10 seconds"):(this._status_checks_count++,b=this,setTimeout(function(){return b._wait_for_video_ready()},100))},b.prototype._status_checks_count=0,b.prototype._add_prefixed_style=function(a,b,c){var d;return d=b.charAt(0).toUpperCase()+b.slice(1),a.style[b]=c,a.style["Webkit"+d]=c,a.style["Moz"+d]=c,a.style["ms"+d]=c,a.style["O"+d]=c},b.prototype._get_video_crop=function(){var a,b,c,d,e,f,g;return g=this._view_width(),e=this._view_height(),c=this.video_width/this.video_height,f=g/e,c>=f?(this._debug("Filling height"),d=e/this.video_height,b=Math.round(this.video_width*d),{width:b,height:e,x_offset:-Math.floor((b-g)/2),y_offset:0}):(this._debug("Filling width"),d=g/this.video_width,a=Math.round(this.video_height*d),{width:g,height:a,x_offset:0,y_offset:-Math.floor((a-e)/2)})},b.prototype._get_capture_crop=function(){var a,b,c,d,e,f;return f=this._view_width(),d=this._view_height(),c=this.video_width/this.video_height,e=f/d,c>=e?(b=Math.round(this.video_height*e),{width:b,height:this.video_height,x_offset:Math.floor((this.video_width-b)/2),y_offset:0}):(a=Math.round(this.video_width/e),{width:this.video_width,height:a,x_offset:0,y_offset:Math.floor((this.video_height-a)/2)})},b}(a),window.JpegCamera=b),c=function(){function a(a,b){this.camera=a,this.options=b,this.id=this.constructor._next_snapshot_id++}return a._next_snapshot_id=1,a.prototype._discarded=!1,a.prototype.show=function(){return this._discarded&&raise("discarded snapshot cannot be used"),this.camera._display(this),this},a.prototype.hide=function(){return this.camera.displayed_snapshot()===this&&this.camera.show_stream(),this},a.prototype.upload=function(a){var b,c;if(null==a&&(a={}),this._discarded&&raise("discarded snapshot cannot be used"),this._uploading)return this._debug("Upload already in progress"),void 0;if(this._uploading=!0,this._upload_options=a,b=this._options(),!b.api_url)throw this.camera._debug("Snapshot#upload called without valid api_url"),"Snapshot#upload called without valid api_url";return c="string"==typeof b.csrf_token&&b.csrf_token.length>0?b.csrf_token:null,this._done=!1,this._response=null,this._fail=!1,this._status=null,this._error_message=null,this.camera._upload(this,b.api_url,c,b.timeout),this},a.prototype._upload_options={},a.prototype._uploading=!1,a.prototype.done=function(a){var b;return this._discarded&&raise("discarded snapshot cannot be used"),this._upload_options.on_upload_done=a,b=this._options(),b.on_upload_done&&this._done&&b.on_upload_done.call(this,this._response),this},a.prototype._done=!1,a.prototype._response=null,a.prototype.fail=function(a){var b;return this._discarded&&raise("discarded snapshot cannot be used"),this._upload_options.on_upload_fail=a,b=this._options(),b.on_upload_fail&&this._fail&&b.on_upload_fail.call(this,this._status,this._error_message,this._response),this},a.prototype._fail=!1,a.prototype._status=null,a.prototype._error_message=null,a.prototype.discard=function(){return this.camera._discard(this),void 0},a.prototype._options=function(){return this.camera._extend({},this.camera.options,this.options,this._upload_options)},a.prototype._upload_done=function(){var a;return this.camera._debug("Upload completed"),this._uploading=!1,this._done=!0,a=this._options(),a.on_upload_done?a.on_upload_done.call(this,this._response):void 0},a.prototype._upload_fail=function(){var a;return this.camera._debug("Upload failed with status "+this._status),this._uploading=!1,this._fail=!0,a=this._options(),a.on_upload_fail?a.on_upload_fail.call(this,this._status,this._error_message,this._response):void 0},a}()}.call(this);
4
+ !function(){var a,b,c,d,e={}.hasOwnProperty,f=function(a,b){function c(){this.constructor=a}for(var d in b)e.call(b,d)&&(a[d]=b[d]);return c.prototype=b.prototype,a.prototype=new c,a.__super__=b.prototype,a};a=function(){function a(a,b){if(this.container="string"==typeof a?document.querySelector(a):a,!this.container||!this.container.offsetWidth)throw"JpegCamera: invalid container";this.container.innerHTML="",this.options=this._extend({},this.constructor.DefaultOptions,b),this._engine_init()}return a.DefaultOptions={shutter_url:"/jpeg_camera/shutter.mp3",swf_url:"/jpeg_camera/jpeg_camera.swf",on_debug:function(a){return console&&console.log?console.log("JpegCamera: "+a):void 0},quality:.9,shutter:!0,mirror:!1,timeout:0,retry_success:!1},a.prototype.ready=function(a){return this.options.on_ready=a,this.options.on_ready&&this._is_ready&&this.options.on_ready.call(this),this},a.prototype._is_ready=!1,a.prototype.error=function(a){return this.options.on_error=a,this.options.on_error&&this._error_occured&&this.options.on_error.call(this,this._error_occured),this},a.prototype._error_occured=!1,a.prototype.capture=function(a){var b,d;return null==a&&(a={}),b=new c(this,a),this._snapshots[b.id]=b,d=b._options(),d.shutter&&this._engine_play_shutter_sound(),this._engine_capture(b,d.mirror,d.quality),b},a.prototype._snapshots={},a.prototype.show_stream=function(){return this._engine_show_stream(),this._displayed_snapshot=null,this},a.prototype.discard_all=function(){var a,b,c;this._displayed_snapshot&&this.show_stream(),c=this._snapshots;for(a in c)b=c[a],this._engine_discard(b),b._discarded=!0;return this._snapshots={},this},a.prototype._extend=function(a){var b,c,d,e,f,g;for(d=Array.prototype.slice.call(arguments,1),f=0,g=d.length;g>f;f++)if(c=d[f])for(b in c)e=c[b],a[b]=e;return a},a.prototype._debug=function(a){return this.options.on_debug?this.options.on_debug.call(this,a):void 0},a.prototype._view_width=function(){return parseInt(this.container.offsetWidth,10)},a.prototype._view_height=function(){return parseInt(this.container.offsetHeight,10)},a.prototype._display=function(a){return this._engine_display(a),this._displayed_snapshot=a},a.prototype._displayed_snapshot=null,a.prototype._upload=function(a,b,c,d){return this._engine_upload(a,b,c,d)},a.prototype._discard=function(a){return this._displayed_snapshot===a&&this.show_stream(),this._engine_discard(a),a._discarded=!0,delete this._snapshots[a.id]},a.prototype._prepared=function(){return this._is_ready=!0,this.options.on_ready?this.options.on_ready.call(this):void 0},a.prototype._got_error=function(a){return this._debug("Error - "+a),this._error_occured=a,this.options.on_error?this.options.on_error.call(this,this._error_occured):void 0},a}(),navigator.getUserMedia||(navigator.getUserMedia=navigator.webkitGetUserMedia||navigator.mozGetUserMedia||navigator.msGetUserMedia),navigator.getUserMedia&&(b=function(a){function b(){return d=b.__super__.constructor.apply(this,arguments)}return f(b,a),b.prototype._engine_init=function(){var a,b,c,d;return this._debug("Using HTML5 engine"),this.internal_container=document.createElement("div"),this.internal_container.style.width="100%",this.internal_container.style.height="100%",this.internal_container.style.position="relative",this.container.appendChild(this.internal_container),d=Math.floor(.2*this._view_height()),b=Math.floor(.2*this._view_width()),this.message=document.createElement("div"),this.message["class"]="message",this.message.style.width="100%",this.message.style.height="100%",this._add_prefixed_style(this.message,"boxSizing","border-box"),this.message.style.overflow="hidden",this.message.style.textAlign="center",this.message.style.paddingTop=""+d+"px",this.message.style.paddingBottom=""+d+"px",this.message.style.paddingLeft=""+b+"px",this.message.style.paddingRight=""+b+"px",this.message.style.position="absolute",this.message.style.zIndex=3,this.message.innerHTML="Please allow camera access when prompted by the browser.",this.internal_container.appendChild(this.message),this.video_container=document.createElement("div"),this.video_container.style.width=""+this._view_width()+"px",this.video_container.style.height=""+this._view_height()+"px",this.video_container.style.overflow="hidden",this.video_container.style.position="absolute",this.video_container.style.zIndex=1,this.internal_container.appendChild(this.video_container),this.video=document.createElement("video"),this.video.autoplay=!0,this._add_prefixed_style(this.video,"transform","scalex(-1.0)"),window.AudioContext||(window.AudioContext=window.webkitAudioContext),window.AudioContext&&this._load_shutter_sound(),a={video:{optional:[{minWidth:1280},{minWidth:640},{minWidth:480},{minWidth:360}]}},c=this,navigator.getUserMedia(a,function(a){return c._remove_message(),c.video.src=window.URL?URL.createObjectURL(a):a,c._wait_for_video_ready()},function(a){var b,d,e;b=a.code;for(d in a)if(e=a[d],"code"!==d)return c._got_error(d),void 0;return c._got_error("UNKNOWN ERROR")})},b.prototype._engine_play_shutter_sound=function(){var a;if(this.shutter_buffer)return a=this.audio_context.createBufferSource(),a.buffer=this.shutter_buffer,a.connect(this.audio_context.destination),a.start(0)},b.prototype._engine_capture=function(a,b,c){var d,e,f;return f=this._get_capture_crop(),d=document.createElement("canvas"),d.width=f.width,d.height=f.height,e=d.getContext("2d"),e.drawImage(this.video,f.x_offset,f.y_offset,f.width,f.height,0,0,f.width,f.height),a._canvas=d,a._mirror=b,a._quality=c},b.prototype._engine_display=function(a){return this.displayed_canvas&&this.internal_container.removeChild(this.displayed_canvas),this.displayed_canvas=a._canvas,this.displayed_canvas.style.width=""+this._view_width()+"px",this.displayed_canvas.style.height=""+this._view_height()+"px",this.displayed_canvas.style.top=0,this.displayed_canvas.style.left=0,this.displayed_canvas.style.position="absolute",this.displayed_canvas.style.zIndex=2,this._add_prefixed_style(this.displayed_canvas,"transform","scalex(-1.0)"),this.internal_container.appendChild(this.displayed_canvas)},b.prototype._engine_discard=function(a){return a._xhr&&a._xhr.abort(),delete a._xhr,delete a._canvas,delete a._jpeg_blob},b.prototype._engine_show_stream=function(){return this.displayed_canvas&&(this.internal_container.removeChild(this.displayed_canvas),this.displayed_canvas=null),this.video_container.style.display="block"},b.prototype._engine_upload=function(a,b,c,d){var e,f,g,h,i;return h=this,a._jpeg_blob?(this._debug("Uploading the file"),g=function(b){return delete a._xhr,a._status=b.target.status,a._response=b.target.responseText,a._status>=200&&a._status<300?a._upload_done():(a._error_message=b.target.statusText||"Unknown error",a._upload_fail())},i=new XMLHttpRequest,i.open("POST",b),i.timeout=d,c&&i.setRequestHeader("X-CSRF-Token",c),i.onload=g,i.onerror=g,i.onabort=g,i.send(a._jpeg_blob),a._xhr=i):(this._debug("Generating JPEG file"),a._mirror?(e=document.createElement("canvas"),e.width=a._canvas.width,e.height=a._canvas.height,f=e.getContext("2d"),f.setTransform(1,0,0,1,0,0),f.translate(e.width,0),f.scale(-1,1),f.drawImage(a._canvas,0,0)):e=a._canvas,e.toBlob(function(e){return a._jpeg_blob=e,h._engine_upload(a,b,c,d)},"image/jpeg",this.quality))},b.prototype._remove_message=function(){return this.message.style.display="none"},b.prototype._load_shutter_sound=function(){var a,b;if(!this.audio_context)return this.audio_context=new AudioContext,a=new XMLHttpRequest,a.open("GET",this.options.shutter_url,!0),a.responseType="arraybuffer",b=this,a.onload=function(){return b.audio_context.decodeAudioData(a.response,function(a){return b.shutter_buffer=a})},a.send()},b.prototype._wait_for_video_ready=function(){var a,b,c,d;return d=parseInt(this.video.videoWidth),c=parseInt(this.video.videoHeight),d>0&&c>0?(this.video_container.appendChild(this.video),this.video_width=d,this.video_height=c,this._debug("Camera resolution "+this.video_width+"x"+this.video_height+"px"),a=this._get_video_crop(),this.video.style.position="relative",this.video.style.width=""+a.width+"px",this.video.style.height=""+a.height+"px",this.video.style.left=""+a.x_offset+"px",this.video.style.top=""+a.y_offset+"px",this._prepared()):this._status_checks_count>100?this._got_error("Camera failed to initialize in 10 seconds"):(this._status_checks_count++,b=this,setTimeout(function(){return b._wait_for_video_ready()},100))},b.prototype._status_checks_count=0,b.prototype._add_prefixed_style=function(a,b,c){var d;return d=b.charAt(0).toUpperCase()+b.slice(1),a.style[b]=c,a.style["Webkit"+d]=c,a.style["Moz"+d]=c,a.style["ms"+d]=c,a.style["O"+d]=c},b.prototype._get_video_crop=function(){var a,b,c,d,e,f,g;return g=this._view_width(),e=this._view_height(),c=this.video_width/this.video_height,f=g/e,c>=f?(this._debug("Filling height"),d=e/this.video_height,b=Math.round(this.video_width*d),{width:b,height:e,x_offset:-Math.floor((b-g)/2),y_offset:0}):(this._debug("Filling width"),d=g/this.video_width,a=Math.round(this.video_height*d),{width:g,height:a,x_offset:0,y_offset:-Math.floor((a-e)/2)})},b.prototype._get_capture_crop=function(){var a,b,c,d,e,f;return f=this._view_width(),d=this._view_height(),c=this.video_width/this.video_height,e=f/d,c>=e?(b=Math.round(this.video_height*e),{width:b,height:this.video_height,x_offset:Math.floor((this.video_width-b)/2),y_offset:0}):(a=Math.round(this.video_width/e),{width:this.video_width,height:a,x_offset:0,y_offset:Math.floor((this.video_height-a)/2)})},b}(a),window.JpegCamera=b),c=function(){function a(a,b){this.camera=a,this.options=b,this.id=this.constructor._next_snapshot_id++}return a._next_snapshot_id=1,a.prototype._discarded=!1,a.prototype.show=function(){return this._discarded&&raise("discarded snapshot cannot be used"),this.camera._display(this),this},a.prototype.hide=function(){return this.camera.displayed_snapshot()===this&&this.camera.show_stream(),this},a.prototype.upload=function(a){var b;if(null==a&&(a={}),this._discarded&&raise("discarded snapshot cannot be used"),this._uploading)return this.camera._debug("Upload already in progress"),void 0;if(this._uploading=!0,this._retry=1,this._upload_options=a,b=this._options(),!b.api_url)throw this.camera._debug("Snapshot#upload called without valid api_url"),"Snapshot#upload called without valid api_url";return this._start_upload(b),this},a.prototype._upload_options={},a.prototype._uploading=!1,a.prototype._retry=1,a.prototype.done=function(a){var b;return this._discarded&&raise("discarded snapshot cannot be used"),this._upload_options.on_upload_done=a,b=this._options(),b.on_upload_done&&this._done&&b.on_upload_done.call(this,this._response),this},a.prototype._done=!1,a.prototype._response=null,a.prototype.fail=function(a){var b;return this._discarded&&raise("discarded snapshot cannot be used"),this._upload_options.on_upload_fail=a,b=this._options(),b.on_upload_fail&&this._fail&&b.on_upload_fail.call(this,this._status,this._error_message,this._response),this},a.prototype._fail=!1,a.prototype._status=null,a.prototype._error_message=null,a.prototype.discard=function(){return this.camera._discard(this),void 0},a.prototype._options=function(){return this.camera._extend({},this.camera.options,this.options,this._upload_options)},a.prototype._start_upload=function(a){var b;return b="string"==typeof a.csrf_token&&a.csrf_token.length>0?a.csrf_token:null,this._done=!1,this._response=null,this._fail=!1,this._status=null,this._error_message=null,this.camera._upload(this,a.api_url,b,a.timeout)},a.prototype._upload_done=function(){var a,b,c,d;return this.camera._debug("Upload completed with status "+this._status),this._done=!0,a=this._options(),c=a.retry_success&&a.retry_if&&a.retry_if.call(this,this._status,this._error_message,this._response,this._retry),!0===c&&(c=0),"number"==typeof c?(this._retry++,c>0?(b=parseInt(c),this.camera._debug("Will retry the upload in "+b+"ms (attempt #"+this._retry+")"),d=this,setTimeout(function(){return d._start_upload(a)},b)):(this.camera._debug("Will retry the upload immediately (attempt #"+this._retry+")"),this._start_upload(a))):(this._uploading=!1,a.on_upload_done?a.on_upload_done.call(this,this._response):void 0)},a.prototype._upload_fail=function(){var a,b,c,d;return this.camera._debug("Upload failed with status "+this._status),this._fail=!0,a=this._options(),c=a.retry_if&&a.retry_if.call(this,this._status,this._error_message,this._response,this._retry),!0===c&&(c=0),"number"==typeof c?(this._retry++,c>0?(b=parseInt(c),this.camera._debug("Will retry the upload in "+b+"ms (attempt #"+this._retry+")"),d=this,setTimeout(function(){return d._start_upload(a)},b)):(this.camera._debug("Will retry the upload immediately (attempt #"+this._retry+")"),this._start_upload(a))):(this._uploading=!1,a.on_upload_fail?a.on_upload_fail.call(this,this._status,this._error_message,this._response):void 0)},a}()}.call(this);
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jpeg_camera
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Wróbel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-29 00:00:00.000000000 Z
11
+ date: 2013-07-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -70,7 +70,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
70
  version: '0'
71
71
  requirements: []
72
72
  rubyforge_project:
73
- rubygems_version: 2.0.6
73
+ rubygems_version: 2.0.5
74
74
  signing_key:
75
75
  specification_version: 4
76
76
  summary: Use JpegCamera in Rails 3 app