【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);
    }
  }
相关推荐
鸿蒙小白龙20 分钟前
鸿蒙openharmony操作系统LiteOS-A内核技术全解析:架构、性能与开发实践
华为·架构·harmonyos·鸿蒙·鸿蒙系统·open harmony
2503_928411563 小时前
10.23 @Observed深层监听
华为·harmonyos·鸿蒙
KongHen4 小时前
UTS编写字符串编解码/加密插件(安卓及鸿蒙端)
前端·harmonyos
做运维的阿瑞4 小时前
鸿蒙6.0技术解析:五大行业迎来的智能化革命
人工智能·harmonyos
鸿蒙Jy4 小时前
一篇文章带你理解什么是鸿蒙开发中V1&&V2装饰器
harmonyos
SunkingYang4 小时前
C++变量与函数命名规范技术指南 (基于华为编码规范与现代C++最佳实践)
c++·华为·编码规范·命名规则·命名规范·函数名字·成员变量
王嘉俊9254 小时前
HarmonyOS 项目入门:构建跨设备智能应用的强大框架
华为·harmonyos
Francek Chen5 小时前
【HarmonyOS 6 特别发布】鸿蒙 6 正式登场:功能升级,构建跨设备安全流畅新生态
人工智能·华为·harmonyos·harmonyos 6
王嘉俊9255 小时前
HarmonyOS 分布式与 AI 集成:构建智能协同应用的进阶实践
人工智能·分布式·harmonyos
The 旺5 小时前
【案例实战】HarmonyOS分布式购物车:多设备无缝协同的电商体验
分布式·wpf·harmonyos