Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     function dataURItoBlob(dataURI) {
  2.         var binary = atob(dataURI.split(',')[1]);
  3.         var array = [];
  4.         for(var i = 0; i < binary.length; i++) {
  5.             array.push(binary.charCodeAt(i));
  6.         }
  7.         return new Blob([new Uint8Array(array)], {type: 'image/jpeg'});
  8.     }
  9.  
  10.    
  11.     window.addEventListener("DOMContentLoaded", function() {
  12.         // Grab elements, create settings, etc.
  13.         var canvas = document.getElementById("canvas"),
  14.             context = canvas.getContext("2d"),
  15.             video = document.getElementById("video"),
  16.             videoObj = { "video": true },
  17.             errBack = function(error) {
  18.                 console.log("Video capture error: ", error.code);
  19.             };
  20.  
  21.         // Put video listeners into place
  22.         if(navigator.getUserMedia) { // Standard
  23.             navigator.getUserMedia(videoObj, function(stream) {
  24.                 video.src = stream;
  25.                 video.play();
  26.             }, errBack);
  27.         } else if(navigator.webkitGetUserMedia) { // WebKit-prefixed
  28.             navigator.webkitGetUserMedia(videoObj, function(stream){
  29.                 video.src = window.webkitURL.createObjectURL(stream);
  30.                 video.play();
  31.             }, errBack);
  32.         } else if(navigator.mozGetUserMedia) { // WebKit-prefixed
  33.             navigator.mozGetUserMedia(videoObj, function(stream){
  34.                 video.src = window.URL.createObjectURL(stream);
  35.                 video.play();
  36.             }, errBack);
  37.         }
  38.         var ws = null;
  39.         // Trigger photo take
  40.         document.getElementById("capture").addEventListener("click", function() {
  41.             context.drawImage(video, 0, 0, 640, 480);
  42.             if (ws == null) {
  43.                 ws = new WebSocket("ws://localhost:8080/VideoCaptureTomcat/capture");
  44.                 ws.onopen = function () {
  45.                           console.log("Openened connection to websocket");
  46.                 }
  47.             }
  48.            
  49.             var data = canvas.toDataURL('image/jpeg', 1.0);
  50.             console.log(data);
  51.             var bb = dataURItoBlob(data);
  52.             ws.send(data);
  53.         });
  54.     }, false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement