webcam_recorder.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. (function () {
  2. var config = {
  3. selectors: {
  4. btnFreeze: '#btnCapture',
  5. btnUnFreeze: '#btnClean',
  6. btnSave: '#btnSave',
  7. btnAuto: '#btnAuto',
  8. btnStop: '#btnStop',
  9. camera: '#chamilo-camera',
  10. results: '#upload_results'
  11. },
  12. urlReceiver: '',
  13. video: {
  14. fps: 1000,
  15. maxClip: 25
  16. },
  17. callbacks: {
  18. save: null,
  19. auto: null
  20. }
  21. };
  22. function snap() {
  23. var deferred = $.Deferred();
  24. Webcam.snap(function (dataUri) {
  25. Webcam.upload(dataUri, config.urlReceiver, function (code, response) {
  26. $(config.selectors.results).html(
  27. '<h3>' + response + '</h3>'
  28. + '<div>'
  29. + '<img src="' + dataUri + '" class="webcamclip_bg">'
  30. + '</div>'
  31. );
  32. deferred.resolve();
  33. });
  34. });
  35. return deferred.promise();
  36. };
  37. var videoInterval = 0,
  38. videoDeferred = $.Deferred();
  39. function startVideo() {
  40. var counter = 0;
  41. videoDeferred = $.Deferred();
  42. videoInterval = window.setInterval(function () {
  43. counter++;
  44. if (config.video.maxClip >= counter) {
  45. snap();
  46. } else {
  47. stopVideo();
  48. }
  49. }, config.video.fps);
  50. return videoDeferred.promise();
  51. }
  52. function stopVideo() {
  53. if (!videoInterval) {
  54. return;
  55. }
  56. window.clearTimeout(videoInterval);
  57. videoDeferred.resolve();
  58. }
  59. window.RecordWebcam = {
  60. init: function (params) {
  61. config = $.extend(true, config, params);
  62. $(document).ready(function () {
  63. Webcam.set({
  64. width: 320,
  65. height: 240,
  66. image_format: 'jpeg',
  67. jpeg_quality: 90
  68. });
  69. Webcam.attach(config.selectors.camera);
  70. $('video').addClass('skip');
  71. $(config.selectors.btnFreeze).on('click', function (e) {
  72. e.preventDefault();
  73. Webcam.freeze();
  74. });
  75. $(config.selectors.btnUnFreeze).on('click', function (e) {
  76. e.preventDefault();
  77. Webcam.unfreeze();
  78. });
  79. $(config.selectors.btnSave).on('click', function (e) {
  80. e.preventDefault();
  81. snap()
  82. .always(function () {
  83. if (config.callbacks.save) {
  84. config.callbacks.save.apply(null, []);
  85. }
  86. });
  87. });
  88. $(config.selectors.btnAuto).on('click', function (e) {
  89. e.preventDefault();
  90. startVideo()
  91. .always(function () {
  92. if (config.callbacks.auto) {
  93. config.callbacks.auto.apply(null, []);
  94. }
  95. });
  96. });
  97. $(config.selectors.btnStop).on('click', function (e) {
  98. e.preventDefault();
  99. stopVideo();
  100. });
  101. })
  102. }
  103. };
  104. })();