首页 > 其他分享 >uniapp开发——使用uni.chooseImage调用摄像头功能

uniapp开发——使用uni.chooseImage调用摄像头功能

时间:2024-09-27 17:38:53浏览次数:1  
标签:uniapp chooseImage res let uni 权限 data 摄像头

1.前端调用代码:

//获取摄像头权限失败的处理
async function requestAndroidPermission(permisionID) {
  var result = await permision.requestAndroidPermission(permisionID)
  if (result == -1) {
    //被永久拒绝权限"
    let msg = '您还没有开启摄像头权限,无法拍照,是否去开启?'
    uni.showModal({
      title: '温馨提示',
      content: msg,
      confirmText: "去开启",
      success: function(res) {
        if (res.confirm) {
          uni.openAppAuthorizeSetting()
        }
      }
    })
  }
}
/**
 * 选择并上传图片
 */
function uploadImage(callback) {
  let self = this;
  uni.chooseImage({
    count: 1,
    sourceType: ["album", "camera"],
    sizeType: ["compressed"],
    success(res) {
      let tempFilePath = res.tempFilePaths[0];
      let size = res.tempFiles[0].size / 1024 / 1024;
      if (size > 2) {
        toast("上传图片不能大于2MB");
        return;
      }
      uni.showLoading();
      uni.uploadFile({
        url: SaasUrl + '/upload/uploadimg',
        filePath: tempFilePath,
        name: 'file',
        formData: {
          app_id: AppId,
          resp_type: "json",
          filename: new Date().getTime() + '.jpg',
          type: 'headimg'
        },
        success(res) {
          console.log('/upload/uploadimg', res)
          uni.hideLoading();
          if (res.statusCode == 200) {
            let data = JSON.parse(res.data);
            if (data.ret_code == 0) {
              let url = data.data.url;
              callback(url);
            } else {
              toast(data.ret_msg);
            }
          }
        }
      });
    },
    fail(err) {
      console.log('uploadImageErr', err)
      if (err.code == 11) {
        //查询摄像头权限
        requestAndroidPermission("android.permission.CAMERA")
      }
    }
  })
}

2. uniapp工程权限配置:

3. AndroidStudio功能配置:

AndroidManifest.xml文件添加如下权限代码即可:

<uses-permission android:name="android.permission.CAMERA" />

 

标签:uniapp,chooseImage,res,let,uni,权限,data,摄像头
From: https://www.cnblogs.com/xyyt/p/18436215

相关文章