微信小程小程序自定义相机实现文件扫描遇到的问题记录

该功能使用自定义相机每秒拍照一次,对照片进行剪裁和展示。如果图片质量不佳则重新拍摄。相机自动获取拍照权限,同时处理了低版本微信在安卓上的相册保存问题,提示用户升级微信版本。代码中包含了拍照、上传图片、版本对比的实现细节。
摘要由CSDN通过智能技术生成


1.需求:实现文件扫描。
             本功能具体实现:
             使用自定义相机间隔1s 拍照,由接口进行剪裁后,并展示剪裁后的图片,如果图片倾斜,模糊等情况等会重新调起拍照。

需要注意的是

1.自定义相机的调起会自动获取拍照权限,无需手动调起,否则会重复调用两次。

2.自定义相机的拍照在安卓手机微信8.0.35 以前的版本会默认保存到相册,需要兼容低版本的微信,提示用户升级版本。


        

       takePhoto() {
               
                const ctx = uni.createCameraContext()
                ctx.takePhoto({
                    quality: 'high',
                    success: (res) => {
                        //调接口上传
                        this.toUpload(res.tempImagePath);
                       
                    },
                    fail: (err) => {
                        console.log("拍照出错", err)
                        
                        // 防止相机未初始化
                        setTimeout(() => {
                            this.takePhoto()
                        }, 1000)
                    }
                })
            },


     

		// 上传拍到的图片
			toUpload(url) {
				let photoorl = url;
				uni.uploadFile({
				
					url:****接口 ,
					filePath: photoorl,
					name: 'file',
					formData: {
						type: this.isFile,//这里是区分上传的文件类型,可以忽略

					},
					header: {
						'Content-Type': 'multipart/form-data',
						'Access-Control-Allow-Origin': '*',
					},
					success: res => {
						if (typeof res.data === 'string') {
							res.data = JSON.parse(res.data)
						}
						if (res.statusCode == 200) {
							if (res.data.code == 200) {
								this.preBaseUrl = "https://oss.zgxmall.com/" + res.data.data;
								this.scanType = res.data.type;
								uni.showToast({
									title: '扫描完成!',
									duration:300
								})
                                
                                //预览图片
								setTimeout(() => {
									this.scanShow = true
								}, 0)
								return
							} else {
                                // 图片模糊等情况,提示错误信息,并在1s后重新拍照
								uni.showToast({
									icon: 'none',
									duration:500,
									title: res.data.msg
								})
								setTimeout(() => {
									this.takePhoto();
								}, 1000)
							}

						}else{
							this.takePhoto();
						}
					}
				});
			},

	// 版本号比较,兼容低版本
			compareVersion(v1, v2) {
				v1 = v1.split('.')
				v2 = v2.split('.')
				const len = Math.max(v1.length, v2.length)

				while (v1.length < len) {
					v1.push('0')
				}
				while (v2.length < len) {
					v2.push('0')
				}

				for (let i = 0; i < len; i++) {
					const num1 = parseInt(v1[i])
					const num2 = parseInt(v2[i])

					if (num1 > num2) {
						return 1
					} else if (num1 < num2) {
						return -1
					}
				}
				return 0
			},

-----------------------------------------------------------------------------------------
         下面是条件的判断。
                    let myplatform = "";
 					let version = '';
					
					let _this = this;
					
					uni.getSystemInfo({
						success: function(res) {
							//  区分系统与版本号
							myplatform = res.platform;
							version = res.hostVersion;
							if (myplatform == 'android') {
								if (_this.compareVersion(version, '8.0.35') >= 0) {
									// 无需升级版本,直接可以扫描文件
								} else {  
                                        //需要升级
									// 如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示
									uni.showModal({
										title: '提示',
										content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
									})
									return
								}
							} else {
								// 无需升级版本,直接可以扫描文件
							}
						},
						fail: function(err) {
							console.log("errrrrrrrr", err)
						},
						complete:function(res){
							console.log("调用完了",res)
						}
					})        

微信版本好的比较方法,微信官网也有。

兼容 | 微信开放文档
            
             

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值