-
Notifications
You must be signed in to change notification settings - Fork 2
/
slow-camera.html
173 lines (159 loc) · 4.13 KB
/
slow-camera.html
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Slow Rate Camera</title>
<style>
canvas, video {
border: 1px solid gray;
}
button.btn {
font-size: 20px;
background: LightGray;
margin-right: 10px;
}
input.ipt {
font-size: 20px;
width: 80px;
}
select.slc {
font-size: 20px;
width: 100px;
}
</style>
</head>
<body>
<h1>Slow Camera</h1>
<font size="+1">
<button class="btn" onclick="getCameraList()">Probe</button>
<select id="camera_list" class="slc" size="1">
<option>(camera)</option>
</select>
<button class="btn" onclick="startCamera()">Start</button>
<form name="param" style="display: inline">
Interval(msec):
<input name="interval" class="ipt" type="number" size="8" value="1000" step="100" autofocus>
</form>
<button class="btn" onclick="changeInterval()">Set</button>
<button id="pause" class="btn" onclick="pauseCamera()">Pause</button>
<button id="live" class="btn" onclick="switchLive()">Live Monitor</button>
</font>
<br>
<canvas id="picture"></canvas>
<video id="camera" hidden muted autoplay playsinline></video>
<br>
<span id="message">No camera found</span>
</body>
<script>
const camlist = document.getElementById("camera_list");
const video = document.getElementById("camera");
const canvas = document.getElementById("picture");
const message = document.getElementById("message");
var interval = 1000;
var ivid = 0;
var sel = -1;
window.onload = () => {
getCameraList();
}
function getCameraList() {
while(camlist.lastChild) {
camlist.removeChild(camlist.lastChild);
}
navigator.mediaDevices.enumerateDevices()
.then(function(devices) {
devices.forEach(function(device) {
if (device.kind === 'videoinput') {
var id = device.deviceId;
var label = device.label || 'cam'; // label is available only for https
var option = document.createElement('option');
option.setAttribute('value', id);
option.innerHTML = label + '(' + id + ')';
camlist.appendChild(option);
}
});
})
.catch(function(err) {
message.innerHTML = err.name + ": " + err.message;
});
setTimeout(() => {
if(camlist.length > 0){
message.innerHTML = camlist.length + " camera(s) found";
}
},200)
}
function switchLive(){
video.hidden = !video.hidden;
document.getElementById("live").style.backgroundColor = video.hidden?"LightGray":"Red";
}
function changeInterval(){
interval = document.param.interval.value;
clearInterval(ivid);
ivid = setInterval(capture, interval);
}
function capture(){
const ctx = canvas.getContext("2d");
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
ctx.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
}
function startCamera(){
if(camlist.length == 0){
return;
}
clearInterval(ivid);
ivid = 0;
if (sel >= 0){ // Stop video
video.pause();
video.srcObject.getVideoTracks().forEach( (camera) => {
camera.stop();
});
video.srcObject = null;
}
if (sel === camlist.selectedIndex){ // Stop only
sel = -1;
message.innerHTML = "Stop";
return;
}
sel = camlist.selectedIndex;
var constraints = {
audio: false,
video: {
width: 1920,
height: 1080,
deviceId: camlist.options[camlist.selectedIndex].value
}
};
// Start with another camera
navigator.mediaDevices.getUserMedia(constraints)
.then( (stream) => {
video.srcObject = stream;
video.onloadedmetadata = (e) => {
video.play();
};
})
.catch( (err) => {
message.innerHTML = err.name + ": " + err.message;
});
ivid = setInterval(capture, interval);
message.innerHTML = "Running";
resetPause();
}
function resetPause() {
document.getElementById("pause").style.backgroundColor = "LightGray";
}
function pauseCamera() {
if(sel<0){
return;
}
if(ivid){
clearInterval(ivid);
ivid = 0;
message.innerHTML = "Pause";
} else {
ivid = setInterval(capture, interval);
message.innerHTML = "Running";
}
document.getElementById("pause").style.backgroundColor = ivid?"LightGray":"Red";
}
</script>
</html>