【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);
    }
  }
相关推荐
Huanzhi_Lin15 小时前
Laya导出的鸿蒙NEXT工程目录说明
华为·harmonyos·鸿蒙·laya·deveco·devecostudio·layaair
积水成渊,蛟龙生焉15 小时前
鸿蒙手势处理篇(滑动冲突、基础手势、组合手势)
华为·arkts·鸿蒙·滑动冲突·手势冲突·基础手势·组合手势
纯爱掌门人21 小时前
聊聊 HarmonyOS 上的应用内通知授权弹窗
前端·harmonyos·arkts
不喝水就会渴1 天前
从基础到实战:鸿蒙 ArkUI 属性动画开发指南
华为·交互·动画·harmonyos
代码论斤卖1 天前
OpenHarmony teecd频繁崩溃问题分析
linux·harmonyos
南村群童欺我老无力.1 天前
鸿蒙 - TextInput高度设置的边界行为
华为·harmonyos
木斯佳1 天前
HarmonyOS 悬浮球实战:从页面内组件到 SubWindow 方案
harmonyos·悬浮球
仓颉编程语言1 天前
直播预告 |【仓颉社区】第44期WORKSHOP
华为·ai·ai编程·鸿蒙·仓颉编程语言
Lanren的编程日记1 天前
Flutter 鸿蒙应用AR功能集成实战:多平台AR框架+模拟模式,打造增强现实体验
flutter·ar·harmonyos
南村群童欺我老无力.1 天前
鸿蒙PC开发的Slider组件blockSize参数的类型要求
华为·harmonyos