【HarmonyOS】鸿蒙应用唤起系统相机拍照

【HarmonyOS】鸿蒙应用唤起系统相机拍照

方案一:

官方推荐的方式,使用CameraPicker来调用安全相机进行拍照。

dart 复制代码
    let pathDir = getContext().filesDir;
    let fileName = `${new Date().getTime()}`
    let filePath = pathDir + `/${fileName}.tmp`
    fileIo.createRandomAccessFileSync(filePath, fileIo.OpenMode.CREATE);

    let uri = fileUri.getUriFromPath(filePath);
    let pickerProfile: picker.PickerProfile = {
      cameraPosition: camera.CameraPosition.CAMERA_POSITION_BACK,
      saveUri: uri // 用于保存图片
    };
    let result: picker.PickerResult =
      await picker.pick(getContext(), [picker.PickerMediaType.PHOTO],
        pickerProfile);
    console.info(`picker resultCode: ${result.resultCode},resultUri: ${result.resultUri},mediaType: ${result.mediaType}`);
    if (result.resultCode == 0) {
      if (result.mediaType === picker.PickerMediaType.PHOTO) {
        this.imgSrc = result.resultUri;
      } 
    }

注意:
在应用调试时,开发者需在release模式下调用系统相机(CameraPicker)。
由于系统限制了debug模式下不允许调试release应用,在debug模式下调用系统相机,将导致异常。

方案二:

使用startAbility拉起相机系统应用,通过want接收回调信息。

dart 复制代码
  private async thirdPartyCall(supportMultiMode: boolean) {
    this.isCrop = false;
    console.log("thirdPartyCall savaPath=" + this.savePath);
    // 拉起拍照功能
    let want: Want = {
      "action": 'ohos.want.action.imageCapture',
      "parameters": {
        supportMultiMode: supportMultiMode,
        // 回调包名很重要,若不匹配将无法获取返回图片Uri的操作权限
        callBundleName: "com.example.persontest"
      }
    };
    // 获取图片uri
    if (this.context) {
      let result: common.AbilityResult = await this.context.startAbilityForResult(want);
      let params = result?.want?.parameters as Record<string, string | number>
      let imagePathSrc = params?.resourceUri as string;
      console.info(this.TAG, 'thirdPartyCall imagePathSrc= ' + imagePathSrc);
      console.info(this.TAG, 'thirdPartyCall params= ' + JSON.stringify(params));
      await this.getImage(imagePathSrc);
    }
  }
相关推荐
namehao46514 小时前
从HarmonyOS基础认证到赋能资源建设:我的鸿蒙开发进阶之路
华为·harmonyos
航Hang*17 小时前
第十五章:网络系统建设与运维(高级)—— 总复习
网络·华为·ensp·期末·复习
前端不太难17 小时前
用一张“状态扩散图”,定位 RN 列表性能风险
react.js·harmonyos
坚持学习前端日记18 小时前
桌面端与移动端JS桥技术对比及跨平台实现
开发语言·javascript·harmonyos
彭不懂赶紧问19 小时前
鸿蒙NEXT开发浅进阶到精通15:从零搭建Navigation路由框架
前端·笔记·harmonyos·鸿蒙
钟睿1 天前
HarmonyOS弹窗+bindSheet半模态+浮层通用解决方案覆盖全业务场景
android·harmonyos
ChinaDragon1 天前
HarmonyOS:管理Cookie及数据存储
harmonyos
ChinaDragon1 天前
HarmonyOS:User-Agent开发指导
harmonyos
●VON1 天前
在 Ubuntu 24.04 上搭建 OpenHarmony(鸿蒙)PC 命令行移植开发环境
linux·学习·ubuntu·harmonyos·von
爸爸6192 天前
Flutter跨平台开发:Fullscreen 在鸿蒙系统上的使用指南
flutter·华为·harmonyos