uniapp 小程序支持打开手机相册和摄像头

开发uniapp 时,有时需要让用户上传手机相册或者拍摄图片,对图片进行处理,下面提供了一个method,支持打开摄像头拍照和相册功能,完成后,对图片做base64处理。

// 打开相册的方法
			openCamera() {
				let _this=this
			   uni.chooseImage({
				 count: 1, // 默认9, 设置图片的选择数量
				 sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
				 sourceType: ['album','camera'], // 从相册选择
				 success: (res) => {
				   // 成功选择图片后的回调
				   console.log(res.tempFilePaths);
				   _this.localChooseImgUrl = res.tempFilePaths[0]
				   uni.showLoading({
				   		title: 'loading'
				   })
				   uni.getFileSystemManager().readFile({
				   		filePath: _this.localChooseImgUrl,
				   		encoding: 'base64',
				   		success: r => { // r.data 
				   			let base64 = r.data;
				   			//把arraybuffer转成base64
				   			base64 = 'data:image/jpeg;base64,' + base64;
				   			//不加上这串字符,在页面无法显示
				   			//console.log('base64', base64);
							uni.showToast({
							    title: "图片解析成功 !",icon:"none",duration:3000,
							});
							uni.hideLoading();
							
						
				   		},fail() {
				   			uni.showToast({
				   			    title: "图片处理异常 !",icon:"error",duration:5000,
				   			});
							uni.hideLoading();
				   			return
				   		}
				   })
				   uni.hideLoading();
				 },
				 fail: (err) => {
				   // 选择失败的回调
				   console.error(err);
				 }
				})
			},

相对应的button 设置click到该函数即可

<u-button type="success"  @click="openCamera" text="图片智能识别"></u-button>

这里我使用该功能,开发了个自动识别图片的功能,支持动植物,人物,物体解析功能,可以扫码体验一下,给些建议~
在这里插入图片描述

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

robin5911

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

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

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

打赏作者

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

抵扣说明:

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

余额充值