1# Interface (ImageSource) 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 12The **ImageSource** class provides APIs to obtain image information. Before calling any API in ImageSource, you must use [createImageSource](arkts-apis-image-f.md#imagecreateimagesource) to create an ImageSource instance. 13 14## Modules to Import 15 16```ts 17import { image } from '@kit.ImageKit'; 18``` 19 20## Properties 21 22**System capability**: SystemCapability.Multimedia.Image.ImageSource 23 24| Name | Type | Read Only| Optional| Description | 25| ---------------- | -------------- | ---- | ---- | ------------------------------------------------------------ | 26| supportedFormats | Array\<string> | Yes | No | Supported formats, including .png, .jpeg, .bmp, .gif, .webp, .dng., and .heic<sup>12+</sup> (depending on the hardware).| 27 28## getImageInfo 29 30getImageInfo(index: number, callback: AsyncCallback\<ImageInfo>): void 31 32Obtains information about an image with the specified index. This API uses an asynchronous callback to return the result. 33 34**Widget capability**: This API can be used in ArkTS widgets since API version 12. 35 36**Atomic service API**: This API can be used in atomic services since API version 11. 37 38**System capability**: SystemCapability.Multimedia.Image.ImageSource 39 40**Parameters** 41 42| Name | Type | Mandatory| Description | 43| -------- | -------------------------------------- | ---- | ---------------------------------------- | 44| index | number | Yes | Index of the image source. The default value is **0**, indicating the first image. If this parameter is set to N, the (N+1)th image is used. For single-frame images, the value is always **0**. For multi-frame images such as animations, the value ranges from 0 to (Number of frames – 1). | 45| callback | AsyncCallback<[ImageInfo](arkts-apis-image-i.md#imageinfo)> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the image information obtained; otherwise, **err** is an error object.| 46 47**Example** 48 49```ts 50import { BusinessError } from '@kit.BasicServicesKit'; 51 52imageSourceApi.getImageInfo(0, (error: BusinessError, imageInfo: image.ImageInfo) => { 53 if (error) { 54 console.error(`Failed to obtain the image information.code is ${error.code}, message is ${error.message}`); 55 } else { 56 console.info('Succeeded in obtaining the image information.'); 57 } 58}) 59``` 60 61## getImageInfo 62 63getImageInfo(callback: AsyncCallback\<ImageInfo>): void 64 65Obtains information about this image. This API uses an asynchronous callback to return the result. 66 67**Widget capability**: This API can be used in ArkTS widgets since API version 12. 68 69**Atomic service API**: This API can be used in atomic services since API version 11. 70 71**System capability**: SystemCapability.Multimedia.Image.ImageSource 72 73**Parameters** 74 75| Name | Type | Mandatory| Description | 76| -------- | -------------------------------------- | ---- | ---------------------------------------- | 77| callback | AsyncCallback<[ImageInfo](arkts-apis-image-i.md#imageinfo)> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the image information obtained; otherwise, **err** is an error object.| 78 79**Example** 80 81```ts 82import { BusinessError } from '@kit.BasicServicesKit'; 83 84imageSourceApi.getImageInfo((err: BusinessError, imageInfo: image.ImageInfo) => { 85 if (err) { 86 console.error(`Failed to obtain the image information.code is ${err.code}, message is ${err.message}`); 87 } else { 88 console.info('Succeeded in obtaining the image information.'); 89 } 90}) 91``` 92 93## getImageInfo 94 95getImageInfo(index?: number): Promise\<ImageInfo> 96 97Obtains information about an image with the specified index. This API uses a promise to return the result. 98 99**Widget capability**: This API can be used in ArkTS widgets since API version 12. 100 101**Atomic service API**: This API can be used in atomic services since API version 11. 102 103**System capability**: SystemCapability.Multimedia.Image.ImageSource 104 105**Parameters** 106 107| Name| Type | Mandatory| Description | 108| ----- | ------ | ---- | ------------------------------------- | 109| index | number | No | Index of the image source. The default value is **0**, indicating the first image. If this parameter is set to N, the (N+1)th image is used. For single-frame images, the value is always **0**. For multi-frame images such as animations, the value ranges from 0 to (Number of frames – 1).| 110 111**Return value** 112 113| Type | Description | 114| -------------------------------- | ---------------------- | 115| Promise<[ImageInfo](arkts-apis-image-i.md#imageinfo)> | Promise used to return the image information.| 116 117**Example** 118 119```ts 120import { BusinessError } from '@kit.BasicServicesKit'; 121 122imageSourceApi.getImageInfo(0) 123 .then((imageInfo: image.ImageInfo) => { 124 console.info('Succeeded in obtaining the image information.'); 125 }).catch((error: BusinessError) => { 126 console.error(`Failed to obtain the image information.code is ${error.code}, message is ${error.message}`); 127 }) 128``` 129 130## getImageInfoSync<sup>12+</sup> 131 132getImageInfoSync(index?: number): ImageInfo 133 134Obtains information about an image with the specified index. This API returns the result synchronously. 135 136**System capability**: SystemCapability.Multimedia.Image.ImageSource 137 138**Parameters** 139 140| Name| Type | Mandatory| Description | 141| ----- | ------ | ---- | ------------------------------------- | 142| index | number | No | Index of the image source. The default value is **0**, indicating the first image. If this parameter is set to N, the (N+1)th image is used. For single-frame images, the value is always **0**. For multi-frame images such as animations, the value ranges from 0 to (Number of frames – 1).| 143 144**Return value** 145 146| Type | Description | 147| -------------------------------- | ---------------------- | 148| [ImageInfo](arkts-apis-image-i.md#imageinfo) | Image information.| 149 150**Example** 151 152<!--code_no_check--> 153```ts 154import { common } from '@kit.AbilityKit'; 155import { image } from '@kit.ImageKit'; 156 157// Obtain the context from the component and ensure that the return value of this.getUIContext().getHostContext() is UIAbilityContext. 158let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 159// 'test.jpg' is only an example. Replace it with the actual one in use. Otherwise, the imageSource instance fails to be created, and subsequent operations cannot be performed. 160let filePath: string = context.filesDir + "/test.jpg"; 161let imageSource = image.createImageSource(filePath); 162let imageInfo = imageSource.getImageInfoSync(0); 163if (imageInfo == undefined) { 164 console.error('Failed to obtain the image information.'); 165} else { 166 console.info('Succeeded in obtaining the image information.'); 167 console.info('imageInfo.size.height:' + imageInfo.size.height); 168 console.info('imageInfo.size.width:' + imageInfo.size.width); 169} 170``` 171 172## getImageProperty<sup>11+</sup> 173 174getImageProperty(key:PropertyKey, options?: ImagePropertyOptions): Promise\<string> 175 176Obtains the value of a property with the specified index in this image. This API uses a promise to return the result. This API applies only to images that are in JPEG, PNG, or HEIF<sup>12+</sup> (depending on the hardware) format and contain the EXIF information. You can use the **supportedFormats** property to check whether the EXIF read/write operation for images in HEIF format is supported. 177 178**System capability**: SystemCapability.Multimedia.Image.ImageSource 179 180**Parameters** 181 182| Name | Type | Mandatory| Description | 183| ------- | ---------------------------------------------------- | ---- | ------------------------------------ | 184| key | [PropertyKey](arkts-apis-image-e.md#propertykey7) | Yes | Name of the property. | 185| options | [ImagePropertyOptions](arkts-apis-image-i.md#imagepropertyoptions11) | No | Image properties, including the image index and default property value.| 186 187**Return value** 188 189| Type | Description | 190| ---------------- | ----------------------------------------------------------------- | 191| Promise\<string> | Promise used to return the property value. If the operation fails, the default value is returned.| 192 193**Error codes** 194 195For details about the error codes, see [Image Error Codes](errorcode-image.md). 196 197| ID| Error Message| 198| ------- | --------------------------------------------| 199| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types;3.Parameter verification failed; | 200| 62980096 | The operation failed. Possible cause: 1.Image upload exception. 2. Decoding process exception. 3. Insufficient memory. | 201| 62980103 | The image data is not supported. | 202| 62980110 | The image source data is incorrect. | 203| 62980111 | The image source data is incomplete. | 204| 62980112 | The image format does not match. | 205| 62980113| Unknown image format.The image data provided is not in a recognized or supported format, or it may be occorrupted. | 206| 62980115 | Invalid image parameter. | 207| 62980118 | Failed to create the image plugin. | 208| 62980122 | Failed to decode the image header. | 209| 62980123| The image does not support EXIF decoding. | 210| 62980135| The EXIF value is invalid. | 211 212**Example** 213 214```ts 215import { BusinessError } from '@kit.BasicServicesKit'; 216 217let options: image.ImagePropertyOptions = { index: 0, defaultValue: '9999' } 218imageSourceApi.getImageProperty(image.PropertyKey.BITS_PER_SAMPLE, options) 219.then((data: string) => { 220 console.info('Succeeded in getting the value of the specified attribute key of the image.'); 221}).catch((error: BusinessError) => { 222 console.error('Failed to get the value of the specified attribute key of the image.'); 223}) 224``` 225 226## getImageProperties<sup>12+</sup> 227 228getImageProperties(key: Array<PropertyKey>): Promise<Record<PropertyKey, string|null>> 229 230Obtains the values of properties with the given names in this image. This API uses a promise to return the result. This API applies only to images that are in JPEG, PNG, or HEIF (depending on the hardware) format and contain the EXIF information. You can use the **supportedFormats** property to check whether the EXIF read/write operation for images in HEIF format is supported. 231 232**System capability**: SystemCapability.Multimedia.Image.ImageSource 233 234**Parameters** 235 236| Name | Type | Mandatory| Description | 237| ------- | ---------------------------------------------------- | ---- | ------------------------------------ | 238| key | Array\<[PropertyKey](arkts-apis-image-e.md#propertykey7)> | Yes | Array of properties names. | 239 240**Return value** 241 242| Type | Description | 243| ---------------- | ----------------------------------------------------------------- | 244| Promise\<Record<[PropertyKey](arkts-apis-image-e.md#propertykey7), string \| null>> | Promise used to return the property values. If the operation fails, **null** is returned.| 245 246**Error codes** 247 248For details about the error codes, see [Image Error Codes](errorcode-image.md). 249 250| ID| Error Message| 251| ------- | --------------------------------------------| 252| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types;3.Parameter verification failed; | 253| 62980096| The operation failed. Possible cause: 1.Image upload exception. 2. Decoding process exception. 3. Insufficient memory. | 254| 62980110| The image source data is incorrect. | 255| 62980113| Unknown image format.The image data provided is not in a recognized or supported format, or it may be occorrupted. | 256| 62980116| Failed to decode the image. | 257 258**Example** 259 260```ts 261import { image } from '@kit.ImageKit'; 262import { BusinessError } from '@kit.BasicServicesKit'; 263 264let key = [image.PropertyKey.IMAGE_WIDTH, image.PropertyKey.IMAGE_LENGTH]; 265imageSourceApi.getImageProperties(key).then((data) => { 266 console.info(JSON.stringify(data)); 267}).catch((err: BusinessError) => { 268 console.error(JSON.stringify(err)); 269}); 270``` 271 272## getImagePropertySync<sup>20+</sup> 273 274getImagePropertySync(key:PropertyKey): string 275 276Obtains the value of a specified EXIF property. This API returns the property value in a string. 277 278>**NOTE** 279> 280> This API applies only to images that are in JPEG, PNG, or HEIF (depending on the hardware) format and contain the EXIF information. 281> 282> EXIF information is metadata of the image, including shooting time, camera model, aperture, focal length, and ISO. 283 284 285**System capability**: SystemCapability.Multimedia.Image.ImageSource 286 287**Parameters** 288 289| Name | Type | Mandatory| Description | 290| ------- | ---------------------------------------------------- | ---- | ------------------------------------ | 291| key | [PropertyKey](arkts-apis-image-e.md#propertykey7) | Yes | Name of the property. | 292 293**Return value** 294 295| Type | Description | 296| ---------------- | ----------------------------------------------------------------- | 297| string | Value of the specified EXIF property. If retrieval fails, the default value of the property is returned. For details about the meaning of each data value, see [PropertyKey](arkts-apis-image-e.md#propertykey7).| 298 299**Error codes** 300 301For details about the error codes, see [Image Error Codes](errorcode-image.md). 302| ID| Error Message| 303| ------- | --------------------------------------------| 304| 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.| 305| 7700102 | Unsupported MIME type.| 306| 7700202 | Unsupported metadata. For example, key is not supported.| 307 308**Example** 309 310<!--code_no_check--> 311```ts 312import { image } from '@kit.ImageKit'; 313import { common } from '@kit.AbilityKit'; 314 315// Obtain the context from the component and ensure that the return value of this.getUIContext().getHostContext() is UIAbilityContext. 316let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 317let resourceMgr = context.resourceManager; 318if (resourceMgr == null) { 319 return; 320} 321let fd = resourceMgr.getRawFdSync("example.jpg"); 322 323const imageSourceApi = image.createImageSource(fd); 324console.info("getImagePropertySync"); 325let bits_per_sample = imageSourceApi.getImagePropertySync(image.PropertyKey.BITS_PER_SAMPLE); 326console.info("bits_per_sample : " + bits_per_sample); 327``` 328 329## modifyImageProperty<sup>11+</sup> 330 331modifyImageProperty(key: PropertyKey, value: string): Promise\<void> 332 333Modifies the value of a property in this image. This API uses a promise to return the result. This API applies only to images that are in JPEG, PNG, or HEIF<sup>12+</sup> (depending on the hardware) format and contain the EXIF information. You can use the **supportedFormats** property to check whether the EXIF read/write operation for images in HEIF format is supported. 334 335> **NOTE** 336> 337> The property byte length is changed when the **modifyImageProperty** API is called to modify the value of a property. Currently, you can call the API in an ImageSource instance created based on a file descriptor or path, but not an ImageSource instance created based on buffers. 338 339**System capability**: SystemCapability.Multimedia.Image.ImageSource 340 341**Parameters** 342 343| Name | Type | Mandatory| Description | 344| ------- | ------ | ---- | ------------ | 345| key | [PropertyKey](arkts-apis-image-e.md#propertykey7) | Yes | Name of the property.| 346| value | string | Yes | New value of the property. | 347 348**Return value** 349 350| Type | Description | 351| -------------- | --------------------------- | 352| Promise\<void> | Promise that returns no value.| 353 354**Error codes** 355 356For details about the error codes, see [Image Error Codes](errorcode-image.md). 357 358| ID| Error Message| 359| ------- | --------------------------------------------| 360| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types; | 361| 62980123| The image does not support EXIF decoding. | 362| 62980133| The EXIF data is out of range. | 363| 62980135| The EXIF value is invalid. | 364| 62980146| The EXIF data failed to be written to the file. | 365 366**Example** 367 368```ts 369import { BusinessError } from '@kit.BasicServicesKit'; 370 371imageSourceApi.modifyImageProperty(image.PropertyKey.IMAGE_WIDTH, "120").then(() => { 372 imageSourceApi.getImageProperty(image.PropertyKey.IMAGE_WIDTH).then((width: string) => { 373 console.info(`ImageWidth is :${width}`); 374 }).catch((error: BusinessError) => { 375 console.error('Failed to get the Image Width.'); 376 }) 377}).catch((error: BusinessError) => { 378 console.error('Failed to modify the Image Width'); 379}) 380``` 381 382## modifyImageProperties<sup>12+</sup> 383 384modifyImageProperties(records: Record<PropertyKey, string|null>): Promise\<void> 385 386Modifies the values of properties in this image. This API uses a promise to return the result. This API applies only to images that are in JPEG, PNG, or HEIF (depending on the hardware) format and contain the EXIF information. You can use the **supportedFormats** property to check whether the EXIF read/write operation for images in HEIF format is supported. 387 388> **NOTE** 389> 390> The property byte length is changed when the **modifyImageProperties** API is called to modify the values of properties. Currently, you can call the API in an ImageSource instance created based on a file descriptor or path, but not an ImageSource instance created based on buffers. 391> 392 393**System capability**: SystemCapability.Multimedia.Image.ImageSource 394 395**Parameters** 396 397| Name | Type | Mandatory| Description | 398| ------- | ------ | ---- | ------------ | 399| records | Record<[PropertyKey](arkts-apis-image-e.md#propertykey7), string \| null> | Yes | Array of property names and property values.| 400 401**Return value** 402 403| Type | Description | 404| -------------- | --------------------------- | 405| Promise\<void> | Promise that returns no value.| 406 407**Error codes** 408 409For details about the error codes, see [Image Error Codes](errorcode-image.md). 410 411| ID| Error Message| 412| ------- | --------------------------------------------| 413| 401 | Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types;3.Parameter verification failed; | 414| 62980123| The image does not support EXIF decoding. | 415| 62980135| The EXIF value is invalid. | 416| 62980146| The EXIF data failed to be written to the file. | 417 418**Example** 419 420```ts 421import { image } from '@kit.ImageKit'; 422import { BusinessError } from '@kit.BasicServicesKit'; 423 424let keyValues: Record<PropertyKey, string|null> = { 425 [image.PropertyKey.IMAGE_WIDTH] : "1024", 426 [image.PropertyKey.IMAGE_LENGTH] : "1024" 427}; 428let checkKey = [image.PropertyKey.IMAGE_WIDTH, image.PropertyKey.IMAGE_LENGTH]; 429imageSourceApi.modifyImageProperties(keyValues).then(() => { 430 imageSourceApi.getImageProperties(checkKey).then((data) => { 431 console.info(JSON.stringify(data)); 432 }).catch((err: BusinessError) => { 433 console.error(JSON.stringify(err)); 434 }); 435}).catch((err: BusinessError) => { 436 console.error(JSON.stringify(err)); 437}); 438``` 439 440## updateData<sup>9+</sup> 441 442updateData(buf: ArrayBuffer, isFinished: boolean, offset: number, length: number): Promise\<void> 443 444Updates incremental data. This API uses a promise to return the result. 445 446**System capability**: SystemCapability.Multimedia.Image.ImageSource 447 448**Parameters** 449 450| Name | Type | Mandatory| Description | 451| ---------- | ----------- | ---- | ------------ | 452| buf | ArrayBuffer | Yes | Buffer for storing the incremental data. | 453| isFinished | boolean | Yes | Whether data update is complete. The value **true** means that the data update is complete and the last segment of data is stored in the buffer. The value **false** means that the data update is still in progress.| 454| offset | number | Yes | Offset of the data in the buffer, measured from the start of the entire image file, in bytes. | 455| length | number | Yes | Length of the buffer, in bytes. | 456 457**Return value** 458 459| Type | Description | 460| -------------- | -------------------------- | 461| Promise\<void> | Promise that returns no value.| 462 463**Example** 464 465```ts 466import { BusinessError } from '@kit.BasicServicesKit'; 467 468const array: ArrayBuffer = new ArrayBuffer(100); 469imageSourceApi.updateData(array, false, 0, 10).then(() => { 470 console.info('Succeeded in updating data.'); 471}).catch((err: BusinessError) => { 472 console.error(`Failed to update data.code is ${err.code},message is ${err.message}`); 473}) 474``` 475 476 477## updateData<sup>9+</sup> 478 479updateData(buf: ArrayBuffer, isFinished: boolean, offset: number, length: number, callback: AsyncCallback\<void>): void 480 481Updates incremental data. This API uses an asynchronous callback to return the result. 482 483**System capability**: SystemCapability.Multimedia.Image.ImageSource 484 485**Parameters** 486 487| Name | Type | Mandatory| Description | 488| ---------- | ------------------- | ---- | -------------------- | 489| buf | ArrayBuffer | Yes | Buffer for storing the incremental data. | 490| isFinished | boolean | Yes | Whether data update is complete. The value **true** means that the data update is complete and the last segment of data is stored in the buffer. The value **false** means that the data update is still in progress.| 491| offset | number | Yes | Offset of the data in the buffer, measured from the start of the entire image file, in bytes. | 492| length | number | Yes | Length of the buffer, in bytes. | 493| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 494 495**Example** 496 497```ts 498import { BusinessError } from '@kit.BasicServicesKit'; 499 500const array: ArrayBuffer = new ArrayBuffer(100); 501imageSourceApi.updateData(array, false, 0, 10, (err: BusinessError) => { 502 if (err) { 503 console.error(`Failed to update data.code is ${err.code},message is ${err.message}`); 504 } else { 505 console.info('Succeeded in updating data.'); 506 } 507}) 508``` 509 510## createPicture<sup>13+</sup> 511 512createPicture(options?: DecodingOptionsForPicture): Promise\<Picture> 513 514Creates a Picture object based on decoding options. This API uses a promise to return the result. 515 516**System capability**: SystemCapability.Multimedia.Image.ImageSource 517 518**Parameters** 519 520| Name | Type | Mandatory| Description | 521| ------- | ------------------------------------------------------ | ---- | ---------- | 522| options | [DecodingOptionsForPicture](arkts-apis-image-i.md#decodingoptionsforpicture13) | No | Decoding options.| 523 524**Return value** 525 526| Type | Description | 527| ---------------------------- | -------------------------- | 528| Promise\<[Picture](arkts-apis-image-Picture.md)> | Promise used to return the Picture object.| 529 530**Error codes** 531 532For details about the error codes, see [Image Error Codes](errorcode-image.md). 533 534| ID| Error Message | 535| -------- | ------------------------------------------------------------ | 536| 401 | Parameter error.Possible causes: 1.Mandatory parameters are left unspecified.2.Incorrect parameter types; 3.Parameter verification failed. | 537| 7700301 | Failed to decode image. | 538 539**Example** 540 541```ts 542import { image } from '@kit.ImageKit'; 543 544async function CreatePicture() { 545 let options: image.DecodingOptionsForPicture = { 546 desiredAuxiliaryPictures: [image.AuxiliaryPictureType.GAINMAP] // GAINMAP indicates the type of the auxiliary picture to be decoded. 547 }; 548 let pictureObj: image.Picture = await imageSourceApi.createPicture(options); 549 if (pictureObj != null) { 550 console.info('Create picture succeeded'); 551 } else { 552 console.error('Create picture failed'); 553 } 554} 555``` 556 557## createPictureAtIndex<sup>20+</sup> 558 559createPictureAtIndex(index: number): Promise\<Picture> 560 561Creates a Picture object from the specified index of an image. Only GIF images are supported. This API uses a promise to return the result. 562 563**System capability**: SystemCapability.Multimedia.Image.ImageSource 564 565**Parameters** 566 567| Name| Type | Mandatory| Description | 568| ------ | ------ | ---- | ------------------------------------------------ | 569| index | number | Yes | Index of the image. The value range is [0, Number of frames – 1].| 570 571**Return value** 572 573| Type | Description | 574| ---------------------------- | ---------------------------- | 575| Promise\<[Picture](arkts-apis-image-Picture.md)> | Promise used to return the Picture object.| 576 577**Error codes** 578 579For details about the error codes, see [Image Error Codes](errorcode-image.md). 580 581| ID| Error Message | 582| -------- | ------------------------------------------------------------ | 583| 7700101 | Bad source. | 584| 7700102 | Unsupported MIME type. | 585| 7700103 | Image too large. | 586| 7700203 | Unsupported options. For example, index is invalid. | 587| 7700301 | Decoding failed. | 588 589**Example** 590 591```ts 592import { image } from '@kit.ImageKit'; 593 594async function CreatePictures() { 595 let frameCount: number = await imageSourceApi.getFrameCount(); 596 for (let index = 0; index < frameCount; index++) { 597 try { 598 let pictureObj: image.Picture = await imageSourceApi.createPictureAtIndex(index); 599 console.info('Create picture succeeded for frame: ' + index); 600 } catch (e) { 601 console.error('Create picture failed for frame: ' + index); 602 } 603 } 604} 605``` 606 607## createPixelMap<sup>7+</sup> 608 609createPixelMap(options?: DecodingOptions): Promise\<PixelMap> 610 611Creates a PixelMap object based on decoding options. This API uses a promise to return the result. 612 613Starting from API version 15, you are advised to use [createPixelMapUsingAllocator](#createpixelmapusingallocator15). This API can be used to specify the memory type [AllocatorType](arkts-apis-image-e.md#allocatortype15) of the output PixelMap. For details, see [Allocating Memory for Image Decoding (ArkTS)](../../media/image/image-allocator-type.md). 614 615**Widget capability**: This API can be used in ArkTS widgets since API version 12. 616 617**Atomic service API**: This API can be used in atomic services since API version 11. 618 619**System capability**: SystemCapability.Multimedia.Image.ImageSource 620 621**Parameters** 622 623| Name | Type | Mandatory| Description | 624| ------- | ------------------------------------ | ---- | ---------- | 625| options | [DecodingOptions](arkts-apis-image-i.md#decodingoptions7) | No | Decoding options.| 626 627**Return value** 628 629| Type | Description | 630| -------------------------------- | --------------------- | 631| Promise\<[PixelMap](arkts-apis-image-PixelMap.md)> | Promise used to return the PixelMap object.| 632 633**Example** 634 635```ts 636import { BusinessError } from '@kit.BasicServicesKit'; 637 638imageSourceApi.createPixelMap().then((pixelMap: image.PixelMap) => { 639 console.info('Succeeded in creating pixelMap object through image decoding parameters.'); 640}).catch((error: BusinessError) => { 641 console.error('Failed to create pixelMap object through image decoding parameters.'); 642}) 643``` 644 645## createPixelMap<sup>7+</sup> 646 647createPixelMap(callback: AsyncCallback\<PixelMap>): void 648 649Creates a PixelMap object based on the default parameters. This API uses an asynchronous callback to return the result. 650 651Starting from API version 15, you are advised to use [createPixelMapUsingAllocator](#createpixelmapusingallocator15). This API can be used to specify the memory type [AllocatorType](arkts-apis-image-e.md#allocatortype15) of the output PixelMap. For details, see [Allocating Memory for Image Decoding (ArkTS)](../../media/image/image-allocator-type.md). 652 653**Widget capability**: This API can be used in ArkTS widgets since API version 12. 654 655**Atomic service API**: This API can be used in atomic services since API version 11. 656 657**System capability**: SystemCapability.Multimedia.Image.ImageSource 658 659**Parameters** 660 661| Name | Type | Mandatory| Description | 662| -------- | ------------------------------------- | ---- | -------------------------- | 663| callback | AsyncCallback<[PixelMap](arkts-apis-image-PixelMap.md)> | Yes | Callback used to return the result. If the operation is successful, **err** is undefined and **data** is the PixelMap object obtained; otherwise, **err** is an error object.| 664 665**Example** 666 667```ts 668import { BusinessError } from '@kit.BasicServicesKit'; 669 670imageSourceApi.createPixelMap((err: BusinessError, pixelMap: image.PixelMap) => { 671 if (err) { 672 console.error(`Failed to create pixelMap.code is ${err.code},message is ${err.message}`); 673 } else { 674 console.info('Succeeded in creating pixelMap object.'); 675 } 676}) 677``` 678 679## createPixelMap<sup>7+</sup> 680 681createPixelMap(options: DecodingOptions, callback: AsyncCallback\<PixelMap>): void 682 683Creates a PixelMap object based on decoding options. This API uses a promise to return the result. 684 685Starting from API version 15, you are advised to use [createPixelMapUsingAllocator](#createpixelmapusingallocator15). This API can be used to specify the memory type [AllocatorType](arkts-apis-image-e.md#allocatortype15) of the output PixelMap. For details, see [Allocating Memory for Image Decoding (ArkTS)](../../media/image/image-allocator-type.md). 686 687**Widget capability**: This API can be used in ArkTS widgets since API version 12. 688 689**Atomic service API**: This API can be used in atomic services since API version 11. 690 691**System capability**: SystemCapability.Multimedia.Image.ImageSource 692 693**Parameters** 694 695| Name | Type | Mandatory| Description | 696| -------- | ------------------------------------- | ---- | -------------------------- | 697| options | [DecodingOptions](arkts-apis-image-i.md#decodingoptions7) | Yes | Decoding options. | 698| callback | AsyncCallback<[PixelMap](arkts-apis-image-PixelMap.md)> | Yes | Callback used to return the result. If the operation is successful, **err** is undefined and **data** is the PixelMap object obtained; otherwise, **err** is an error object.| 699 700**Example** 701 702```ts 703import { BusinessError } from '@kit.BasicServicesKit'; 704 705let decodingOptions: image.DecodingOptions = { 706 sampleSize: 1, 707 editable: true, 708 desiredSize: { width: 1, height: 2 }, 709 rotate: 10, 710 desiredPixelFormat: image.PixelMapFormat.RGBA_8888, 711 desiredRegion: { size: { width: 1, height: 2 }, x: 0, y: 0 }, 712 // If both desiredSize and desiredRegion are passed to the decoding API, you must also include cropAndScaleStrategy to determine whether to crop or scale first. CROP_FIRST is recommended. 713 cropAndScaleStrategy: image.CropAndScaleStrategy.CROP_FIRST, 714 index: 0 715}; 716imageSourceApi.createPixelMap(decodingOptions, (err: BusinessError, pixelMap: image.PixelMap) => { 717 if (err) { 718 console.error(`Failed to create pixelMap.code is ${err.code},message is ${err.message}`); 719 } else { 720 console.info('Succeeded in creating pixelMap object.'); 721 } 722}) 723``` 724 725## createPixelMapSync<sup>12+</sup> 726 727createPixelMapSync(options?: DecodingOptions): PixelMap 728 729Creates a PixelMap object based on decoding options. This API returns the result synchronously. 730 731Starting from API version 15, you are advised to use [createPixelMapUsingAllocatorSync](#createpixelmapusingallocatorsync15). This API can be used to specify the memory type [AllocatorType](arkts-apis-image-e.md#allocatortype15) of the output PixelMap. For details, see [Allocating Memory for Image Decoding (ArkTS)](../../media/image/image-allocator-type.md). 732 733**System capability**: SystemCapability.Multimedia.Image.ImageSource 734 735**Parameters** 736 737| Name | Type | Mandatory| Description | 738| -------- | ------------------------------------- | ---- | -------------------------- | 739| options | [DecodingOptions](arkts-apis-image-i.md#decodingoptions7) | No | Decoding options. | 740 741**Return value** 742 743| Type | Description | 744| -------------------------------- | --------------------- | 745| [PixelMap](arkts-apis-image-PixelMap.md) | PixelMap object.| 746 747**Example** 748 749<!--code_no_check--> 750```ts 751import { common } from '@kit.AbilityKit'; 752import { image } from '@kit.ImageKit'; 753 754// Obtain the context from the component and ensure that the return value of this.getUIContext().getHostContext() is UIAbilityContext. 755let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 756// 'test.jpg' is only an example. Replace it with the actual one in use. Otherwise, the imageSource instance fails to be created, and subsequent operations cannot be performed. 757let filePath: string = context.filesDir + "/test.jpg"; 758let imageSource = image.createImageSource(filePath); 759let decodingOptions: image.DecodingOptions = { 760 sampleSize: 1, 761 editable: true, 762 desiredSize: { width: 1, height: 2 }, 763 rotate: 10, 764 desiredPixelFormat: image.PixelMapFormat.RGBA_8888, 765 desiredRegion: { size: { width: 1, height: 2 }, x: 0, y: 0 }, 766 // If both desiredSize and desiredRegion are passed to the decoding API, you must also include cropAndScaleStrategy to determine whether to crop or scale first. CROP_FIRST is recommended. 767 cropAndScaleStrategy: image.CropAndScaleStrategy.CROP_FIRST, 768 index: 0 769}; 770let pixelmap = imageSource.createPixelMapSync(decodingOptions); 771if (pixelmap != undefined) { 772 console.info('Succeeded in creating pixelMap object.'); 773} else { 774 console.error('Failed to create pixelMap.'); 775} 776``` 777 778## createPixelMapList<sup>10+</sup> 779 780createPixelMapList(options?: DecodingOptions): Promise<Array\<PixelMap>> 781 782Creates an array of PixelMap objects based on decoding options. This API uses a promise to return the result. For dynamic images such as GIF and WebP images, this API returns the data of each frame of the image. For static images, this API returns the data of the unique frame of the image. 783 784> **NOTE** 785> 786> This function decodes all frames at once. If the number of frames is high or the size of individual frames is large, it can lead to significant memory usage. In these cases, you are advised to use the **Image** component for displaying animations. The **Image** component decodes frames one by one, which uses less memory than this function. 787 788**System capability**: SystemCapability.Multimedia.Image.ImageSource 789 790**Parameters** 791 792| Name | Type | Mandatory| Description | 793| -------- | ------------------------------------- | ---- | -------------------------- | 794| options | [DecodingOptions](arkts-apis-image-i.md#decodingoptions7) | No | Decoding options. | 795 796**Return value** 797 798| Type | Description | 799| -------------------------------- | --------------------- | 800| Promise<Array<[PixelMap](arkts-apis-image-PixelMap.md)>> | Promise used to return an array of PixelMap objects.| 801 802**Error codes** 803 804For details about the error codes, see [Image Error Codes](errorcode-image.md). 805 806| ID| Error Message| 807| ------- | --------------------------------------------| 808| 62980096| The operation failed. Possible cause: 1.Image upload exception. 2. Decoding process exception. 3. Insufficient memory. | 809| 62980099 | The shared memory data is abnormal. | 810| 62980101 | The image data is abnormal. | 811| 62980103| The image data is not supported. | 812| 62980106 | The image data is too large. This status code is thrown when an error occurs during the process of checking size. | 813| 62980109 | Failed to crop the image. | 814| 62980111| The image source data is incomplete. | 815| 62980115 | Invalid image parameter. | 816| 62980116 | Failed to decode the image. | 817| 62980118| Failed to create the image plugin. | 818| 62980137 | Invalid media operation. | 819| 62980173 | The DMA memory does not exist. | 820| 62980174 | The DMA memory data is abnormal. | 821 822**Example** 823 824```ts 825import { BusinessError } from '@kit.BasicServicesKit'; 826 827let decodeOpts: image.DecodingOptions = { 828 sampleSize: 1, 829 editable: true, 830 desiredSize: { width: 198, height: 202 }, 831 rotate: 0, 832 desiredPixelFormat: image.PixelMapFormat.RGBA_8888, 833 index: 0, 834}; 835imageSourceApi.createPixelMapList(decodeOpts).then((pixelMapList: Array<image.PixelMap>) => { 836 console.info('Succeeded in creating pixelMapList object.'); 837}).catch((err: BusinessError) => { 838 console.error(`Failed to create pixelMapList object, error code is ${err}`); 839}) 840``` 841 842## createPixelMapList<sup>10+</sup> 843 844createPixelMapList(callback: AsyncCallback<Array\<PixelMap>>): void 845 846Creates an array of PixelMap objects based on the default parameters. This API uses an asynchronous callback to return the result. For dynamic images such as GIF and WebP images, this API returns the data of each frame of the image. For static images, this API returns the data of the unique frame of the image. 847 848> **NOTE** 849> 850> This function decodes all frames at once. If the number of frames is high or the size of individual frames is large, it can lead to significant memory usage. In these cases, you are advised to use the **Image** component for displaying animations. The **Image** component decodes frames one by one, which uses less memory than this function. 851 852**System capability**: SystemCapability.Multimedia.Image.ImageSource 853 854**Parameters** 855 856| Name | Type | Mandatory| Description | 857| -------- | ------------------------------------- | ---- | -------------------------- | 858| callback | AsyncCallback<Array<[PixelMap](arkts-apis-image-PixelMap.md)>> | Yes | Callback used to return the result. If the operation is successful, **err** is undefined and **data** is the array of PixelMap objects obtained; otherwise, **err** is an error object. | 859 860**Error codes** 861 862For details about the error codes, see [Image Error Codes](errorcode-image.md). 863 864| ID| Error Message| 865| ------- | --------------------------------------------| 866| 62980096 | The operation failed. Possible cause: 1.Image upload exception. 2. Decoding process exception. 3. Insufficient memory. | 867| 62980099 | The shared memory data is abnormal. | 868| 62980101 | The image data is abnormal. | 869| 62980103 | The image data is not supported. | 870| 62980106 | The image data is too large. This status code is thrown when an error occurs during the process of checking size. | 871| 62980109 | Failed to crop the image. | 872| 62980111 | The image source data is incomplete. | 873| 62980115 | Invalid image parameter. | 874| 62980116 | Failed to decode the image. | 875| 62980118 | Failed to create the image plugin. | 876| 62980137 | Invalid media operation. | 877| 62980173 | The DMA memory does not exist. | 878| 62980174 | The DMA memory data is abnormal. | 879 880**Example** 881 882```ts 883import { BusinessError } from '@kit.BasicServicesKit'; 884 885imageSourceApi.createPixelMapList((err: BusinessError, pixelMapList: Array<image.PixelMap>) => { 886 if (err) { 887 console.error(`Failed to create pixelMapList object, error code is ${err}`); 888 } else { 889 console.info('Succeeded in creating pixelMapList object.'); 890 } 891}) 892``` 893 894## createPixelMapList<sup>10+</sup> 895 896createPixelMapList(options: DecodingOptions, callback: AsyncCallback<Array\<PixelMap>>): void 897 898Creates an array of PixelMap objects based on decoding options. This API uses an asynchronous callback to return the result. For dynamic images such as GIF and WebP images, this API returns the data of each frame of the image. For static images, this API returns the data of the unique frame of the image. 899 900> **NOTE** 901> 902> This function decodes all frames at once. If the number of frames is high or the size of individual frames is large, it can lead to significant memory usage. In these cases, you are advised to use the **Image** component for displaying animations. The **Image** component decodes frames one by one, which uses less memory than this function. 903 904**System capability**: SystemCapability.Multimedia.Image.ImageSource 905 906**Parameters** 907 908| Name | Type | Mandatory| Description | 909| -------- | -------------------- | ---- | ---------------------------------- | 910| options | [DecodingOptions](arkts-apis-image-i.md#decodingoptions7) | Yes| Decoding options.| 911| callback | AsyncCallback<Array<[PixelMap](arkts-apis-image-PixelMap.md)>> | Yes | Callback used to return the result. If the operation is successful, **err** is undefined and **data** is the array of PixelMap objects obtained; otherwise, **err** is an error object. | 912 913**Error codes** 914 915For details about the error codes, see [Image Error Codes](errorcode-image.md). 916 917| ID| Error Message| 918| ------- | --------------------------------------------| 919| 62980096 | The operation failed. Possible cause: 1.Image upload exception. 2. Decoding process exception. 3. Insufficient memory. | 920| 62980099 | The shared memory data is abnormal. | 921| 62980101 | The image data is abnormal. | 922| 62980103 | The image data is not supported. | 923| 62980106 | The image data is too large. This status code is thrown when an error occurs during the process of checking size. | 924| 62980109 | Failed to crop the image. | 925| 62980111 | The image source data is incomplete. | 926| 62980115 | Invalid image parameter. | 927| 62980116 | Failed to decode the image. | 928| 62980118 | Failed to create the image plugin. | 929| 62980137 | Invalid media operation. | 930| 62980173 | The DMA memory does not exist. | 931| 62980174 | The DMA memory data is abnormal. | 932 933**Example** 934 935```ts 936import { BusinessError } from '@kit.BasicServicesKit'; 937 938let decodeOpts: image.DecodingOptions = { 939 sampleSize: 1, 940 editable: true, 941 desiredSize: { width: 198, height: 202 }, 942 rotate: 0, 943 desiredPixelFormat: image.PixelMapFormat.RGBA_8888, 944 index: 0, 945}; 946imageSourceApi.createPixelMapList(decodeOpts, (err: BusinessError, pixelMapList: Array<image.PixelMap>) => { 947 if (err) { 948 console.error(`Failed to create pixelMapList object, error code is ${err}`); 949 } else { 950 console.info('Succeeded in creating pixelMapList object.'); 951 } 952}) 953``` 954 955## createPixelMapUsingAllocator<sup>15+</sup> 956 957createPixelMapUsingAllocator(options?: DecodingOptions, allocatorType?: AllocatorType): Promise\<PixelMap> 958 959Creates a PixelMap object based on decoding options and memory type. This API uses a promise to return the result. For details about how to use the API, see [Allocating Memory for Image Decoding (ArkTS)](../../media/image/image-allocator-type.md). 960 961**System capability**: SystemCapability.Multimedia.Image.ImageSource 962 963**Parameters** 964 965| Name | Type | Mandatory| Description | 966| ------------- | ------------------------------------ | ---- | ------------------------ | 967| options | [DecodingOptions](arkts-apis-image-i.md#decodingoptions7) | No | Decoding options. | 968| allocatorType | [AllocatorType](arkts-apis-image-e.md#allocatortype15) | No | Type of the memory. The default value is **AllocatorType.AUTO**.| 969 970**Return value** 971 972| Type | Description | 973| -------------------------------- | --------------------------- | 974| Promise\<[PixelMap](arkts-apis-image-PixelMap.md)> | Promise used to return the PixelMap object.| 975 976**Error codes** 977 978For details about the error codes, see [Image Error Codes](errorcode-image.md). 979 980| ID| Error Message | 981| -------- | ------------------------------------------------------------ | 982| 401 | Parameter error.Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types;3.Parameter verification failed. | 983| 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. | 984| 7700102 | Unsupported mimetype. | 985| 7700103 | Image too large. This status code is thrown when an error occurs during the process of checking size. | 986| 7700201 | Unsupported allocator type, e.g., use share memory to decode a HDR image as only DMA supported hdr metadata. | 987| 7700203 | Unsupported options, e.g., cannot convert image into desired pixel format. | 988| 7700301 | Failed to decode image. | 989| 7700302 | Failed to allocate memory. | 990 991**Example** 992 993<!--code_no_check--> 994```ts 995import { common } from '@kit.AbilityKit'; 996import image from '@ohos.multimedia.image'; 997 998// Obtain the context from the component and ensure that the return value of this.getUIContext().getHostContext() is UIAbilityContext. 999let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 1000// 'test.jpg' is only an example. Replace it with the actual one in use. Otherwise, the imageSource instance fails to be created, and subsequent operations cannot be performed. 1001let filePath: string = context.filesDir + "/test.jpg"; 1002let imageSource = image.createImageSource(filePath); 1003let decodingOptions: image.DecodingOptions = { 1004 editable: true, 1005 desiredSize: { width: 3072, height: 4096 }, 1006 rotate: 10, 1007 desiredPixelFormat: image.PixelMapFormat.RGBA_8888, 1008 desiredRegion: { size: { width: 3072, height: 4096 }, x: 0, y: 0 }, 1009 // If both desiredSize and desiredRegion are passed to the decoding API, you must also include cropAndScaleStrategy to determine whether to crop or scale first. CROP_FIRST is recommended. 1010 cropAndScaleStrategy: image.CropAndScaleStrategy.CROP_FIRST, 1011 index: 0 1012}; 1013let pixelmap = imageSource.createPixelMapUsingAllocator(decodingOptions, image.AllocatorType.AUTO); 1014if (pixelmap != undefined) { 1015 console.info('Succeeded in creating pixelMap object.'); 1016} else { 1017 console.error('Failed to create pixelMap.'); 1018} 1019``` 1020 1021## createPixelMapUsingAllocatorSync<sup>15+</sup> 1022 1023createPixelMapUsingAllocatorSync(options?: DecodingOptions, allocatorType?: AllocatorType): PixelMap 1024 1025Creates a PixelMap object based on decoding options and memory type. This API returns the result synchronously. For details about how to use the API, see [Allocating Memory for Image Decoding (ArkTS)](../../media/image/image-allocator-type.md). 1026 1027**System capability**: SystemCapability.Multimedia.Image.ImageSource 1028 1029**Parameters** 1030 1031| Name | Type | Mandatory| Description | 1032| ------------- | ------------------------------------ | ---- | ------------------------ | 1033| options | [DecodingOptions](arkts-apis-image-i.md#decodingoptions7) | No | Decoding options. | 1034| allocatorType | [AllocatorType](arkts-apis-image-e.md#allocatortype15) | No | Type of the memory. The default value is **AllocatorType.AUTO**.| 1035 1036**Return value** 1037 1038| Type | Description | 1039| ---------------------- | ---------------------- | 1040| [PixelMap](arkts-apis-image-PixelMap.md) | PixelMap object.| 1041 1042**Error codes** 1043 1044For details about the error codes, see [Image Error Codes](errorcode-image.md). 1045 1046| ID| Error Message | 1047| -------- | ------------------------------------------------------------ | 1048| 401 | Parameter error.Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types;3.Parameter verification failed. | 1049| 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. | 1050| 7700102 | Unsupported mimetype. | 1051| 7700103 | Image too large. This status code is thrown when an error occurs during the process of checking size. | 1052| 7700201 | Unsupported allocator type, e.g., use share memory to decode a HDR image as only DMA supported hdr metadata. | 1053| 7700203 | Unsupported options, e.g., cannot convert image into desired pixel format. | 1054| 7700301 | Failed to decode image. | 1055| 7700302 | Failed to allocate memory. | 1056 1057**Example** 1058 1059<!--code_no_check--> 1060```ts 1061import { common } from '@kit.AbilityKit'; 1062import image from '@ohos.multimedia.image'; 1063 1064// Obtain the context from the component and ensure that the return value of this.getUIContext().getHostContext() is UIAbilityContext. 1065let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 1066// 'test.jpg' is only an example. Replace it with the actual one in use. Otherwise, the imageSource instance fails to be created, and subsequent operations cannot be performed. 1067let filePath: string = context.filesDir + "/test.jpg"; 1068let imageSource = image.createImageSource(filePath); 1069let 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 // If both desiredSize and desiredRegion are passed to the decoding API, you must also include cropAndScaleStrategy to determine whether to crop or scale first. CROP_FIRST is recommended. 1076 cropAndScaleStrategy: image.CropAndScaleStrategy.CROP_FIRST, 1077 index: 0 1078}; 1079let pixelmap = imageSource.createPixelMapUsingAllocatorSync(decodingOptions, image.AllocatorType.AUTO); 1080if (pixelmap != undefined) { 1081 console.info('Succeeded in creating pixelMap object.'); 1082} else { 1083 console.error('Failed to create pixelMap.'); 1084} 1085``` 1086 1087## getDelayTimeList<sup>10+</sup> 1088 1089getDelayTimeList(callback: AsyncCallback<Array\<number>>): void 1090 1091Obtains an array of delay times. This API uses an asynchronous callback to return the result. This API applies only to images in GIF or WebP format. 1092 1093**System capability**: SystemCapability.Multimedia.Image.ImageSource 1094 1095**Parameters** 1096 1097| Name | Type | Mandatory| Description | 1098| -------- | -------------------- | ---- | ---------------------------------- | 1099| callback | AsyncCallback<Array\<number>> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the array of delay times obtained; otherwise, **err** is an error object.| 1100 1101**Error codes** 1102 1103For details about the error codes, see [Image Error Codes](errorcode-image.md). 1104 1105| ID| Error Message| 1106| ------- | --------------------------------------------| 1107| 62980096| The operation failed. Possible cause: 1.Image upload exception. 2. Decoding process exception. 3. Insufficient memory. | 1108| 62980110| The image source data is incorrect. | 1109| 62980111| The image source data is incomplete. | 1110| 62980115 | Invalid image parameter. | 1111| 62980116| Failed to decode the image. | 1112| 62980118| Failed to create the image plugin. | 1113| 62980122| Failed to decode the image header. | 1114| 62980149 | Invalid MIME type for the image source. | 1115 1116**Example** 1117 1118```ts 1119import { BusinessError } from '@kit.BasicServicesKit'; 1120 1121imageSourceApi.getDelayTimeList((err: BusinessError, delayTimes: Array<number>) => { 1122 if (err) { 1123 console.error(`Failed to get delayTimes object.code is ${err.code},message is ${err.message}`); 1124 } else { 1125 console.info('Succeeded in getting delayTimes object.'); 1126 } 1127}) 1128``` 1129 1130## getDelayTimeList<sup>10+</sup> 1131 1132getDelayTimeList(): Promise<Array\<number>> 1133 1134Obtains an array of delay times. This API uses a promise to return the result. This API applies only to images in GIF or WebP format. 1135 1136**System capability**: SystemCapability.Multimedia.Image.ImageSource 1137 1138**Return value** 1139 1140| Type | Description | 1141| -------------- | --------------------------- | 1142| Promise<Array\<number>> | Promise used to return an array of delay times.| 1143 1144**Error codes** 1145 1146For details about the error codes, see [Image Error Codes](errorcode-image.md). 1147 1148| ID| Error Message| 1149| ------- | --------------------------------------------| 1150| 62980096 | The operation failed. Possible cause: 1.Image upload exception. 2. Decoding process exception. 3. Insufficient memory. | 1151| 62980110 | The image source data is incorrect. | 1152| 62980111 | The image source data is incomplete. | 1153| 62980115 | Invalid image parameter. | 1154| 62980116 | Failed to decode the image. | 1155| 62980118 | Failed to create the image plugin. | 1156| 62980122 | Failed to decode the image header. | 1157| 62980149 | Invalid MIME type for the image source. | 1158 1159**Example** 1160 1161```ts 1162import { BusinessError } from '@kit.BasicServicesKit'; 1163 1164imageSourceApi.getDelayTimeList().then((delayTimes: Array<number>) => { 1165 console.info('Succeeded in getting delayTimes object.'); 1166}).catch((err: BusinessError) => { 1167 console.error(`Failed to get delayTimes object.code is ${err.code},message is ${err.message}`); 1168}) 1169``` 1170 1171## getFrameCount<sup>10+</sup> 1172 1173getFrameCount(callback: AsyncCallback\<number>): void 1174 1175Obtains the number of frames. This API uses an asynchronous callback to return the result. 1176 1177**System capability**: SystemCapability.Multimedia.Image.ImageSource 1178 1179**Parameters** 1180 1181| Name | Type | Mandatory| Description | 1182| -------- | -------------------- | ---- | ---------------------------------- | 1183| callback | AsyncCallback\<number> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the number of frames obtained; otherwise, **err** is an error object.| 1184 1185**Error codes** 1186 1187For details about the error codes, see [Image Error Codes](errorcode-image.md). 1188 1189| ID| Error Message| 1190| ------- | --------------------------------------------| 1191| 62980096| The operation failed. Possible cause: 1.Image upload exception. 2. Decoding process exception. 3. Insufficient memory. | 1192| 62980111| The image source data is incomplete. | 1193| 62980112| The image format does not match. | 1194| 62980113| Unknown image format.The image data provided is not in a recognized or supported format, or it may be occorrupted. | 1195| 62980115| Invalid image parameter. | 1196| 62980116| Failed to decode the image. | 1197| 62980118| Failed to create the image plugin. | 1198| 62980122| Failed to decode the image header. | 1199| 62980137| Invalid media operation. | 1200 1201**Example** 1202 1203```ts 1204import { BusinessError } from '@kit.BasicServicesKit'; 1205 1206imageSourceApi.getFrameCount((err: BusinessError, frameCount: number) => { 1207 if (err) { 1208 console.error(`Failed to get frame count.code is ${err.code},message is ${err.message}`); 1209 } else { 1210 console.info('Succeeded in getting frame count.'); 1211 } 1212}) 1213``` 1214 1215## getFrameCount<sup>10+</sup> 1216 1217getFrameCount(): Promise\<number> 1218 1219Obtains the number of frames. This API uses a promise to return the result. 1220 1221**System capability**: SystemCapability.Multimedia.Image.ImageSource 1222 1223**Return value** 1224 1225| Type | Description | 1226| -------------- | --------------------------- | 1227| Promise\<number> | Promise used to return the number of frames.| 1228 1229**Error codes** 1230 1231For details about the error codes, see [Image Error Codes](errorcode-image.md). 1232 1233| ID| Error Message| 1234| ------- | --------------------------------------------| 1235| 62980096 | The operation failed. Possible cause: 1.Image upload exception. 2. Decoding process exception. 3. Insufficient memory. | 1236| 62980111 | The image source data is incomplete. | 1237| 62980112 | The image format does not match. | 1238| 62980113| Unknown image format.The image data provided is not in a recognized or supported format, or it may be occorrupted. | 1239| 62980115 | Invalid image parameter. | 1240| 62980116 | Failed to decode the image. | 1241| 62980118 | Failed to create the image plugin. | 1242| 62980122 | Failed to decode the image header. | 1243| 62980137 | Invalid media operation. | 1244 1245**Example** 1246 1247```ts 1248import { BusinessError } from '@kit.BasicServicesKit'; 1249 1250imageSourceApi.getFrameCount().then((frameCount: number) => { 1251 console.info('Succeeded in getting frame count.'); 1252}).catch((err: BusinessError) => { 1253 console.error(`Failed to get frame count.code is ${err.code},message is ${err.message}`); 1254}) 1255``` 1256 1257## getDisposalTypeList<sup>12+</sup> 1258 1259getDisposalTypeList(): Promise\<Array\<number>> 1260 1261Obtains the list of disposal types. This API uses a promise to return the result. It is used only for GIF images. 1262 1263**System capability**: SystemCapability.Multimedia.Image.ImageSource 1264 1265**Return value** 1266 1267| Type | Description | 1268| -------------- | --------------------------- | 1269| Promise\<Array\<number>> | Promise used to return an array of disposal types.| 1270 1271**Error codes** 1272 1273For details about the error codes, see [Image Error Codes](errorcode-image.md). 1274 1275| ID| Error Message| 1276| ------- | --------------------------------------------| 1277| 62980096 | The operation failed. Possible cause: 1.Image upload exception. 2. Decoding process exception. 3. Insufficient memory. | 1278| 62980101 | The image data is abnormal. | 1279| 62980137 | Invalid media operation. | 1280| 62980149 | Invalid MIME type for the image source. | 1281 1282**Example** 1283 1284```ts 1285import { BusinessError } from '@kit.BasicServicesKit'; 1286imageSourceApi.getDisposalTypeList().then((disposalTypes: Array<number>) => { 1287 console.info('Succeeded in getting disposalTypes object.'); 1288}).catch((err: BusinessError) => { 1289 console.error(`Failed to get disposalTypes object.code ${err.code},message is ${err.message}`); 1290}) 1291``` 1292 1293## release 1294 1295release(callback: AsyncCallback\<void>): void 1296 1297Releases this ImageSource instance. This API uses an asynchronous callback to return the result. 1298 1299ArkTS supports memory reclamation. Even if the application does not call **release()**, the memory of the ImageSource 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. 1300 1301**System capability**: SystemCapability.Multimedia.Image.ImageSource 1302 1303**Parameters** 1304 1305| Name | Type | Mandatory| Description | 1306| -------- | -------------------- | ---- | ---------------------------------- | 1307| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object. | 1308 1309**Example** 1310 1311```ts 1312import { BusinessError } from '@kit.BasicServicesKit'; 1313 1314imageSourceApi.release((err: BusinessError) => { 1315 if (err) { 1316 console.error(`Failed to release the image source instance.code ${err.code},message is ${err.message}`); 1317 } else { 1318 console.info('Succeeded in releasing the image source instance.'); 1319 } 1320}) 1321``` 1322 1323## release 1324 1325release(): Promise\<void> 1326 1327Releases this ImageSource instance. This API uses a promise to return the result. 1328 1329ArkTS supports memory reclamation. Even if the application does not call **release()**, the memory of the ImageSource 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. 1330 1331**System capability**: SystemCapability.Multimedia.Image.ImageSource 1332 1333**Return value** 1334 1335| Type | Description | 1336| -------------- | --------------------------- | 1337| Promise\<void> | Promise that returns no value.| 1338 1339**Example** 1340 1341```ts 1342import { BusinessError } from '@kit.BasicServicesKit'; 1343 1344imageSourceApi.release().then(() => { 1345 console.info('Succeeded in releasing the image source instance.'); 1346}).catch((error: BusinessError) => { 1347 console.error(`Failed to release the image source instance.code ${error.code},message is ${error.message}`); 1348}) 1349``` 1350 1351## getImageProperty<sup>(deprecated)</sup> 1352 1353getImageProperty(key:string, options?: GetImagePropertyOptions): Promise\<string> 1354 1355Obtains the value of a property with the specified index in this image. This API uses a promise to return the result. This API applies only to images that are in JPEG, PNG, or HEIF<sup>12+</sup> (depending on the hardware) format and contain the EXIF information. You can use the **supportedFormats** property to check whether the EXIF read/write operation for images in HEIF format is supported. 1356 1357> **NOTE** 1358> 1359> This API is deprecated since API version 11. You are advised to use [getImageProperty](#getimageproperty11). 1360 1361**System capability**: SystemCapability.Multimedia.Image.ImageSource 1362 1363**Parameters** 1364 1365| Name | Type | Mandatory| Description | 1366| ------- | ---------------------------------------------------- | ---- | ------------------------------------ | 1367| key | string | Yes | Name of the property. | 1368| options | [GetImagePropertyOptions](arkts-apis-image-i.md#getimagepropertyoptionsdeprecated) | No | Image properties, including the image index and default property value.| 1369 1370**Return value** 1371 1372| Type | Description | 1373| ---------------- | ----------------------------------------------------------------- | 1374| Promise\<string> | Promise used to return the property value. If the operation fails, the default value is returned.| 1375 1376**Example** 1377 1378```ts 1379import { BusinessError } from '@kit.BasicServicesKit'; 1380 1381imageSourceApi.getImageProperty("BitsPerSample") 1382 .then((data: string) => { 1383 console.info('Succeeded in getting the value of the specified attribute key of the image.'); 1384 }).catch((error: BusinessError) => { 1385 console.error('Failed to get the value of the specified attribute key of the image.'); 1386 }) 1387``` 1388 1389## getImageProperty<sup>(deprecated)</sup> 1390 1391getImageProperty(key:string, callback: AsyncCallback\<string>): void 1392 1393Obtains the value of a property with the specified index in this image. This API uses an asynchronous callback to return the result. This API applies only to images that are in JPEG, PNG, or HEIF<sup>12+</sup> (depending on the hardware) format and contain the EXIF information. You can use the **supportedFormats** property to check whether the EXIF read/write operation for images in HEIF format is supported. 1394 1395> **NOTE** 1396> 1397> This API is deprecated since API version 11. You are advised to use [getImageProperty](#getimageproperty11). 1398 1399**System capability**: SystemCapability.Multimedia.Image.ImageSource 1400 1401**Parameters** 1402 1403| Name | Type | Mandatory| Description | 1404| -------- | ---------------------- | ---- | ------------------------------------------------------------ | 1405| key | string | Yes | Name of the property. | 1406| callback | AsyncCallback\<string> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the property value obtained; otherwise, **err** is an error object.| 1407 1408**Example** 1409 1410```ts 1411import { BusinessError } from '@kit.BasicServicesKit'; 1412 1413imageSourceApi.getImageProperty("BitsPerSample", (error: BusinessError, data: string) => { 1414 if (error) { 1415 console.error('Failed to get the value of the specified attribute key of the image.'); 1416 } else { 1417 console.info('Succeeded in getting the value of the specified attribute key of the image.'); 1418 } 1419}) 1420``` 1421 1422## getImageProperty<sup>(deprecated)</sup> 1423 1424getImageProperty(key:string, options: GetImagePropertyOptions, callback: AsyncCallback\<string>): void 1425 1426Obtains the value of a property in this image. This API uses an asynchronous callback to return the result. This API applies only to images that are in JPEG, PNG, or HEIF<sup>12+</sup> (depending on the hardware) format and contain the EXIF information. You can use the **supportedFormats** property to check whether the EXIF read/write operation for images in HEIF format is supported. 1427 1428> **NOTE** 1429> 1430> This API is deprecated since API version 11. You are advised to use [getImageProperty](#getimageproperty11). 1431 1432**System capability**: SystemCapability.Multimedia.Image.ImageSource 1433 1434**Parameters** 1435 1436| Name | Type | Mandatory| Description | 1437| -------- | ---------------------------------------------------- | ---- | ------------------------------------------------------------- | 1438| key | string | Yes | Name of the property. | 1439| options | [GetImagePropertyOptions](arkts-apis-image-i.md#getimagepropertyoptionsdeprecated) | Yes | Image properties, including the image index and default property value. | 1440| callback | AsyncCallback\<string> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined** and **data** is the property value obtained; otherwise, **err** is an error object.| 1441 1442**Example** 1443 1444```ts 1445import { BusinessError } from '@kit.BasicServicesKit'; 1446 1447let property: image.GetImagePropertyOptions = { index: 0, defaultValue: '9999' } 1448imageSourceApi.getImageProperty("BitsPerSample", property, (error: BusinessError, data: string) => { 1449 if (error) { 1450 console.error('Failed to get the value of the specified attribute key of the image.'); 1451 } else { 1452 console.info('Succeeded in getting the value of the specified attribute key of the image.'); 1453 } 1454}) 1455``` 1456 1457## modifyImageProperty<sup>(deprecated)</sup> 1458 1459modifyImageProperty(key: string, value: string): Promise\<void> 1460 1461Modifies the value of a property in this image. This API uses a promise to return the result. This API applies only to images that are in JPEG, PNG, or HEIF<sup>12+</sup> (depending on the hardware) format and contain the EXIF information. You can use the **supportedFormats** property to check whether the EXIF read/write operation for images in HEIF format is supported. 1462 1463> **NOTE** 1464> 1465> The property byte length is changed when the **modifyImageProperty** API is called to modify the value of a property. Currently, you can call the API in an ImageSource instance created based on a file descriptor or path, but not an ImageSource instance created based on buffers. 1466> 1467> This API is deprecated since API version 11. You are advised to use [modifyImageProperty](#modifyimageproperty11). 1468 1469**System capability**: SystemCapability.Multimedia.Image.ImageSource 1470 1471**Parameters** 1472 1473| Name | Type | Mandatory| Description | 1474| ------- | ------ | ---- | ------------ | 1475| key | string | Yes | Name of the property.| 1476| value | string | Yes | New value of the property. | 1477 1478**Return value** 1479 1480| Type | Description | 1481| -------------- | --------------------------- | 1482| Promise\<void> | Promise that returns no value.| 1483 1484**Example** 1485 1486```ts 1487import { BusinessError } from '@kit.BasicServicesKit'; 1488 1489imageSourceApi.modifyImageProperty("ImageWidth", "120").then(() => { 1490 imageSourceApi.getImageProperty("ImageWidth").then((width: string) => { 1491 console.info(`ImageWidth is :${width}`); 1492 }).catch((error: BusinessError) => { 1493 console.error('Failed to get the Image Width.'); 1494 }) 1495}).catch((error: BusinessError) => { 1496 console.error('Failed to modify the Image Width'); 1497}) 1498``` 1499 1500## modifyImageProperty<sup>(deprecated)</sup> 1501 1502modifyImageProperty(key: string, value: string, callback: AsyncCallback\<void>): void 1503 1504Modifies the value of a property in this image. This API uses an asynchronous callback to return the result. This API applies only to images that are in JPEG, PNG, or HEIF<sup>12+</sup> (depending on the hardware) format and contain the EXIF information. You can use the **supportedFormats** property to check whether the EXIF read/write operation for images in HEIF format is supported. 1505 1506> **NOTE** 1507> 1508> The property byte length is changed when the **modifyImageProperty** API is called to modify the value of a property. Currently, you can call the API in an ImageSource instance created based on a file descriptor or path, but not an ImageSource instance created based on buffers. 1509> 1510>This API is deprecated since API version 11. You are advised to use [modifyImageProperty](#modifyimageproperty11). 1511 1512**System capability**: SystemCapability.Multimedia.Image.ImageSource 1513 1514**Parameters** 1515 1516| Name | Type | Mandatory| Description | 1517| -------- | ------------------- | ---- | ------------------------------ | 1518| key | string | Yes | Name of the property. | 1519| value | string | Yes | New value of the property. | 1520| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 1521 1522**Example** 1523 1524```ts 1525import { BusinessError } from '@kit.BasicServicesKit'; 1526 1527imageSourceApi.modifyImageProperty("ImageWidth", "120", (err: BusinessError) => { 1528 if (err) { 1529 console.error(`Failed to modify the Image Width.code is ${err.code}, message is ${err.message}`); 1530 } else { 1531 console.info('Succeeded in modifying the Image Width.'); 1532 } 1533}) 1534``` 1535