#鸿蒙通关秘籍#如何在HarmonyOS NEXT中实现拍照功能与上传照片

HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
墨韵流香ROM

调用相机拍照并上传图片,以下是实现的代码:

async takePhoto() {
  try {
    let pickerProfile: cameraPicker.PickerProfile = {
      cameraPosition: camera.CameraPosition.CAMERA_POSITION_BACK
    };
    let pickerResult: cameraPicker.PickerResult = await cameraPicker.pick(this.context,
      [cameraPicker.PickerMediaType.PHOTO, cameraPicker.PickerMediaType.PHOTO], pickerProfile);
    console.log('takePhoto', JSON.stringify(pickerResult))
    if (pickerResult?.resultUri) {
      this.modalShow = false;
      this.loaded = false;
      try {
        let filePath = await copyFileToCache(pickerResult.resultUri, this.context)
        if (filePath) {
          this.uploadImg(filePath)
        }
      } catch (err) {
        this.loaded = true;
      }
    } else {
      this.loaded = true;
    }
  } catch (error) {
    console.error(`The pick call failed. error code: ${error.code}`);
  }
}
分享
微博
QQ
微信
回复
2天前
相关问题
如何在HarmonyOS实现图片上传功能
150浏览 • 0回复 待解决