|
44 | 44 | <body> |
45 | 45 | <div class="wrapper"> |
46 | 46 | <button id="openCamera" title="开启摄像头">开启摄像头权限</button> |
47 | | - |
| 47 | + <div class="select"> |
| 48 | + <label for="videoSource">Video source: </label><select id="videoSource"></select> |
| 49 | + </div> |
48 | 50 | <video width="700px" height="390px" class="video" autoplay="autoplay"></video> |
49 | 51 |
|
50 | 52 | <button title="拍照" id="photograph">拍照</button> |
|
60 | 62 | let video = document.querySelector('video'); |
61 | 63 | let canvas = document.getElementById('canvas'); |
62 | 64 | let context1 = canvas.getContext('2d'); |
| 65 | + |
| 66 | + const videoSelect = document.querySelector('select#videoSource'); |
| 67 | + |
63 | 68 | window.URL = window.URL || window.webkitURL || window.mozURL || window.msURL; |
64 | 69 | // 切换到后置摄像头 |
65 | | - let exArray = []; |
66 | 70 |
|
67 | 71 | function gotDevices(deviceInfos) { |
68 | | - console.log('[deviceInfos]', deviceInfos); |
69 | 72 |
|
70 | 73 | deviceInfos && deviceInfos.forEach(device => { |
| 74 | + const option = document.createElement('option'); |
| 75 | + option.value = device.deviceId; |
71 | 76 | if (device.kind === 'videoinput') { |
72 | 77 | 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); |
74 | 81 | } |
75 | 82 | }) |
76 | 83 |
|
|
91 | 98 | if (navigator.getUserMedia) { |
92 | 99 |
|
93 | 100 | if (this.isOpenCamera) return console.log('[已经开启过摄像头的权限了]'); |
94 | | - |
| 101 | + videoSource = videoSelect.value; |
95 | 102 | const constraints = { |
96 | 103 | video: { |
97 | 104 | video: { |
|
113 | 120 | this.isOpenCamera = true |
114 | 121 | video.srcObject = stream; |
115 | 122 | navigator.mediaDevices.enumerateDevices(); |
| 123 | + |
116 | 124 | } |
117 | 125 | //失败的回调 |
118 | 126 | function handleError(e) { |
|
132 | 140 | context1.drawImage(video, 50, 50); //将video对象内指定的区域捕捉绘制到画布上指定的区域,实现拍照。 |
133 | 141 | context1.drawImage(img, 50, 50); |
134 | 142 | }) |
| 143 | + videoSelect.onchange = startOpenCamera; |
135 | 144 | startOpenCamera() |
136 | 145 | </script> |
137 | 146 | </body> |
|
0 commit comments