• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.multimedia.cameraPicker (相机选择器)
2
3本模块提供相机拍照与录制的能力。应用可以自行选择媒体类型实现拍照和录制的功能。该类接口,需要应用在界面UIAbility中调用,否则无法拉起cameraPicker应用。
4
5> **说明:**
6>
7> 本模块首批接口从API version 11开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8
9## 导入模块
10
11```ts
12import picker from '@ohos.multimedia.cameraPicker';
13```
14
15## pick
16
17pick(context: Context, mediaTypes: Array\<PickerMediaType\>, pickerProfile: PickerProfile): Promise\<PickerResult\>
18
19拉起相机选择器,根据媒体类型进入相应的模式。操作结束通过Promise形式获取结果。
20
21**系统能力:** SystemCapability.Multimedia.Camera.Core
22
23**参数:**
24
25| 参数名          | 类型                                              | 必填 | 说明                           |
26| -------------- |-------------------------------------------------| ---- | ---------------------------- |
27| context        | [Context](../apis-ability-kit/js-apis-inner-application-context.md) | 是   | 应用上下文。                   |
28| mediaTypes     | Array\<[PickerMediaType](#pickermediatype)\>    | 是   | 媒体类型。                    |
29| pickerProfile  | [PickerProfile](#pickerprofile)                 | 是   | pickerProfile对象。            |
30
31**返回值:**
32
33| 类型                                             | 说明                                                                                   |
34| ----------------------------------------------- | -------------------------------------------------------------------------------------- |
35| Promise\<PickerResult\>                         | 使用Promise的方式获取相机选择器的处理结果。具体返回值[PickerResult](#pickerresult)           |
36
37
38**示例:**
39
40```ts
41import picker from '@ohos.multimedia.cameraPicker';
42import camera from '@ohos.multimedia.camera';
43import common from '@ohos.app.ability.common';
44import { BusinessError } from '@ohos.base';
45let mContext = getContext(this) as common.Context;
46
47async function demo() {
48  try {
49    let pickerProfile: picker.PickerProfile = {
50      cameraPosition: camera.CameraPosition.CAMERA_POSITION_BACK
51    };
52    let pickerResult: picker.PickerResult = await picker.pick(mContext,
53      [picker.PickerMediaType.PHOTO, picker.PickerMediaType.VIDEO], pickerProfile);
54    console.log("the pick pickerResult is:" + JSON.stringify(pickerResult));
55  } catch (error) {
56    let err = error as BusinessError;
57    console.error(`the pick call failed. error code: ${err.code}`);
58  }
59}
60```
61
62## PickerMediaType
63
64枚举,相机选择器的媒体类型。
65
66**系统能力:** SystemCapability.Multimedia.Camera.Core
67
68| 名称             | 值    | 说明     |
69| ----------------| ----  | ---------|
70| PHOTO           | photo | 拍照模式  |
71| VIDEO           | video | 录制模式  |
72
73
74## PickerProfile
75
76相机选择器的配置信息。
77
78**系统能力:** SystemCapability.Multimedia.Camera.Core
79
80| 名称           | 类型                               | 必填   | 说明         |
81| -------------- | --------------------------------- | ----- | ------------ |
82| cameraPosition       | [CameraPosition](js-apis-camera.md#cameraposition) | 是    | 相机的位置。   |
83| saveUri        | string                            | 否    | 保存配置信息的uri。|
84| videoDuration  | number                            | 否    | 录制的最大时长。|
85
86
87## PickerResult
88
89相机选择器的处理结果。
90
91**系统能力:** SystemCapability.Multimedia.Camera.Core
92
93| 名称           | 类型                                | 必填  | 说明                            |
94| -------------- | ---------------------------------- | ----- | -------------------------------- |
95| resultCode     | number                             | 是    | 处理的结果,成功返回0,失败返回-1。 |
96| resultUri      | string                             | 是    | 返回的uri地址。若saveUri为空,resultUri为公共媒体路径。若saveUri不为空且具备写权限,resultUri与saveUri相同。若saveUri不为空且不具备写权限,则无法获取到resultUri。|
97| mediaType      | [PickerMediaType](#pickermediatype)| 是    | 返回的媒体类型。                  |