• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Interface (ImageSource)
2<!--Kit: Image Kit-->
3<!--Subsystem: Multimedia-->
4<!--Owner: @aulight02-->
5<!--Designer: @liyang_bryan-->
6<!--Tester: @xchaosioda-->
7<!--Adviser: @zengyawen-->
8
9> **说明:**
10>
11> 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
12
13ImageSource类,用于获取图片相关信息。在调用ImageSource的方法前,需要先通过[createImageSource](arkts-apis-image-f.md#imagecreateimagesource)构建一个ImageSource实例。
14
15## 导入模块
16
17```ts
18import { image } from '@kit.ImageKit';
19```
20
21## 属性
22
23**系统能力:** SystemCapability.Multimedia.Image.ImageSource
24
25| 名称             | 类型           | 只读 | 可选 | 说明                                                         |
26| ---------------- | -------------- | ---- | ---- | ------------------------------------------------------------ |
27| supportedFormats | Array\<string> | 是   | 否   | 支持的图片格式,包括:png,jpeg,bmp,gif,webp,dng,heic<sup>12+</sup>(不同硬件设备支持情况不同)。 |
28
29## getImageInfo
30
31getImageInfo(index: number, callback: AsyncCallback\<ImageInfo>): void
32
33获取指定序号的图片信息,使用callback形式返回图片信息。
34
35**卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。
36
37**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
38
39**系统能力:** SystemCapability.Multimedia.Image.ImageSource
40
41**参数:**
42
43| 参数名   | 类型                                   | 必填 | 说明                                     |
44| -------- | -------------------------------------- | ---- | ---------------------------------------- |
45| index    | number                                 | 是   | 创建ImageSource时的序号。默认值为0,表示第一张图片。当取值为N时,表示第N+1张图片。单帧图片场景中index取值只能为0,动图等多帧图片场景中index的取值范围为:0~(帧数-1)。                   |
46| callback | AsyncCallback<[ImageInfo](arkts-apis-image-i.md#imageinfo)> | 是   | 回调函数。当获取图片信息成功,err为undefined,data为获取到的图片信息;否则为错误对象。 |
47
48**示例:**
49
50```ts
51import { BusinessError } from '@kit.BasicServicesKit';
52
53async function GetImageInfo(imageSourceObj : image.ImageSource) {
54  imageSourceObj.getImageInfo(0, (error: BusinessError, imageInfo: image.ImageInfo) => {
55    if (error) {
56      console.error(`Failed to obtain the image information.code is ${error.code}, message is ${error.message}`);
57    } else {
58      console.info('Succeeded in obtaining the image information.');
59    }
60  })
61}
62```
63
64## getImageInfo
65
66getImageInfo(callback: AsyncCallback\<ImageInfo>): void
67
68获取图片信息,使用callback形式返回图片信息。
69
70**卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。
71
72**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
73
74**系统能力:** SystemCapability.Multimedia.Image.ImageSource
75
76**参数:**
77
78| 参数名   | 类型                                   | 必填 | 说明                                     |
79| -------- | -------------------------------------- | ---- | ---------------------------------------- |
80| callback | AsyncCallback<[ImageInfo](arkts-apis-image-i.md#imageinfo)> | 是   | 回调函数。当获取图片信息成功,err为undefined,data为获取到的图片信息;否则为错误对象。 |
81
82**示例:**
83
84```ts
85import { BusinessError } from '@kit.BasicServicesKit';
86
87async function GetImageInfo(imageSourceObj : image.ImageSource) {
88  imageSourceObj.getImageInfo((err: BusinessError, imageInfo: image.ImageInfo) => {
89    if (err) {
90      console.error(`Failed to obtain the image information.code is ${err.code}, message is ${err.message}`);
91    } else {
92      console.info('Succeeded in obtaining the image information.');
93    }
94  })
95}
96```
97
98## getImageInfo
99
100getImageInfo(index?: number): Promise\<ImageInfo>
101
102获取图片信息,使用Promise形式返回图片信息。
103
104**卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。
105
106**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
107
108**系统能力:** SystemCapability.Multimedia.Image.ImageSource
109
110**参数:**
111
112| 参数名| 类型   | 必填 | 说明                                  |
113| ----- | ------ | ---- | ------------------------------------- |
114| index | number | 否   | 创建ImageSource时的序号。默认值为0,表示第一张图片。当取值为N时,表示第N+1张图片。单帧图片场景中index取值只能为0,动图等多帧图片场景中index的取值范围为:0~(帧数-1)。 |
115
116**返回值:**
117
118| 类型                             | 说明                   |
119| -------------------------------- | ---------------------- |
120| Promise<[ImageInfo](arkts-apis-image-i.md#imageinfo)> | Promise对象,返回获取到的图片信息。 |
121
122**示例:**
123
124```ts
125import { BusinessError } from '@kit.BasicServicesKit';
126
127async function GetImageInfo(imageSourceObj : image.ImageSource) {
128  imageSourceObj.getImageInfo(0)
129    .then((imageInfo: image.ImageInfo) => {
130      console.info('Succeeded in obtaining the image information.');
131    }).catch((error: BusinessError) => {
132      console.error(`Failed to obtain the image information.code is ${error.code}, message is ${error.message}`);
133    })
134}
135```
136
137## getImageInfoSync<sup>12+</sup>
138
139getImageInfoSync(index?: number): ImageInfo
140
141获取指定序号的图片信息,使用同步形式返回图片信息。
142
143**系统能力:** SystemCapability.Multimedia.Image.ImageSource
144
145**参数:**
146
147| 参数名| 类型   | 必填 | 说明                                  |
148| ----- | ------ | ---- | ------------------------------------- |
149| index | number | 否   | 创建ImageSource时的序号。默认值为0,表示第一张图片。当取值为N时,表示第N+1张图片。单帧图片场景中index取值只能为0,动图等多帧图片场景中index的取值范围为:0~(帧数-1)。 |
150
151**返回值:**
152
153| 类型                             | 说明                   |
154| -------------------------------- | ---------------------- |
155| [ImageInfo](arkts-apis-image-i.md#imageinfo) | 同步返回获取到的图片信息。 |
156
157**示例:**
158
159```ts
160function GetImageInfoSync(context : Context) {
161  // 此处'test.jpg'仅作示例,请开发者自行替换,否则imageSource创建失败会导致后续无法正常执行。
162  let filePath: string = context.filesDir + "/test.jpg";
163  let imageSource = image.createImageSource(filePath);
164  let imageInfo = imageSource.getImageInfoSync(0);
165  if (imageInfo == undefined) {
166    console.error('Failed to obtain the image information.');
167  } else {
168    console.info('Succeeded in obtaining the image information.');
169    console.info('imageInfo.size.height:' + imageInfo.size.height);
170    console.info('imageInfo.size.width:' + imageInfo.size.width);
171  }
172}
173```
174
175## getImageProperty<sup>11+</sup>
176
177getImageProperty(key:PropertyKey, options?: ImagePropertyOptions): Promise\<string>
178
179获取图片中给定索引处图像的指定属性键的值,用Promise形式返回结果,仅支持JPEG、PNG和HEIF<sup>12+</sup>(不同硬件设备支持情况不同)文件,且需要包含exif信息。其中可以通过supportedFormats属性查询是否支持HEIF格式的exif读写。
180
181**系统能力:** SystemCapability.Multimedia.Image.ImageSource
182
183**参数:**
184
185| 参数名  | 类型                                                 | 必填 | 说明                                 |
186| ------- | ---------------------------------------------------- | ---- | ------------------------------------ |
187| key     | [PropertyKey](arkts-apis-image-e.md#propertykey7)                                               | 是   | 图片属性名。                         |
188| options | [ImagePropertyOptions](arkts-apis-image-i.md#imagepropertyoptions11) | 否   | 图片属性,包括图片序号与默认属性值。 |
189
190**返回值:**
191
192| 类型             | 说明                                                              |
193| ---------------- | ----------------------------------------------------------------- |
194| Promise\<string> | Promise对象,返回图片属性值,如获取失败则返回属性默认值。 |
195
196**错误码:**
197
198以下错误码的详细介绍请参见[Image错误码](errorcode-image.md)。
199
200| 错误码ID | 错误信息 |
201| ------- | --------------------------------------------|
202| 401  | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types;3.Parameter verification failed;              |
203| 62980096 | The operation failed. Possible cause: 1.Image upload exception. 2. Decoding process exception. 3. Insufficient memory.             |
204| 62980103 | The image data is not supported.         |
205| 62980110 | The image source data is incorrect.      |
206| 62980111 | The image source data is incomplete. |
207| 62980112 | The image format does not match.       |
208| 62980113| Unknown image format.The image data provided is not in a recognized or supported format, or it may be occorrupted.            |
209| 62980115 | Invalid image parameter.      |
210| 62980118 | Failed to create the image plugin.   |
211| 62980122 | Failed to decode the image header.   |
212| 62980123| The image does not support EXIF decoding. |
213| 62980135| The EXIF value is invalid.             |
214
215**示例:**
216
217```ts
218import { BusinessError } from '@kit.BasicServicesKit';
219
220async function GetImageProperty(imageSourceObj : image.ImageSource) {
221  let options: image.ImagePropertyOptions = { index: 0, defaultValue: '9999' }
222  imageSourceObj.getImageProperty(image.PropertyKey.BITS_PER_SAMPLE, options)
223    .then((data: string) => {
224      console.info('Succeeded in getting the value of the specified attribute key of the image.');
225    }).catch((error: BusinessError) => {
226    console.error(`Failed to get the value of the specified attribute key of the image, error.code ${error.code}, error.message ${error.message}`);
227  })
228}
229```
230
231## getImageProperties<sup>12+</sup>
232
233getImageProperties(key: Array&#60;PropertyKey&#62;): Promise<Record<PropertyKey, string|null>>
234
235批量获取图片中的指定属性键的值,用Promise形式返回结果。仅支持JPEG、PNG和HEIF(不同硬件设备支持情况不同)文件,且需要包含exif信息。其中可以通过supportedFormats属性查询是否支持HEIF格式的exif读写。
236
237**系统能力:** SystemCapability.Multimedia.Image.ImageSource
238
239**参数:**
240
241| 参数名  | 类型                                                 | 必填 | 说明                                 |
242| ------- | ---------------------------------------------------- | ---- | ------------------------------------ |
243| key     | Array\<[PropertyKey](arkts-apis-image-e.md#propertykey7)>                                 | 是   | 图片属性名的数组。                         |
244
245**返回值:**
246
247| 类型             | 说明                                                              |
248| ---------------- | ----------------------------------------------------------------- |
249| Promise\<Record<[PropertyKey](arkts-apis-image-e.md#propertykey7), string \| null>> | Promise对象,返回图片属性值,如获取失败则返回null。 |
250
251**错误码:**
252
253以下错误码的详细介绍请参见[Image错误码](errorcode-image.md)。
254
255| 错误码ID | 错误信息 |
256| ------- | --------------------------------------------|
257| 401  | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types;3.Parameter verification failed;     |
258| 62980096| The operation failed. Possible cause: 1.Image upload exception. 2. Decoding process exception. 3. Insufficient memory.             |
259| 62980110| The image source data is incorrect.            |
260| 62980113| Unknown image format.The image data provided is not in a recognized or supported format, or it may be occorrupted.            |
261| 62980116| Failed to decode the image.            |
262
263**示例:**
264
265```ts
266import { BusinessError } from '@kit.BasicServicesKit';
267
268async function GetImageProperties(imageSourceObj : image.ImageSource) {
269  let key = [image.PropertyKey.IMAGE_WIDTH, image.PropertyKey.IMAGE_LENGTH];
270  imageSourceObj.getImageProperties(key).then((data) => {
271    console.info(JSON.stringify(data));
272  }).catch((err: BusinessError) => {
273    console.error(JSON.stringify(err));
274  });
275}
276```
277
278## getImagePropertySync<sup>20+</sup>
279
280getImagePropertySync(key:PropertyKey): string
281
282获取图片exif指定属性键的值,使用同步形式返回结果。
283
284>**说明:**
285>
286> 该方法仅支持JPEG、PNG和HEIF(不同硬件设备支持情况不同)文件,且需要包含exif信息。
287>
288> exif信息是图片的元数据,包含拍摄时间、相机型号、光圈、焦距、ISO等。
289
290
291**系统能力:** SystemCapability.Multimedia.Image.ImageSource
292
293**参数:**
294
295| 参数名  | 类型                                                 | 必填 | 说明                                 |
296| ------- | ---------------------------------------------------- | ---- | ------------------------------------ |
297| key     | [PropertyKey](arkts-apis-image-e.md#propertykey7)                                               | 是   | 图片属性名。                         |
298
299**返回值:**
300
301| 类型             | 说明                                                              |
302| ---------------- | ----------------------------------------------------------------- |
303| string | 返回图片exif中指定属性键的值(如获取失败则返回属性默认值),各个数据值作用请参考[PropertyKey](arkts-apis-image-e.md#propertykey7)。 |
304
305**错误码:**
306
307以下错误码的详细介绍请参见[Image错误码](errorcode-image.md)。
308| 错误码ID | 错误信息 |
309| ------- | --------------------------------------------|
310| 7700101  | Bad source. e.g.,1. Image has invalid width or height. 2. Image source incomplete. 3. Read image data failed. 4. Codec create failed.|
311| 7700102 | Unsupported MIME type.|
312| 7700202 | Unsupported metadata. For example, key is not supported.|
313
314**示例:**
315
316```ts
317function GetImagePropertySync(context : Context) {
318  let resourceMgr = context.resourceManager;
319  if (resourceMgr == null) {
320    return;
321  }
322  let fd = resourceMgr.getRawFdSync("example.jpg");
323
324  const imageSourceObj = image.createImageSource(fd);
325  console.info("getImagePropertySync");
326  let bits_per_sample = imageSourceObj.getImagePropertySync(image.PropertyKey.BITS_PER_SAMPLE);
327  console.info("bits_per_sample : " + bits_per_sample);
328}
329```
330
331## modifyImageProperty<sup>11+</sup>
332
333modifyImageProperty(key: PropertyKey, value: string): Promise\<void>
334
335通过指定的键修改图片属性的值,使用Promise形式返回结果,仅支持JPEG、PNG和HEIF<sup>12+</sup>(不同硬件设备支持情况不同)文件,且需要包含exif信息。其中可以通过supportedFormats属性查询是否支持HEIF格式的exif读写。
336
337> **说明:**
338>
339> 调用modifyImageProperty修改属性会改变属性字节长度,使用buffer创建的ImageSource调用modifyImageProperty会导致buffer内容覆盖,目前buffer创建的ImageSource不支持调用此接口,请改用fd或path创建的ImageSource。
340
341**系统能力:** SystemCapability.Multimedia.Image.ImageSource
342
343**参数:**
344
345| 参数名  | 类型   | 必填 | 说明         |
346| ------- | ------ | ---- | ------------ |
347| key     | [PropertyKey](arkts-apis-image-e.md#propertykey7)   | 是   | 图片属性名。 |
348| value   | string | 是   | 属性值。     |
349
350**返回值:**
351
352| 类型           | 说明                        |
353| -------------- | --------------------------- |
354| Promise\<void> | Promise对象。无返回结果的Promise对象。 |
355
356**错误码:**
357
358以下错误码的详细介绍请参见[Image错误码](errorcode-image.md)。
359
360| 错误码ID | 错误信息 |
361| ------- | --------------------------------------------|
362| 401  | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types;    |
363| 62980123| The image does not support EXIF decoding.             |
364| 62980133| The EXIF data is out of range.             |
365| 62980135| The EXIF value is invalid.             |
366| 62980146| The EXIF data failed to be written to the file.        |
367
368**示例:**
369
370```ts
371import { BusinessError } from '@kit.BasicServicesKit';
372
373async function ModifyImageProperty(imageSourceObj : image.ImageSource) {
374  imageSourceObj.modifyImageProperty(image.PropertyKey.IMAGE_WIDTH, "120").then(() => {
375    imageSourceObj.getImageProperty(image.PropertyKey.IMAGE_WIDTH).then((width: string) => {
376      console.info(`ImageWidth is :${width}`);
377    }).catch((error: BusinessError) => {
378      console.error(`Failed to get the Image Width, error.code ${error.code}, error.message ${error.message}`);
379    })
380  }).catch((error: BusinessError) => {
381    console.error(`Failed to modify the Image Width, error.code ${error.code}, error.message ${error.message}`);
382  })
383}
384```
385
386## modifyImageProperties<sup>12+</sup>
387
388modifyImageProperties(records: Record<PropertyKey, string|null>): Promise\<void>
389
390批量通过指定的键修改图片属性的值,使用Promise形式返回结果。仅支持JPEG、PNG和HEIF(不同硬件设备支持情况不同)文件,且需要包含exif信息。其中可以通过supportedFormats属性查询是否支持HEIF格式的exif读写。
391
392> **说明:**
393>
394> 调用modifyImageProperties修改属性会改变属性字节长度,使用buffer创建的ImageSource调用modifyImageProperties会导致buffer内容覆盖,目前buffer创建的ImageSource不支持调用此接口,请改用fd或path创建的ImageSource。
395
396**系统能力:** SystemCapability.Multimedia.Image.ImageSource
397
398**参数:**
399
400| 参数名  | 类型   | 必填 | 说明         |
401| ------- | ------ | ---- | ------------ |
402| records     | Record<[PropertyKey](arkts-apis-image-e.md#propertykey7), string \| null>   | 是   | 包含图片属性名和属性值的数组。 |
403
404**返回值:**
405
406| 类型           | 说明                        |
407| -------------- | --------------------------- |
408| Promise\<void> |  Promise对象。无返回结果的Promise对象。 |
409
410**错误码:**
411
412以下错误码的详细介绍请参见[Image错误码](errorcode-image.md)。
413
414| 错误码ID | 错误信息 |
415| ------- | --------------------------------------------|
416| 401  | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types;3.Parameter verification failed;      |
417| 62980123| The image does not support EXIF decoding.             |
418| 62980135| The EXIF value is invalid.             |
419| 62980146| The EXIF data failed to be written to the file.             |
420
421**示例:**
422
423```ts
424import { BusinessError } from '@kit.BasicServicesKit';
425
426async function ModifyImageProperties(imageSourceObj : image.ImageSource) {
427  let keyValues: Record<PropertyKey, string|null> = {
428    [image.PropertyKey.IMAGE_WIDTH] : "1024",
429    [image.PropertyKey.IMAGE_LENGTH] : "1024"
430  };
431  let checkKey = [image.PropertyKey.IMAGE_WIDTH, image.PropertyKey.IMAGE_LENGTH];
432  imageSourceObj.modifyImageProperties(keyValues).then(() => {
433    imageSourceObj.getImageProperties(checkKey).then((data) => {
434      console.info(`Image Width and Image Height:${data}`);
435    }).catch((err: BusinessError) => {
436      console.error(`Failed to modify the Image Width and Image Height, error.code ${err.code}, error.message ${err.message}`);
437    });
438  }).catch((err: BusinessError) => {
439    console.error(`Failed to modify the Image Width and Image Height, error.code ${err.code}, error.message ${err.message}`);
440  });
441}
442```
443
444## updateData<sup>9+</sup>
445
446updateData(buf: ArrayBuffer, isFinished: boolean, offset: number, length: number): Promise\<void>
447
448更新增量数据,使用Promise形式返回结果。
449
450**系统能力:** SystemCapability.Multimedia.Image.ImageSource
451
452**参数:**
453
454| 参数名     | 类型        | 必填 | 说明         |
455| ---------- | ----------- | ---- | ------------ |
456| buf        | ArrayBuffer         | 是   | 存放增量数据的buffer。           |
457| isFinished | boolean             | 是   | true表示数据更新完成,当前buffer内存放最后一段数据;false表示数据还未更新完成,需要继续更新。|
458| offset      | number              | 是   | 即当前buffer中的数据首地址,相对于整个图片文件首地址的偏移量。单位:字节。             |
459| length     | number              | 是   | 当前buffer的长度。单位:字节。            |
460
461**返回值:**
462
463| 类型           | 说明                       |
464| -------------- | -------------------------- |
465| Promise\<void> | Promise对象。无返回结果的Promise对象。|
466
467**示例:**
468
469```ts
470import { BusinessError } from '@kit.BasicServicesKit';
471
472async function UpdateDatay(imageSourceObj : image.ImageSource) {
473  const array: ArrayBuffer = new ArrayBuffer(100);
474  imageSourceObj.updateData(array, false, 0, 10).then(() => {
475    console.info('Succeeded in updating data.');
476  }).catch((err: BusinessError) => {
477    console.error(`Failed to update data.code is ${err.code},message is ${err.message}`);
478  })
479}
480```
481
482
483## updateData<sup>9+</sup>
484
485updateData(buf: ArrayBuffer, isFinished: boolean, offset: number, length: number, callback: AsyncCallback\<void>): void
486
487更新增量数据,callback形式返回结果。
488
489**系统能力:** SystemCapability.Multimedia.Image.ImageSource
490
491**参数:**
492
493| 参数名     | 类型                | 必填 | 说明                 |
494| ---------- | ------------------- | ---- | -------------------- |
495| buf        | ArrayBuffer         | 是   | 存放增量数据的buffer。           |
496| isFinished | boolean             | 是   | true表示数据更新完成,当前buffer内存放最后一段数据;false表示数据还未更新完成,需要继续更新。|
497| offset      | number              | 是   | 即当前buffer中的数据首地址,相对于整个图片文件首地址的偏移量。单位:字节。             |
498| length     | number              | 是   | 当前buffer的长度。单位:字节。            |
499| callback   | AsyncCallback\<void> | 是   |  回调函数,当更新增量数据成功,err为undefined,否则为错误对象。 |
500
501**示例:**
502
503```ts
504import { BusinessError } from '@kit.BasicServicesKit';
505
506async function UpdateDatay(imageSourceObj : image.ImageSource) {
507  const array: ArrayBuffer = new ArrayBuffer(100);
508  imageSourceObj.updateData(array, false, 0, 10, (err: BusinessError) => {
509    if (err) {
510      console.error(`Failed to update data.code is ${err.code},message is ${err.message}`);
511    } else {
512      console.info('Succeeded in updating data.');
513    }
514  })
515}
516```
517
518## createPicture<sup>13+</sup>
519
520createPicture(options?: DecodingOptionsForPicture): Promise\<Picture>
521
522通过图片解码参数创建Picture对象,使用Promise形式返回。
523
524**系统能力:** SystemCapability.Multimedia.Image.ImageSource
525
526**参数:**
527
528| 参数名  | 类型                                                   | 必填 | 说明       |
529| ------- | ------------------------------------------------------ | ---- | ---------- |
530| options | [DecodingOptionsForPicture](arkts-apis-image-i.md#decodingoptionsforpicture13) | 否   | 解码参数。 |
531
532**返回值:**
533
534| 类型                         | 说明                       |
535| ---------------------------- | -------------------------- |
536| Promise\<[Picture](arkts-apis-image-Picture.md)> | Promise对象,返回Picture。 |
537
538**错误码:**
539
540以下错误码的详细介绍请参见[Image错误码](errorcode-image.md)。
541
542| 错误码ID | 错误信息                                                     |
543| -------- | ------------------------------------------------------------ |
544| 401      | Parameter error.Possible causes: 1.Mandatory parameters are left unspecified.2.Incorrect parameter types; 3.Parameter verification failed. |
545| 7700301  | Failed to decode image.                                      |
546
547**示例:**
548
549```ts
550async function CreatePicture(imageSourceObj : image.ImageSource) {
551  let options: image.DecodingOptionsForPicture = {
552    desiredAuxiliaryPictures: [image.AuxiliaryPictureType.GAINMAP] // GAINMAP为需要解码的辅助图类型。
553  };
554  let pictureObj: image.Picture = await imageSourceObj.createPicture(options);
555  if (pictureObj != null) {
556    console.info('Create picture succeeded');
557  } else {
558    console.error('Create picture failed');
559  }
560}
561```
562
563## createPictureAtIndex<sup>20+</sup>
564
565createPictureAtIndex(index: number): Promise\<Picture>
566
567通过指定序号的图片(目前仅支持GIF格式)创建Picture对象。使用Promise异步回调。
568
569**系统能力:** SystemCapability.Multimedia.Image.ImageSource
570
571**参数:**
572
573| 参数名 | 类型    | 必填 | 说明                                              |
574| ------ | ------ | ---- | ------------------------------------------------ |
575| index  | number | 是   | 解码图片序号。图片序号有效的取值范围为:[0,帧数-1]。 |
576
577**返回值:**
578
579| 类型                         | 说明                         |
580| ---------------------------- | ---------------------------- |
581| Promise\<[Picture](arkts-apis-image-Picture.md)> | Promise对象,返回Picture。 |
582
583**错误码:**
584
585以下错误码的详细介绍请参见[Image错误码](errorcode-image.md)。
586
587| 错误码ID | 错误信息                                                      |
588| -------- | ------------------------------------------------------------ |
589| 7700101  | Bad source. |
590| 7700102  | Unsupported MIME type. |
591| 7700103  | Image too large. |
592| 7700203  | Unsupported options. For example, index is invalid. |
593| 7700301  | Decoding failed. |
594
595**示例:**
596
597```ts
598async function CreatePictures(imageSourceObj : image.ImageSource) {
599  let frameCount: number = await imageSourceObj.getFrameCount();
600  for (let index = 0; index < frameCount; index++) {
601    try {
602      let pictureObj: image.Picture = await imageSourceObj.createPictureAtIndex(index);
603      console.info('Create picture succeeded for frame: ' + index);
604    } catch (e) {
605      console.error('Create picture failed for frame: ' + index);
606    }
607  }
608}
609```
610
611## createPixelMap<sup>7+</sup>
612
613createPixelMap(options?: DecodingOptions): Promise\<PixelMap>
614
615通过图片解码参数创建PixelMap对象。
616
617从API version 15开始,推荐使用[createPixelMapUsingAllocator](#createpixelmapusingallocator15),该接口可以指定输出pixelMap的内存类型[AllocatorType](arkts-apis-image-e.md#allocatortype15),详情请参考[申请图片解码内存(ArkTS)](../../media/image/image-allocator-type.md)。
618
619**卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。
620
621**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
622
623**系统能力:** SystemCapability.Multimedia.Image.ImageSource
624
625**参数:**
626
627| 参数名  | 类型                                 | 必填 | 说明       |
628| ------- | ------------------------------------ | ---- | ---------- |
629| options | [DecodingOptions](arkts-apis-image-i.md#decodingoptions7) | 否   | 解码参数。 |
630
631**返回值:**
632
633| 类型                             | 说明                  |
634| -------------------------------- | --------------------- |
635| Promise\<[PixelMap](arkts-apis-image-PixelMap.md)> | Promise对象,返回PixelMap。 |
636
637**示例:**
638
639```ts
640import { BusinessError } from '@kit.BasicServicesKit';
641
642async function CreatePixelMap(imageSourceObj : image.ImageSource) {
643  imageSourceObj.createPixelMap().then((pixelMap: image.PixelMap) => {
644    console.info('Succeeded in creating pixelMap object through image decoding parameters.');
645  }).catch((error: BusinessError) => {
646    console.error(`Failed to create pixelMap object through image decoding parameters, error.code ${error.code}, error.message ${error.message}`);
647  })
648}
649```
650
651## createPixelMap<sup>7+</sup>
652
653createPixelMap(callback: AsyncCallback\<PixelMap>): void
654
655通过默认参数创建PixelMap对象,使用callback形式返回结果。
656
657从API version 15开始,推荐使用[createPixelMapUsingAllocator](#createpixelmapusingallocator15),该接口可以指定输出pixelMap的内存类型[AllocatorType](arkts-apis-image-e.md#allocatortype15),详情请参考[申请图片解码内存(ArkTS)](../../media/image/image-allocator-type.md)。
658
659**卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。
660
661**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
662
663**系统能力:** SystemCapability.Multimedia.Image.ImageSource
664
665**参数:**
666
667| 参数名     | 类型                                  | 必填 | 说明                       |
668| -------- | ------------------------------------- | ---- | -------------------------- |
669| callback | AsyncCallback<[PixelMap](arkts-apis-image-PixelMap.md)> | 是   | 回调函数,当创建PixelMap对象成功,err为undefined,data为获取到的PixelMap对象;否则为错误对象。 |
670
671**示例:**
672
673```ts
674import { BusinessError } from '@kit.BasicServicesKit';
675
676async function CreatePixelMap(imageSourceObj : image.ImageSource) {
677  imageSourceObj.createPixelMap((err: BusinessError, pixelMap: image.PixelMap) => {
678    if (err) {
679      console.error(`Failed to create pixelMap.code is ${err.code},message is ${err.message}`);
680    } else {
681      console.info('Succeeded in creating pixelMap object.');
682    }
683  })
684}
685```
686
687## createPixelMap<sup>7+</sup>
688
689createPixelMap(options: DecodingOptions, callback: AsyncCallback\<PixelMap>): void
690
691通过图片解码参数创建PixelMap对象。
692
693从API version 15开始,推荐使用[createPixelMapUsingAllocator](#createpixelmapusingallocator15),该接口可以指定输出pixelMap的内存类型[AllocatorType](arkts-apis-image-e.md#allocatortype15),详情请参考[申请图片解码内存(ArkTS)](../../media/image/image-allocator-type.md)。
694
695**卡片能力:** 从API version 12开始,该接口支持在ArkTS卡片中使用。
696
697**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
698
699**系统能力:** SystemCapability.Multimedia.Image.ImageSource
700
701**参数:**
702
703| 参数名   | 类型                                  | 必填 | 说明                       |
704| -------- | ------------------------------------- | ---- | -------------------------- |
705| options  | [DecodingOptions](arkts-apis-image-i.md#decodingoptions7)  | 是   | 解码参数。                 |
706| callback | AsyncCallback<[PixelMap](arkts-apis-image-PixelMap.md)> | 是   | 回调函数,当创建PixelMap对象成功,err为undefined,data为获取到的PixelMap对象;否则为错误对象。 |
707
708**示例:**
709
710```ts
711import { BusinessError } from '@kit.BasicServicesKit';
712
713async function CreatePixelMap(imageSourceObj : image.ImageSource) {
714  let decodingOptions: image.DecodingOptions = {
715    sampleSize: 1,
716    editable: true,
717    desiredSize: { width: 1, height: 2 },
718    rotate: 10,
719    desiredPixelFormat: image.PixelMapFormat.RGBA_8888,
720    desiredRegion: { size: { width: 1, height: 2 }, x: 0, y: 0 },
721    // 若解码接口同时传入了desiredSize参数与desiredRegion参数,需进一步传入cropAndScaleStrategy参数指定缩放与裁剪的先后顺序,推荐设置CROP_FIRST。
722    cropAndScaleStrategy: image.CropAndScaleStrategy.CROP_FIRST,
723    index: 0
724  };
725  imageSourceObj.createPixelMap(decodingOptions, (err: BusinessError, pixelMap: image.PixelMap) => {
726    if (err) {
727      console.error(`Failed to create pixelMap.code is ${err.code},message is ${err.message}`);
728    } else {
729      console.info('Succeeded in creating pixelMap object.');
730    }
731  })
732}
733```
734
735## createPixelMapSync<sup>12+</sup>
736
737createPixelMapSync(options?: DecodingOptions): PixelMap
738
739通过图片解码参数同步创建PixelMap对象。
740
741从API version 15开始,推荐使用[createPixelMapUsingAllocatorSync](#createpixelmapusingallocatorsync15),该接口可以指定输出pixelMap的内存类型[AllocatorType](arkts-apis-image-e.md#allocatortype15),详情请参考[申请图片解码内存(ArkTS)](../../media/image/image-allocator-type.md)。
742
743**系统能力:** SystemCapability.Multimedia.Image.ImageSource
744
745**参数:**
746
747| 参数名   | 类型                                  | 必填 | 说明                       |
748| -------- | ------------------------------------- | ---- | -------------------------- |
749| options  | [DecodingOptions](arkts-apis-image-i.md#decodingoptions7)  | 否   | 解码参数。                 |
750
751**返回值:**
752
753| 类型                             | 说明                  |
754| -------------------------------- | --------------------- |
755| [PixelMap](arkts-apis-image-PixelMap.md) | 用于同步返回创建结果。 |
756
757**示例:**
758
759```ts
760function CreatePixelMapSync(context : Context) {
761  // 此处'test.jpg'仅作示例,请开发者自行替换,否则imageSource创建失败会导致后续无法正常执行。
762  let filePath: string = context.filesDir + "/test.jpg";
763  let imageSource = image.createImageSource(filePath);
764  let decodingOptions: image.DecodingOptions = {
765    sampleSize: 1,
766    editable: true,
767    desiredSize: { width: 1, height: 2 },
768    rotate: 10,
769    desiredPixelFormat: image.PixelMapFormat.RGBA_8888,
770    desiredRegion: { size: { width: 1, height: 2 }, x: 0, y: 0 },
771    // 若解码接口同时传入了desiredSize参数与desiredRegion参数,需进一步传入cropAndScaleStrategy参数指定缩放与裁剪的先后顺序,推荐设置CROP_FIRST。
772    cropAndScaleStrategy: image.CropAndScaleStrategy.CROP_FIRST,
773    index: 0
774  };
775  let pixelmap = imageSource.createPixelMapSync(decodingOptions);
776  if (pixelmap != undefined) {
777    console.info('Succeeded in creating pixelMap object.');
778  } else {
779    console.error('Failed to create pixelMap.');
780  }
781}
782```
783
784## createPixelMapList<sup>10+</sup>
785
786createPixelMapList(options?: DecodingOptions): Promise<Array\<PixelMap>>
787
788通过图片解码参数创建PixelMap数组。针对动图如Gif、Webp,此接口返回每帧图片数据;针对静态图,此接口返回唯一的一帧图片数据。
789
790> **注意:**
791> 此接口会一次性解码全部帧,当帧数过多或单帧图像过大时,会占用较大内存,造成系统内存紧张,此种情况推荐使用Image组件显示动图,Image组件采用逐帧解码,占用内存比此接口少。
792
793**系统能力:** SystemCapability.Multimedia.Image.ImageSource
794
795**参数:**
796
797| 参数名   | 类型                                  | 必填 | 说明                       |
798| -------- | ------------------------------------- | ---- | -------------------------- |
799| options  | [DecodingOptions](arkts-apis-image-i.md#decodingoptions7)  | 否   | 解码参数。                 |
800
801**返回值:**
802
803| 类型                             | 说明                  |
804| -------------------------------- | --------------------- |
805| Promise<Array<[PixelMap](arkts-apis-image-PixelMap.md)>> | 异步返回PixelMap数组。 |
806
807**错误码:**
808
809以下错误码的详细介绍请参见[Image错误码](errorcode-image.md)。
810
811| 错误码ID | 错误信息 |
812| ------- | --------------------------------------------|
813| 62980096| The operation failed. Possible cause: 1.Image upload exception. 2. Decoding process exception. 3. Insufficient memory.              |
814| 62980099 | The shared memory data is abnormal. |
815| 62980101 | The image data is abnormal. |
816| 62980103| The image data is not supported.             |
817| 62980106 | The image data is too large. This status code is thrown when an error occurs during the process of checking size. |
818| 62980109 | Failed to crop the image. |
819| 62980111| The image source data is incomplete.           |
820| 62980115 | Invalid image parameter. |
821| 62980116 | Failed to decode the image. |
822| 62980118| Failed to create the image plugin.             |
823| 62980137 | Invalid media operation. |
824| 62980173 | The DMA memory does not exist. |
825| 62980174 | The DMA memory data is abnormal. |
826
827**示例:**
828
829```ts
830import { BusinessError } from '@kit.BasicServicesKit';
831
832async function CreatePixelMapList(imageSourceObj : image.ImageSource) {
833  let decodeOpts: image.DecodingOptions = {
834    sampleSize: 1,
835    editable: true,
836    desiredSize: { width: 198, height: 202 },
837    rotate: 0,
838    desiredPixelFormat: image.PixelMapFormat.RGBA_8888,
839    index: 0,
840  };
841  imageSourceObj.createPixelMapList(decodeOpts).then((pixelMapList: Array<image.PixelMap>) => {
842    console.info('Succeeded in creating pixelMapList object.');
843  }).catch((err: BusinessError) => {
844    console.error(`Failed to create pixelMapList object, error code is ${err}`);
845  })
846}
847```
848
849## createPixelMapList<sup>10+</sup>
850
851createPixelMapList(callback: AsyncCallback<Array\<PixelMap>>): void
852
853通过默认参数创建PixelMap数组,使用callback形式返回结果。针对动图如Gif、Webp,此接口返回每帧图片数据;针对静态图,此接口返回唯一的一帧图片数据。
854
855> **注意:**
856> 此接口会一次性解码全部帧,当帧数过多或单帧图像过大时,会占用较大内存,造成系统内存紧张,此种情况推荐使用Image组件显示动图,Image组件采用逐帧解码,占用内存比此接口少。
857
858**系统能力:** SystemCapability.Multimedia.Image.ImageSource
859
860**参数:**
861
862| 参数名     | 类型                                  | 必填 | 说明                       |
863| -------- | ------------------------------------- | ---- | -------------------------- |
864| callback | AsyncCallback<Array<[PixelMap](arkts-apis-image-PixelMap.md)>> | 是   | 回调函数,当创建PixelMap对象数组成功,err为undefined,data为获取到的PixelMap对象数组;否则为错误对象。  |
865
866**错误码:**
867
868以下错误码的详细介绍请参见[Image错误码](errorcode-image.md)。
869
870| 错误码ID | 错误信息 |
871| ------- | --------------------------------------------|
872| 62980096 | The operation failed. Possible cause: 1.Image upload exception. 2. Decoding process exception. 3. Insufficient memory.             |
873| 62980099 | The shared memory data is abnormal.  |
874| 62980101 | The image data is abnormal.          |
875| 62980103 | The image data is not supported.         |
876| 62980106 | The image data is too large. This status code is thrown when an error occurs during the process of checking size. |
877| 62980109 | Failed to crop the image.            |
878| 62980111 | The image source data is incomplete. |
879| 62980115 | Invalid image parameter.      |
880| 62980116 | Failed to decode the image.         |
881| 62980118 | Failed to create the image plugin.   |
882| 62980137 | Invalid media operation.     |
883| 62980173 | The DMA memory does not exist.        |
884| 62980174 | The DMA memory data is abnormal.    |
885
886**示例:**
887
888```ts
889import { BusinessError } from '@kit.BasicServicesKit';
890
891async function CreatePixelMapList(imageSourceObj : image.ImageSource) {
892  imageSourceObj.createPixelMapList((err: BusinessError, pixelMapList: Array<image.PixelMap>) => {
893    if (err) {
894      console.error(`Failed to create pixelMapList object, error code is ${err}`);
895    } else {
896      console.info('Succeeded in creating pixelMapList object.');
897    }
898  })
899}
900```
901
902## createPixelMapList<sup>10+</sup>
903
904createPixelMapList(options: DecodingOptions, callback: AsyncCallback<Array\<PixelMap>>): void
905
906通过图片解码参数创建PixelMap数组,使用callback形式返回结果。针对动图如Gif、Webp,此接口返回每帧图片数据;针对静态图,此接口返回唯一的一帧图片数据。
907
908> **注意:**
909> 此接口会一次性解码全部帧,当帧数过多或单帧图像过大时,会占用较大内存,造成系统内存紧张,此种情况推荐使用Image组件显示动图,Image组件采用逐帧解码,占用内存比此接口少。
910
911**系统能力:** SystemCapability.Multimedia.Image.ImageSource
912
913**参数:**
914
915| 参数名   | 类型                 | 必填 | 说明                               |
916| -------- | -------------------- | ---- | ---------------------------------- |
917| options | [DecodingOptions](arkts-apis-image-i.md#decodingoptions7) | 是 | 解码参数。 |
918| callback | AsyncCallback<Array<[PixelMap](arkts-apis-image-PixelMap.md)>> | 是   | 回调函数,当创建PixelMap对象数组成功,err为undefined,data为获取到的PixelMap对象数组;否则为错误对象。  |
919
920**错误码:**
921
922以下错误码的详细介绍请参见[Image错误码](errorcode-image.md)。
923
924| 错误码ID | 错误信息 |
925| ------- | --------------------------------------------|
926| 62980096 | The operation failed. Possible cause: 1.Image upload exception. 2. Decoding process exception. 3. Insufficient memory.            |
927| 62980099 | The shared memory data is abnormal.  |
928| 62980101 | The image data is abnormal.         |
929| 62980103 | The image data is not supported.        |
930| 62980106 | The image data is too large. This status code is thrown when an error occurs during the process of checking size. |
931| 62980109 | Failed to crop the image.           |
932| 62980111 | The image source data is incomplete. |
933| 62980115 | Invalid image parameter.      |
934| 62980116 | Failed to decode the image.         |
935| 62980118 | Failed to create the image plugin.  |
936| 62980137 | Invalid media operation.      |
937| 62980173 | The DMA memory does not exist.         |
938| 62980174 | The DMA memory data is abnormal.     |
939
940**示例:**
941
942```ts
943import { BusinessError } from '@kit.BasicServicesKit';
944
945async function CreatePixelMapList(imageSourceObj : image.ImageSource) {
946  let decodeOpts: image.DecodingOptions = {
947    sampleSize: 1,
948    editable: true,
949    desiredSize: { width: 198, height: 202 },
950    rotate: 0,
951    desiredPixelFormat: image.PixelMapFormat.RGBA_8888,
952    index: 0,
953  };
954  imageSourceObj.createPixelMapList(decodeOpts, (err: BusinessError, pixelMapList: Array<image.PixelMap>) => {
955    if (err) {
956      console.error(`Failed to create pixelMapList object, error code is ${err}`);
957    } else {
958      console.info('Succeeded in creating pixelMapList object.');
959    }
960  })
961}
962```
963
964## createPixelMapUsingAllocator<sup>15+</sup>
965
966createPixelMapUsingAllocator(options?: DecodingOptions, allocatorType?: AllocatorType): Promise\<PixelMap>
967
968使用指定的分配器根据图像解码参数异步创建PixelMap对象。使用Promise异步回调。接口使用详情请参考[申请图片解码内存(ArkTS)](../../media/image/image-allocator-type.md)。
969
970**系统能力:** SystemCapability.Multimedia.Image.ImageSource
971
972**参数:**
973
974| 参数名        | 类型                                 | 必填 | 说明                     |
975| ------------- | ------------------------------------ | ---- | ------------------------ |
976| options        | [DecodingOptions](arkts-apis-image-i.md#decodingoptions7) | 否   | 解码参数。               |
977| allocatorType | [AllocatorType](arkts-apis-image-e.md#allocatortype15)   | 否   | 用于图像解码的内存类型。默认值为AllocatorType.AUTO。 |
978
979**返回值:**
980
981| 类型                             | 说明                        |
982| -------------------------------- | --------------------------- |
983| Promise\<[PixelMap](arkts-apis-image-PixelMap.md)> | Promise对象,返回PixelMap。 |
984
985**错误码:**
986
987以下错误码的详细介绍请参见[Image错误码](errorcode-image.md)。
988
989| 错误码ID | 错误信息                                                     |
990| -------- | ------------------------------------------------------------ |
991| 401      | Parameter error.Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types;3.Parameter verification failed. |
992| 7700101  | Bad source. e.g.,1. Image has invalid width or height. 2. Image source incomplete. 3. Read image data failed. 4. Codec create failed. |
993| 7700102  | Unsupported mimetype.                                        |
994| 7700103  | Image too large.  This status code is thrown when an error occurs during the process of checking size. |
995| 7700201  | Unsupported allocator type, e.g., use share memory to decode a HDR image as only DMA supported hdr metadata. |
996| 7700203  | Unsupported options, e.g., cannot convert image into desired pixel format. |
997| 7700301  | Failed to decode image.                                      |
998| 7700302  | Failed to allocate memory.                                   |
999
1000**示例:**
1001
1002```ts
1003async function CreatePixelMapUsingAllocator(context : Context) {
1004  // 此处'test.jpg'仅作示例,请开发者自行替换,否则imageSource创建失败会导致后续无法正常执行。
1005  let filePath: string = context.filesDir + "/test.jpg";
1006  let imageSource = image.createImageSource(filePath);
1007  let decodingOptions: image.DecodingOptions = {
1008    editable: true,
1009    desiredSize: { width: 3072, height: 4096 },
1010    rotate: 10,
1011    desiredPixelFormat: image.PixelMapFormat.RGBA_8888,
1012    desiredRegion: { size: { width: 3072, height: 4096 }, x: 0, y: 0 },
1013    // 若解码接口同时传入了desiredSize参数与desiredRegion参数,需进一步传入cropAndScaleStrategy参数指定缩放与裁剪的先后顺序,推荐设置CROP_FIRST。
1014    cropAndScaleStrategy: image.CropAndScaleStrategy.CROP_FIRST,
1015    index: 0
1016  };
1017  let pixelmap = imageSource.createPixelMapUsingAllocator(decodingOptions, image.AllocatorType.AUTO);
1018  if (pixelmap != undefined) {
1019    console.info('Succeeded in creating pixelMap object.');
1020  } else {
1021    console.error('Failed to create pixelMap.');
1022  }
1023}
1024```
1025
1026## createPixelMapUsingAllocatorSync<sup>15+</sup>
1027
1028createPixelMapUsingAllocatorSync(options?: DecodingOptions, allocatorType?: AllocatorType): PixelMap
1029
1030根据指定的分配器同步创建一个基于图像解码参数的PixelMap对象。接口使用详情请参考[申请图片解码内存(ArkTS)](../../media/image/image-allocator-type.md)。
1031
1032**系统能力:** SystemCapability.Multimedia.Image.ImageSource
1033
1034**参数:**
1035
1036| 参数名        | 类型                                 | 必填 | 说明                     |
1037| ------------- | ------------------------------------ | ---- | ------------------------ |
1038| options        | [DecodingOptions](arkts-apis-image-i.md#decodingoptions7) | 否   | 解码参数。               |
1039| allocatorType | [AllocatorType](arkts-apis-image-e.md#allocatortype15)   | 否   | 用于图像解码的内存类型。默认值为AllocatorType.AUTO。 |
1040
1041**返回值:**
1042
1043| 类型                   | 说明                   |
1044| ---------------------- | ---------------------- |
1045| [PixelMap](arkts-apis-image-PixelMap.md) | 用于同步返回创建结果。 |
1046
1047**错误码:**
1048
1049以下错误码的详细介绍请参见[Image错误码](errorcode-image.md)。
1050
1051| 错误码ID | 错误信息                                                     |
1052| -------- | ------------------------------------------------------------ |
1053| 401      | Parameter error.Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types;3.Parameter verification failed. |
1054| 7700101  | Bad source. e.g.,1. Image has invalid width or height. 2. Image source incomplete. 3. Read image data failed. 4. Codec create failed. |
1055| 7700102  | Unsupported mimetype.                                        |
1056| 7700103  | Image too large.  This status code is thrown when an error occurs during the process of checking size. |
1057| 7700201  | Unsupported allocator type, e.g., use share memory to decode a HDR image as only DMA supported hdr metadata. |
1058| 7700203  | Unsupported options, e.g., cannot convert image into desired pixel format. |
1059| 7700301  | Failed to decode image.                                      |
1060| 7700302  | Failed to allocate memory.                                   |
1061
1062**示例:**
1063
1064```ts
1065async function CreatePixelMapUsingAllocator(context : Context) {
1066  // 此处'test.jpg'仅作示例,请开发者自行替换,否则imageSource创建失败会导致后续无法正常执行。
1067  let filePath: string = context.filesDir + "/test.jpg";
1068  let imageSource = image.createImageSource(filePath);
1069  let decodingOptions: image.DecodingOptions = {
1070    editable: true,
1071    desiredSize: { width: 3072, height: 4096 },
1072    rotate: 10,
1073    desiredPixelFormat: image.PixelMapFormat.RGBA_8888,
1074    desiredRegion: { size: { width: 3072, height: 4096 }, x: 0, y: 0 },
1075    // 若解码接口同时传入了desiredSize参数与desiredRegion参数,需进一步传入cropAndScaleStrategy参数指定缩放与裁剪的先后顺序,推荐设置CROP_FIRST。
1076    cropAndScaleStrategy: image.CropAndScaleStrategy.CROP_FIRST,
1077    index: 0
1078  };
1079  let pixelmap = imageSource.createPixelMapUsingAllocatorSync(decodingOptions, image.AllocatorType.AUTO);
1080  if (pixelmap != undefined) {
1081    console.info('Succeeded in creating pixelMap object.');
1082  } else {
1083    console.error('Failed to create pixelMap.');
1084  }
1085}
1086```
1087
1088## getDelayTimeList<sup>10+</sup>
1089
1090getDelayTimeList(callback: AsyncCallback<Array\<number>>): void
1091
1092获取图像延迟时间数组,使用callback形式返回结果。此接口仅用于gif图片和webp图片。
1093
1094**系统能力:** SystemCapability.Multimedia.Image.ImageSource
1095
1096**参数:**
1097
1098| 参数名   | 类型                 | 必填 | 说明                               |
1099| -------- | -------------------- | ---- | ---------------------------------- |
1100| callback | AsyncCallback<Array\<number>> | 是   | 回调函数,当获取图像延迟时间数组成功,err为undefined,data为获取到的图像延时时间数组;否则为错误对象。 |
1101
1102**错误码:**
1103
1104以下错误码的详细介绍请参见[Image错误码](errorcode-image.md)。
1105
1106| 错误码ID | 错误信息 |
1107| ------- | --------------------------------------------|
1108| 62980096| The operation failed. Possible cause: 1.Image upload exception. 2. Decoding process exception. 3. Insufficient memory.              |
1109| 62980110| The image source data is incorrect.             |
1110| 62980111| The image source data is incomplete.            |
1111| 62980115 | Invalid image parameter. |
1112| 62980116| Failed to decode the image. |
1113| 62980118| Failed to create the image plugin. |
1114| 62980122| Failed to decode the image header. |
1115| 62980149 | Invalid MIME type for the image source. |
1116
1117**示例:**
1118
1119```ts
1120import { BusinessError } from '@kit.BasicServicesKit';
1121
1122async function GetDelayTimeList(imageSourceObj : image.ImageSource) {
1123  imageSourceObj.getDelayTimeList((err: BusinessError, delayTimes: Array<number>) => {
1124    if (err) {
1125      console.error(`Failed to get delayTimes object.code is ${err.code},message is ${err.message}`);
1126    } else {
1127      console.info('Succeeded in getting delayTimes object.');
1128    }
1129  })
1130}
1131```
1132
1133## getDelayTimeList<sup>10+</sup>
1134
1135getDelayTimeList(): Promise<Array\<number>>
1136
1137获取图像延迟时间数组,使用Promise形式返回结果。此接口仅用于gif图片和webp图片。
1138
1139**系统能力:** SystemCapability.Multimedia.Image.ImageSource
1140
1141**返回值:**
1142
1143| 类型           | 说明                        |
1144| -------------- | --------------------------- |
1145| Promise<Array\<number>> | Promise对象,返回延迟时间数组。 |
1146
1147**错误码:**
1148
1149以下错误码的详细介绍请参见[Image错误码](errorcode-image.md)。
1150
1151| 错误码ID | 错误信息 |
1152| ------- | --------------------------------------------|
1153| 62980096 | The operation failed. Possible cause: 1.Image upload exception. 2. Decoding process exception. 3. Insufficient memory.             |
1154| 62980110 | The image source data is incorrect.      |
1155| 62980111 | The image source data is incomplete. |
1156| 62980115 | Invalid image parameter.      |
1157| 62980116 | Failed to decode the image.          |
1158| 62980118 | Failed to create the image plugin.  |
1159| 62980122 | Failed to decode the image header.   |
1160| 62980149 | Invalid MIME type for the image source.      |
1161
1162**示例:**
1163
1164```ts
1165import { BusinessError } from '@kit.BasicServicesKit';
1166
1167async function GetDelayTimeList(imageSourceObj : image.ImageSource) {
1168  imageSourceObj.getDelayTimeList().then((delayTimes: Array<number>) => {
1169    console.info('Succeeded in getting delayTimes object.');
1170  }).catch((err: BusinessError) => {
1171    console.error(`Failed to get delayTimes object.code is ${err.code},message is ${err.message}`);
1172  })
1173}
1174```
1175
1176## getFrameCount<sup>10+</sup>
1177
1178getFrameCount(callback: AsyncCallback\<number>): void
1179
1180获取图像帧数,使用callback形式返回结果。
1181
1182**系统能力:** SystemCapability.Multimedia.Image.ImageSource
1183
1184**参数:**
1185
1186| 参数名   | 类型                 | 必填 | 说明                               |
1187| -------- | -------------------- | ---- | ---------------------------------- |
1188| callback | AsyncCallback\<number> | 是   | 回调函数,当获取图像帧数成功,err为undefined,data为获取到的图像帧数;否则为错误对象。 |
1189
1190**错误码:**
1191
1192以下错误码的详细介绍请参见[Image错误码](errorcode-image.md)。
1193
1194| 错误码ID | 错误信息 |
1195| ------- | --------------------------------------------|
1196| 62980096| The operation failed. Possible cause: 1.Image upload exception. 2. Decoding process exception. 3. Insufficient memory.             |
1197| 62980111| The image source data is incomplete. |
1198| 62980112| The image format does not match. |
1199| 62980113| Unknown image format.The image data provided is not in a recognized or supported format, or it may be occorrupted.            |
1200| 62980115| Invalid image parameter. |
1201| 62980116| Failed to decode the image. |
1202| 62980118| Failed to create the image plugin. |
1203| 62980122| Failed to decode the image header. |
1204| 62980137| Invalid media operation. |
1205
1206**示例:**
1207
1208```ts
1209import { BusinessError } from '@kit.BasicServicesKit';
1210
1211async function GetFrameCount(imageSourceObj : image.ImageSource) {
1212  imageSourceObj.getFrameCount((err: BusinessError, frameCount: number) => {
1213    if (err) {
1214      console.error(`Failed to get frame count.code is ${err.code},message is ${err.message}`);
1215    } else {
1216      console.info('Succeeded in getting frame count.');
1217    }
1218  })
1219}
1220```
1221
1222## getFrameCount<sup>10+</sup>
1223
1224getFrameCount(): Promise\<number>
1225
1226获取图像帧数,使用Promise形式返回结果。
1227
1228**系统能力:** SystemCapability.Multimedia.Image.ImageSource
1229
1230**返回值:**
1231
1232| 类型           | 说明                        |
1233| -------------- | --------------------------- |
1234| Promise\<number> | Promise对象,返回图像帧数。 |
1235
1236**错误码:**
1237
1238以下错误码的详细介绍请参见[Image错误码](errorcode-image.md)。
1239
1240| 错误码ID | 错误信息 |
1241| ------- | --------------------------------------------|
1242| 62980096 | The operation failed. Possible cause: 1.Image upload exception. 2. Decoding process exception. 3. Insufficient memory.             |
1243| 62980111 | The image source data is incomplete. |
1244| 62980112 | The image format does not match.        |
1245| 62980113| Unknown image format.The image data provided is not in a recognized or supported format, or it may be occorrupted.            |
1246| 62980115 | Invalid image parameter.      |
1247| 62980116 | Failed to decode the image.          |
1248| 62980118 | Failed to create the image plugin.   |
1249| 62980122 | Failed to decode the image header.  |
1250| 62980137 | Invalid media operation.      |
1251
1252**示例:**
1253
1254```ts
1255import { BusinessError } from '@kit.BasicServicesKit';
1256
1257async function GetFrameCount(imageSourceObj : image.ImageSource) {
1258  imageSourceObj.getFrameCount().then((frameCount: number) => {
1259    console.info('Succeeded in getting frame count.');
1260  }).catch((err: BusinessError) => {
1261    console.error(`Failed to get frame count.code is ${err.code},message is ${err.message}`);
1262  })
1263}
1264```
1265
1266## getDisposalTypeList<sup>12+</sup>
1267
1268getDisposalTypeList(): Promise\<Array\<number>>
1269
1270获取图像帧过渡模式数组,使用Promise形式返回结果。此接口仅用于gif图片。
1271
1272**系统能力:** SystemCapability.Multimedia.Image.ImageSource
1273
1274**返回值:**
1275
1276| 类型           | 说明                        |
1277| -------------- | --------------------------- |
1278| Promise\<Array\<number>> | Promise对象,返回帧过渡模式数组。 |
1279
1280**错误码:**
1281
1282以下错误码的详细介绍请参见[Image错误码](errorcode-image.md)。
1283
1284| 错误码ID | 错误信息 |
1285| ------- | --------------------------------------------|
1286| 62980096 | The operation failed. Possible cause: 1.Image upload exception. 2. Decoding process exception. 3. Insufficient memory.      |
1287| 62980101 | The image data is abnormal. |
1288| 62980137 | Invalid media operation.        |
1289| 62980149 | Invalid MIME type for the image source.      |
1290
1291**示例:**
1292
1293```ts
1294import { BusinessError } from '@kit.BasicServicesKit';
1295
1296async function GetDisposalTypeList(imageSourceObj : image.ImageSource) {
1297  imageSourceObj.getDisposalTypeList().then((disposalTypes: Array<number>) => {
1298    console.info('Succeeded in getting disposalTypes object.');
1299  }).catch((err: BusinessError) => {
1300    console.error(`Failed to get disposalTypes object.code ${err.code},message is ${err.message}`);
1301  })
1302}
1303```
1304
1305## release
1306
1307release(callback: AsyncCallback\<void>): void
1308
1309释放ImageSource实例,使用callback形式返回结果。
1310
1311ArkTS有内存回收机制,ImageSource对象不调用release方法,内存最终也会由系统统一释放。但图片使用的内存往往较大,为尽快释放内存,建议应用在使用完成后主动调用release方法提前释放内存。
1312
1313**系统能力:** SystemCapability.Multimedia.Image.ImageSource
1314
1315**参数:**
1316
1317| 参数名   | 类型                 | 必填 | 说明                               |
1318| -------- | -------------------- | ---- | ---------------------------------- |
1319| callback | AsyncCallback\<void> | 是   | 回调函数,当资源释放成功,err为undefined,否则为错误对象。  |
1320
1321**示例:**
1322
1323```ts
1324import { BusinessError } from '@kit.BasicServicesKit';
1325
1326async function Release(imageSourceObj : image.ImageSource) {
1327  imageSourceObj.release((err: BusinessError) => {
1328    if (err) {
1329      console.error(`Failed to release the image source instance.code ${err.code},message is ${err.message}`);
1330    } else {
1331      console.info('Succeeded in releasing the image source instance.');
1332    }
1333  })
1334}
1335```
1336
1337## release
1338
1339release(): Promise\<void>
1340
1341释放ImageSource实例,使用Promise形式返回结果。
1342
1343ArkTS有内存回收机制,ImageSource对象不调用release方法,内存最终也会由系统统一释放。但图片使用的内存往往较大,为尽快释放内存,建议应用在使用完成后主动调用release方法提前释放内存。
1344
1345**系统能力:** SystemCapability.Multimedia.Image.ImageSource
1346
1347**返回值:**
1348
1349| 类型           | 说明                        |
1350| -------------- | --------------------------- |
1351| Promise\<void> |  Promise对象。无返回结果的Promise对象。 |
1352
1353**示例:**
1354
1355```ts
1356import { BusinessError } from '@kit.BasicServicesKit';
1357
1358async function Release(imageSourceObj : image.ImageSource) {
1359  imageSourceObj.release().then(() => {
1360    console.info('Succeeded in releasing the image source instance.');
1361  }).catch((error: BusinessError) => {
1362    console.error(`Failed to release the image source instance.code ${error.code},message is ${error.message}`);
1363  })
1364}
1365```
1366
1367## getImageProperty<sup>(deprecated)</sup>
1368
1369getImageProperty(key:string, options?: GetImagePropertyOptions): Promise\<string>
1370
1371获取图片中给定索引处图像的指定属性键的值,用Promise形式返回结果,仅支持JPEG、PNG和HEIF<sup>12+</sup>(不同硬件设备支持情况不同)文件,且需要包含exif信息。其中可以通过supportedFormats属性查询是否支持HEIF格式的exif读写。
1372
1373> **说明:**
1374>
1375> 从API version 7开始支持,从API version 11废弃,建议使用[getImageProperty](#getimageproperty11)代替。
1376
1377**系统能力:** SystemCapability.Multimedia.Image.ImageSource
1378
1379**参数:**
1380
1381| 参数名  | 类型                                                 | 必填 | 说明                                 |
1382| ------- | ---------------------------------------------------- | ---- | ------------------------------------ |
1383| key     | string                                               | 是   | 图片属性名。                         |
1384| options | [GetImagePropertyOptions](arkts-apis-image-i.md#getimagepropertyoptionsdeprecated) | 否   | 图片属性,包括图片序号与默认属性值。 |
1385
1386**返回值:**
1387
1388| 类型             | 说明                                                              |
1389| ---------------- | ----------------------------------------------------------------- |
1390| Promise\<string> | Promise对象,返回图片属性值,如获取失败则返回属性默认值。 |
1391
1392**示例:**
1393
1394```ts
1395import { BusinessError } from '@kit.BasicServicesKit';
1396
1397async function GetImageProperty(imageSourceObj : image.ImageSource) {
1398  imageSourceObj.getImageProperty("BitsPerSample")
1399    .then((data: string) => {
1400      console.info('Succeeded in getting the value of the specified attribute key of the image.');
1401    }).catch((error: BusinessError) => {
1402    console.error(`Failed to get the value of the specified attribute key of the image, error.code ${error.code}, error.message ${error.message}`);
1403  })
1404}
1405```
1406
1407## getImageProperty<sup>(deprecated)</sup>
1408
1409getImageProperty(key:string, callback: AsyncCallback\<string>): void
1410
1411获取图片中给定索引处图像的指定属性键的值,用callback形式返回结果,仅支持JPEG、PNG和HEIF<sup>12+</sup>(不同硬件设备支持情况不同)文件,且需要包含exif信息。其中可以通过supportedFormats属性查询是否支持HEIF格式的exif读写。
1412
1413> **说明:**
1414>
1415> 从API version 7开始支持,从API version 11废弃,建议使用[getImageProperty](#getimageproperty11)代替。
1416
1417**系统能力:** SystemCapability.Multimedia.Image.ImageSource
1418
1419**参数:**
1420
1421| 参数名   | 类型                   | 必填 | 说明                                                         |
1422| -------- | ---------------------- | ---- | ------------------------------------------------------------ |
1423| key      | string                 | 是   | 图片属性名。                                                 |
1424| callback | AsyncCallback\<string> | 是   | 回调函数,当获取图片属性值成功,err为undefined,data为获取到的图片属性值;否则为错误对象。 |
1425
1426**示例:**
1427
1428```ts
1429import { BusinessError } from '@kit.BasicServicesKit';
1430
1431async function GetImageProperty(imageSourceObj : image.ImageSource) {
1432  imageSourceObj.getImageProperty("BitsPerSample", (error: BusinessError, data: string) => {
1433    if (error) {
1434      console.error('Failed to get the value of the specified attribute key of the image.');
1435    } else {
1436      console.info('Succeeded in getting the value of the specified attribute key of the image.');
1437    }
1438  })
1439}
1440```
1441
1442## getImageProperty<sup>(deprecated)</sup>
1443
1444getImageProperty(key:string, options: GetImagePropertyOptions, callback: AsyncCallback\<string>): void
1445
1446获取图片指定属性键的值,callback形式返回结果,仅支持JPEG、PNG和HEIF<sup>12+</sup>(不同硬件设备支持情况不同)文件,且需要包含exif信息。其中可以通过supportedFormats属性查询是否支持HEIF格式的exif读写。
1447
1448> **说明:**
1449>
1450> 从API version 7开始支持,从API version 11废弃,建议使用[getImageProperty](#getimageproperty11)代替。
1451
1452**系统能力:** SystemCapability.Multimedia.Image.ImageSource
1453
1454**参数:**
1455
1456| 参数名   | 类型                                                 | 必填 | 说明                                                          |
1457| -------- | ---------------------------------------------------- | ---- | ------------------------------------------------------------- |
1458| key      | string                                               | 是   | 图片属性名。                                                  |
1459| options  | [GetImagePropertyOptions](arkts-apis-image-i.md#getimagepropertyoptionsdeprecated) | 是   | 图片属性,包括图片序号与默认属性值。                          |
1460| callback | AsyncCallback\<string>                               | 是   | 回调函数,当获取图片属性值成功,err为undefined,data为获取到的图片属性值;否则为错误对象。|
1461
1462**示例:**
1463
1464```ts
1465import { BusinessError } from '@kit.BasicServicesKit';
1466
1467async function GetImageProperty(imageSourceObj : image.ImageSource) {
1468  let property: image.GetImagePropertyOptions = { index: 0, defaultValue: '9999' }
1469  imageSourceObj.getImageProperty("BitsPerSample", property, (error: BusinessError, data: string) => {
1470    if (error) {
1471      console.error('Failed to get the value of the specified attribute key of the image.');
1472    } else {
1473      console.info('Succeeded in getting the value of the specified attribute key of the image.');
1474    }
1475  })
1476}
1477```
1478
1479## modifyImageProperty<sup>(deprecated)</sup>
1480
1481modifyImageProperty(key: string, value: string): Promise\<void>
1482
1483通过指定的键修改图片属性的值,使用Promise形式返回结果,仅支持JPEG、PNG和HEIF<sup>12+</sup>(不同硬件设备支持情况不同)文件,且需要包含exif信息。其中可以通过supportedFormats属性查询是否支持HEIF格式的exif读写。
1484
1485> **说明:**
1486>
1487> 调用modifyImageProperty修改属性会改变属性字节长度,使用buffer创建的ImageSource调用modifyImageProperty会导致buffer内容覆盖,目前buffer创建的ImageSource不支持调用此接口,请改用fd或path创建的ImageSource。
1488>
1489> 从API version 9开始支持,从API version 11废弃,建议使用[modifyImageProperty](#modifyimageproperty11)代替。
1490
1491**系统能力:** SystemCapability.Multimedia.Image.ImageSource
1492
1493**参数:**
1494
1495| 参数名  | 类型   | 必填 | 说明         |
1496| ------- | ------ | ---- | ------------ |
1497| key     | string | 是   | 图片属性名。 |
1498| value   | string | 是   | 属性值。     |
1499
1500**返回值:**
1501
1502| 类型           | 说明                        |
1503| -------------- | --------------------------- |
1504| Promise\<void> |  Promise对象。无返回结果的Promise对象。|
1505
1506**示例:**
1507
1508```ts
1509import { BusinessError } from '@kit.BasicServicesKit';
1510
1511async function ModifyImageProperty(imageSourceObj : image.ImageSource) {
1512  imageSourceObj.modifyImageProperty("ImageWidth", "120").then(() => {
1513    imageSourceObj.getImageProperty("ImageWidth").then((width: string) => {
1514      console.info(`ImageWidth is :${width}`);
1515    }).catch((error: BusinessError) => {
1516      console.error(`Failed to get the Image Width, error.code ${error.code}, error.message ${error.message}`);
1517    })
1518  }).catch((error: BusinessError) => {
1519    console.error(`Failed to modify the Image Width, error.code ${error.code}, error.message ${error.message}`);
1520  })
1521}
1522```
1523
1524## modifyImageProperty<sup>(deprecated)</sup>
1525
1526modifyImageProperty(key: string, value: string, callback: AsyncCallback\<void>): void
1527
1528通过指定的键修改图片属性的值,callback形式返回结果,仅支持JPEG、PNG和HEIF<sup>12+</sup>(不同硬件设备支持情况不同)文件,且需要包含exif信息。其中可以通过supportedFormats属性查询是否支持HEIF格式的exif读写。
1529
1530> **说明:**
1531>
1532> 调用modifyImageProperty修改属性会改变属性字节长度,使用buffer创建的ImageSource调用modifyImageProperty会导致buffer内容覆盖,目前buffer创建的ImageSource不支持调用此接口,请改用fd或path创建的ImageSource。
1533>
1534> 从API version 9开始支持,从API version 11废弃,建议使用[modifyImageProperty](#modifyimageproperty11)代替。
1535
1536**系统能力:** SystemCapability.Multimedia.Image.ImageSource
1537
1538**参数:**
1539
1540| 参数名   | 类型                | 必填 | 说明                           |
1541| -------- | ------------------- | ---- | ------------------------------ |
1542| key      | string              | 是   | 图片属性名。                   |
1543| value    | string              | 是   | 属性值。                       |
1544| callback | AsyncCallback\<void> | 是   | 回调函数,当修改图片属性值成功,err为undefined,否则为错误对象。 |
1545
1546**示例:**
1547
1548```ts
1549import { BusinessError } from '@kit.BasicServicesKit';
1550
1551async function ModifyImageProperty(imageSourceObj : image.ImageSource) {
1552  imageSourceObj.modifyImageProperty("ImageWidth", "120", (err: BusinessError) => {
1553    if (err) {
1554      console.error(`Failed to modify the Image Width.code is ${err.code}, message is ${err.message}`);
1555    } else {
1556      console.info('Succeeded in modifying the Image Width.');
1557    }
1558  })
1559}
1560```
1561