uniapp h5打包后调用安卓相机拍照上传

2022/11/15 apkuniapph5

参考链接 (opens new window) 参考链接 (opens new window) 参考链接 (opens new window) 参考链接 (opens new window)

# 调用拍照

      if(!plus){
        return false
      }
      // 获取摄像头管理对象 getCamera 参数 index 指定要获取摄像头的索引值,1表示主摄像头,2表示辅摄像头。如果没有设置则使用系统默认主摄像头。
      const cmr = plus.camera.getCamera()
      // 字符串数组,摄像头支持的拍照分辨率
      const res = cmr.supportedImageResolutions[0]
      // 字符串数组,摄像头支持的拍照文件格式
      const fmt = cmr.supportedImageFormats[0]
      // 进行拍照操作
      cmr.captureImage((path) => {
          console.log(path)
          this.compressImage(path,path)
          // path 图片地址
          // let file = new File(path)
      },
      (error) => {
          console.log('Capture image failed: ' + error.message)
      }, {
          resolution: res,
          format: fmt
      })
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# 拍照后回调是个本地路径 不可以直接使用 进行转换

# 压缩图片

      const _this= this
       var name="_doc/upload/"+filename;  
       plus.zip.compressImage({    
               src:url,//src: (String 类型 )压缩转换原始图片的路径    
               dst:name,//压缩转换目标图片的路径    
               quality:90,//quality: (Number 类型 )压缩图片的质量.取值范围为1-100    
               overwrite:true,//overwrite: (Boolean 类型 )覆盖生成新文件
              //  width:'250',
              //   height:'320'
           },    
           function(zip) {  
            console.log('压缩')
               //页面显示图片  
               _this.urlToFile(zip.target,name); 
               console.log(zip.target)
           },function(error) {    
               plus.nativeUI.toast("压缩图片失败,请稍候再试");    
       });  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# 准备上传 从本地读取文件

// 使用uniapp 读取成base64 
       plus.io.resolveLocalFileSystemURL(url,function(entry){  
           entry.file( function(file){  
             const reader = new plus.io.FileReader();
             	reader.readAsDataURL(file,'utf-8');
              reader.onloadend = e => {
                console.log('read')
                const base64=e.target.result;
                console.log(base64)
                _this.uploadFile(base64)
            //一些逻辑操作
            // e.target.result就是base64字符串
          };
        });  
      });  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
最后更新时间: 2023/7/16 15:32:35