-
zhangwei
2025-01-23 e5a77d6a551d13e4cf74624b31b0fa3e328b304d
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
<template>
    <view class="page wrap-fix">
        <view class="canvas-wrap">
            <!-- :style="{width: (widthScale * 100)+'%'; height:(heightScale * 100)+'%';}" -->
            <canvas type="webgl" id="canvas"></canvas>
        </view>
 
        <!-- <view class="hint-words">提示:点击获取,会将摄像头资源转成贴图(需基础库3.4.4)</view> -->
 
        <view class="wrap-option">
            <view class="row">
                <button type="primary" bindtap="getJpgImg">获取jpg图像</button>
                <button type="primary" bindtap="getLog">开关调试log(时间戳)</button>
            </view>
 
        </view>
 
        <view class="hint-img-wrap">
            <view class="hint-img-box">
                <image class="hint-img" v-if="jpgUrl !== ''" :src="jpgUrl" mode="aspectFit" />
            </view>
        </view>
 
 
    </view>
</template>
 
<script>
    import arBehavior from '@/components/behavior/behavior-ar'
    import threeBehavior from '@/components/behavior/behavior-three'
    export default {
        mixins: [arBehavior, threeBehavior],
        onLoad() {
            // if (wx.offThemeChange) {
            //     wx.offThemeChange()
            // }
            // this.theme = wx.getSystemInfoSync().theme || 'light'
            // if (wx.onThemeChange) {
            //     wx.onThemeChange(({
            //         theme
            //     }) => {
            //         this.theme = theme
            //     })
            // }
            this.getAuthSetting()
            wx.getSetting({
                success: (res) => {
                    console.log(res, 'rlsb');
                },
            })
        },
        data() {
            return {
                theme: 'light',
                widthScale: 1 * 100, // canvas宽度缩放值
                heightScale: 0.57 * 100, // canvas高度缩放值
                jpgUrl: '',
            }
        },
        methods: {
            getAuthSetting() {
                wx.getSetting({
                    success: (res) => {
                        console.log(res, 'rlsb');
                        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()
                                                }
                                            }
                                        })
                                    }
                                }
                            })
                        }
                    }
                })
            },
            // 对应案例的初始化逻辑,由统一的 behavior 触发
            init() {
                // 初始化 Three.js,用于模型相关的渲染
                this.initTHREE()
 
                // 初始化 GL,基于 Three.js 的 Context,用于相机YUV渲染
                this.initYUV()
 
                // 初始化VK
                // start完毕后,进行更新渲染循环
                this.initVK();
 
                this.useLoopLog = false;
                this.imgIndex = 0;
            },
            initVK() {
                // VKSession 配置
                const session = this.session = wx.createVKSession({
                    track: {
                        face: {
                            mode: 1
                        },
                    },
                    version: 'v2',
                    gl: this.gl
                });
 
                session.start(err => {
                    if (err) return console.error('VK error: ', err)
 
                    console.log('@@@@@@@@ VKSession.version', session.version)
 
                    //  VKSession EVENT resize
                    session.on('resize', () => {
                        this.calcCanvasSize();
                    })
 
                    // VKSession EVENT addAnchors
                    session.on('addAnchors', anchors => {})
 
                    // VKSession EVENT updateAnchors
                    session.on('updateAnchors', anchors => {
 
                    })
 
                    // VKSession removeAnchors
                    // 识别目标丢失时,会触发一次
                    session.on('removeAnchors', anchors => {
                        // console.log('removeAnchors', anchors)
                    });
 
 
                    console.log('ready to initloop')
                    // start 初始化完毕后,进行更新渲染循环
                    this.initLoop();
 
                    // 绘制双面,以及去掉清屏,用于显示yuv
                    this.renderer.state.setCullFace(this.THREE.CullFaceNone)
 
                });
            },
            loop() {
 
                // 获取 VKFrame
                const frame = this.session.getVKFrame(this.canvas.width, this.canvas.height)
 
                // 成功获取 VKFrame 才进行
                if (!frame) {
                    return;
                }
 
                // 更新相机 YUV 数据
                this.renderYUV(frame)
 
                // 获取 VKCamera
                const VKCamera = frame.camera
 
                if (this.useLoopLog) {
                    // let transformStr = '';
                    // for (let i = 0; i < 16; i++)
                    // transformStr += VKCamera.transform[i] + ' ';
                    // console.log('VKCamera.transform', transformStr);
 
                    console.log('timeStamp', frame.timestamp);
 
 
                    let viewMatrixStr = '';
                    for (let i = 0; i < 16; i++)
                        viewMatrixStr += VKCamera.viewMatrix[i] + ' ';
                    // console.log('VKCamera.viewMatrix', viewMatrixStr);
                    if (this.preTimestamp && this.preTimestamp === frame.timestamp) {
                        if (this.preViewMatrixStr && this.preViewMatrixStr !== viewMatrixStr) {
                            console.log('preViewMatrixStr', this.preViewMatrixStr)
                            console.log('viewMatrixStr', viewMatrixStr)
                            console.log('Timestamp is same. But viewMatrix is not same');
                        }
                    }
                    this.preTimestamp = frame.timestamp;
                    this.preViewMatrixStr = viewMatrixStr;
                }
 
            },
            getJpgImg() {
                console.log('Function getJpgImg');
 
                // 按需写入获取 jpg 的 大小 和质量
                const width = 640;
                const height = 480;
                const quality = 90;
 
                // 获取 VKFrame
                const frame = this.session.getVKFrame(width, height)
 
                // 成功获取 VKFrame 才进行
                if (!frame) {
                    return;
                }
 
                console.log('getCameraJpgBuffer: ', width, height, quality)
 
                const t1 = new Date().getTime()
                const jpgBuffer = frame.getCameraJpgBuffer(width, height, quality);
                const t2 = new Date().getTime()
 
                console.log(`getCameraJpgBuffer cost ${t2 - t1}ms`)
 
                // console.log('jpgBuffer', jpgBuffer);
 
                const jpgUrl = this.saveLocalJPG(jpgBuffer, 'cameraJPG');
 
                console.log('jpgUrl', jpgUrl)
 
                this.setData({
                    'jpgUrl': jpgUrl
                })
 
 
            },
            saveLocalJPG(bufferContent, name) {
                var url = `${wx.env.USER_DATA_PATH}/${name + this.imgIndex + '.jpg'}`
 
                const fs = wx.getFileSystemManager()
                try {
                    // 存在即删除
                    const unlinkRes = fs.unlinkSync(url)
                    // console.log('unlinkSync', unlinkRes)
 
                    this.imgIndex++;
                    const newUrl = `${wx.env.USER_DATA_PATH}/${name + this.imgIndex + '.jpg'}`;
 
                    // console.log('write newUrl', newUrl)
                    // 写入,新图片
                    const writeRes = fs.writeFileSync(
                        newUrl,
                        bufferContent,
                        'utf8'
                    )
 
                    return newUrl
 
                } catch (e) {
                    // 利用catch实现,此时,为新写入
                    try {
                        // console.log('write url', url)
 
                        // 写入
                        const writeRes = fs.writeFileSync(
                            url,
                            bufferContent,
                            'utf8'
                        )
                    } catch (e) {
                        console.error(e);
                    }
                }
 
                return url
            },
            getLog() {
                console.log('Function getLog');
 
                this.useLoopLog = !this.useLoopLog;
            },
 
        },
    }
</script>
 
<style>
    .canvas-wrap {
        position: relative;
        width: 100%;
        background-color: #000;
    }
 
    .canvas-wrap #canvas {
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
    }
 
    .hint-box {
        position: absolute;
        left: 0%;
        top: 0%;
        width: 0;
        height: 0;
    }
 
    .hint-id {
        position: absolute;
        left: -6rpx;
        right: -6rpx;
        bottom: 100%;
        color: #fff;
        font-size: 20rpx;
        line-height: 40rpx;
        text-align: center;
        text-overflow: ellipsis;
        background-color: #07c160;
        border-radius: 8rpx;
 
    }
 
    .wrap-fix {
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
    }
 
    .hint-words {
        position: absolute;
        left: 20rpx;
        top: 20rpx;
        right: 20rpx;
        padding: 10rpx 20rpx;
        line-height: 40rpx;
        text-align: center;
        border-radius: 6rpx;
        background-color: rgba(0, 0, 0, .3);
        font-size: 26rpx;
        color: #fff;
    }
 
 
    .hint-img-wrap {
        position: absolute;
        left: 25%;
        bottom: 0%;
        width: 50%;
        height: 30%;
    }
 
    .hint-img-box {
        position: absolute;
        left: 5%;
        top: 5%;
        right: 5%;
        bottom: 5%;
        background-color: #fafafa;
        border: 4rpx solid;
    }
 
    .hint-img {
        position: absolute;
        left: 4%;
        top: 4%;
        width: 92%;
        height: 92%;
    }
 
    .wrap-option {
        position: absolute;
        left: 20rpx;
        right: 0;
        bottom: 30%;
        height: 10%;
        flex-direction: column;
        display: flex;
    }
 
    .wrap-option .row {
        flex: 1;
        display: flex;
        flex-direction: row;
        text-align: center;
    }
 
    .wrap-option button {
        flex: 1;
        margin: 5rpx 20rpx 20rpx 0;
        font-size: 30rpx;
        line-height: 1.2;
    }
</style>