-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebcam.js
70 lines (61 loc) · 1.87 KB
/
webcam.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"use strict";
define([],function() {
var getUserMedia = function(t, onsuccess, onerror) {
var result = undefined;
if (navigator.getUserMedia) {
result = navigator.getUserMedia(t, onsuccess, onerror);
} else if (navigator.webkitGetUserMedia) {
result = navigator.webkitGetUserMedia(t, onsuccess, onerror);
} else if (navigator.mozGetUserMedia) {
result = navigator.mozGetUserMedia(t, onsuccess, onerror);
} else if (navigator.msGetUserMedia) {
result = navigator.msGetUserMedia(t, onsuccess, onerror);
} else {
onerror(new Error("No getUserMedia implementation found."));
}
return result;
};
var ready = false;
var onGetUserMediaSuccess = function(stream) {
video.src = window.URL.createObjectURL(stream);
video.play();
ready = true;
if( authorizedCallback ) {
authorizedCallback();
}
}
var authorizedCallback = undefined;
var waitForAuthorization = function(callback) {
authorizedCallback = callback;
if(ready) {
callback();
}
}
var onGetUserMediaError = function(error) {
alert("Couldn't access webcam.");
console.log(error);
}
var video = document.createElement('video');
video.width = 640;
video.height = 480;
video.autoplay = true;
getUserMedia(
{'video': true},
onGetUserMediaSuccess,
onGetUserMediaError
);
var getDimensions = function() {
return {
width:video.width,
height:video.height
}
}
var copyToContext = function(context) {
context.drawImage(video, 0, 0);
}
return {
waitForAuthorization: waitForAuthorization,
copyToContext:copyToContext,
getDimensions:getDimensions,
}
}());