首页 星云 工具 资源 星选 资讯 热门工具
:

PDF转图片 完全免费 小红书视频下载 无水印 抖音视频下载 无水印 数字星空

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

编程知识
2024年08月30日 14:17

小程序录制视频;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 text-xl text-white margin-tb-xs使用了 ColorUI-UniApp 插件内容

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
            }
        },
        onLoad() {
            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)
	}
})

  

From:https://www.cnblogs.com/lovejielive/p/18388794
本文地址: http://www.shuzixingkong.net/article/1589
0评论
提交 加载更多评论
其他文章 .NET 8 Moq mock GetRequiredKeyedService Setup报错
.NET 8 Moq mock GetRequiredKeyedService Setup报错 项目代码里有地方用到IServiceProvider.GetRequiredKeyedService&lt;T&gt;来解析服务,在写单元测试时需要Mock它,本以为像下面这样写就可以了: var ser
VS Code 代码片段指南: 从基础到高级技巧
前言 “ 系列首发于公众号『非同质前端札记』 ,若不想错过更多精彩内容,请“星标”一下,敬请关注公众号最新消息。 今天咱们来聊聊 VS Code 里的自定义代码片段。 这玩意儿简直是提升编码效率的神器, 用好了能让你敲代码更方便! 不管你是刚入行的菜鸟还是身经百战的老兵,这篇攻略都能让你在代码片段的
VS Code 代码片段指南: 从基础到高级技巧
Clobotics 计算机视觉场景存储实践:多云架构、 POSIX 全兼容、低运维的统一存储
Clobotics 是一家将计算机视觉和机器学习技术应用于风电以及零售行业的企业。在风电行业,Clobotics 利用无人机对风力发电机叶片进行检查,显著降低了对人工作业的依赖。在零售领域,公司通过分析捕获的包装商品图像来提供基于实时数据的洞察,以增加销售额并减少运营成本。 存储方面,Cloboti
Clobotics 计算机视觉场景存储实践:多云架构、 POSIX 全兼容、低运维的统一存储 Clobotics 计算机视觉场景存储实践:多云架构、 POSIX 全兼容、低运维的统一存储 Clobotics 计算机视觉场景存储实践:多云架构、 POSIX 全兼容、低运维的统一存储
基于 Quanto 和 Diffusers 的内存高效 transformer 扩散模型
过去的几个月,我们目睹了使用基于 transformer 模型作为扩散模型的主干网络来进行高分辨率文生图 (text-to-image,T2I) 的趋势。和一开始的许多扩散模型普遍使用 UNet 架构不同,这些模型使用 transformer 架构作为扩散过程的主模型。由于 transformer
基于 Quanto 和 Diffusers 的内存高效 transformer 扩散模型 基于 Quanto 和 Diffusers 的内存高效 transformer 扩散模型 基于 Quanto 和 Diffusers 的内存高效 transformer 扩散模型
NET Core 多身份校验与策略模式
背景需求: 系统需要对接到XXX官方的API,但因此官方对接以及管理都十分严格。而本人部门的系统中包含诸多子系统,系统间为了稳定,程序间多数固定Token+特殊验证进行调用,且后期还要提供给其他兄弟部门系统共同调用。 原则上:每套系统都必须单独接入到官方,但官方的接入复杂,还要官方指定机构认证的证书
NET Core 多身份校验与策略模式 NET Core 多身份校验与策略模式 NET Core 多身份校验与策略模式
倾斜摄影osgb格式文件,进行坐标转换
倾斜摄影OSGB格式的文件,很棘手,今天需要把osgb放到UE中渲染。碰到的问题如下: 1、osgb文件导进去后,Z轴不想上,是歪的,小人放进去后,就斜站在马路上。 2、根本原因是坐标系,UE的插件cesium for UE支持WGS84坐标(wkid:4326)。 怎么解决问题呢? 1、当然是问G
containerd:配置https私有镜像仓库的最新方法
随着containerd应用越来越广泛,我们必须紧跟官网的节奏。 之前配置https私有镜像仓库的方法比较繁琐,并且不易梳理,下边介绍一下目前最新的配置方法。 配置https私有镜像仓库 我假设你现在已经有私有仓库并且是https 再假设你的harbor域名是harbor.example.cn 你只
sipp模拟uas发送update
概述 freeswitch是一款简单好用的VOIP开源软交换平台。 但是fs在处理update消息时候有BUG,为了复现问题,使用sipp模拟uas,发送update并发送DTMF码。 本文档记录sipp的配置方案。 环境 CentOS 7.9 freeswitch 1.10.7 sipp.3.6.
sipp模拟uas发送update sipp模拟uas发送update