# 摄像头
主要使用:
- navigator.mediaDevices.getUserMedia
- getVideoTracks
# 效果
# 代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>摄像头</title>
</head>
<body>
<h2>摄像头</h2>
<video id="video" src="" autoplay style="width: 500px; height: 300px; background-color: #000000;"></video>
<br>
<button onclick="useCamera()">调用摄像头</button>
<script>
const videoElm = document.getElementById('video');
console.log(videoElm);
function useCamera() {
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia({ video: true })
.then((stream) => {
console.log(stream);
const videoTracks = stream.getVideoTracks();
console.log(videoTracks);
videoElm.srcObject = stream;
})
.catch((err) => {
const tip = '摄像头授权失败!';
alert(tip);
throw Error(tip);
});
} else {
alert('摄像头已禁用,或浏览器不支持此功能,请到浏览器设置中查看。');
throw Error('浏览器不支持此功能!');
}
}
</script>
</body>
</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
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
← 文件操作 microphone麦克风 →