uniapp和vue实现打开手机前置摄像头和相机的方法

5 篇文章 1 订阅

最近在项目遇到了一个问题,用uniapp的框架做一个功能,就是打开平板自带的前置摄像头拍照,uniapp上的功能只有打开默认相机的功能,但是前置摄像头需要你自己去手动转换一下,客户体验感很不好,产品经理要求必须要打开就是前置摄像头~~~~~~~~~~~~~

这可愁坏了我,于是各种百度,小伙伴球高人指点,uniapp的官方群也问了,都么有用,其中群里有个人给了一个方法,但是调用腾讯云的接口,但是小编开发的是本地开发~

网上有个input方法,但是要在https环境下才能用。小编没用过,不知道有么有用~

好了废话不多

直接上代码

html部分

<template>
	<view class="content">
		<!-- 顶部tab -->
		<uni-nav-bar class="nav-tab" left-icon="back" color="#FFFFFF" backgroundColor="#3f62f6" statusBar="true"
			@clickLeft="tapBackIdColle">
			<template v-slot:left>
				<view class=" uni-flex uni-row" style="text-align: center;padding-top: 3px;">
					<text class="px14">面部采集</text>
				</view>
			</template>
			<!-- 	<view class=" uni-flex uni-row" style="text-align: center;position: absolute;right: 40px;" @tap="submit">
				<text style="font-size: 16px;">提交</text>
			</view> -->
		</uni-nav-bar>
		<view class="up-img ">
			<span class="up-span">点击采集面部照片</span>
			<view class="img-picker">
				<!-- 第一次采集 -->
				<view class="first">
					<image :class="imgFlag==true?'up-img-after':'up-img-box'" @click="takePhoto" :src="up_img" mode="">
					</image>
					<view class="btns" v-if="imgFlag==true">
						<button class="btn1" type="default" @click="retakePhoto" size="mini">重新采集</button>
						<button class="btn2" type="default" @click="confirm" size="mini">确定</button>
					</view>
				</view>

			</view>
		</view>


	</view>
</template>

<script>
	import {
		pathToBase64,
		base64ToPath
	} from 'image-tools';
	export default {
		data() {
			return {
				up_img: '../../../static/img/tzcj/up_img.png',
				// up_img:'file:///storage/emulated/0/Android/data/io.dcloud.HBuilder/apps/HBuilder/doc/camera/1633765326477.jpg',
				imgBase64: '',
				src: '',
				imgFlag: false
			}
		},
		onLoad() {
			let Camera = plus.camera.getCamera();
			console.log(Camera);
		},
		methods: {
			confirm(){//确定事件
			uni.showModal({
			    title: '提示',
			    content: '是否确定使用当前的图片',
			    success: function (res) {
			        if (res.confirm) {
			            console.log('用户点击确定');
						uni.navigateTo({
							url:'./tzcj?imgBase64='+that.imgBase64
						})
						
			        } else if (res.cancel) {
			            console.log('用户点击取消');
			        }
			    }
			});
				
			},
			retakePhoto(){//重新采集
			this.takePhoto();
				
			},
			tapBackIdColle() {//返回上一页
				uni.navigateTo({
					url: './tzcj'
				})
			},
			takePhoto() {// 拍照   
								  
				var cmr = plus.camera.getCamera(1);
				let that = this;
				cmr.captureImage(function(p) {
					//alert(p);//_doc/camera/1467602809090.jpg 
					console.log(p);
					plus.io.resolveLocalFileSystemURL(p, function(entry) {
						//alert(entry.toLocalURL());//file:///storage/emulated/0/Android/data/io.dcloud...../doc/camera/1467602809090.jpg   
						that.$nextTick(function() {
							that.up_img = entry.toLocalURL();
						})
						that.imgFlag = true;
						that.compressImage(entry.toLocalURL(), entry.name);


					}, function(e) {
						plus.nativeUI.toast("读取拍照文件错误:" + e.message);
					});
				}, function(e) {}, {
					filename: "_doc/camera/",
					index: 1
				});

			},
			//压缩图片   
			compressImage(url, filename) {
				let name = "_doc/upload/-" + filename; //_doc/upload/F_ZDDZZ-1467602809090.jpg   
				plus.zip.compressImage({
						src: url, //src: (String 类型 )压缩转换原始图片的路径   
						dst: name, //压缩转换目标图片的路径   
						quality: 20, //quality: (Number 类型 )压缩图片的质量.取值范围为1-100   
						overwrite: true //overwrite: (Boolean 类型 )覆盖生成新文件   
					},
					function(event) {
						//uploadf(event.target,divid);   
						var path = name; //压缩转换目标图片的路径   
						//event.target获取压缩转换后的图片url路   
						//filename图片名称   

						pathToBase64(path).then(p => {

							that.imgBase64 = p;
						}).catch(error => {
							console.error(error)
						});
					},
					function(error) {
						plus.nativeUI.toast("压缩图片失败,请稍候再试");
					});
			},





		}
	}
</script>

<style>
	.content {
		width: 100%;
		height: 100%;
		position: relative;
	}

	.up-img {
		display: flex;
		flex-direction: column;
		justify-content: center;
	}

	.up-span {
		/* position: absolute;
		top: 80px;		
		left: 40%;
		right: 30%; */

		color: #B23C3C;
		margin: 0 auto;
		margin-top: 15px;
		font-size: 20px;

	}

	.btns {
		transform: translateX(36%);
		margin-top: 20px;
	}

	.btn1 {
		background-color: #B13C3C !important;
		color: #FFFFFF !important;

	}

	.btn2 {
		background-color: #B13C3C !important;
		margin-left: 25px;
		color: #FFFFFF !important;
	}

	.up-img-box {
		width: 160px;
		height: 160px;
		border-radius: 25px;
		margin-left: 50%;
		margin-top: 15px;
		transform: translateX(-80px);
	}

	.up-img-after {
		width: 300px;
		height: 180px;
		border-radius: 25px;
		margin-left: 50%;
		margin-top: 15px;
		transform: translateX(-150px);
	}
</style>

  • 2
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
uniappvue2和vue3的区别在于以下几个方面: 1. 导航栏双页切换: 在uniapp中,使用vue2时,可以使用vue-router进行导航栏双页切换;而在使用vue3时,可以使用uni.navigateTo进行导航栏双页切换。 2. 实例化: 在uniapp中,使用vue2时,可以通过new Vue()来实例化Vue对象;而在使用vue3时,可以通过createApp()方法来实例化Vue对象。 3. 参数输出: 在uniapp中,使用vue2时,可以通过this.$route.query来获取页面参数;而在使用vue3时,可以通过this.$router.currentRoute.value.query来获取页面参数。 4. 方法调用: 在uniapp中,使用vue2时,可以通过this.\$refs来调用组件内的方法;而在使用vue3时,可以通过ref()来调用组件内的方法。 5. 模块化: 在uniapp中,使用vue2时,可以使用import和export来实现模块化;而在使用vue3时,可以使用import和export来实现模块化。 6. 持久化存储: 在uniapp中,使用vue2时,可以使用local.storage进行持久化存储;而在使用vue3时,可以使用uni.storage进行持久化存储。 7. 其他API差异: 在uniapp中,使用vue2时,需要将小程序的API进行适配,如将wx.request改为uni.request;而在使用vue3时,仍然需要将小程序的API进行适配,如将wx.request改为uni.request。 总结来说,uniapp在使用vue3时,相比于使用vue2,有一些API和语法的差异,需要进行相应的调整和适配。如果您有具体的问题,可以随时私信或加入技术服务群进行咨询[3]。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

镜中的女孩-potato

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

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

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

打赏作者

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

抵扣说明:

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

余额充值