• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Interface (Image)
2<!--Kit: Image Kit-->
3<!--Subsystem: Multimedia-->
4<!--Owner: @aulight02-->
5<!--SE: @liyang_bryan-->
6<!--TSE: @xchaosioda-->
7
8> **NOTE**
9>
10> - The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version.
11> - The initial APIs of this interface are supported since API version 9.
12
13The Image class provides APIs for basic image operations, including obtaining image information and reading and writing image data. An Image instance is returned when [readNextImage](arkts-apis-image-ImageReceiver.md#readnextimage9) and [readLatestImage](arkts-apis-image-ImageReceiver.md#readlatestimage9) are called.
14
15## Modules to Import
16
17```ts
18import { image } from '@kit.ImageKit';
19```
20
21## Properties
22
23**System capability**: SystemCapability.Multimedia.Image.Core
24
25| Name    | Type              | Read Only| Optional| Description                                              |
26| -------- | ------------------ | ---- | ---- | -------------------------------------------------- |
27| clipRect<sup>9+</sup> | [Region](arkts-apis-image-i.md#region8) | Yes  | Yes  | Image area to be cropped.                                |
28| size<sup>9+</sup>     | [Size](arkts-apis-image-i.md#size)      | Yes  | No  | Image size. If the image object stores the camera preview stream data (YUV image data), the width and height in **size** obtained correspond to those of the YUV image. If the image object stores the camera photo stream data (JPEG image data, which is already encoded), the width in **size** obtained is the JPEG data size, and the height is 1. The type of data stored in the image object depends on whether the application passes the surface ID in the receiver to a previewOutput or captureOutput object of the camera. For details about the best practices of camera preview and photo capture, see [Dual-Channel Preview (ArkTS)](../../media/camera/camera-dual-channel-preview.md) and [Photo Capture Sample (ArkTS)](../../media/camera/camera-shooting-case.md).                               |
29| format<sup>9+</sup>    | number             | Yes  | No  | Image format. For details, see [OH_NativeBuffer_Format](../apis-arkgraphics2d/capi-native-buffer-h.md#oh_nativebuffer_format).|
30| timestamp<sup>12+</sup> | number         | Yes     | No  | Image timestamp. Timestamps, measured in nanoseconds, are usually monotonically increasing. The specific meaning and baseline of these timestamps are determined by the image producer, which is the camera in the camera preview and photo scenarios. As a result, images from different producers may carry timestamps with distinct meanings and baselines, making direct comparison between them infeasible. To obtain the generation time of a photo, you can use [getImageProperty](arkts-apis-image-ImageSource.md#getimageproperty11) to read the related EXIF information.|
31
32## getComponent<sup>9+</sup>
33
34getComponent(componentType: ComponentType, callback: AsyncCallback\<Component>): void
35
36Obtains the component buffer from the Image instance based on the color component type. This API uses an asynchronous callback to return the result.
37
38**System capability**: SystemCapability.Multimedia.Image.Core
39
40**Parameters**
41
42| Name       | Type                                   | Mandatory| Description                |
43| ------------- | --------------------------------------- | ---- | -------------------- |
44| componentType | [ComponentType](arkts-apis-image-e.md#componenttype9)        | Yes  | Color component type of the image. (Currently, only **ComponentType:JPEG** is supported. The format actually returned is determined by the producer, for example, camera.)   |
45| callback      | AsyncCallback<[Component](arkts-apis-image-i.md#component9)> | Yes  | Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the component buffer obtained; otherwise, **err** is an error object. |
46
47**Example**
48
49```ts
50import { BusinessError } from '@kit.BasicServicesKit';
51
52img.getComponent(4, (err: BusinessError, component: image.Component) => {
53  if (err) {
54    console.error(`Failed to get the component.code ${err.code},message is ${err.message}`);
55  } else {
56    console.info('Succeeded in getting component.');
57  }
58})
59```
60
61## getComponent<sup>9+</sup>
62
63getComponent(componentType: ComponentType): Promise\<Component>
64
65Obtains the component buffer from the Image instance based on the color component type. This API uses a promise to return the result.
66
67**System capability**: SystemCapability.Multimedia.Image.Core
68
69**Parameters**
70
71| Name       | Type                            | Mandatory| Description            |
72| ------------- | -------------------------------- | ---- | ---------------- |
73| componentType | [ComponentType](arkts-apis-image-e.md#componenttype9) | Yes  | Color component type of the image. (Currently, only **ComponentType:JPEG** is supported. The format actually returned is determined by the producer, for example, camera.)|
74
75**Return value**
76
77| Type                             | Description                             |
78| --------------------------------- | --------------------------------- |
79| Promise<[Component](arkts-apis-image-i.md#component9)> | Promise used to return the component buffer.|
80
81**Example**
82
83```ts
84import { BusinessError } from '@kit.BasicServicesKit';
85
86img.getComponent(4).then((component: image.Component) => {
87  console.info('Succeeded in getting component.');
88}).catch((error: BusinessError) => {
89  console.error(`Failed to get the component.code ${error.code},message is ${error.message}`);
90})
91```
92
93## release<sup>9+</sup>
94
95release(callback: AsyncCallback\<void>): void
96
97Releases this Image instance. This API uses an asynchronous callback to return the result.
98
99The corresponding resources must be released before another image arrives.
100
101ArkTS supports memory reclamation. Even if the application does not call **release()**, the memory of the Image object will be released by the system. However, images usually occupy a large amount of memory. Therefore, it is recommended that the application proactively call the API to release the memory when the object is no longer required.
102
103**System capability**: SystemCapability.Multimedia.Image.Core
104
105**Parameters**
106
107| Name  | Type                | Mandatory| Description          |
108| -------- | -------------------- | ---- | -------------- |
109| callback | AsyncCallback\<void> | Yes  | Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object. |
110
111**Example**
112
113```ts
114import { BusinessError } from '@kit.BasicServicesKit';
115
116img.release((err: BusinessError) => {
117  if (err) {
118    console.error(`Failed to release the image instance.code ${err.code},message is ${err.message}`);
119  } else {
120    console.info('Succeeded in releasing the image instance.');
121  }
122})
123```
124
125## release<sup>9+</sup>
126
127release(): Promise\<void>
128
129Releases this Image instance. This API uses a promise to return the result.
130
131The corresponding resources must be released before another image arrives.
132
133ArkTS supports memory reclamation. Even if the application does not call **release()**, the memory of the Image object will be released by the system. However, images usually occupy a large amount of memory. Therefore, it is recommended that the application proactively call the API to release the memory when the object is no longer required.
134
135**System capability**: SystemCapability.Multimedia.Image.Core
136
137**Return value**
138
139| Type          | Description                 |
140| -------------- | --------------------- |
141| Promise\<void> |  Promise that returns no value.|
142
143**Example**
144
145```ts
146import { BusinessError } from '@kit.BasicServicesKit';
147
148img.release().then(() => {
149  console.info('Succeeded in releasing the image instance.');
150}).catch((error: BusinessError) => {
151  console.error(`Failed to release the image instance.code ${error.code},message is ${error.message}`);
152})
153```
154
155<!--no_check-->