Skip to content

Commit 11cd56e

Browse files
committed
添加
1 parent 0f832b5 commit 11cd56e

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

‎userMedia.html‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@
4444
<body>
4545
<div class="wrapper">
4646
<button id="openCamera" title="开启摄像头">开启摄像头权限</button>
47-
47+
<div class="select">
48+
<label for="videoSource">Video source: </label><select id="videoSource"></select>
49+
</div>
4850
<video width="700px" height="390px" class="video" autoplay="autoplay"></video>
4951

5052
<button title="拍照" id="photograph">拍照</button>
@@ -60,17 +62,22 @@
6062
let video = document.querySelector('video');
6163
let canvas = document.getElementById('canvas');
6264
let context1 = canvas.getContext('2d');
65+
66+
const videoSelect = document.querySelector('select#videoSource');
67+
6368
window.URL = window.URL || window.webkitURL || window.mozURL || window.msURL;
6469
// 切换到后置摄像头
65-
let exArray = [];
6670

6771
function gotDevices(deviceInfos) {
68-
console.log('[deviceInfos]', deviceInfos);
6972

7073
deviceInfos && deviceInfos.forEach(device => {
74+
const option = document.createElement('option');
75+
option.value = device.deviceId;
7176
if (device.kind === 'videoinput') {
7277
videoSource = device.deviceId;
73-
alert(videoSource, 'videoSource')
78+
option.text = device.label || `camera ${videoSelect.length + 1}`;
79+
videoSelect.appendChild(option);
80+
console.log('[videoSource]', videoSource);
7481
}
7582
})
7683

@@ -91,7 +98,7 @@
9198
if (navigator.getUserMedia) {
9299

93100
if (this.isOpenCamera) return console.log('[已经开启过摄像头的权限了]');
94-
101+
videoSource = videoSelect.value;
95102
const constraints = {
96103
video: {
97104
video: {
@@ -113,6 +120,7 @@
113120
this.isOpenCamera = true
114121
video.srcObject = stream;
115122
navigator.mediaDevices.enumerateDevices();
123+
116124
}
117125
//失败的回调
118126
function handleError(e) {
@@ -132,6 +140,7 @@
132140
context1.drawImage(video, 50, 50); //将video对象内指定的区域捕捉绘制到画布上指定的区域,实现拍照。
133141
context1.drawImage(img, 50, 50);
134142
})
143+
videoSelect.onchange = startOpenCamera;
135144
startOpenCamera()
136145
</script>
137146
</body>

0 commit comments

Comments
 (0)