• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.multimedia.cameraPicker (Camera Picker)
2<!--Kit: Camera Kit-->
3<!--Subsystem: Multimedia-->
4<!--Owner: @qano-->
5<!--SE: @leo_ysl-->
6<!--TSE: @xchaosioda-->
7
8The module provides APIs for an application to use the system camera to take photos or record videos, depending on the media type specified by the application. The application must call these APIs within a UIAbility. Otherwise, the camera picker cannot be started.
9
10> **NOTE**
11>
12> The initial APIs of this module are supported since API version 11. Newly added APIs will be marked with a superscript to indicate their earliest API version.
13
14## Modules to Import
15
16```ts
17import { cameraPicker as picker } from '@kit.CameraKit';
18```
19
20## cameraPicker.pick
21
22pick(context: Context, mediaTypes: Array\<PickerMediaType\>, pickerProfile: PickerProfile): Promise\<PickerResult\>
23
24Starts the camera picker and enters the corresponding mode based on the media type. This API uses a promise to return the result.
25
26**Atomic service API**: This API can be used in atomic services since API version 12.
27
28**System capability**: SystemCapability.Multimedia.Camera.Core
29
30**Parameters**
31
32| Name         | Type                                           | Mandatory| Description                         |
33| -------------- |-------------------------------------------------| ---- | ---------------------------- |
34| context        | [Context](../apis-ability-kit/js-apis-inner-application-context.md) | Yes  | Application context.                  |
35| mediaTypes     | Array\<[PickerMediaType](#pickermediatype)\>    | Yes  | Media type.                   |
36| pickerProfile  | [PickerProfile](#pickerprofile)                 | Yes  | Profile of the camera picker.           |
37
38**Return value**
39
40| Type                                            | Description                                                                                  |
41| ----------------------------------------------- | -------------------------------------------------------------------------------------- |
42| Promise\<PickerResult\>                         | Promise used to return the result, which is defined in [PickerResult](#pickerresult).       |
43
44**Example**
45
46```ts
47import { cameraPicker as picker } from '@kit.CameraKit';
48import { camera } from '@kit.CameraKit';
49import { BusinessError } from '@kit.BasicServicesKit';
50
51async function demo(context: Context) {
52  try {
53    let pickerProfile: picker.PickerProfile = {
54      cameraPosition: camera.CameraPosition.CAMERA_POSITION_BACK
55    };
56    let pickerResult: picker.PickerResult = await picker.pick(context,
57      [picker.PickerMediaType.PHOTO, picker.PickerMediaType.VIDEO], pickerProfile);
58    console.log("the pick pickerResult is:" + JSON.stringify(pickerResult));
59  } catch (error) {
60    let err = error as BusinessError;
61    console.error(`the pick call failed. error code: ${err.code}`);
62  }
63}
64```
65
66## PickerMediaType
67
68Enumerates the media types displayed in the camera picker.
69
70**Atomic service API**: This API can be used in atomic services since API version 12.
71
72**System capability**: SystemCapability.Multimedia.Camera.Core
73
74| Name            | Value   | Description    |
75| ----------------| ----  | ---------|
76| PHOTO           | photo | Photo mode. |
77| VIDEO           | video | Video mode.|
78
79
80## PickerProfile
81
82Defines the configuration information about the camera picker.
83
84**Atomic service API**: This API can be used in atomic services since API version 12.
85
86**System capability**: SystemCapability.Multimedia.Camera.Core
87
88| Name          | Type                              | Read-Only| Optional| Description        |
89| -------------- | --------------------------------- | ----- | ----- | ------------ |
90| cameraPosition       | [camera.CameraPosition](arkts-apis-camera-e.md#cameraposition) | No  | No  | Camera position.  |
91| saveUri        | string                            | No  | Yes  | URI for saving the configuration information. For details about the default value, see [File URI](../apis-core-file-kit/js-apis-file-fileuri.md#constructor10).|
92| videoDuration  | number                            | No  | Yes  | Maximum video duration, in seconds.|
93
94
95## PickerResult
96
97Defines the processing result of the camera picker.
98
99**Atomic service API**: This API can be used in atomic services since API version 12.
100
101**System capability**: SystemCapability.Multimedia.Camera.Core
102
103| Name          | Type                               | Read-Only| Optional| Description                           |
104| -------------- | ---------------------------------- | ----- | ----- | -------------------------------- |
105| resultCode     | number                             | No  | No  | Result code. The value **0** means that the processing is successful, and **-1** means that the processing fails.|
106| resultUri      | string                             | No  | No  | URI of the result. If **saveUri** is empty, **resultUri** is a public media path. If **saveUri** is not empty and the application has the write permission on the URI, the value of **resultUri** is the same as that of **saveUri**. If **saveUri** is not empty and the application does not have the write permission on the URI, **resultUri** cannot be obtained.|
107| mediaType      | [PickerMediaType](#pickermediatype)| No  | No  | Media type.                 |
108