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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
| <template>
| <view class="">
| <view class="title">{{verifyText}}</view>
| <view class="container">
| <image :src="faceImg" v-if="faceImg" class="faceImg" mode="widthFix" />
| <camera class="camera" device-position="front" flash="off"></camera>
| </view>
|
| </view>
| </template>
|
| <script>
| let listener = null;
| let videoCtx = null;
| let VKSession = null;
| let faceVerifyTime = null; //面容验证倒计时
| export default {
| data() {
| return {
| faceImgHeight: 314,
| faceImgWidth: 314,
| face: {
| origin: {
| x: 0,
| y: 0
| },
| size: {
| width: 0,
| height: 0
| },
| points: []
| },
| verifyText: "请移动面容到框内",
| isCentre: false, //是否面容在正中间
| startVerify: false, //是否正在验证
| faceImg: "" //面容图片地址
| }
| },
| onLoad() {
| // this.getAuthSetting(); //获取权限
| this.initFaceVerify()
| },
| onUnload() {
| VKSession.destroy();
| },
| methods: {
| getAuthSetting() {
| wx.getSetting({
| success: (res) => {
| if (!res.authSetting['scope.camera']) {
| wx.showModal({
| title: '请允许获取摄像头权限',
| showCancel: false,
| complete: (modalRes) => {
| if (modalRes.confirm) {
| wx.openSetting({
| success: (settingRes) => {
| if (!settingRes.authSetting[
| 'scope.camera']) {
| this.getAuthSetting()
| } else {
| wx.navigateBack()
| }
| }
| })
| }
| }
| })
| }
| }
| })
| },
| initFaceVerify() {
| console.log('执行');
| videoCtx = null;
| listener = null;
| VKSession = null;
| videoCtx = wx.createCameraContext();
| let count = 0;
| listener = videoCtx.onCameraFrame((frame) => {
| count++;
| if (count === 5) {
| this.detectFace(frame);
| count = 0;
| }
| });
| VKSession = wx.createVKSession({
| version: 'v1',
| track: {
| plane: {
| mode: 1
| },
| face: {
| mode: 2
| }
| }
| });
| VKSession.on('updateAnchors', (anchors) => {
| // 有面容
| console.log(anchors, '有面容')
|
| if (this.data.startVerify) {
| return;
| }
| let anchor = anchors[0];
| this.setData({
| face: {
| points: anchor.points,
| origin: anchor.origin,
| size: anchor.size
| }
| }, () => {
| this.isFaceCentered()
| })
| })
| VKSession.on('removeAnchors', (anchors) => {
| // 面容消失
| if (this.data.startVerify) {
| return;
| }
| this.verifyText='请移动面容到框内'
| this.setData({
| verifyText: '请移动面容到框内',
| isCentre: false,
| face: {}
| }, () => {
| clearTimeout(faceVerifyTime)
| faceVerifyTime = null;
| })
| })
| setTimeout(() => {
| // 直接开始
| this.handleStart()
| }, 500);
| },
|
| picture() {
| this.setData({
| startVerify: true
| }, () => {
| clearTimeout(faceVerifyTime)
| faceVerifyTime = null;
| videoCtx.takePhoto({
| quality: 'original',
| success: (e) => {
| //上传照片接口,图片换成远端url地址 自行替换
| uploadImage(e.tempImagePath).then((res) => {
| this.setData({
| faceImg: res
| })
| wx.showLoading({
| title: '正在验证',
| })
|
| try {
| // 执行后端分析人脸
| api({
| img: res
| }).then((writeoffRes) => {
| //识别成功
| //自行处理识别成功结果
| }).catch(err => {
| wx.hideLoading();
| wx.showModal({
| title: err.msg,
| showCancel: false,
| confirmText: '重新核验',
| complete: (res) => {
| if (res.confirm) {
| this.setData({
| startVerify: false,
| faceImg: "",
| verifyText: '请移动面容到框内',
| isCentre: false,
| face: {}
| }, () => {
| this.handleStart()
| })
| }
| }
| })
| })
| } catch (error) {
| console.log(error)
| }
| }).catch(() => {
| this.setData({
| startVerify: false,
| faceImg: "",
| verifyText: '请移动面容到框内',
| isCentre: false,
| face: {}
| }, () => {
| this.handleStart()
| wx.showToast({
| title: '网络连接失败,请重试',
| })
| })
| })
| }
| })
| })
|
| },
| handleStart() {
| VKSession.start((errno) => {
| console.warn('VKSession.start errno', errno);
| });
| listener.start();
| },
| handleStop() {
| listener.stop({
| complete: (res) => {
| console.warn('listener.stop', res);
| }
| });
| VKSession.stop();
| },
| async detectFace(frame) {
| // 获取面容
| VKSession.detectFace({
| frameBuffer: frame.data,
| width: frame.width,
| height: frame.height,
| scoreThreshold: 0.8,
| sourceType: 0,
| modelMode: 2
| });
| },
| isFaceCentered() {
| // 判断面容是否在中间
| if (!this.data.face.points) {
| return;
| }
| let points = this.data.face.points[43]; //位置
| let size = this.data.face.size; //大小
| if (points.x > 0.65 || points.x < 0.4 || points.y > 0.65 || points.y < 0.35) {
| this.setData({
| verifyText: '请移动面容到框内',
| isCentre: false,
| face: {}
| }, () => {
| clearTimeout(faceVerifyTime)
| faceVerifyTime = null;
| })
| return
| } else if (size.width > 0.95 || size.width < 0.32) {
| this.setData({
| verifyText: '请移动面容显示完整面容',
| isCentre: false,
| face: {}
| }, () => {
| clearTimeout(faceVerifyTime)
| faceVerifyTime = null;
| })
| return
| }
| this.setData({
| verifyText: '请保持不动',
| isCentre: true
| }, () => {
| this.verifyCentre()
| })
| },
| verifyCentre() {
| // 开始验证面容
| if (faceVerifyTime || this.data.startVerify) {
| return
| } else {
| faceVerifyTime = setTimeout(() => {
| if (this.data.isCentre) {
| this.handleStop()
| this.picture()
| } else {
| clearTimeout(faceVerifyTime)
| faceVerifyTime = null;
| }
| }, 1500);
| }
| }
| }
|
| }
| </script>
|
| <style>
| .container {
| position: relative;
| padding: 200rpx 0;
| }
|
| .title {
| position: absolute;
| width: 100%;
| text-align: center;
| padding-top: 100rpx;
| font-size: 36rpx;
| }
|
| .camera {
| width: 600rpx;
| height: 600rpx;
| border-radius: 50% 50%;
| margin: 0 auto;
| }
|
| .faceImg {
| width: 600rpx;
| height: 600rpx;
| border-radius: 50% 50%;
| margin: 0 auto;
| position: absolute;
| z-index: 999;
| }
| </style>
|
|