Repository navigation
Expand file tree
/
Copy pathcamera.html
More file actions
73 lines (72 loc) · 2.11 KB
/
Copy pathcamera.html
File metadata and controls
73 lines (72 loc) · 2.11 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
html,
body {
width: 100%;
height: 100%;
}
</style>
<body>
<video width="200px" height="150px"></video>
<canvas width="200px" height="150px"></canvas>
<p>
<button id="start">打开摄像头</button>
<button id="snap">截取图像</button>
<button id="close">关闭摄像头</button>
</p>
</body>
<script type="text/javascript">
window.onload = function () {
var canvas = document.getElementsByTagName('canvas')[0],
context = canvas.getContext('2d'),
video = document.getElementsByTagName("video")[0],
snap = document.getElementById("snap"),
close = document.getElementById("close"),
start = document.getElementById("start"),
MediaStreamTrack;
start.addEventListener('click', function () {
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia({
video: true,
audio: true
}).then(function (stream) {
console.log(stream)
MediaStreamTrack=typeof stream.stop==='function'?stream:stream.getTracks()[1]
video.src=(window.URL).createObjectURL(stream)
video.play()
}).catch(function(err){
console.log(err)
})
} else if (navigator.getMedia){
navigator.getMedia({
video: true
}).then(function (stream) {
console.log(stream)
MediaStreamTrack=stream.getTracks()[1]
video.src=(window.webkitURL).createObjectURL(stream)
video.play()
}).catch(function(err){
console.log(err)
})
}
})
snap.addEventListener('click', function () {
context.drawImage(video, 0, 0,200,150);
})
close.addEventListener('click', function () {
MediaStreamTrack && MediaStreamTrack.stop()
})
}
</script>
</html>