首页 > 其他分享 >uni-app之camera组件-人脸拍摄

uni-app之camera组件-人脸拍摄

时间:2024-08-30 15:16:48浏览次数:8  
标签:拍摄 console log res app camera uni

小程序录制视频;10-30秒;需要拍摄人脸,大声朗读数字(123456)这种。

1.camera组件

camera页面内嵌的区域相机组件。注意这不是点击后全屏打开的相机

camera只支持小程序使用;官网链接

1.2 效果图

 

1.3 页面布局

camera 设置宽100%,高度通过uni.getSystemInfo获取,全屏展示。在通过定位把提示文字等信息放上去;

录制完毕,遮罩提示,完成录制,确认返回;

<template>
    <view class="camera-position">
        <camera device-position="front" flash="auto" @error="onCameraError"
            :style="'width: 100%; height: '+ screenHeight +'px;'">
            <!-- 人脸轮廓-图片 -->
            <image src="../../static/face/face-avater.png" style="width: 100%; height: 55vh; margin:22vh 0 0 0;"
                v-if="!achieveShow"></image>
        </camera>

        <!-- 顶部提示信息 -->
        <view class="camera-top text-center" v-show="!achieveShow">
            <view class="text-lg text-red">
                请面向屏幕
            </view>
            <view class="text-xl text-white margin-tb-xs">
                <text class="text-lg">用普通话大声读</text>
                <text class="text-red text-bold margin-left-xs">123456</text>
            </view>
            <view class="text-xxl text-red">
                <text class="text-df text-white">倒计时</text>
                <text class="text-red text-bold margin-lr-xs">{{totalSeconds}}</text>
                <text class="text-df text-white">S</text>
            </view>
        </view>

        <!-- 完成拍摄 -->
        <view class="achieve-shade" :style="'width: 100%; height: '+ screenHeight +'px;'" v-if="achieveShow">
            <view class="" style="font-size: 120rpx;color: #1977FF;">
                <text class="cuIcon-roundcheck"></text>
            </view>
            <view class="text-xl text-white margin-tb-sm">
                已完成人脸识别
            </view>
            <button class="cu-btn line-blue round lg" @click="confirmBut">确定</button>
        </view>
    </view>
</template>
View

css样式

<style lang="scss" scoped>
    .camera-position {
        position: relative;

        .camera-top {
            position: absolute;
            left: 0;
            top: 50rpx;
            width: 100%;
        }

        .camera-bottom {
            position: absolute;
            left: 0;
            bottom: 0;
            width: 100%;
        }

        .achieve-shade {
            position: absolute;
            left: 0;
            top: 0;
            background-color: rgba(222, 222, 222, 0.9);
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;

            button {
                width: 300rpx;
            }
        }
    }
</style>
css

js代码

<script>
    export default {
        data() {
            return {
                cameraContext: null,
                //计时器
                timer: null,
                //录制时长
                totalSeconds: 10,
                //屏幕高度
                screenHeight: "",
                //是否显示-完成遮罩
                achieveShow: false
            }
        },
        onl oad() {
            let that = this
            uni.getSystemInfo({
                success: (res) => {
                    console.log('屏幕宽度,单位为px:', res.windowWidth);
                    console.log('屏幕高度,单位为px:', res.windowHeight);
                    that.screenHeight = res.windowHeight;
                },
            });

            setTimeout(() => {
                this.startShoot()
            }, 500)
        },
        onReady() {
            // 创建 camera 上下文 CameraContext 对象
            this.cameraContext = uni.createCameraContext()
        },
        methods: {
            // 开始拍摄
            startShoot() {
                this.cameraContext.startRecord({
                    timeoutCallback: () => {
                        console.error('超出限制时长', this.totalSecond);
                    },
                    timeout: this.totalSeconds,
                    success: (res) => {
                        //开启计时器
                        this.timer = setInterval(() => {
                            this.totalSeconds--
                        }, 1000)
                        console.log(res, '开始拍摄');
                    },
                    fail: (err) => {
                        this.errToast('摄像头启动失败,请重新打开')
                    }
                })
            },
            // 结束拍摄
            stopShoot() {
                // 接触 计时器
                if (this.timer) clearInterval(this.timer)

                this.cameraContext.stopRecord({
                    compressed: true,
                    success: (res) => {
                        //显示遮罩
                        this.achieveShow = true
                        // TODO 获取数据帧
                        console.log(res, '结束拍摄');
                    },
                    fail: (err) => {
                        this.errToast('视频保存失败,请重新录制')
                    },
                })
            },
            // 摄像头错误
            onCameraError(error) {
                console.error('摄像头错误: ', error.detail);
            },
            //摄像头-失败操作
            errToast(e) {
                this.$operate.toast({
                    title: e
                })
                setTimeout(() => {
                    this.confirmBut()
                }, 500)
            },
            //确定-返回上一页
            confirmBut() {
                uni.navigateBack()
            },
        },
        watch: {
            //监听倒计时 
            totalSeconds: {
                handler(newVal) {
                    // console.log(newVal, '倒计时');
                    //倒计时 = 1 的时候结束拍摄 
                    if (newVal == 1) {
                        //结束拍摄
                        this.stopShoot()
                    }
                }
            }
        }
    }
</script>
js

注:第一次进入页面,有时候摄像头会启动失败,需要重新点击打开;

2.微信官方api

微信小程序中需要使用手机拍摄照片以及视频;使用wx.chooseMediaAPI来实现;

该API用于拍摄或从手机相册中选择图片或视频,官网链接为:wx.chooseMedia-微信开放文档

wx.chooseMedia({
	//数量 1-9
	count: 1,
	//时长
	maxDuration: '10',
	// 文件类型  image 图片  video视频   mix同时选择图片和视频
	mediaType: ['video'],
	// 图片和视频选择的来源: album 相册  camera相机拍摄
	sourceType: ['camera'],
	//摄像头: back 前置  front 后置摄像头 
	camera: 'back',
	success(res) {
		console.log(res)
		console.log(res.tempFiles[0].tempFilePath)
	},
	fail(err) {
		console.log(err)
	}
})

  

标签:拍摄,console,log,res,app,camera,uni
From: https://www.cnblogs.com/lovejielive/p/18388794

相关文章

  • 【Unity踩坑记录】使用Rigidbody模拟跳跃时,刚体会突然上升
    最初的写法privatevoidFixedUpdate(){if(!isGrounded){return;}floatrawHorizontal=Input.GetAxis("Horizontal");floatrawVertical=Input.GetAxis("Vertical");Vector3localDirection=new(rawHorizon......
  • uniapp js 划消小游戏 1.0 去控制台看打印(仅作参考)
    <template> <viewclass="wrap">  划消:{{sdNum}}*{{sdNum}}  <viewclass="btn"style="padding:32rpx;background:pink"@click="clickBtn">点击划消按钮</view>  <viewclass="btn&q......
  • JMeter手机app录制
    在移动应用的性能测试中,如何准确、全面地捕捉用户操作并生成可复用的测试脚本,始终是测试工程师面临的一大挑战。而JMeter,作为一款功能强大的开源性能测试工具,不仅在Web测试中表现优异,在手机App的录制方面同样拥有独到的优势。那么,如何利用JMeter来进行手机App的录制测试?它的录制......
  • uniapp js 数独小游戏 写死的简单数独 数独 3.0
    <template> <viewclass="wrap">  数独:{{sdNum}}*{{sdNum}}  <viewclass="btn"style="padding:32rpx;background:pink"@click="startFun">点击开始计时</view>  <viewclass="btn&q......
  • 野兔在线文库网站程序系统源码/app/小程序开发定制
    野兔文库系统是一套采用高速稳定的PHP+MYSQL开发,采用thinkPHP8框架、高性能、功能强、负载强的文库系统。文档支持格式:doc、docx、ppt、pptx、xls、xlsx、wps、et、dps、txt、pdf等程序开发: 程序名称:野兔文库系统程序开发:PHP+MySQL+tp8程序源码:100%开源,支持任意二开,商用......
  • The Power of Scale for Parameter-Efficient Prompt Tuning
    系列论文研读目录文章目录系列论文研读目录论文题目含义Abstract1Introduction2PromptTuning2.1DesignDecisions2.2UnlearningSpanCorruption3Results3.1ClosingtheGap3.2AblationStudy4ComparisontoSimilarApproaches5ResiliencetoDomainShif......
  • appsettins.json 复制到输出文件夹 CopyToOutpuDirectory 配置文件 csproj
    复制配置文件到输出文件夹<ItemGroup><NoneUpdate="appsettings.json"><CopyToOutputDirectory>Always</CopyToOutputDirectory></None><NoneUpdate="nlog.config"CopyToOutputDirectory="Always&qu......
  • IDEA报错:Error running 'XXXApplication' Error running XXXXApplication. Command li
     IDEA启动SpringBoot项目报错Errorrunning'XXXApplication'ErrorrunningXXXXApplication.Commandlineistoolong.ShortenthecommandlineviaJARmanifestorviaaclasspathfileandrerun   点击在高版本IDEA下只需要点击就会自动选择  低版本......
  • JMeter手机app录制
    在移动应用的性能测试中,如何准确、全面地捕捉用户操作并生成可复用的测试脚本,始终是测试工程师面临的一大挑战。而JMeter,作为一款功能强大的开源性能测试工具,不仅在Web测试中表现优异,在手机App的录制方面同样拥有独到的优势。那么,如何利用JMeter来进行手机App的录制测试?它的录制......
  • The 1st Universal Cup. Stage 7: Zaporizhzhia
    Preface在寝室白兰了一周多后也是终于等到徐神归来开始训练了这场的题感觉比较偏数学了,感觉和之前打的一个Tokyo的OpenCup很像,因此后期挺坐牢的4h左右堪堪写出7题,最后全队RushD结果发现暴力打表都打错了,怎么回事呢A.SquareSum这题在去年暑假前集训数学专题中......