Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function dataURItoBlob(dataURI) {
- var binary = atob(dataURI.split(',')[1]);
- var array = [];
- for(var i = 0; i < binary.length; i++) {
- array.push(binary.charCodeAt(i));
- }
- return new Blob([new Uint8Array(array)], {type: 'image/jpeg'});
- }
- window.addEventListener("DOMContentLoaded", function() {
- // Grab elements, create settings, etc.
- var canvas = document.getElementById("canvas"),
- context = canvas.getContext("2d"),
- video = document.getElementById("video"),
- videoObj = { "video": true },
- errBack = function(error) {
- console.log("Video capture error: ", error.code);
- };
- // Put video listeners into place
- if(navigator.getUserMedia) { // Standard
- navigator.getUserMedia(videoObj, function(stream) {
- video.src = stream;
- video.play();
- }, errBack);
- } else if(navigator.webkitGetUserMedia) { // WebKit-prefixed
- navigator.webkitGetUserMedia(videoObj, function(stream){
- video.src = window.webkitURL.createObjectURL(stream);
- video.play();
- }, errBack);
- } else if(navigator.mozGetUserMedia) { // WebKit-prefixed
- navigator.mozGetUserMedia(videoObj, function(stream){
- video.src = window.URL.createObjectURL(stream);
- video.play();
- }, errBack);
- }
- var ws = null;
- // Trigger photo take
- document.getElementById("capture").addEventListener("click", function() {
- context.drawImage(video, 0, 0, 640, 480);
- if (ws == null) {
- ws = new WebSocket("ws://localhost:8080/VideoCaptureTomcat/capture");
- ws.onopen = function () {
- console.log("Openened connection to websocket");
- }
- }
- var data = canvas.toDataURL('image/jpeg', 1.0);
- console.log(data);
- var bb = dataURItoBlob(data);
- ws.send(data);
- });
- }, false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement