Advertisement
Guest User

Untitled

a guest
Jul 14th, 2012
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.17 KB | None | 0 0
  1. /*!
  2. * jQuery Webcam Plugin v1.0.2
  3. *
  4. * Plugin which allows jQuery to read data from a user's webcam or other video capture device.
  5. *
  6. * Copyright 2010, David "Mackers" McNamara (http://mackers.com)
  7. * Dual licensed under the MIT or GPL Version 2 licenses.
  8. *
  9. * This plugin uses code from CamCanvas API 0.2
  10. * Copyright 2009 Taboca (http://www.taboca.com/p/camcanvas/)
  11. *
  12. * Date: Tue Mar 9 14:42:05 CET 2010
  13. */
  14.  
  15. (function($)
  16. {
  17. $.webcam = {};
  18.  
  19. $.webcam._initialized = false;
  20. $.webcam._capturing = false;
  21. $.webcam._flashready = false;
  22. $.webcam._width = 250;
  23. $.webcam._height = 180;
  24. $.webcam._interval = 100;
  25. $.webcam._imageData = null;
  26. $.webcam._imageDataCount = 0;
  27. $.webcam._flash = null;
  28. $.webcam._canvas = null;
  29. $.webcam._triggerCaptureTimeout = null;
  30. $.webcam._callbacks = [];
  31. $.webcam._filters = [];
  32. $.webcam._dialogBody = "<p>This site wants to access your webcam.</p><p>Please ensure you can see yourself above. Some visitors may have to right-click or context-click then choose \'Settings\'.</p>";
  33. $.webcam._dialogProperties = null;
  34. $.webcam._imageSize = 0;
  35.  
  36. $.webcam.init = function(canvas, flashContainer, properties)
  37. {
  38. $.webcam._imageData = null;
  39. $.webcam._imageDataCount = 0;
  40.  
  41. if (properties)
  42. {
  43. if (properties.width)
  44. $.webcam._width = properties.width;
  45.  
  46. if (properties.height)
  47. $.webcam._height = properties.height;
  48.  
  49. if (properties.interval)
  50. $.webcam._interval = properties.interval;
  51.  
  52. if (properties.dialogBody)
  53. $.webcam._dialogBody = properties.dialogBody;
  54.  
  55. if (properties.dialogProperties)
  56. $.webcam._dialogProperties = properties.dialogProperties;
  57. }
  58.  
  59. $.webcam._imageSize = $.webcam._width*$.webcam._height*4;
  60.  
  61. $.webcam._flash = flashContainer;
  62. $.webcam._initFlash();
  63.  
  64. $.webcam._canvas = canvas;
  65. $.webcam._initCanvas();
  66.  
  67. $.webcam._initialized = true;
  68. };
  69.  
  70. $.webcam.startCapture = function(fn)
  71. {
  72. if (!$.webcam._initialized)
  73. return;
  74.  
  75. $.webcam._capturing = true;
  76.  
  77. if (fn)
  78. $.webcam.addCallback(fn, $.webcam._interval);
  79.  
  80. $.webcam._triggerCapture();
  81. }
  82.  
  83. $.webcam.stopCapture = function()
  84. {
  85. $.webcam._capturing = false;
  86. window.clearTimeout($.webcam._triggerCaptureTimeout);
  87. }
  88.  
  89. $.webcam.addCallback = function(fn, interval)
  90. {
  91. $.webcam._callbacks.push({fn: fn, interval: interval, last: 0});
  92. }
  93.  
  94. $.webcam.addFilter = function(fn)
  95. {
  96. $.webcam._filters.push({fn: fn});
  97. }
  98.  
  99. $.webcam._triggerCapture = function()
  100. {
  101. try
  102. {
  103. if (!$.webcam._flashready)
  104. throw("Flash not ready");
  105.  
  106. $.webcam._flash.find('embed').get(0).ccCapture();
  107. }
  108. catch (e)
  109. {
  110. // flash may not be loaded yet
  111. window.clearTimeout($.webcam._triggerCaptureTimeout);
  112. $.webcam._triggerCaptureTimeout = window.setTimeout($.webcam._triggerCapture, $.webcam._interval);
  113. }
  114. }
  115.  
  116. $.webcam._initFlash = function()
  117. {
  118. if ($($.webcam._flash).length == 0)
  119. {
  120. var onclose = function()
  121. {
  122. $.webcam._flash = $("#jquerywebcamflash");
  123. $.webcam._flash.find("embed").attr("width", "0");
  124. $.webcam._flash.find("embed").attr("height", "0");
  125. $.webcam._flash.find("embed").attr("allowScriptAccess", "always");
  126. $.webcam._flash.find("embed").attr("mayscript", "true");
  127. //$(document).find("body").append($.webcam._flash);
  128. $dialog.dialog('close');
  129. setTimeout(function()
  130. {
  131. $('#jquerywebcamdialog').parent().get(0).style.display = "block";
  132. $('#jquerywebcamdialog').parent().get(0).style.left = "-1000px";
  133. $.webcam._flashready = true;
  134. }, 200);
  135. }
  136.  
  137. // the user did not specify a flash container to use - therefore we'll create our own container to prompt the user to accept video in flash.
  138.  
  139. $.webcam._flash = $("#jquerywebcamflash");
  140. }
  141. else
  142. {
  143. $.webcam._flashready = true;
  144. }
  145.  
  146. $.webcam._flash.flash(
  147. {
  148. src: 'jquerywebcamhelper.swf',
  149. width: $.webcam._width,
  150. height: $.webcam._height,
  151. },
  152. {
  153. version: 8
  154. }
  155. );
  156. }
  157.  
  158. $.webcam._initCanvas = function()
  159. {
  160. if ($($.webcam._canvas).length == 0)
  161. {
  162. var canvas = document.createElement("canvas");
  163. canvas.setAttribute("id", "jquerywebcamcanvas");
  164. canvas.setAttribute("width", $.webcam._width);
  165. canvas.setAttribute("height", $.webcam._height);
  166. canvas.setAttribute("style", "position: absolute; top: 0; left: -1000px;");
  167. $(document).find("body").append($(canvas));
  168. $.webcam._canvas = $('#jquerywebcamcanvas');
  169. }
  170.  
  171. $.webcam._canvas.each(function()
  172. {
  173. var e = $(this).get(0);
  174. e.style.width = $.webcam._width + "px";
  175. e.style.height = $.webcam._height + "px";
  176. e.width = $.webcam._width;
  177. e.height = $.webcam._height;
  178.  
  179. var ctx = e.getContext("2d");
  180. ctx.clearRect(0, 0, $.webcam._width, $.webcam._height);
  181. $.webcam._imageData = ctx.getImageData(0, 0, 320, 240);
  182.  
  183. $(this).data('ctx', ctx);
  184. });
  185. }
  186.  
  187. $.webcam._passLine = function(data)
  188. {
  189. if (!$.webcam._initialized || !$.webcam._capturing || !$.webcam._flashready)
  190. return;
  191.  
  192. var coll = data.split("-");
  193.  
  194. for(var i=0; i<$.webcam._width; i++)
  195. {
  196. var intVal = parseInt(coll[i]);
  197. r = (intVal >> 16) & 0xff;
  198. g = (intVal >> 8) & 0xff;
  199. b = (intVal ) & 0xff;
  200. $.webcam._imageData.data[$.webcam._imageDataCount+0]=r;
  201. $.webcam._imageData.data[$.webcam._imageDataCount+1]=g;
  202. $.webcam._imageData.data[$.webcam._imageDataCount+2]=b;
  203. $.webcam._imageData.data[$.webcam._imageDataCount+3]=255;
  204.  
  205. for (var j=0; j<$.webcam._filters.length; j++)
  206. {
  207. var filtered = {
  208. r: $.webcam._imageData.data[$.webcam._imageDataCount+0],
  209. g: $.webcam._imageData.data[$.webcam._imageDataCount+1],
  210. b: $.webcam._imageData.data[$.webcam._imageDataCount+2],
  211. a: $.webcam._imageData.data[$.webcam._imageDataCount+3]
  212. };
  213.  
  214. $.webcam._filters[j].fn(filtered);
  215.  
  216. $.webcam._imageData.data[$.webcam._imageDataCount+0]=filtered.r;
  217. $.webcam._imageData.data[$.webcam._imageDataCount+1]=filtered.g;
  218. $.webcam._imageData.data[$.webcam._imageDataCount+2]=filtered.b;
  219. $.webcam._imageData.data[$.webcam._imageDataCount+3]=filtered.a;
  220. }
  221.  
  222. $.webcam._imageDataCount+=4;
  223. }
  224.  
  225. if ($.webcam._imageDataCount >= $.webcam._imageSize)
  226. {
  227. $.webcam._imageDataCount = 0;
  228.  
  229. $.webcam._canvas.each(function()
  230. {
  231. var ctx = $(this).data('ctx');
  232. ctx.putImageData($.webcam._imageData, 0, 0);
  233. });
  234.  
  235. for (var i=0; i<$.webcam._callbacks.length; i++)
  236. {
  237. var d = (new Date()).getTime();
  238.  
  239. if ($.webcam._callbacks[i].last < d - $.webcam._callbacks[i].interval)
  240. {
  241. $.webcam._callbacks[i].last = d;
  242. $.webcam._callbacks[i].fn($.webcam._imageData);
  243. }
  244. }
  245.  
  246. $.webcam._triggerCaptureTimeout = window.setTimeout($.webcam._triggerCapture, $.webcam._interval);
  247. }
  248. }
  249. })(jQuery);
  250.  
  251. // TODO fix this global function
  252. function passLine(data) { $.webcam._passLine(data); }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement