uni-app 人脸扫描抓拍

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

本文是在使用uni-app开发安卓app时,做一个扫描抓拍进行人脸识别功能。


一、通过 uni-app 提供的 live-pusher 组件进行直播推流

注意:是 .nvue 文件,而不是 .vue 文件

<template>
	<view>
		<div style="width: 100%; height: 320px;">
			<live-pusher id='livePusher' ref="livePusher" class="livePusher" url="" mode="FHD" :muted="true"
				:enable-camera="true" :auto-focus="true" :beauty="1" whiteness="2" aspect="3:2" @statechange="statechange"
				@netstatus="netstatus" @error="error"></live-pusher>
		</div>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				snapshotInterval: null
			}
		},
		onReady() {
			// 注意:需要在onReady中 或 onLoad 延时
			this.context = uni.createLivePusherContext("livePusher", this);
			this.startPreview() // 开启摄像头
			this.switchCamera() // 切换为后置摄像头

			this.snapshotInterval = setInterval(() => {
				this.snapshot();
			}, 3000);
		},
		methods: {
			/* 开启摄像头 */
			startPreview: function() {
				this.context.startPreview({
					success: (a) => {
						console.log("livePusher.startPreview:" + a);
					}
				});
			},

			/* 默认开启的是前置摄像头,根据需求决定是否调用摄像头切换方法 */
			switchCamera: function() {
				this.context.switchCamera({
					success: (a) => {
						console.log("livePusher.switchCamera:" + JSON.stringify(a));
					}
				});
			},

			/* 自动抓拍方法 */
			snapshot: function() {
				this.context.snapshot({
					success: (e) => {
						/* log输出抓拍的图片的路径 */
						console.log(e.message.tempImagePath);

						/* 抓拍成功后可以清除计时器 */
						clearInterval(this.snapshotInterval)

						/* 将抓拍的照片的路径传给人脸识别处理方法 */
						this.scanFace()
					}
				});
			},

			/* 人脸识别方法 */
			scanFace() {
				/* 成功 */
				// 跳转到对应页面

				/* 失败,则重新创建计时器开始抓拍图片 */
				that.snapshotInterval = setInterval(() => {
					this.snapshot()
				}, 3000)
			}
		}
	}
</script>

二、以下代码为可运行代码

使用 HBuilder 创建一个空项目,把 pages 里的 index.vue 改为 index.nvue,其他的完全不用改,然后将下述代码替换进去即可运行。
可以参考 live-pusher
在这里插入图片描述

<template>
	<view>
		<div style="width: 100%; height: 320px;">
			<live-pusher id='livePusher' ref="livePusher" class="livePusher" url="" mode="FHD" :muted="true"
				:enable-camera="true" :auto-focus="true" :beauty="1" whiteness="2" aspect="3:2" @statechange="statechange"
				@netstatus="netstatus" @error="error"></live-pusher>
		</div>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				snapshotInterval: null
			}
		},
		onReady() {
			// 注意:需要在onReady中 或 onLoad 延时
			this.context = uni.createLivePusherContext("livePusher", this);
			this.startPreview() // 开启摄像头
			this.switchCamera() // 切换为后置摄像头

			this.snapshotInterval = setInterval(() => {
				this.snapshot();
			}, 3000);
		},
		methods: {
			/* 开启摄像头 */
			startPreview: function() {
				this.context.startPreview({
					success: (a) => {
						console.log("livePusher.startPreview:" + a);
					}
				});
			},

			/* 默认开启的是前置摄像头,根据需求决定是否调用摄像头切换方法 */
			switchCamera: function() {
				this.context.switchCamera({
					success: (a) => {
						console.log("livePusher.switchCamera:" + JSON.stringify(a));
					}
				});
			},

			/* 自动抓拍方法 */
			snapshot: function() {
				this.context.snapshot({
					success: (e) => {
						/* log输出抓拍的图片的路径 */
						console.log(e.message.tempImagePath);

						/* 抓拍成功后可以清除计时器 */
						clearInterval(this.snapshotInterval)
					}
				});
			},
		}
	}
</script>
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
开源uni-app是一种跨平台的应用开发框架,可以实现在多个平台上开发应用程序。PDA(便携式数据终端)扫描功能可以用于扫描条码或二维码,并将扫描的数据存入数据库中。 首先,需要在uni-app中引入PDA扫描的相关插件或组件,例如uni-PDA插件。通过该插件,可以调用PDA设备的扫描功能,并获取到扫描到的数据。 然后,创建一个数据库,并建立相应的表来存储扫描到的数据。可以使用uni-app的本地数据库或者远程数据库(如MySQL、MongoDB等)。 在uni-app中,编写前端页面,包括扫描按钮和显示扫描结果的区域。通过监听扫描按钮的点击事件,调用PDA插件进行扫描操作,并获取到扫描结果。将扫描结果显示在页面上,并同时将结果存入数据库中。 在后端,编写相应的接口来接收前端传递的扫描数据,并将数据存入数据库。可以使用后端开发框架如Node.js、Spring Boot等来完成接口的编写和数据库的连接操作。 最后,在uni-app的页面中,可以添加查询按钮或搜索框,通过调用相应的接口从数据库中查询对应的扫描结果,并在页面上展示查询结果。 总结来说,使用开源uni-app开发PDA扫描存入数据库的功能需要引入PDA扫描插件或组件,建立数据库并创建对应的表,编写前端页面和后端接口来实现扫描和存入数据库的操作,并可以通过查询功能来展示扫描结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值