【HarmonyOS 5】Invoking the System Camera for Photo and Video Capture in HarmonyOS Applications

GeorgeGcs
• 阅读 2
  1. Method One: Using CameraPicker (Official Recommended Approach) This method utilizes the CameraPicker API to invoke the secure system camera for photo and video capture.
    Implementation Code:
    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 // Path to save the captured image };

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; } } Key Notes:
● Debug vs. Release Mode: The system camera (CameraPicker) must be tested in Release mode during development. Debug mode restricts access to release-only features, causing exceptions.
● Permissions: Ensure the app has the ohos.permission.CAMERA permission declared in config.json.

  1. Method Two: Using startAbility to Launch the Camera App This method launches the system camera via an ability request (startAbilityForResult) and handles callbacks through Want parameters.
    Implementation Code:
    private async thirdPartyCall(supportMultiMode: boolean) { this.isCrop = false; console.log("thirdPartyCall savePath=" + this.savePath);

    // Launch camera intent let want: Want = { "action": 'ohos.want.action.imageCapture', "parameters": { supportMultiMode: supportMultiMode, // Important: Callback bundle name must match to access the returned URI callBundleName: "com.example.persontest" } };

    // Handle result 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); } } Key Notes:
    ● Callback Bundle Name: The callBundleName parameter must match the app's bundle name to ensure permission to access the returned image URI.
    ● Result Handling: The captured media URI is retrieved via result.want.parameters.resourceUri.

Comparison & Recommendations Feature CameraPicker (Method 1) startAbility (Method 2) Implementation Complexity Lower (simpler API) Higher (requires Want handling) Security Secure (official API) Standard system interaction Compatibility Recommended for HarmonyOS NEXT Works with older versions Media Types Supports photos and videos Primarily for photos (customizable) Debug Restrictions Requires Release mode for testing No such restriction Additional Tips

  1. Video Capture:
    ○ Modify PickerMediaType.PHOTO to PickerMediaType.VIDEO in CameraPicker for video recording.
    ○ For startAbility, use ohos.want.action.videoCapture as the action.
  2. Permissions:
    ○ Declare ohos.permission.CAMERA in config.json for both methods.
    ○ For video recording, add ohos.permission.MICROPHONE.
  3. Error Handling:
    ○ Implement robust error checks for URI handling and permission requests. By following these methods, you can effectively integrate camera functionality into your HarmonyOS application while adhering to system security requirements.
点赞
收藏
评论区
推荐文章
美凌格栋栋酱 美凌格栋栋酱
5个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
GeorgeGcs GeorgeGcs
19小时前
【 HarmonyOS 5 入门系列 】鸿蒙HarmonyOS示例项目讲解
【HarmonyOS5入门系列】鸿蒙HarmonyOS示例项目讲解\鸿蒙开发能力HarmonyOSSDK应用服务鸿蒙金融类应用(金融理财一、前言:移动开发声明式UI框架的技术变革在移动操作系统的发展历程中,UI开发模式经历了从命令式到声明式的重大变革。根据
GeorgeGcs GeorgeGcs
8小时前
【HarmonyOS 5】Integrating Weibo Sharing into HarmonyOS Applications
●OfficialSDKWebsite:https://open.weibo.com/wiki/SDK●DownloadtheHarmonyOSSDKdemoprojectpackage.Aftercompletion,thecompressedfiles
GeorgeGcs GeorgeGcs
8小时前
【HarmonyOS 5】Integrating WeChat Sharing into HarmonyOS Applications
1.DownloadHarmonyOSWeChatOpenSDKandDemo1.1WeChatOpenSDKInsertimagedescriptionhereClicktodownload@tencent/wechatopensdk(V1.0.7).1
GeorgeGcs
GeorgeGcs
Lv1
男 · 金融头部企业 · 鸿蒙应用架构师
HarmonyOS认证创作先锋,华为HDE专家,鸿蒙讲师,作者。目前任职鸿蒙应用架构师。 历经腾讯,宝马,研究所,金融。 待过私企,外企,央企。 深耕大应用开发领域十年。 AAE,Harmony(OpenHarmony\HarmonyOS),MAE(Android\IOS),FE(H5\Vue\RN)。
文章
56
粉丝
1
获赞
2