|
@@ -1,16 +1,166 @@
|
|
<template>
|
|
<template>
|
|
- <view> 我的页面 </view>
|
|
|
|
|
|
+ <view class="home">
|
|
|
|
+ <camera class="camera" device-position="front" flash="off"></camera>
|
|
|
|
+ <image v-if="tempImg" mode="widthFix" :src="tempImg" />
|
|
|
|
+ </view>
|
|
</template>
|
|
</template>
|
|
|
|
|
|
<script>
|
|
<script>
|
|
|
|
+ import { imgToBase64 } from '@/utils/utils'
|
|
export default {
|
|
export default {
|
|
name: 'HomePage',
|
|
name: 'HomePage',
|
|
data() {
|
|
data() {
|
|
- return {}
|
|
|
|
|
|
+ return {
|
|
|
|
+ cameraAuth: false,
|
|
|
|
+ tempImg: '',
|
|
|
|
+ VKSession: null,
|
|
|
|
+ videoCtx: null,
|
|
|
|
+ listener: null
|
|
|
|
+ }
|
|
},
|
|
},
|
|
- created() {},
|
|
|
|
- methods: {}
|
|
|
|
|
|
+
|
|
|
|
+ mounted() {
|
|
|
|
+ this.getAuth()
|
|
|
|
+ this.initData()
|
|
|
|
+ },
|
|
|
|
+ onUnload() {
|
|
|
|
+ this.VKSession?.destroy()
|
|
|
|
+ this.listener?.stop({
|
|
|
|
+ complete: (res) => {
|
|
|
|
+ console.log('listener.stop', res)
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ },
|
|
|
|
+ methods: {
|
|
|
|
+ async openSetting() {
|
|
|
|
+ const _this = this
|
|
|
|
+ let promise = new Promise((resolve, reject) => {
|
|
|
|
+ uni.showModal({
|
|
|
|
+ title: '授权',
|
|
|
|
+ content: '请先授权获取摄像头权限',
|
|
|
|
+ success(res) {
|
|
|
|
+ if (res.confirm) {
|
|
|
|
+ uni.openSetting({
|
|
|
|
+ success(res) {
|
|
|
|
+ if (res.authSetting['scope.camera']) {
|
|
|
|
+ // 用户打开了授权开关
|
|
|
|
+ resolve(true)
|
|
|
|
+ } else {
|
|
|
|
+ // 用户没有打开授权开关, 继续打开设置页面
|
|
|
|
+ _this.openSetting().then((res) => {
|
|
|
|
+ resolve(true)
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+ },
|
|
|
|
+ fail(res) {
|
|
|
|
+ console.log(res)
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ } else if (res.cancel) {
|
|
|
|
+ setTimeout(() => {
|
|
|
|
+ _this.openSetting().then((res) => {
|
|
|
|
+ resolve(true)
|
|
|
|
+ })
|
|
|
|
+ }, 3000)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ })
|
|
|
|
+ return promise
|
|
|
|
+ },
|
|
|
|
+ async getAuth() {
|
|
|
|
+ const _this = this
|
|
|
|
+ uni.getSetting({
|
|
|
|
+ success(res) {
|
|
|
|
+ console.log('sss', res.authSetting)
|
|
|
|
+ if (res.authSetting['scope.camera']) {
|
|
|
|
+ // 用户已经授权
|
|
|
|
+ _this.cameraAuth = true
|
|
|
|
+ } else {
|
|
|
|
+ uni.authorize({
|
|
|
|
+ scope: 'scope.camera',
|
|
|
|
+ success() {
|
|
|
|
+ // 用户同意授权
|
|
|
|
+ _this.cameraAuth = true
|
|
|
|
+ },
|
|
|
|
+ fail() {
|
|
|
|
+ // 用户不同意授权
|
|
|
|
+ _this.openSetting().then((res) => {
|
|
|
|
+ console.log('终于授权了')
|
|
|
|
+ _this.cameraAuth = true
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ },
|
|
|
|
+ async detectFace(frame) {
|
|
|
|
+ this.VKSession.detectFace({
|
|
|
|
+ frameBuffer: frame.data,
|
|
|
|
+ width: frame.width,
|
|
|
|
+ height: frame.height,
|
|
|
|
+ scoreThreshold: 0.8,
|
|
|
|
+ sourceType: 0,
|
|
|
|
+ modelMode: 1
|
|
|
|
+ })
|
|
|
|
+ },
|
|
|
|
+ initData() {
|
|
|
|
+ this.videoCtx = wx.createCameraContext()
|
|
|
|
+ console.log('videoCtx:', this.videoCtx)
|
|
|
|
+ let count = 0
|
|
|
|
+ this.listener = this.videoCtx.onCameraFrame((frame) => {
|
|
|
|
+ count++
|
|
|
|
+ if (count === 10) {
|
|
|
|
+ this.detectFace(frame)
|
|
|
|
+ count = 0
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ this.VKSession = wx.createVKSession({
|
|
|
|
+ version: 'v1',
|
|
|
|
+ track: {
|
|
|
|
+ plane: {
|
|
|
|
+ mode: 1
|
|
|
|
+ },
|
|
|
|
+ face: { mode: 2 }
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ this.VKSession.on('updateAnchors', (anchors) => {
|
|
|
|
+ console.log('anchors', anchors)
|
|
|
|
+ if (anchors.length && !this.tempImg) {
|
|
|
|
+ this.videoCtx.takePhoto({
|
|
|
|
+ quality: 'high',
|
|
|
|
+ success: (res) => {
|
|
|
|
+ this.tempImg = res.tempImagePath
|
|
|
|
+ imgToBase64(this.tempImg).then((base64) => {
|
|
|
|
+ console.log('人脸拍照图片 base64', base64)
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ this.VKSession.start((error) => {
|
|
|
|
+ if (error) {
|
|
|
|
+ this.$u.toast('VKSession start error')
|
|
|
|
+ // 如果失败,将返回 errno
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ this.listener.start()
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
</script>
|
|
</script>
|
|
|
|
|
|
-<style></style>
|
|
|
|
|
|
+<style lang="scss" scoped>
|
|
|
|
+ .home {
|
|
|
|
+ width: 100%;
|
|
|
|
+ height: 100%;
|
|
|
|
+ text-align: center;
|
|
|
|
+ .camera {
|
|
|
|
+ width: 400upx;
|
|
|
|
+ height: 400upx;
|
|
|
|
+ border-radius: 50%;
|
|
|
|
+ margin: 40rpx auto;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+</style>
|