• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Interface (Metadata)
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 13.
12
13The **Metadata** class is used to store image metadata. For details about the supported metadata types, see [MetadataType](arkts-apis-image-e.md#metadatatype13).
14
15## Modules to Import
16
17```ts
18import { image } from '@kit.ImageKit';
19```
20
21## getProperties<sup>13+</sup>
22
23getProperties(key: Array\<string>): Promise\<Record\<string, string | null>>
24
25Obtains the values of properties from the image's metadata. This API uses a promise to return the result. For details about how to obtain the property values, see [PropertyKey](arkts-apis-image-e.md#propertykey7), [FragmentMapPropertyKey](arkts-apis-image-e.md#fragmentmappropertykey13), and [GifPropertyKey](arkts-apis-image-e.md#gifpropertykey20).
26
27**System capability**: SystemCapability.Multimedia.Image.Core
28
29**Parameters**
30
31| Name| Type          | Mandatory| Description                    |
32| ------ | -------------- | ---- | ------------------------ |
33| key    | Array\<string> | Yes  | Names of the properties.|
34
35**Return value**
36
37| Type                                    | Description                                                        |
38| ---------------------------------------- | ------------------------------------------------------------ |
39| Promise\<Record<string, string \| null>> | Promise used to return the property values. If the operation fails, an error code is returned.|
40
41**Error codes**
42
43For details about the error codes, see [Image Error Codes](errorcode-image.md).
44
45| ID| Error Message                                                    |
46| -------- | ------------------------------------------------------------ |
47| 401      | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types;3.Parameter verification failed; |
48| 7600202  | Unsupported metadata. Possible causes: 1. Unsupported metadata type. 2. The metadata type does not match the auxiliary picture type. |
49
50**Example**
51
52```ts
53import { BusinessError } from '@kit.BasicServicesKit';
54import { image } from '@kit.ImageKit';
55
56async function GetProperties(context: Context) {
57  const resourceMgr = context.resourceManager;
58  const rawFile = await resourceMgr.getRawFileContent("exif.jpg"); // The image contains EXIF metadata.
59  let ops: image.SourceOptions = {
60    sourceDensity: 98,
61  }
62  let imageSource: image.ImageSource = image.createImageSource(rawFile.buffer as ArrayBuffer, ops);
63  let commodityPixelMap: image.PixelMap = await imageSource.createPixelMap();
64  let pictureObj: image.Picture = image.createPicture(commodityPixelMap);
65  let metadataType: image.MetadataType = image.MetadataType.EXIF_METADATA;
66  let metaData: image.Metadata | null = await pictureObj.getMetadata(metadataType);
67  if (metaData != null) {
68    await metaData.getProperties(["ImageWidth", "ImageLength"]).then((data2) => {
69      console.info('Get properties ',JSON.stringify(data2));
70    }).catch((error: BusinessError) => {
71      console.error('Get properties failed error.code: ' +JSON.stringify(error.code) + ' ,error.message:' + JSON.stringify(error.message));
72    });
73  } else {
74    console.error('Metadata is null.');
75  }
76}
77```
78
79## setProperties<sup>13+</sup>
80
81setProperties(records: Record\<string, string | null>): Promise\<void>
82
83Sets the values of properties for the image's metadata. This API uses a promise to return the result. For details about how to obtain the property values, see [PropertyKey](arkts-apis-image-e.md#propertykey7), [FragmentMapPropertyKey](arkts-apis-image-e.md#fragmentmappropertykey13), and [GifPropertyKey](arkts-apis-image-e.md#gifpropertykey20).
84
85**System capability**: SystemCapability.Multimedia.Image.Core
86
87**Parameters**
88
89| Name | Type                          | Mandatory| Description                    |
90| ------- | ------------------------------ | ---- | ------------------------ |
91| records | Record<string, string \| null> | Yes  | Array of properties and their values.|
92
93**Return value**
94
95| Type          | Description                                 |
96| -------------- | ------------------------------------- |
97| Promise\<void> | Promise that returns no value. If the operation fails, an error code is returned.|
98
99**Error codes**
100
101For details about the error codes, see [Image Error Codes](errorcode-image.md).
102
103| ID| Error Message                                                    |
104| -------- | ------------------------------------------------------------ |
105| 401      | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types;3.Parameter verification failed; |
106| 7600202  | Unsupported metadata. Possible causes: 1. Unsupported metadata type. 2. The metadata type does not match the auxiliary picture type. |
107
108**Example**
109
110```ts
111import { BusinessError } from '@kit.BasicServicesKit';
112import { image } from '@kit.ImageKit';
113
114async function SetProperties(context: Context) {
115  const resourceMgr = context.resourceManager;
116  const rawFile = await resourceMgr.getRawFileContent("exif.jpg"); // The image contains EXIF metadata.
117  let ops: image.SourceOptions = {
118    sourceDensity: 98,
119  }
120  let imageSource: image.ImageSource = image.createImageSource(rawFile.buffer as ArrayBuffer, ops);
121  let commodityPixelMap: image.PixelMap = await imageSource.createPixelMap();
122  let pictureObj: image.Picture = image.createPicture(commodityPixelMap);
123  let metadataType: image.MetadataType = image.MetadataType.EXIF_METADATA;
124  let metaData: image.Metadata | null = await pictureObj.getMetadata(metadataType);
125  if (metaData != null) {
126    let setkey: Record<string, string | null> = {
127      "ImageWidth": "200",
128      "ImageLength": "300"
129    };
130    await metaData.setProperties(setkey).then(async () => {
131      console.info('Set auxpictureobj properties success.');
132    }).catch((error: BusinessError) => {
133      console.error('Failed to set metadata Properties. code is ${error.code}, message is ${error.message}');
134    })
135  } else {
136    console.error('AuxPictureObj metadata is null. ');
137  }
138}
139```
140
141## getAllProperties<sup>13+</sup>
142
143getAllProperties(): Promise\<Record<string, string | null>>
144
145Obtains all properties and values from the image's metadata. This API uses a promise to return the result. For details about how to obtain the property values, see [PropertyKey](arkts-apis-image-e.md#propertykey7), [FragmentMapPropertyKey](arkts-apis-image-e.md#fragmentmappropertykey13), and [GifPropertyKey](arkts-apis-image-e.md#gifpropertykey20).
146
147**System capability**: SystemCapability.Multimedia.Image.Core
148
149**Return value**
150
151| Type                                    | Description                                       |
152| ---------------------------------------- | ------------------------------------------- |
153| Promise\<Record<string, string \| null>> | Promise used to return the values of all properties.|
154
155**Example**
156
157```ts
158import { BusinessError } from '@kit.BasicServicesKit';
159import { image } from '@kit.ImageKit';
160
161async function GetAllProperties(context: Context) {
162  const resourceMgr = context.resourceManager;
163  const rawFile = await resourceMgr.getRawFileContent("exif.jpg"); // The image contains EXIF metadata.
164  let ops: image.SourceOptions = {
165    sourceDensity: 98,
166  }
167  let imageSource: image.ImageSource = image.createImageSource(rawFile.buffer as ArrayBuffer, ops);
168  let commodityPixelMap: image.PixelMap = await imageSource.createPixelMap();
169  let pictureObj: image.Picture = image.createPicture(commodityPixelMap);
170  let metadataType: image.MetadataType = image.MetadataType.EXIF_METADATA;
171  let metaData: image.Metadata | null = await pictureObj.getMetadata(metadataType);
172  if (metaData != null) {
173    await metaData.getAllProperties().then((data2) => {
174      const count = Object.keys(data2).length;
175      console.info('Metadata have ', count, ' properties');
176      console.info('Get metadata all properties: ', JSON.stringify(data2));
177    }).catch((error: BusinessError) => {
178      console.error('Get metadata all properties failed error.code: ' +JSON.stringify(error.code) + ' ,error.message:' + JSON.stringify(error.message));
179    });
180  } else {
181    console.error('Metadata is null.');
182  }
183}
184```
185
186## clone<sup>13+</sup>
187
188clone(): Promise\<Metadata>
189
190Clones the metadata. This API uses a promise to return the result.
191
192**System capability**: SystemCapability.Multimedia.Image.Core
193
194**Return value**
195
196| Type                             | Description                             |
197| --------------------------------- | --------------------------------- |
198| Promise\<[Metadata](arkts-apis-image-Metadata.md)> | Promise used to return the metadata instance.|
199
200**Example**
201
202```ts
203import { BusinessError } from '@kit.BasicServicesKit';
204import { image } from '@kit.ImageKit';
205
206async function clone(context: Context) {
207  const resourceMgr = context.resourceManager;
208  const rawFile = await resourceMgr.getRawFileContent("exif.jpg"); // The image contains EXIF metadata.
209  let ops: image.SourceOptions = {
210    sourceDensity: 98,
211  }
212  let imageSource: image.ImageSource = image.createImageSource(rawFile.buffer as ArrayBuffer, ops);
213  let commodityPixelMap: image.PixelMap = await imageSource.createPixelMap();
214  let pictureObj: image.Picture = image.createPicture(commodityPixelMap);
215  let metadataType: image.MetadataType = image.MetadataType.EXIF_METADATA;
216  let metaData: image.Metadata | null = await pictureObj.getMetadata(metadataType);
217  if (metaData != null) {
218    let new_metadata: image.Metadata = await metaData.clone();
219    new_metadata.getProperties(["ImageWidth"]).then((data1) => {
220      console.info('Clone new_metadata and get Properties.', JSON.stringify(data1));
221    }).catch((err: BusinessError) => {
222      console.error('Clone new_metadata failed.', JSON.stringify(err));
223    });
224  } else {
225    console.error('Metadata is null.');
226  }
227}
228```
229