-
Notifications
You must be signed in to change notification settings - Fork 1
/
camera2.js
120 lines (75 loc) · 3 KB
/
camera2.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
var webcam = (function(){
var video = document.createElement('video'),
photo = document.createElement('canvas');
function play() {
if (navigator.getUserMedia) {
navigator.getUserMedia({video: true, audio: true, toString : function() {return "video,audio";} }, onSuccess, onError);
} else {
changeStatus('getUserMedia is not supported in this browser.', true);
}
}
function onSuccess(stream) {
var source;
if (window.webkitURL) {
source = window.webkitURL.createObjectURL(stream);
} else {
source = stream; // Opera and Firefox
}
video.autoplay = true;
video.src = source;
changeStatus('', false);
}
function onError() {
changeStatus('Please accept the getUserMedia permissions! Refresh to try again.', true);
}
function changeStatus(msg, error) {
var status = document.getElementById('status');
status.innerHTML = msg;
status.style.color = (error) ? 'red' : 'green';
}
// allow the user to take a screenshot
function setupPhotoBooth() {
var takeButton = document.createElement('a');
takeButton.innerText = 'Smile!';
takeButton.classList.add('button');
takeButton.addEventListener('click', takePhoto, true);
document.getElementById('camera').appendChild(takeButton);
var saveButton = document.createElement('a');
saveButton.id = 'save';
saveButton.classList.add('button');
saveButton.innerText = 'Save!';
saveButton.disabled = true;
saveButton.addEventListener('click', savePhoto, true);
document.getElementById('camera').appendChild(saveButton);
}
function takePhoto() {
// set our canvas to the same size as our video
photo.width = video.width;
photo.height = video.height;
var context = photo.getContext('2d');
context.drawImage(video, 0, 0, photo.width, photo.height);
// allow us to save
var saveButton = document.getElementById('save');
saveButton.disabled = false;
}
function savePhoto() {
var data = photo.toDataURL("image/png");
data = data.replace("image/png","image/octet-stream");
document.location.href = data;
}
return {
init: function() {
changeStatus('Look up and click "ALLOW".', true);
video.width = 400;
video.height = 300;
document.getElementById('camera').appendChild(video);
document.getElementById('camera').appendChild(photo);
startCam.style.display = 'none';
navigator.getUserMedia || (navigator.getUserMedia = navigator.mozGetUserMedia || navigator.webkitGetUserMedia || navigator.msGetUserMedia);
play();
setupPhotoBooth();
}()
}
});
var startCam = document.getElementById('startCam');
startCam.addEventListener('click', function(){webcam(); return false;});