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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<template>
<div class="camera_outer">
<div class="img">
<video id="videoCamera" :width="videoWidth" :height="videoHeight" autoplay></video>
<canvas style="display:none;" id="canvasCamera" :width="videoWidth" :height="videoHeight"></canvas>

<div v-if="imgSrc" class="img_bg_camera">
<img :src="imgSrc" alt="" class="tx_img">
</div>
</div>
<el-button type="primary" @click="getCompetence">打开摄像头</el-button>
<el-button type="primary" @click="stopNavigator">关闭摄像头</el-button>
<el-button type="primary" @click="setImage">拍照</el-button>
<el-button type="primary" @click="cleanImage">清除</el-button>
<div>
<el-select style="width: 200px;" @change="changeCamera" v-model="value">
<el-option v-for="device in videoDevices" :key="device.deviceId" :value="device.deviceId">{{ device.label }}</el-option>
</el-select>
</div>
</div>
</template>

<script>
export default {
data() {
return {
videoWidth: 400,
videoHeight: 300,
imgSrc: '',
thisCancas: null,
thisContext: null,
thisVideo: null,
videoDevices: [],
currentDeviceId: null,
value: ''
}
},
mounted() {
this.getVideoDevices()
},
methods: {
async getCompetence() {
var _this = this
this.thisCancas = document.getElementById('canvasCamera')
this.thisContext = this.thisCancas.getContext('2d')
this.thisVideo = document.getElementById('videoCamera')

if (navigator.mediaDevices === undefined) {
navigator.mediaDevices = {}
}
if (navigator.mediaDevices.getUserMedia === undefined) {
navigator.mediaDevices.getUserMedia = function (constraints) {
var getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.getUserMedia
if (!getUserMedia) {
return Promise.reject(new Error('getUserMedia is not implemented in this browser'))
}
return new Promise(function (resolve, reject) {
getUserMedia.call(navigator, constraints, resolve, reject)
})
}
}

await this.getVideoDevices()
if (this.videoDevices.length > 0) {
this.currentDeviceId = this.videoDevices[0].deviceId
this.value = this.videoDevices[0].label
}

var constraints = { audio: false, video: { deviceId: this.currentDeviceId ? { exact: this.currentDeviceId } : undefined, width: this.videoWidth, height: this.videoHeight, transform: 'scaleX(-1)' } }
navigator.mediaDevices.getUserMedia(constraints).then(function (stream) {
if ('srcObject' in _this.thisVideo) {
_this.thisVideo.srcObject = stream
} else {
_this.thisVideo.src = window.URL.createObjectURL(stream)
}
_this.thisVideo.onloadedmetadata = function (e) {
_this.thisVideo.play()
}
}).catch(err => {
console.log(err)
})
},
async getVideoDevices() {
const devices = await navigator.mediaDevices.enumerateDevices()
this.videoDevices = devices.filter(device => device.kind === 'videoinput')
console.log(this.videoDevices);
},
changeCamera(value) {
console.log(value)
this.currentDeviceId = value
this.stopNavigator()
this.getCompetence()
},
setImage() {
var _this = this
_this.thisContext.drawImage(_this.thisVideo, 0, 0, _this.videoWidth, _this.videoHeight)
var image = this.thisCancas.toDataURL('image/png')
_this.imgSrc = image
this.$emit('refreshDataList', this.imgSrc)
},
dataURLtoFile(dataurl, filename) {
var arr = dataurl.split(',')
var mime = arr[0].match(/:(.*?);/)[1]
var bstr = atob(arr[1])
var n = bstr.length
var u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
return new File([u8arr], filename, { type: mime })
},
stopNavigator() {
this.thisVideo?.srcObject?.getTracks()?.forEach(track => track.stop())
},
cleanImage() {
this.imgSrc = ''
this.value = ''
this.thisVideo = null
}
},
}
</script>

<style lang="scss" scoped>
.camera_outer {
position: relative;
overflow: hidden;
background: url("../../assets/img/user_0608_04.png") no-repeat center;
background-size: 100%;

video,
canvas,
.tx_img {
-moz-transform: scaleX(-1);
-webkit-transform: scaleX(-1);
-o-transform: scaleX(-1);
transform: scaleX(-1);
}

.btn_camera {
position: absolute;
bottom: 4px;
left: 0;
right: 0;
height: 50px;
background-color: rgba(0, 0, 0, 0.3);
line-height: 50px;
text-align: center;
color: #ffffff;
}

.bg_r_img {
position: absolute;
bottom: 0;
left: 0;
right: 0;
top: 0;
}

.img_bg_camera {
position: absolute;
bottom: 0;
left: 0;
right: 0;
top: 0;
width: 100%;
height: fit-content;

.img_btn_camera {
position: absolute;
bottom: 0;
left: 0;
right: 0;
height: 50px;
line-height: 50px;
text-align: center;
background-color: rgba(0, 0, 0, 0.3);
color: #ffffff;

.loding_img {
width: 50px;
height: 50px;
}
}
}
}
</style>