webcam.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* JPEGCam v1.0.9 */
  2. /* Webcam library for capturing JPEG images and submitting to a server */
  3. /* Copyright (c) 2008 - 2009 Joseph Huckaby <jhuckaby@goldcartridge.com> */
  4. /* Licensed under the GNU Lesser Public License */
  5. /* http://www.gnu.org/licenses/lgpl.html */
  6. /* Usage:
  7. <script language="JavaScript">
  8. document.write( webcam.get_html(320, 240) );
  9. webcam.set_api_url( 'test.php' );
  10. webcam.set_hook( 'onComplete', 'my_callback_function' );
  11. function my_callback_function(response) {
  12. alert("Success! PHP returned: " + response);
  13. }
  14. </script>
  15. <a href="javascript:void(webcam.snap())">Take Snapshot</a>
  16. */
  17. // Everything is under a 'webcam' Namespace
  18. window.webcam = {
  19. version: '1.0.9',
  20. // globals
  21. ie: !!navigator.userAgent.match(/MSIE/),
  22. protocol: location.protocol.match(/https/i) ? 'https' : 'http',
  23. callback: null, // user callback for completed uploads
  24. swf_url: 'webcam.swf', // URI to webcam.swf movie (defaults to cwd)
  25. shutter_url: 'shutter.mp3', // URI to shutter.mp3 sound
  26. api_url: '', // URL to upload script
  27. loaded: false, // true when webcam movie finishes loading
  28. quality: 90, // JPEG quality (1 - 100)
  29. shutter_sound: true, // shutter sound effect on/off
  30. stealth: false, // stealth mode (do not freeze image upon capture)
  31. hooks: {
  32. onLoad: null,
  33. onComplete: null,
  34. onError: null
  35. }, // callback hook functions
  36. set_hook: function(name, callback) {
  37. // set callback hook
  38. // supported hooks: onLoad, onComplete, onError
  39. if (typeof(this.hooks[name]) == 'undefined')
  40. return alert("Hook type not supported: " + name);
  41. this.hooks[name] = callback;
  42. },
  43. fire_hook: function(name, value) {
  44. // fire hook callback, passing optional value to it
  45. if (this.hooks[name]) {
  46. if (typeof(this.hooks[name]) == 'function') {
  47. // callback is function reference, call directly
  48. this.hooks[name](value);
  49. }
  50. else if (typeof(this.hooks[name]) == 'array') {
  51. // callback is PHP-style object instance method
  52. this.hooks[name][0][this.hooks[name][1]](value);
  53. }
  54. else if (window[this.hooks[name]]) {
  55. // callback is global function name
  56. window[ this.hooks[name] ](value);
  57. }
  58. return true;
  59. }
  60. return false; // no hook defined
  61. },
  62. set_api_url: function(url) {
  63. // set location of upload API script
  64. this.api_url = url;
  65. },
  66. set_swf_url: function(url) {
  67. // set location of SWF movie (defaults to webcam.swf in cwd)
  68. this.swf_url = url;
  69. },
  70. get_html: function(width, height, server_width, server_height) {
  71. // Return HTML for embedding webcam capture movie
  72. // Specify pixel width and height (640x480, 320x240, etc.)
  73. // Server width and height are optional, and default to movie width/height
  74. if (!server_width) server_width = width;
  75. if (!server_height) server_height = height;
  76. var html = '';
  77. var flashvars = 'shutter_enabled=' + (this.shutter_sound ? 1 : 0) +
  78. '&shutter_url=' + escape(this.shutter_url) +
  79. '&width=' + width +
  80. '&height=' + height +
  81. '&server_width=' + server_width +
  82. '&server_height=' + server_height;
  83. if (this.ie) {
  84. html += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="'+this.protocol+'://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="'+width+'" height="'+height+'" id="webcam_movie" align="middle"><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="false" /><param name="movie" value="'+this.swf_url+'" /><param name="loop" value="false" /><param name="menu" value="false" /><param name="quality" value="best" /><param name="bgcolor" value="#ffffff" /><param name="flashvars" value="'+flashvars+'"/></object>';
  85. }
  86. else {
  87. html += '<embed id="webcam_movie" src="'+this.swf_url+'" loop="false" menu="false" quality="best" bgcolor="#ffffff" width="'+width+'" height="'+height+'" name="webcam_movie" align="middle" allowScriptAccess="always" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" flashvars="'+flashvars+'" />';
  88. }
  89. this.loaded = false;
  90. return html;
  91. },
  92. get_movie: function() {
  93. // get reference to movie object/embed in DOM
  94. if (!this.loaded) return alert("ERROR: Movie is not loaded yet");
  95. var movie = document.getElementById('webcam_movie');
  96. if (!movie) alert("ERROR: Cannot locate movie 'webcam_movie' in DOM");
  97. return movie;
  98. },
  99. set_stealth: function(stealth) {
  100. // set or disable stealth mode
  101. this.stealth = stealth;
  102. },
  103. snap: function(url, callback, stealth) {
  104. // take snapshot and send to server
  105. // specify fully-qualified URL to server API script
  106. // and callback function (string or function object)
  107. if (callback) this.set_hook('onComplete', callback);
  108. if (url) this.set_api_url(url);
  109. if (typeof(stealth) != 'undefined') this.set_stealth( stealth );
  110. this.get_movie()._snap( this.api_url, this.quality, this.shutter_sound ? 1 : 0, this.stealth ? 1 : 0 );
  111. },
  112. freeze: function() {
  113. // freeze webcam image (capture but do not upload)
  114. this.get_movie()._snap('', this.quality, this.shutter_sound ? 1 : 0, 0 );
  115. },
  116. upload: function(url, callback) {
  117. // upload image to server after taking snapshot
  118. // specify fully-qualified URL to server API script
  119. // and callback function (string or function object)
  120. if (callback) this.set_hook('onComplete', callback);
  121. if (url) this.set_api_url(url);
  122. this.get_movie()._upload( this.api_url );
  123. },
  124. reset: function() {
  125. // reset movie after taking snapshot
  126. this.get_movie()._reset();
  127. },
  128. configure: function(panel) {
  129. // open flash configuration panel -- specify tab name:
  130. // "camera", "privacy", "default", "localStorage", "microphone", "settingsManager"
  131. if (!panel) panel = "camera";
  132. this.get_movie()._configure(panel);
  133. },
  134. set_quality: function(new_quality) {
  135. // set the JPEG quality (1 - 100)
  136. // default is 90
  137. this.quality = new_quality;
  138. },
  139. set_shutter_sound: function(enabled, url) {
  140. // enable or disable the shutter sound effect
  141. // defaults to enabled
  142. this.shutter_sound = enabled;
  143. this.shutter_url = url ? url : 'shutter.mp3';
  144. },
  145. flash_notify: function(type, msg) {
  146. // receive notification from flash about event
  147. switch (type) {
  148. case 'flashLoadComplete':
  149. // movie loaded successfully
  150. this.loaded = true;
  151. this.fire_hook('onLoad');
  152. break;
  153. case 'error':
  154. // HTTP POST error most likely
  155. if (!this.fire_hook('onError', msg)) {
  156. alert("JPEGCam Flash Error: " + msg);
  157. }
  158. break;
  159. case 'success':
  160. // upload complete, execute user callback function
  161. // and pass raw API script results to function
  162. this.fire_hook('onComplete', msg.toString());
  163. break;
  164. default:
  165. // catch-all, just in case
  166. alert("jpegcam flash_notify: " + type + ": " + msg);
  167. break;
  168. }
  169. }
  170. };