uniapp使用live-pusher进行人脸识别打卡(安卓跟ios)

效果图

代码

使用live-pusher

<live-pusher id='livePusher' class="livePusher" mode="FHD" beauty="0" whiteness="0" aspect="9:16"
					min-bitrate="1000" audio-quality="16KHz" device-position="front" :auto-focus="true" :muted="true"
					:enable-camera="true" :enable-mic="false" :zoom="true"></live-pusher>

 获取实例

context = uni.createLivePusherContext("livePusher", ctx.proxy);

开启摄像头预览

function startPreview() {
				context.startPreview({
					success: (a) => {
						console.log("livePusher.startPreview:" + JSON.stringify(a));
					}
				});
			}

 让用户选择进出卡,然后使用定时器循环执行打卡

function openCard() {
				snapshot()
				state.intervalId = setInterval(() => {
					snapshot()
				}, 2500);
			}

截取图片转为base64传递给后台,这里是一个重点

一开始我是将图片压缩 然后上传给后台,获取http的图片,然后使用uni.reques转为base64

function snapshot() {
				context.snapshot({
					success: (e) => {
						console.log('打卡', e.message.tempImagePath)
						uni.compressImage({
							src: e.message.tempImagePath,
							quality: 10,
							success: res => {
								console.log('截取', res.tempFilePath)
								uni.uploadFile({
									url: 'xxxxxxxxx/upload/files',
									name: 'formFile',
									filePath: res.tempFilePath,
									fileType: 'image',
									formData: {
										'formParam': `{"userId":"${state.userInfo.userId}", "fileType":"images", "subDir":"usr"}`
									},
									header: {
										'token': localStorage.get('token'),
										'uid': state.userInfo.userId,
										'pid': state.projInfo.proId
									},
									success: (uploadFileRes) => {
										let imgDate = JSON.parse(uploadFileRes.data)
										console.log('图片', imgDate.result.fileUrls[0])
										uni.request({
											url: imgDate.result.fileUrls[0],
											method: 'GET',
											responseType: 'arraybuffer',
											success: (res) => {
												const base64 =uni.arrayBufferToBase64(res.data)
												let dataObj = {
													"projId":state.projInfo.proId,
													"installType":state.activeTopIsImg,
													"ioTime":ACutils.time.getCurrentTime(
														'yy-mm-dd HH:MM:SS'),
													"image":base64,
													"longitude":state.center_lng,
													"latitude":state.center_lat
												}
												console.log('整个参数:',dataObj);
											},
											fail: (err) => {
												console.log(err);
											},
										})
									},
									fail: (res) => {
										console.log('上传失败', res)
									},
								})
							}
						})
					}
				});
			};

但是每次传递base64人脸图片都要上传一张图片 就会让打开速度很慢,如何直接使用本地图片转为base64,就需要 plus.io

  • 什么是plus?

以plus开头的方法都是属于HTML5+环境调用的方法。

plus不能在浏览器环境下使用,它必须在手机APP上才能使用,因为以安卓为例,他是操纵webview的API。在5+中,我们在使用plus之前要监听HTML5+环境是否已经加载完毕,而在uniapp中,则可以直接调用,可以参看uni-app使用plus注意事项。

plus地址:HTML5+ API Reference

续代码

let privateDocPath = filePath
					plus.io.resolveLocalFileSystemURL(privateDocPath, function(entry) {
						entry.file(function(file) {
							const fileReader = new plus.io.FileReader();
							console.log(fileReader);
							fileReader.onloadend = function(e) {
								console.log('55555',e.target.result);
							};
							fileReader.readAsDataURL(file);
						});
					}, function(err) {
						console.log(err, '444444');
					});

使用plus.io完整代码

context.snapshot({
					success: (e) => {
						console.log('打卡', e.message.tempImagePath)
						uni.compressImage({
							src: e.message.tempImagePath,
							quality: 20,
							success: res => {
								console.log('截取', res.tempFilePath)
								let privateDocPath = res.tempFilePath
								plus.io.resolveLocalFileSystemURL(privateDocPath, function(entry) {
									entry.file(function(file) {
										const fileReader = new plus.io.FileReader();
										console.log(fileReader);
										fileReader.onloadend = function(e) {
											console.log('55555');
											let dataObj = {
												"projId":state.projInfo.proId,
												"installType":state.activeTopIsImg,
												"ioTime":ACutils.time.getCurrentTime(
													'yy-mm-dd HH:MM:SS'),
												"image":e.target.result.split(',')[1],
												"longitude":state.center_lng,
												"latitude":state.center_lat
											}
											console.log('整个参数:',dataObj);
										};
										fileReader.readAsDataURL(file);
									});
								}, function(err) {
									console.log(err, '444444');
								});
							}
						})
					}
				});

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你好!要使用uniapplive-pusher组件录制视频,你需要按照以下步骤进行操作: 1. 在uniapp项目中的页面中引入live-pusher组件。可以在需要的页面的`<template>`标签中,添加如下代码: ```html <live-pusher id="live-pusher" url="{{pusherUrl}}" mode="{{mode}}" muted="{{muted}}" beauty="{{beauty}}" enable-camera="{{enableCamera}}" bindstatechange="pusherStateChange" bindnetstatus="pusherNetStatus"></live-pusher> ``` 2. 在相应的页面的`<script>`标签中,定义相关参数和方法。例如: ```javascript export default { data() { return { pusherUrl: '', // 推流地址 mode: 'RTC', muted: false, beauty: 6, enableCamera: true } }, methods: { pusherStateChange(e) { // 推流状态变化时的回调函数 console.log('推流状态变化:', e) }, pusherNetStatus(e) { // 推流网络状态变化时的回调函数 console.log('推流网络状态变化:', e) }, startPusher() { // 开始推流 const pusherContext = uni.createLivePusherContext('live-pusher', this) pusherContext.start() }, stopPusher() { // 停止推流 const pusherContext = uni.createLivePusherContext('live-pusher', this) pusherContext.stop() } } } ``` 3. 在页面中添加按钮或其他触发事件的元素,调用对应的方法。例如: ```html <button @tap="startPusher">开始录制</button> <button @tap="stopPusher">停止录制</button> ``` 这样就可以通过uniapplive-pusher组件来录制视频了。你可以根据自己的需求,调整相关参数和方法,以实现更多功能。希望能帮到你!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

来自湖南的阿晨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值