#鸿蒙学习大百科#如何使用相机Picker (Camera Picker)实现拍照、录制?

如何使用相机Picker (Camera Picker)实现拍照、录制?

HarmonyOS
2024-10-21 09:26:54
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
自封的不算

在应用需要申请权限ohos.permission.CAMERA以使用相机时,可以使用Camera Picker替代。

import picker from '@ohos.multimedia.cameraPicker';
import { common } from '@kit.AbilityKit';
import { camera } from '@kit.CameraKit';
import { BusinessError } from '@kit.BasicServicesKit';

let mContext = getContext(this) as common.Context;
async function openCamera() {
  try {
    let pickerProfile: picker.PickerProfile = {
      cameraPosition: camera.CameraPosition.CAMERA_POSITION_BACK
    };
    let pickerResult: picker.PickerResult = await picker.pick(mContext,
      [picker.PickerMediaType.PHOTO, picker.PickerMediaType.VIDEO], pickerProfile);
    console.log("the pick pickerResult is:" + JSON.stringify(pickerResult));
  } catch (error) {
    let err = error as BusinessError;
    console.error(`the pick call failed. error code: ${err.code}`);
  }
}
@Entry
@Component
struct Index {
  build() {
    Row() {
      Column() {
        Text("打开下相机")
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
              openCamera()
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}

打印日志:

the pick pickerResult is:{"resultCode":0,"resultUri":"file://media/Photo/122/IMG_1728713908_005/IMG_20241012_141648.jpg","mediaType":"photo"}
分享
微博
QQ
微信
回复
2024-10-21 14:48:05
相关问题
#鸿蒙学习大百科#如何实现ui优化?
239浏览 • 1回复 待解决
#鸿蒙学习大百科#ArkTS如何生成xml?
302浏览 • 1回复 待解决