-
Notifications
You must be signed in to change notification settings - Fork 0
/
photo.html
75 lines (63 loc) · 1.61 KB
/
photo.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
<!DOCTYPE html>
<html>
<head>
<style>
body {
display: -webkit-flex;
-webkit-justify-content: center;
text-align: center;
}
#video {
height: 300px;
}
#image {
height: 300px;
}
</style>
</head>
<body onload="init()">
<div>
<div>
<video id="video" autoplay></video>
</div>
<div>
<img id="image">
</div>
<div>
<button onclick="capture()">Take picture</button>
</div>
</div>
<canvas id="canvas" style="display:none"></canvas>
<script>
navigator.getUserMedia = navigator.webkitGetUserMedia || navigator.getUserMedia;
window.URL = window.URL || window.webkitURL;
var image = document.getElementById('image');
var video = document.getElementById('video');
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
function init() {
navigator.getUserMedia({video: true}, stream, error);
}
function capture() {
ctx.drawImage(video, 0, 0);
// image.src = canvas.toDataURL('image/webp');
image.src = canvas.toDataURL('image/png');
console.log(image.src);
}
function stream(stream) {
video.src = window.URL.createObjectURL(stream);
stream.onended = error;
video.onerror = function (e) {
stream.stop();
};
setTimeout(function () {
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
}, 50);
}
function error(e) {
console.log(e);
}
</script>
</body>
</html>