-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy path021.调用摄镜头拍照.html
101 lines (90 loc) · 3.28 KB
/
021.调用摄镜头拍照.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
<!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">
<script src="https://unpkg.com/vconsole@latest/dist/vconsole.min.js"></script>
<link rel="stylesheet" href="./assets/global.css">
<style>
html,
body,
video,
canvas {
width: 100%;
height: 100%;
}
.take-photo,
.save-photo {
width: 70px;
height: 70px;
background-image: linear-gradient(120deg, #a1c4fd 0%, #c2e9fb 100%);
border-radius: 50%;
position: absolute;
left: 50%;
bottom: 120px;
transform: translateX(-50%);
}
.save-photo {
background-image: linear-gradient(120deg, #d4fc79 0%, #96e6a1 100%);
}
.save-photo:active,
.take-photo:active {
opacity: .7;
}
</style>
</head>
<body>
<video></video>
<canvas></canvas>
<div class="take-photo"></div>
<div class="save-photo"></div>
<script>
let vConsole = new window.VConsole();
let video = document.querySelector('video');
let canvas = document.querySelector("canvas");
let taskPhoto = document.querySelector('.take-photo')
let savePhoto = document.querySelector('.save-photo')
canvas.hidden = true;
taskPhoto.hidden = true;
savePhoto.hidden = true;
if (navigator.mediaDevices.getUserMedia) {
// facingMode: "user" 前置摄像头
// facingMode: { exact: "environment" } 强制使用后置摄像头
let constraints = { audio: false, video: { width: document.body.clientHeight, height: document.body.clientWidth, facingMode: { exact: "environment" } } };
navigator.mediaDevices.getUserMedia(constraints)
.then(function (mediaStream) {
video.setAttribute('autoplay', '');
video.setAttribute('muted', '');
video.setAttribute('playsinline', '');
video.srcObject = mediaStream;
taskPhoto.hidden = false;
})
.catch(function (err) {
console.log(err.name + ": " + err.message);
document.write("您的浏览器不支持")
}); // 总是在最后检查错误
} else console.log("您的浏览器不支持");
taskPhoto.addEventListener('click', () => {
let c = canvas.getContext('2d');
canvas.width = video.clientWidth;
canvas.height = video.clientHeight;
c.drawImage(video, 0, 0, video.clientWidth, video.clientHeight);
video.pause();
canvas.hidden = false;
savePhoto.hidden = false;
taskPhoto.hidden = true;
})
savePhoto.addEventListener('click', () => {
let a = document.createElement('a');
a.href = canvas.toDataURL()
a.download = "照片.png";
a.click();
savePhoto.hidden = true;
canvas.hidden = true;
taskPhoto.hidden = false;
video.play();
});
</script>
</body>
</html>