1# Interface (PixelMap) 2<!--Kit: Image Kit--> 3<!--Subsystem: Multimedia--> 4<!--Owner: @yaozhupeng--> 5<!--SE: @yaozhupeng--> 6<!--TSE: @zhaoxiaoguang2--> 7 8> **NOTE** 9> 10> - The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version. 11> - The initial APIs of this interface are supported since API version 7. 12 13The **PixelMap** class provides APIs to read or write image data and obtain image information. Before calling any API in PixelMap, you must use [createPixelMap](arkts-apis-image-f.md#imagecreatepixelmap8) to create a PixelMap object. Currently, the maximum size of a serialized PixelMap is 128 MB. A larger size will cause a display failure. The size is calculated as follows: Width * Height * Number of bytes occupied by each pixel. 14 15Since API version 11, PixelMap supports cross-thread calls through workers. If a PixelMap object is invoked by another thread through [Worker](../apis-arkts/js-apis-worker.md), all APIs of the PixelMap object cannot be called in the original thread. Otherwise, error 501 is reported, indicating that the server cannot complete the request. 16 17Before calling any API in PixelMap, you must use [createPixelMap](arkts-apis-image-f.md#imagecreatepixelmap8) to create a PixelMap object. 18 19To develop an atomic service, use [ImageSource](arkts-apis-image-ImageSource.md) to create a PixelMap object. 20 21## Modules to Import 22 23```ts 24import { image } from '@kit.ImageKit'; 25``` 26 27## Properties 28 29**System capability**: SystemCapability.Multimedia.Image.Core 30 31| Name | Type | Read Only| Optional| Description | 32| -----------------| ------- | ---- | ---- | -------------------------- | 33| isEditable<sup>7+</sup> | boolean | Yes | No | Whether the PixelMap is editable. **true** if editable, **false** otherwise.<br>**Atomic service API**: This API can be used in atomic services since API version 11.<br>**Widget capability**: This API can be used in ArkTS widgets since API version 12.| 34| isStrideAlignment<sup>11+</sup> | boolean | Yes | No | Whether the PixelMap uses DMA memory. ** true** if the PixelMap uses DMA memory, **false** otherwise.| 35 36## readPixelsToBuffer<sup>7+</sup> 37 38readPixelsToBuffer(dst: ArrayBuffer): Promise\<void> 39 40Reads the pixels of this PixelMap object based on the PixelMap's pixel format and writes the data to the buffer. This API uses a promise to return the result. 41 42**Widget capability**: This API can be used in ArkTS widgets since API version 12. 43 44**Atomic service API**: This API can be used in atomic services since API version 11. 45 46**System capability**: SystemCapability.Multimedia.Image.Core 47 48**Parameters** 49 50| Name| Type | Mandatory| Description | 51| ------ | ----------- | ---- | ----------------------------------------------------------------------------------------------------- | 52| dst | ArrayBuffer | Yes | Buffer to which the pixels will be written. The buffer size is obtained by calling [getPixelBytesNumber](#getpixelbytesnumber7).| 53 54**Return value** 55 56| Type | Description | 57| -------------- | ----------------------------------------------- | 58| Promise\<void> | Promise that returns no value. | 59 60**Example** 61 62```ts 63import { BusinessError } from '@kit.BasicServicesKit'; 64import { image } from '@kit.ImageKit'; 65 66async function ReadPixelsToBuffer(pixelMap : image.PixelMap) { 67 const readBuffer: ArrayBuffer = new ArrayBuffer(96); // 96 is the size of the pixel buffer to create. The value is calculated as follows: height * width *4. 68 if (pixelMap != undefined) { 69 pixelMap.readPixelsToBuffer(readBuffer).then(() => { 70 console.info('Succeeded in reading image pixel data.'); // Called if the condition is met. 71 }).catch((error: BusinessError) => { 72 console.error(`Failed to read image pixel data. code is ${error.code}, message is ${error.message}`); // Called if no condition is met. 73 }) 74 } 75} 76``` 77 78## readPixelsToBuffer<sup>7+</sup> 79 80readPixelsToBuffer(dst: ArrayBuffer, callback: AsyncCallback\<void>): void 81 82Reads the pixels of this PixelMap object based on the PixelMap's pixel format and writes the data to the buffer. This API uses an asynchronous callback to return the result. 83 84**Widget capability**: This API can be used in ArkTS widgets since API version 12. 85 86**Atomic service API**: This API can be used in atomic services since API version 11. 87 88**System capability**: SystemCapability.Multimedia.Image.Core 89 90**Parameters** 91 92| Name | Type | Mandatory| Description | 93| -------- | -------------------- | ---- | ----------------------------------------------------------------------------------------------------- | 94| dst | ArrayBuffer | Yes | Buffer to which the pixels will be written. The buffer size is obtained by calling [getPixelBytesNumber](#getpixelbytesnumber7).| 95| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object. | 96 97**Example** 98 99```ts 100import { BusinessError } from '@kit.BasicServicesKit'; 101import { image } from '@kit.ImageKit'; 102 103async function ReadPixelsToBuffer(pixelMap : image.PixelMap) { 104 const readBuffer: ArrayBuffer = new ArrayBuffer(96); // 96 is the size of the pixel buffer to create. The value is calculated as follows: height * width *4. 105 if (pixelMap != undefined) { 106 pixelMap.readPixelsToBuffer(readBuffer, (error: BusinessError, res: void) => { 107 if(error) { 108 console.error(`Failed to read image pixel data. code is ${error.code}, message is ${error.message}`); // Called if no condition is met. 109 return; 110 } else { 111 console.info('Succeeded in reading image pixel data.'); // Called if the condition is met. 112 } 113 }) 114 } 115} 116``` 117 118## readPixelsToBufferSync<sup>12+</sup> 119 120readPixelsToBufferSync(dst: ArrayBuffer): void 121 122Reads the pixels of this PixelMap object based on the PixelMap's pixel format and writes the data to the buffer. This API returns the result synchronously. 123 124**Widget capability**: This API can be used in ArkTS widgets since API version 12. 125 126**Atomic service API**: This API can be used in atomic services since API version 12. 127 128**System capability**: SystemCapability.Multimedia.Image.Core 129 130**Parameters** 131 132| Name | Type | Mandatory| Description | 133| -------- | -------------------- | ---- | ----------------------------------------------------------------------------------------------------- | 134| dst | ArrayBuffer | Yes | Buffer to which the pixels will be written. The buffer size is obtained by calling [getPixelBytesNumber](#getpixelbytesnumber7).| 135 136**Error codes** 137 138For details about the error codes, see [Image Error Codes](errorcode-image.md). 139 140| ID| Error Message| 141| ------- | --------------------------------------------| 142| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed | 143| 501 | Resource Unavailable | 144 145**Example** 146 147```ts 148import { BusinessError } from '@kit.BasicServicesKit'; 149import { image } from '@kit.ImageKit'; 150 151async function ReadPixelsToBufferSync(pixelMap : image.PixelMap) { 152 const bufferSize = pixelMap.getPixelBytesNumber(); 153 const readBuffer = new ArrayBuffer(bufferSize); 154 if (pixelMap != undefined) { 155 pixelMap.readPixelsToBufferSync(readBuffer); 156 } 157} 158``` 159 160## readPixels<sup>7+</sup> 161 162readPixels(area: PositionArea): Promise\<void> 163 164Reads the pixels in the area specified by [PositionArea](arkts-apis-image-i.md#positionarea7).region of this PixelMap object in the BGRA_8888 format and writes the data to the [PositionArea](arkts-apis-image-i.md#positionarea7).pixels buffer. This API uses a promise to return the result. 165 166You can use a formula to calculate the size of the memory to be applied for based on **PositionArea**. 167 168YUV region calculation formula: region to read (region.size{width * height}) * 1.5 (1 * Y component + 0.25 * U component + 0.25 * V component) 169 170RGBA region calculation formula: region to read (region.size{width * height}) * 4 (1 * R component + 1 * G component + 1 * B component + 1 * A component) 171 172**Widget capability**: This API can be used in ArkTS widgets since API version 12. 173 174**Atomic service API**: This API can be used in atomic services since API version 11. 175 176**System capability**: SystemCapability.Multimedia.Image.Core 177 178**Parameters** 179 180| Name| Type | Mandatory| Description | 181| ------ | ------------------------------ | ---- | ------------------------ | 182| area | [PositionArea](arkts-apis-image-i.md#positionarea7) | Yes | Area from which the pixels will be read.| 183 184**Return value** 185 186| Type | Description | 187| :------------- | :-------------------------------------------------- | 188| Promise\<void> | Promise that returns no value. | 189 190**Example** 191 192```ts 193import { BusinessError } from '@kit.BasicServicesKit'; 194import { image } from '@kit.ImageKit'; 195 196async function ReadPixelsRGBA(pixelMap : image.PixelMap) { 197 const area: image.PositionArea = { 198 pixels: new ArrayBuffer(8), // 8 is the size of the PixelMap buffer to create. The value is calculated as follows: height * width * 4. 199 offset: 0, 200 stride: 8, 201 region: { size: { height: 1, width: 2 }, x: 0, y: 0 } 202 }; 203 if (pixelMap != undefined) { 204 pixelMap.readPixels(area).then(() => { 205 console.info('Succeeded in reading the image data in the area.'); // Called if the condition is met. 206 console.info('RGBA data is ', new Uint8Array(area.pixels)); 207 }).catch((error: BusinessError) => { 208 console.error(`Failed to read the image data in the area. code is ${error.code}, message is ${error.message}`); // Called if no condition is met. 209 }) 210 } 211} 212 213async function ReadPixelsYUV(pixelMap : image.PixelMap) { 214 const area: image.PositionArea = { 215 pixels: new ArrayBuffer(6), // 6 is the size of the PixelMap buffer to create. The value is calculated as follows: height * width * 1.5. 216 offset: 0, 217 stride: 8, 218 region: { size: { height: 2, width: 2 }, x: 0, y: 0 } 219 }; 220 if (pixelMap != undefined) { 221 pixelMap.readPixels(area).then(() => { 222 console.info('Succeeded in reading the image data in the area.'); // Called if the condition is met. 223 console.info('YUV data is ', new Uint8Array(area.pixels)); 224 }).catch((error: BusinessError) => { 225 console.error(`Failed to read the image data in the area. code is ${error.code}, message is ${error.message}`); // Called if no condition is met. 226 }) 227 } 228} 229``` 230 231## readPixels<sup>7+</sup> 232 233readPixels(area: PositionArea, callback: AsyncCallback\<void>): void 234 235Reads the pixels in the area specified by [PositionArea](arkts-apis-image-i.md#positionarea7).region of this PixelMap object in the BGRA_8888 format and writes the data to the [PositionArea](arkts-apis-image-i.md#positionarea7).pixels buffer. This API uses an asynchronous callback to return the result. 236 237You can use a formula to calculate the size of the memory to be applied for based on **PositionArea**. 238 239YUV region calculation formula: region to read (region.size{width * height}) * 1.5 (1 * Y component + 0.25 * U component + 0.25 * V component) 240 241RGBA region calculation formula: region to read (region.size{width * height}) * 4 (1 * R component + 1 * G component + 1 * B component + 1 * A component) 242 243**Widget capability**: This API can be used in ArkTS widgets since API version 12. 244 245**Atomic service API**: This API can be used in atomic services since API version 11. 246 247**System capability**: SystemCapability.Multimedia.Image.Core 248 249**Parameters** 250 251| Name | Type | Mandatory| Description | 252| -------- | ------------------------------ | ---- | ------------------------------ | 253| area | [PositionArea](arkts-apis-image-i.md#positionarea7) | Yes | Area from which the pixels will be read. | 254| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 255 256**Example** 257 258```ts 259import { BusinessError } from '@kit.BasicServicesKit'; 260import { image } from '@kit.ImageKit'; 261 262async function ReadPixelsRGBA(pixelMap : image.PixelMap) { 263 const area: image.PositionArea = { 264 pixels: new ArrayBuffer(8), // 8 is the size of the PixelMap buffer to create. The value is calculated as follows: height * width * 4. 265 offset: 0, 266 stride: 8, 267 region: { size: { height: 1, width: 2 }, x: 0, y: 0 } 268 }; 269 if (pixelMap != undefined) { 270 pixelMap.readPixels(area, (error: BusinessError) => { 271 if (error) { 272 console.error(`Failed to read pixelmap from the specified area. code is ${error.code}, message is ${error.message}`); 273 return; 274 } else { 275 console.info('Succeeded in reading pixelmap from the specified area.'); 276 console.info('RGBA data is ', new Uint8Array(area.pixels)); 277 } 278 }) 279 } 280} 281 282async function ReadPixelsYUV(pixelMap : image.PixelMap) { 283 const area: image.PositionArea = { 284 pixels: new ArrayBuffer(6), // 6 is the size of the PixelMap buffer to create. The value is calculated as follows: height * width * 1.5. 285 offset: 0, 286 stride: 8, 287 region: { size: { height: 2, width: 2 }, x: 0, y: 0 } 288 }; 289 if (pixelMap != undefined) { 290 pixelMap.readPixels(area, (error: BusinessError) => { 291 if (error) { 292 console.error(`Failed to read pixelmap from the specified area. code is ${error.code}, message is ${error.message}`); 293 return; 294 } else { 295 console.info('Succeeded in reading pixelmap from the specified area.'); 296 console.info('YUV data is ', new Uint8Array(area.pixels)); 297 } 298 }) 299 } 300} 301``` 302 303## readPixelsSync<sup>12+</sup> 304 305readPixelsSync(area: PositionArea): void 306 307Reads the pixels in the area specified by [PositionArea](arkts-apis-image-i.md#positionarea7).region of this PixelMap object in the BGRA_8888 format and writes the data to the [PositionArea](arkts-apis-image-i.md#positionarea7).pixels buffer. This API returns the result synchronously. 308 309**Atomic service API**: This API can be used in atomic services since API version 12. 310 311**System capability**: SystemCapability.Multimedia.Image.Core 312 313**Parameters** 314 315| Name| Type | Mandatory| Description | 316| ------ | ------------------------------ | ---- | ------------------------ | 317| area | [PositionArea](arkts-apis-image-i.md#positionarea7) | Yes | Area from which the pixels will be read.| 318 319**Error codes** 320 321For details about the error codes, see [Image Error Codes](errorcode-image.md). 322 323| ID| Error Message| 324| ------- | --------------------------------------------| 325| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed | 326| 501 | Resource Unavailable | 327 328**Example** 329 330```ts 331import { BusinessError } from '@kit.BasicServicesKit'; 332import {image} from '@kit.ImageKit'; 333 334async function ReadPixelsSync(pixelMap : image.PixelMap) { 335 const area : image.PositionArea = { 336 pixels: new ArrayBuffer(8), 337 offset: 0, 338 stride: 8, 339 region: { size: { height: 1, width: 2 }, x: 0, y: 0 } 340 }; 341 if (pixelMap != undefined) { 342 pixelMap.readPixelsSync(area); 343 } 344} 345``` 346 347## writePixels<sup>7+</sup> 348 349writePixels(area: PositionArea): Promise\<void> 350 351Reads the pixels in the [PositionArea](arkts-apis-image-i.md#positionarea7).region buffer in the BGRA_8888 format and writes the data to the area specified by [PositionArea](arkts-apis-image-i.md#positionarea7).pixels in this PixelMap object. This API uses a promise to return the result. 352 353You can use a formula to calculate the size of the memory to be applied for based on **PositionArea**. 354 355YUV region calculation formula: region to read (region.size{width * height}) * 1.5 (1 * Y component + 0.25 * U component + 0.25 * V component) 356 357RGBA region calculation formula: region to read (region.size{width * height}) * 4 (1 * R component + 1 * G component + 1 * B component + 1 * A component) 358 359**Widget capability**: This API can be used in ArkTS widgets since API version 12. 360 361**Atomic service API**: This API can be used in atomic services since API version 11. 362 363**System capability**: SystemCapability.Multimedia.Image.Core 364 365**Parameters** 366 367| Name| Type | Mandatory| Description | 368| ------ | ------------------------------ | ---- | -------------------- | 369| area | [PositionArea](arkts-apis-image-i.md#positionarea7) | Yes | Area to which the pixels will be written.| 370 371**Return value** 372 373| Type | Description | 374| :------------- | :-------------------------------------------------- | 375| Promise\<void> | Promise that returns no value. | 376 377**Example** 378 379```ts 380import { BusinessError } from '@kit.BasicServicesKit'; 381import {image} from '@kit.ImageKit'; 382 383async function WritePixelsRGBA(pixelMap:image.PixelMap) { 384 const area: image.PositionArea = { 385 pixels: new ArrayBuffer(8), // 8 is the size of the PixelMap buffer to create. The value is calculated as follows: height * width * 4. 386 offset: 0, 387 stride: 8, 388 region: { size: { height: 1, width: 2 }, x: 0, y: 0 } 389 }; 390 let bufferArr: Uint8Array = new Uint8Array(area.pixels); 391 for (let i = 0; i < bufferArr.length; i++) { 392 bufferArr[i] = i + 1; 393 } 394 if (pixelMap != undefined) { 395 pixelMap.writePixels(area).then(() => { 396 console.info('Succeeded in writing pixelmap into the specified area.'); 397 }).catch((error: BusinessError) => { 398 console.error(`Failed to write pixelmap into the specified area. code is ${error.code}, message is ${error.message}`); 399 }) 400 } 401} 402 403async function WritePixelsYUV(pixelMap:image.PixelMap) { 404 const area: image.PositionArea = { 405 pixels: new ArrayBuffer(6), // 6 is the size of the PixelMap buffer to create. The value is calculated as follows: height * width * 1.5. 406 offset: 0, 407 stride: 8, // This variable is not used by writePixels when the PixelMap is in YUV format. 408 region: { size: { height: 2, width: 2 }, x: 0, y: 0 } 409 }; 410 let bufferArr: Uint8Array = new Uint8Array(area.pixels); 411 for (let i = 0; i < bufferArr.length; i++) { 412 bufferArr[i] = i + 1; 413 } 414 if (pixelMap != undefined) { 415 pixelMap.writePixels(area).then(() => { 416 console.info('Succeeded in writing pixelmap into the specified area.'); 417 }).catch((error: BusinessError) => { 418 console.error(`Failed to write pixelmap into the specified area. code is ${error.code}, message is ${error.message}`); 419 }) 420 } 421} 422``` 423 424## writePixels<sup>7+</sup> 425 426writePixels(area: PositionArea, callback: AsyncCallback\<void>): void 427 428Reads the pixels in the [PositionArea](arkts-apis-image-i.md#positionarea7).region buffer in the BGRA_8888 format and writes the data to the area specified by [PositionArea](arkts-apis-image-i.md#positionarea7).pixels in this PixelMap object. This API uses an asynchronous callback to return the result. 429 430You can use a formula to calculate the size of the memory to be applied for based on **PositionArea**. 431 432YUV region calculation formula: region to read (region.size{width * height}) * 1.5 (1 * Y component + 0.25 * U component + 0.25 * V component) 433 434RGBA region calculation formula: region to read (region.size{width * height}) * 4 (1 * R component + 1 * G component + 1 * B component + 1 * A component) 435 436**Widget capability**: This API can be used in ArkTS widgets since API version 12. 437 438**Atomic service API**: This API can be used in atomic services since API version 11. 439 440**System capability**: SystemCapability.Multimedia.Image.Core 441 442**Parameters** 443 444| Name | Type | Mandatory| Description | 445| --------- | ------------------------------ | ---- | ------------------------------ | 446| area | [PositionArea](arkts-apis-image-i.md#positionarea7) | Yes | Area to which the pixels will be written. | 447| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 448 449**Example** 450 451```ts 452import { BusinessError } from '@kit.BasicServicesKit'; 453 454async function WritePixelsRGBA(pixelMap:image.PixelMap) { 455 const area: image.PositionArea = { pixels: new ArrayBuffer(8), // 8 is the size of the PixelMap buffer to create. The value is calculated as follows: height * width * 4. 456 offset: 0, 457 stride: 8, 458 region: { size: { height: 1, width: 2 }, x: 0, y: 0 } 459 }; 460 let bufferArr: Uint8Array = new Uint8Array(area.pixels); 461 for (let i = 0; i < bufferArr.length; i++) { 462 bufferArr[i] = i + 1; 463 } 464 if (pixelMap != undefined) { 465 pixelMap.writePixels(area, (error : BusinessError) => { 466 if (error) { 467 console.error(`Failed to write pixelmap into the specified area. code is ${error.code}, message is ${error.message}`); 468 return; 469 } else { 470 console.info('Succeeded in writing pixelmap into the specified area.'); 471 } 472 }) 473 } 474} 475 476async function WritePixelsYUV(pixelMap:image.PixelMap) { 477 const area: image.PositionArea = { pixels: new ArrayBuffer(6), // 6 is the size of the PixelMap buffer to create. The value is calculated as follows: height * width * 1.5. 478 offset: 0, 479 stride: 8, // This variable is not used by writePixels when the PixelMap is in YUV format. 480 region: { size: { height: 2, width: 2 }, x: 0, y: 0 } 481 }; 482 let bufferArr: Uint8Array = new Uint8Array(area.pixels); 483 for (let i = 0; i < bufferArr.length; i++) { 484 bufferArr[i] = i + 1; 485 } 486 if (pixelMap != undefined) { 487 pixelMap.writePixels(area, (error : BusinessError) => { 488 if (error) { 489 console.error(`Failed to write pixelmap into the specified area. code is ${error.code}, message is ${error.message}`); 490 return; 491 } else { 492 console.info('Succeeded in writing pixelmap into the specified area.'); 493 } 494 }) 495 } 496} 497``` 498 499## writePixelsSync<sup>12+</sup> 500 501writePixelsSync(area: PositionArea): void 502 503Reads the pixels in the [PositionArea](arkts-apis-image-i.md#positionarea7).region buffer in the BGRA_8888 format and writes the data to the area specified by [PositionArea](arkts-apis-image-i.md#positionarea7).pixels in this PixelMap object. This API returns the result synchronously. 504 505**Widget capability**: This API can be used in ArkTS widgets since API version 12. 506 507**Atomic service API**: This API can be used in atomic services since API version 12. 508 509**System capability**: SystemCapability.Multimedia.Image.Core 510 511**Parameters** 512 513| Name| Type | Mandatory| Description | 514| ------ | ------------------------------ | ---- | -------------------- | 515| area | [PositionArea](arkts-apis-image-i.md#positionarea7) | Yes | Area to which the pixels will be written.| 516 517**Error codes** 518 519For details about the error codes, see [Image Error Codes](errorcode-image.md). 520 521| ID| Error Message| 522| ------- | --------------------------------------------| 523| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed | 524| 501 | Resource Unavailable | 525 526**Example** 527 528```ts 529import { BusinessError } from '@kit.BasicServicesKit'; 530import {image} from '@kit.ImageKit'; 531 532async function WritePixelsSync(pixelMap:image.PixelMap) { 533 const area: image.PositionArea = { 534 pixels: new ArrayBuffer(8), 535 offset: 0, 536 stride: 8, 537 region: { size: { height: 1, width: 2 }, x: 0, y: 0 } 538 }; 539 let bufferArr: Uint8Array = new Uint8Array(area.pixels); 540 for (let i = 0; i < bufferArr.length; i++) { 541 bufferArr[i] = i + 1; 542 } 543 if (pixelMap != undefined) { 544 pixelMap.writePixelsSync(area); 545 } 546} 547``` 548 549## writeBufferToPixels<sup>7+</sup> 550 551writeBufferToPixels(src: ArrayBuffer): Promise\<void> 552 553Reads the pixels in the buffer based on the PixelMap's pixel format and writes the data to this PixelMap object. This API uses a promise to return the result. 554 555**Widget capability**: This API can be used in ArkTS widgets since API version 12. 556 557**Atomic service API**: This API can be used in atomic services since API version 11. 558 559**System capability**: SystemCapability.Multimedia.Image.Core 560 561**Parameters** 562 563| Name| Type | Mandatory| Description | 564| ------ | ----------- | ---- | -------------- | 565| src | ArrayBuffer | Yes | Buffer from which the pixels are read. The buffer size is obtained by calling [getPixelBytesNumber](#getpixelbytesnumber7).| 566 567**Return value** 568 569| Type | Description | 570| -------------- | ----------------------------------------------- | 571| Promise\<void> | Promise that returns no value. | 572 573**Example** 574 575```ts 576import { BusinessError } from '@kit.BasicServicesKit'; 577import {image} from '@kit.ImageKit'; 578 579async function WriteBufferToPixels(pixelMap:image.PixelMap) { 580 const color: ArrayBuffer = new ArrayBuffer(96); // 96 is the size of the pixel buffer to create. The value is calculated as follows: height * width *4. 581 let bufferArr: Uint8Array = new Uint8Array(color); 582 for (let i = 0; i < bufferArr.length; i++) { 583 bufferArr[i] = i + 1; 584 } 585 if (pixelMap != undefined) { 586 pixelMap.writeBufferToPixels(color).then(() => { 587 console.info("Succeeded in writing data from a buffer to a PixelMap."); 588 }).catch((error: BusinessError) => { 589 console.error(`Failed to write data from a buffer to a PixelMap. code is ${error.code}, message is ${error.message}`); 590 }) 591 } 592} 593``` 594 595## writeBufferToPixels<sup>7+</sup> 596 597writeBufferToPixels(src: ArrayBuffer, callback: AsyncCallback\<void>): void 598 599Reads the pixels in the buffer based on the PixelMap's pixel format and writes the data to this PixelMap object. This API uses an asynchronous callback to return the result. 600 601**Widget capability**: This API can be used in ArkTS widgets since API version 12. 602 603**Atomic service API**: This API can be used in atomic services since API version 11. 604 605**System capability**: SystemCapability.Multimedia.Image.Core 606 607**Parameters** 608 609| Name | Type | Mandatory| Description | 610| -------- | -------------------- | ---- | ------------------------------ | 611| src | ArrayBuffer | Yes | Buffer from which the pixels are read. The buffer size is obtained by calling [getPixelBytesNumber](#getpixelbytesnumber7).| 612| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the pixels in the buffer are successfully written to the PixelMap, **err** is **undefined**; otherwise, **err** is an error object.| 613 614**Example** 615 616```ts 617import { BusinessError } from '@kit.BasicServicesKit'; 618import {image} from '@kit.ImageKit'; 619 620async function WriteBufferToPixels(pixelMap:image.PixelMap) { 621 const color: ArrayBuffer = new ArrayBuffer(96); // 96 is the size of the pixel buffer to create. The value is calculated as follows: height * width *4. 622 let bufferArr: Uint8Array = new Uint8Array(color); 623 for (let i = 0; i < bufferArr.length; i++) { 624 bufferArr[i] = i + 1; 625 } 626 if (pixelMap != undefined) { 627 pixelMap.writeBufferToPixels(color, (error: BusinessError) => { 628 if (error) { 629 console.error(`Failed to write data from a buffer to a PixelMap. code is ${error.code}, message is ${error.message}`); 630 return; 631 } else { 632 console.info("Succeeded in writing data from a buffer to a PixelMap."); 633 } 634 }) 635 } 636} 637``` 638 639## writeBufferToPixelsSync<sup>12+</sup> 640 641writeBufferToPixelsSync(src: ArrayBuffer): void 642 643Reads the pixels in the buffer based on the PixelMap's pixel format and writes the data to this PixelMap object. This API returns the result synchronously. 644 645**Atomic service API**: This API can be used in atomic services since API version 12. 646 647**System capability**: SystemCapability.Multimedia.Image.Core 648 649**Parameters** 650 651| Name| Type | Mandatory| Description | 652| ------ | ----------- | ---- | -------------- | 653| src | ArrayBuffer | Yes | Buffer from which the pixels are read. The buffer size is obtained by calling [getPixelBytesNumber](#getpixelbytesnumber7).| 654 655**Error codes** 656 657For details about the error codes, see [Image Error Codes](errorcode-image.md). 658 659| ID| Error Message| 660| ------- | --------------------------------------------| 661| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed | 662| 501 | Resource Unavailable | 663 664**Example** 665 666```ts 667import { BusinessError } from '@kit.BasicServicesKit'; 668import {image} from '@kit.ImageKit'; 669 670async function WriteBufferToPixelsSync(pixelMap:image.PixelMap) { 671 const color : ArrayBuffer = new ArrayBuffer(96); // 96 is the size of the pixel buffer to create. The value is calculated as follows: height * width *4. 672 let bufferArr : Uint8Array = new Uint8Array(color); 673 for (let i = 0; i < bufferArr.length; i++) { 674 bufferArr[i] = i + 1; 675 } 676 if (pixelMap != undefined) { 677 pixelMap.writeBufferToPixelsSync(color); 678 } 679} 680``` 681 682 683## getImageInfo<sup>7+</sup> 684 685getImageInfo(): Promise\<ImageInfo> 686 687Obtains the image information. This API uses a promise to return the result. 688 689**Widget capability**: This API can be used in ArkTS widgets since API version 12. 690 691**Atomic service API**: This API can be used in atomic services since API version 11. 692 693**System capability**: SystemCapability.Multimedia.Image.Core 694 695**Return value** 696 697| Type | Description | 698| --------------------------------- | ----------------------------------------------------------- | 699| Promise\<[ImageInfo](arkts-apis-image-i.md#imageinfo)> | Promise used to return the image information.| 700 701**Example** 702 703```ts 704import { BusinessError } from '@kit.BasicServicesKit'; 705 706async function GetImageInfo() { 707 if (pixelMap != undefined) { 708 pixelMap.getImageInfo().then((imageInfo: image.ImageInfo) => { 709 if (imageInfo != undefined) { 710 console.info("Succeeded in obtaining the image pixel map information."+ imageInfo.size.height); 711 } 712 }).catch((error: BusinessError) => { 713 console.error(`Failed to obtain the image pixel map information. code is ${error.code}, message is ${error.message}`); 714 }) 715 } 716} 717``` 718 719## getImageInfo<sup>7+</sup> 720 721getImageInfo(callback: AsyncCallback\<ImageInfo>): void 722 723Obtains the image information. This API uses an asynchronous callback to return the result. 724 725**Widget capability**: This API can be used in ArkTS widgets since API version 12. 726 727**Atomic service API**: This API can be used in atomic services since API version 11. 728 729**System capability**: SystemCapability.Multimedia.Image.Core 730 731**Parameters** 732 733| Name | Type | Mandatory| Description | 734| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 735| 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.| 736 737**Example** 738 739```ts 740import { BusinessError } from '@kit.BasicServicesKit'; 741import { image } from '@kit.ImageKit'; 742 743function GetImageInfoSync(pixelMap : image.PixelMap){ 744 if (pixelMap != undefined) { 745 pixelMap.getImageInfo((error: BusinessError, imageInfo: image.ImageInfo) => { 746 if (error) { 747 console.error(`Failed to obtain the image pixel map information. code is ${error.code}, message is ${error.message}`); 748 return; 749 } else { 750 console.info("Succeeded in obtaining the image pixel map information."+ imageInfo.size.height); 751 } 752 }) 753 } 754} 755``` 756 757## getImageInfoSync<sup>12+</sup> 758 759getImageInfoSync(): ImageInfo 760 761Obtains the image information. This API returns the result synchronously. 762 763**Widget capability**: This API can be used in ArkTS widgets since API version 12. 764 765**Atomic service API**: This API can be used in atomic services since API version 12. 766 767**System capability**: SystemCapability.Multimedia.Image.ImageSource 768 769**Return value** 770 771| Type | Description | 772| --------------------------------- | ----------------------------------------------------------- | 773| [ImageInfo](arkts-apis-image-i.md#imageinfo) | Image information. | 774 775**Error codes** 776 777For details about the error codes, see [Image Error Codes](errorcode-image.md). 778 779| ID| Error Message| 780| ------- | --------------------------------------------| 781| 501 | Resource Unavailable | 782 783**Example** 784 785```ts 786import { BusinessError } from '@kit.BasicServicesKit'; 787import {image} from '@kit.ImageKit'; 788 789function GetImageInfoSync(pixelMap:image.PixelMap) { 790 if (pixelMap != undefined) { 791 let imageInfo : image.ImageInfo = pixelMap.getImageInfoSync(); 792 return imageInfo; 793 } 794 return undefined; 795} 796``` 797 798## getBytesNumberPerRow<sup>7+</sup> 799 800getBytesNumberPerRow(): number 801 802Obtains the number of bytes per row of this image. 803 804**Widget capability**: This API can be used in ArkTS widgets since API version 12. 805 806**Atomic service API**: This API can be used in atomic services since API version 11. 807 808**System capability**: SystemCapability.Multimedia.Image.Core 809 810**Return value** 811 812| Type | Description | 813| ------ | -------------------- | 814| number | Number of bytes per row.| 815 816**Example** 817 818```ts 819let rowCount: number = pixelMap.getBytesNumberPerRow(); 820``` 821 822## getPixelBytesNumber<sup>7+</sup> 823 824getPixelBytesNumber(): number 825 826Obtains the total number of bytes of this image. 827 828**Widget capability**: This API can be used in ArkTS widgets since API version 12. 829 830**Atomic service API**: This API can be used in atomic services since API version 11. 831 832**System capability**: SystemCapability.Multimedia.Image.Core 833 834**Return value** 835 836| Type | Description | 837| ------ | -------------------- | 838| number | Total number of bytes.| 839 840**Example** 841 842```ts 843let pixelBytesNumber: number = pixelMap.getPixelBytesNumber(); 844``` 845 846## getDensity<sup>9+</sup> 847 848getDensity():number 849 850Obtains the pixel density of this image. 851 852**Widget capability**: This API can be used in ArkTS widgets since API version 12. 853 854**Atomic service API**: This API can be used in atomic services since API version 11. 855 856**System capability**: SystemCapability.Multimedia.Image.Core 857 858**Return value** 859 860| Type | Description | 861| ------ | --------------- | 862| number | Pixel density, in ppi.| 863 864**Example** 865 866```ts 867let getDensity: number = pixelMap.getDensity(); 868``` 869 870## opacity<sup>9+</sup> 871 872opacity(rate: number, callback: AsyncCallback\<void>): void 873 874Sets an opacity rate for this image. This API uses an asynchronous callback to return the result. It is invalid for YUV images. 875 876**Widget capability**: This API can be used in ArkTS widgets since API version 12. 877 878**Atomic service API**: This API can be used in atomic services since API version 11. 879 880**System capability**: SystemCapability.Multimedia.Image.Core 881 882**Parameters** 883 884| Name | Type | Mandatory| Description | 885| -------- | -------------------- | ---- | ------------------------------ | 886| rate | number | Yes | Opacity rate. The value range is (0,1]. | 887| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 888 889**Example** 890 891```ts 892import { BusinessError } from '@kit.BasicServicesKit'; 893import {image} from '@kit.ImageKit'; 894 895async function Opacity(pixelMap:image.PixelMap) { 896 let rate: number = 0.5; 897 if (pixelMap != undefined) { 898 pixelMap.opacity(rate, (err: BusinessError) => { 899 if (err) { 900 console.error(`Failed to set opacity. code is ${err.code}, message is ${err.message}`); 901 return; 902 } else { 903 console.info("Succeeded in setting opacity."); 904 } 905 }) 906 } 907} 908``` 909 910## opacity<sup>9+</sup> 911 912opacity(rate: number): Promise\<void> 913 914Sets an opacity rate for this image. This API uses a promise to return the result. It is invalid for YUV images. 915 916**Widget capability**: This API can be used in ArkTS widgets since API version 12. 917 918**Atomic service API**: This API can be used in atomic services since API version 11. 919 920**System capability**: SystemCapability.Multimedia.Image.Core 921 922**Parameters** 923 924| Name| Type | Mandatory| Description | 925| ------ | ------ | ---- | --------------------------- | 926| rate | number | Yes | Opacity rate. The value range is (0,1].| 927 928**Return value** 929 930| Type | Description | 931| -------------- | ----------------------------------------------- | 932| Promise\<void> | Promise that returns no value. | 933 934**Example** 935 936```ts 937import { BusinessError } from '@kit.BasicServicesKit'; 938import {image} from '@kit.ImageKit'; 939 940async function Opacity(pixelMap:image.PixelMap) { 941 let rate: number = 0.5; 942 if (pixelMap != undefined) { 943 pixelMap.opacity(rate).then(() => { 944 console.info('Succeeded in setting opacity.'); 945 }).catch((err: BusinessError) => { 946 console.error(`Failed to set opacity. code is ${err.code}, message is ${err.message}`); 947 }) 948 } 949} 950``` 951 952## opacitySync<sup>12+</sup> 953 954opacitySync(rate: number): void 955 956Sets an opacity rate for this image. This API returns the result synchronously. It is invalid for YUV images. 957 958**Atomic service API**: This API can be used in atomic services since API version 12. 959 960**System capability**: SystemCapability.Multimedia.Image.Core 961 962**Parameters** 963 964| Name | Type | Mandatory| Description | 965| -------- | -------------------- | ---- | ------------------------------ | 966| rate | number | Yes | Opacity rate. The value range is (0,1]. | 967 968**Error codes** 969 970For details about the error codes, see [Image Error Codes](errorcode-image.md). 971 972| ID| Error Message| 973| ------- | --------------------------------------------| 974| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed | 975| 501 | Resource Unavailable | 976 977**Example** 978 979```ts 980import { BusinessError } from '@kit.BasicServicesKit'; 981import {image} from '@kit.ImageKit'; 982 983async function OpacitySync(pixelMap:image.PixelMap) { 984 let rate : number = 0.5; 985 if (pixelMap != undefined) { 986 pixelMap.opacitySync(rate); 987 } 988} 989``` 990 991## createAlphaPixelmap<sup>9+</sup> 992 993createAlphaPixelmap(): Promise\<PixelMap> 994 995Creates a PixelMap object that contains only the alpha channel information. This object can be used for the shadow effect. This API uses a promise to return the result. It is invalid for YUV images. 996 997**Widget capability**: This API can be used in ArkTS widgets since API version 12. 998 999**Atomic service API**: This API can be used in atomic services since API version 11. 1000 1001**System capability**: SystemCapability.Multimedia.Image.Core 1002 1003**Return value** 1004 1005| Type | Description | 1006| -------------------------------- | --------------------------- | 1007| Promise\<[PixelMap](arkts-apis-image-PixelMap.md)> | Promise used to return the PixelMap object.| 1008 1009**Example** 1010 1011```ts 1012import { BusinessError } from '@kit.BasicServicesKit'; 1013import {image} from '@kit.ImageKit'; 1014 1015async function CreateAlphaPixelmap(pixelMap:image.PixelMap) { 1016 if (pixelMap != undefined) { 1017 pixelMap.createAlphaPixelmap().then((alphaPixelMap: image.PixelMap) => { 1018 console.info('Succeeded in creating alpha pixelmap.'); 1019 }).catch((error: BusinessError) => { 1020 console.error(`Failed to create alpha pixelmap. code is ${error.code}, message is ${error.message}`); 1021 }) 1022 } 1023} 1024``` 1025 1026## createAlphaPixelmap<sup>9+</sup> 1027 1028createAlphaPixelmap(callback: AsyncCallback\<PixelMap>): void 1029 1030Creates a PixelMap object that contains only the alpha channel information. This object can be used for the shadow effect. This API uses an asynchronous callback to return the result. It is invalid for YUV images. 1031 1032**Widget capability**: This API can be used in ArkTS widgets since API version 12. 1033 1034**Atomic service API**: This API can be used in atomic services since API version 11. 1035 1036**System capability**: SystemCapability.Multimedia.Image.Core 1037 1038**Parameters** 1039 1040| Name | Type | Mandatory| Description | 1041| -------- | ------------------------ | ---- | ------------------------ | 1042| 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.| 1043 1044**Example** 1045 1046```ts 1047import { BusinessError } from '@kit.BasicServicesKit'; 1048import {image} from '@kit.ImageKit'; 1049 1050async function CreateAlphaPixelmap(pixelMap:image.PixelMap) { 1051 if (pixelMap != undefined) { 1052 pixelMap.createAlphaPixelmap((err: BusinessError, alphaPixelMap: image.PixelMap) => { 1053 if (alphaPixelMap == undefined) { 1054 console.error(`Failed to obtain new pixel map. code is ${err.code}, message is ${err.message}`); 1055 return; 1056 } else { 1057 console.info('Succeeded in obtaining new pixel map.'); 1058 } 1059 }) 1060 } 1061} 1062``` 1063 1064## createAlphaPixelmapSync<sup>12+</sup> 1065 1066createAlphaPixelmapSync(): PixelMap 1067 1068Creates a PixelMap object that contains only the alpha channel information. This object can be used for the shadow effect. This API returns the result synchronously. It is invalid for YUV images. 1069 1070**Atomic service API**: This API can be used in atomic services since API version 12. 1071 1072**System capability**: SystemCapability.Multimedia.Image.Core 1073 1074**Return value** 1075 1076| Type | Description | 1077| -------------------------------- | --------------------- | 1078| [PixelMap](arkts-apis-image-PixelMap.md) | PixelMap object. If the operation fails, an error is thrown.| 1079 1080**Error codes** 1081 1082For details about the error codes, see [Image Error Codes](errorcode-image.md). 1083 1084| ID| Error Message| 1085| ------- | --------------------------------------------| 1086| 401 | Parameter error. Possible causes: 1.Parameter verification failed | 1087| 501 | Resource Unavailable | 1088 1089**Example** 1090 1091```ts 1092import { BusinessError } from '@kit.BasicServicesKit'; 1093import {image} from '@kit.ImageKit'; 1094 1095async function CreateAlphaPixelmapSync(pixelMap:image.PixelMap) { 1096 1097 if (pixelMap != undefined) { 1098 let pixelmap : image.PixelMap = pixelMap.createAlphaPixelmapSync(); 1099 return pixelmap; 1100 } 1101 return undefined; 1102} 1103``` 1104 1105## scale<sup>9+</sup> 1106 1107scale(x: number, y: number, callback: AsyncCallback\<void>): void 1108 1109Scales this image based on the scale factors of the width and height. This API uses an asynchronous callback to return the result. 1110 1111> **NOTE** 1112> 1113> You are advised to set the scale factors to non-negative numbers to avoid a flipping effect. 1114> 1115> Scale factors of the width and height = Width and height of the resized image/Width and height of the original image 1116 1117**Widget capability**: This API can be used in ArkTS widgets since API version 12. 1118 1119**Atomic service API**: This API can be used in atomic services since API version 11. 1120 1121**System capability**: SystemCapability.Multimedia.Image.Core 1122 1123**Parameters** 1124 1125| Name | Type | Mandatory| Description | 1126| -------- | -------------------- | ---- | ------------------------------- | 1127| x | number | Yes | Scale factor of the width.| 1128| y | number | Yes | Scale factor of the height.| 1129| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 1130 1131**Example** 1132 1133```ts 1134import { BusinessError } from '@kit.BasicServicesKit'; 1135import {image} from '@kit.ImageKit' 1136 1137async function Scale(pixelMap:image.PixelMap) { 1138 let scaleX: number = 2.0; 1139 let scaleY: number = 1.0; 1140 if (pixelMap != undefined) { 1141 await pixelMap.scale(scaleX, scaleY, (err: BusinessError) => { 1142 if (err) { 1143 console.error(`Failed to scale pixelmap. code is ${err.code}, message is ${err.message}`); 1144 return; 1145 } else { 1146 console.info("Succeeded in scaling pixelmap."); 1147 } 1148 }) 1149 } 1150} 1151``` 1152 1153## scale<sup>9+</sup> 1154 1155scale(x: number, y: number): Promise\<void> 1156 1157Scales this image based on the scale factors of the width and height. This API uses a promise to return the result. 1158 1159> **NOTE** 1160> 1161> You are advised to set the scale factors to non-negative numbers to avoid a flipping effect. 1162> 1163> Scale factors of the width and height = Width and height of the resized image/Width and height of the original image 1164 1165**Widget capability**: This API can be used in ArkTS widgets since API version 12. 1166 1167**Atomic service API**: This API can be used in atomic services since API version 11. 1168 1169**System capability**: SystemCapability.Multimedia.Image.Core 1170 1171**Parameters** 1172 1173| Name| Type | Mandatory| Description | 1174| ------ | ------ | ---- | ------------------------------- | 1175| x | number | Yes | Scale factor of the width.| 1176| y | number | Yes | Scale factor of the height.| 1177 1178**Return value** 1179 1180| Type | Description | 1181| -------------- | --------------------------- | 1182| Promise\<void> | Promise that returns no value.| 1183 1184**Example** 1185 1186```ts 1187import { BusinessError } from '@kit.BasicServicesKit'; 1188import {image} from '@kit.ImageKit'; 1189 1190async function ScaleSync(pixelMap:image.PixelMap) { 1191 let scaleX: number = 2.0; 1192 let scaleY: number = 1.0; 1193 if (pixelMap != undefined) { 1194 pixelMap.scale(scaleX, scaleY).then(() => { 1195 console.info('Succeeded in scaling pixelmap.'); 1196 }).catch((err: BusinessError) => { 1197 console.error(`Failed to scale pixelmap. code is ${err.code}, message is ${err.message}`); 1198 1199 }) 1200 } 1201} 1202``` 1203 1204## scaleSync<sup>12+</sup> 1205 1206scaleSync(x: number, y: number): void 1207 1208Scales this image based on the scale factors of the width and height. This API returns the result synchronously. 1209 1210> **NOTE** 1211> 1212> You are advised to set the scale factors to non-negative numbers to avoid a flipping effect. 1213> 1214> Scale factors of the width and height = Width and height of the resized image/Width and height of the original image 1215 1216**Atomic service API**: This API can be used in atomic services since API version 12. 1217 1218**System capability**: SystemCapability.Multimedia.Image.Core 1219 1220**Parameters** 1221 1222| Name| Type | Mandatory| Description | 1223| ------ | ------ | ---- | ------------------------------- | 1224| x | number | Yes | Scale factor of the width.| 1225| y | number | Yes | Scale factor of the height.| 1226 1227**Error codes** 1228 1229For details about the error codes, see [Image Error Codes](errorcode-image.md). 1230 1231| ID| Error Message| 1232| ------- | --------------------------------------------| 1233| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed | 1234| 501 | Resource Unavailable | 1235 1236**Example** 1237 1238```ts 1239import { BusinessError } from '@kit.BasicServicesKit'; 1240 1241async function ScaleSync() { 1242 let scaleX: number = 2.0; 1243 let scaleY: number = 1.0; 1244 if (pixelMap != undefined) { 1245 pixelMap.scaleSync(scaleX, scaleY); 1246 } 1247} 1248``` 1249 1250## scale<sup>12+</sup> 1251 1252scale(x: number, y: number, level: AntiAliasingLevel): Promise\<void> 1253 1254Scales this image based on the specified anti-aliasing level and the scale factors for the width and height. This API uses a promise to return the result. 1255 1256> **NOTE** 1257> 1258> You are advised to set the scale factors to non-negative numbers to avoid a flipping effect. 1259> 1260> Scale factors of the width and height = Width and height of the resized image/Width and height of the original image 1261 1262**Widget capability**: This API can be used in ArkTS widgets since API version 12. 1263 1264**Atomic service API**: This API can be used in atomic services since API version 12. 1265 1266**System capability**: SystemCapability.Multimedia.Image.Core 1267 1268**Parameters** 1269 1270| Name| Type | Mandatory| Description | 1271| ------ | ------ | ---- | ------------------------------- | 1272| x | number | Yes | Scale factor of the width.| 1273| y | number | Yes | Scale factor of the height.| 1274| level | [AntiAliasingLevel](arkts-apis-image-e.md#antialiasinglevel12) | Yes | Anti-aliasing level.| 1275 1276**Return value** 1277 1278| Type | Description | 1279| -------------- | --------------------------- | 1280| Promise\<void> | Promise that returns no value.| 1281 1282**Error codes** 1283 1284For details about the error codes, see [Image Error Codes](errorcode-image.md). 1285 1286| ID| Error Message| 1287| ------- | --------------------------------------------| 1288| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed | 1289| 501 | Resource Unavailable | 1290 1291**Example** 1292 1293```ts 1294import { BusinessError } from '@kit.BasicServicesKit'; 1295import {image} from '@kit.ImageKit'; 1296 1297async function ScaleSync(pixelMap:image.PixelMap) { 1298 let scaleX: number = 2.0; 1299 let scaleY: number = 1.0; 1300 if (pixelMap != undefined) { 1301 pixelMap.scale(scaleX, scaleY, image.AntiAliasingLevel.LOW).then(() => { 1302 console.info('Succeeded in scaling pixelmap.'); 1303 }).catch((err: BusinessError) => { 1304 console.error(`Failed to scale pixelmap. code is ${err.code}, message is ${err.message}`); 1305 1306 }) 1307 } 1308} 1309``` 1310 1311## scaleSync<sup>12+</sup> 1312 1313scaleSync(x: number, y: number, level: AntiAliasingLevel): void 1314 1315Scales this image based on the specified anti-aliasing level and the scale factors for the width and height. This API returns the result synchronously. 1316 1317> **NOTE** 1318> 1319> You are advised to set the scale factors to non-negative numbers to avoid a flipping effect. 1320> 1321> Scale factors of the width and height = Width and height of the resized image/Width and height of the original image 1322 1323**Atomic service API**: This API can be used in atomic services since API version 12. 1324 1325**System capability**: SystemCapability.Multimedia.Image.Core 1326 1327**Parameters** 1328 1329| Name| Type | Mandatory| Description | 1330| ------ | ------ | ---- | ------------------------------- | 1331| x | number | Yes | Scale factor of the width.| 1332| y | number | Yes | Scale factor of the height.| 1333| level | [AntiAliasingLevel](arkts-apis-image-e.md#antialiasinglevel12) | Yes | Anti-aliasing level.| 1334 1335**Error codes** 1336 1337For details about the error codes, see [Image Error Codes](errorcode-image.md). 1338 1339| ID| Error Message| 1340| ------- | --------------------------------------------| 1341| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed | 1342| 501 | Resource Unavailable | 1343 1344**Example** 1345 1346```ts 1347import { BusinessError } from '@kit.BasicServicesKit'; 1348 1349async function ScaleSync() { 1350 let scaleX: number = 2.0; 1351 let scaleY: number = 1.0; 1352 if (pixelMap != undefined) { 1353 pixelMap.scaleSync(scaleX, scaleY, image.AntiAliasingLevel.LOW); 1354 } 1355} 1356``` 1357 1358## createScaledPixelMap<sup>18+</sup> 1359 1360createScaledPixelMap(x: number, y: number, level?: AntiAliasingLevel): Promise\<PixelMap> 1361 1362Creates an image that has been resized based on the specified anti-aliasing level and the scale factors of the width and height. This API uses a promise to return the result. 1363 1364**System capability**: SystemCapability.Multimedia.Image.Core 1365 1366**Parameters** 1367 1368| Name| Type | Mandatory| Description | 1369| ------ | ------ | ---- | ------------------------------- | 1370| x | number | Yes | Scale factor of the width.| 1371| y | number | Yes | Scale factor of the height.| 1372| level | [AntiAliasingLevel](arkts-apis-image-e.md#antialiasinglevel12) | No | Anti-aliasing level.| 1373 1374**Return value** 1375 1376| Type | Description | 1377| -------------- | --------------------------- | 1378| Promise\<[PixelMap](arkts-apis-image-PixelMap.md)> | Promise used to return the PixelMap object.| 1379 1380**Error codes** 1381 1382For details about the error codes, see [Image Error Codes](errorcode-image.md). 1383 1384| ID| Error Message| 1385| ------- | --------------------------------------------| 1386| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed | 1387| 501 | Resource Unavailable | 1388 1389**Example** 1390 1391```ts 1392import { BusinessError } from '@kit.BasicServicesKit'; 1393import {image} from '@kit.ImageKit'; 1394 1395async function CreateScaledPixelMap(pixelMap:image.PixelMap) { 1396 let scaleX: number = 2.0; 1397 let scaleY: number = 1.0; 1398 if (pixelMap != undefined) { 1399 pixelMap.createScaledPixelMap(scaleX, scaleY, image.AntiAliasingLevel.LOW).then((scaledPixelMap: image.PixelMap) => { 1400 console.info('Succeeded in creating scaledPixelMap.'); 1401 }).catch((error: BusinessError) => { 1402 console.error(`Failed to create scaledPixelMap. Error code is ${error.code}, error message is ${error.message}`); 1403 }) 1404 } 1405} 1406``` 1407 1408## createScaledPixelMapSync<sup>18+</sup> 1409 1410createScaledPixelMapSync(x: number, y: number, level?: AntiAliasingLevel): PixelMap 1411 1412Creates an image that has been resized based on the specified anti-aliasing level and the scale factors of the width and height. This API returns the result synchronously. 1413 1414**System capability**: SystemCapability.Multimedia.Image.Core 1415 1416**Parameters** 1417 1418| Name| Type | Mandatory| Description | 1419| ------ | ------ | ---- | ------------------------------- | 1420| x | number | Yes | Scale factor of the width.| 1421| y | number | Yes | Scale factor of the height.| 1422| level | [AntiAliasingLevel](arkts-apis-image-e.md#antialiasinglevel12) | No | Anti-aliasing level.| 1423 1424**Return value** 1425 1426| Type | Description | 1427| -------------------------------- | --------------------- | 1428| [PixelMap](arkts-apis-image-PixelMap.md) | PixelMap object. If the operation fails, an error is thrown.| 1429 1430**Error codes** 1431 1432For details about the error codes, see [Image Error Codes](errorcode-image.md). 1433 1434| ID| Error Message| 1435| ------- | --------------------------------------------| 1436| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed | 1437| 501 | Resource Unavailable | 1438 1439**Example** 1440 1441```ts 1442import { BusinessError } from '@kit.BasicServicesKit'; 1443import {image} from '@kit.ImageKit'; 1444 1445async function CreateScaledPixelMapSync(pixelMap:image.PixelMap) { 1446 let scaleX: number = 2.0; 1447 let scaleY: number = 1.0; 1448 if (pixelMap != undefined) { 1449 let scaledPixelMap = pixelMap.createScaledPixelMapSync(scaleX, scaleY, image.AntiAliasingLevel.LOW); 1450 } 1451} 1452``` 1453 1454## clone<sup>18+</sup> 1455 1456clone(): Promise\<PixelMap> 1457 1458Copies this PixelMap object. This API uses a promise to return the result. 1459 1460**System capability**: SystemCapability.Multimedia.Image.Core 1461 1462**Return value** 1463 1464| Type | Description | 1465| -------------------------------- | --------------------------- | 1466| Promise\<[PixelMap](arkts-apis-image-PixelMap.md)> | Promise used to return the PixelMap object.| 1467 1468**Error codes** 1469 1470For details about the error codes, see [Image Error Codes](errorcode-image.md). 1471 1472| ID| Error Message| 1473| ------- | --------------------------------------------| 1474| 501 | Resource unavailable. | 1475| 62980102 | Image malloc abnormal. This status code is thrown when an error occurs during the process of copying data. | 1476| 62980103 | Image YUV And ASTC types are not supported. | 1477| 62980104 | Image initialization abnormal. This status code is thrown when an error occurs during the process of createing empty pixelmap. | 1478| 62980106 | The image data is to large. This status code is thrown when an error occurs during the process of checking size. | 1479 1480**Example** 1481 1482```ts 1483import { BusinessError } from '@kit.BasicServicesKit'; 1484import {image} from '@kit.ImageKit'; 1485 1486async function Demo(pixelMap:image.PixelMap) { 1487 if (pixelMap != undefined) { 1488 pixelMap.clone().then((clonePixelMap: image.PixelMap) => { 1489 console.info('Succeeded clone pixelmap.'); 1490 }).catch((error: BusinessError) => { 1491 console.error(`Failed to clone pixelmap. code is ${error.code}, message is ${error.message}`); 1492 }) 1493 } 1494} 1495``` 1496 1497## cloneSync<sup>18+</sup> 1498 1499cloneSync(): PixelMap 1500 1501Copies this PixelMap object. This API returns the result synchronously. 1502 1503**System capability**: SystemCapability.Multimedia.Image.Core 1504 1505**Return value** 1506 1507| Type | Description | 1508| -------------------------------- | --------------------------- | 1509| [PixelMap](arkts-apis-image-PixelMap.md) | PixelMap object. If the operation fails, an error is thrown.| 1510 1511**Error codes** 1512 1513For details about the error codes, see [Image Error Codes](errorcode-image.md). 1514 1515| ID| Error Message| 1516| ------- | --------------------------------------------| 1517| 501 | Resource unavailable. | 1518| 62980102 | Image malloc abnormal. This status code is thrown when an error occurs during the process of copying data. | 1519| 62980103 | Image YUV And ASTC types are not supported. | 1520| 62980104 | Image initialization abnormal. This status code is thrown when an error occurs during the process of createing empty pixelmap. | 1521| 62980106 | The image data is to large. This status code is thrown when an error occurs during the process of checking size. | 1522 1523**Example** 1524 1525```ts 1526import { BusinessError } from '@kit.BasicServicesKit'; 1527import {image} from '@kit.ImageKit'; 1528 1529async function Demo(pixelMap: image.PixelMap) { 1530 if (pixelMap != undefined) { 1531 try { 1532 let clonedPixelMap:image.PixelMap = pixelMap.cloneSync(); 1533 } catch(e) { 1534 let error = e as BusinessError; 1535 console.error(`clone pixelmap error. code is ${error.code}, message is ${error.message}`); 1536 } 1537 } 1538} 1539``` 1540 1541## translate<sup>9+</sup> 1542 1543translate(x: number, y: number, callback: AsyncCallback\<void>): void 1544 1545Translates this image based on given coordinates. This API uses an asynchronous callback to return the result. 1546 1547The size of the translated image is changed to width+X and height+Y. It is recommended that the new width and height not exceed the width and height of the screen. 1548 1549**Widget capability**: This API can be used in ArkTS widgets since API version 12. 1550 1551**Atomic service API**: This API can be used in atomic services since API version 11. 1552 1553**System capability**: SystemCapability.Multimedia.Image.Core 1554 1555**Parameters** 1556 1557| Name | Type | Mandatory| Description | 1558| -------- | -------------------- | ---- | ----------------------------- | 1559| x | number | Yes | X coordinate to translate, in px.| 1560| y | number | Yes | Y coordinate to translate, in px.| 1561| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 1562 1563**Example** 1564 1565```ts 1566import { BusinessError } from '@kit.BasicServicesKit'; 1567import {image} from '@kit.ImageKit'; 1568 1569async function Translate(pixelMap:image.PixelMap) { 1570 let translateX: number = 50.0; 1571 let translateY: number = 10.0; 1572 if (pixelMap != undefined) { 1573 pixelMap.translate(translateX, translateY, (err: BusinessError) => { 1574 if (err) { 1575 console.error(`Failed to translate pixelmap. code is ${err.code}, message is ${err.message}`); 1576 return; 1577 } else { 1578 console.info("Succeeded in translating pixelmap."); 1579 } 1580 }) 1581 } 1582} 1583``` 1584 1585## translate<sup>9+</sup> 1586 1587translate(x: number, y: number): Promise\<void> 1588 1589Translates this image based on given coordinates. This API uses a promise to return the result. 1590 1591The size of the translated image is changed to width+X and height+Y. It is recommended that the new width and height not exceed the width and height of the screen. 1592 1593**Widget capability**: This API can be used in ArkTS widgets since API version 12. 1594 1595**Atomic service API**: This API can be used in atomic services since API version 11. 1596 1597**System capability**: SystemCapability.Multimedia.Image.Core 1598 1599**Parameters** 1600 1601| Name| Type | Mandatory| Description | 1602| ------ | ------ | ---- | ----------- | 1603| x | number | Yes | X coordinate to translate, in px.| 1604| y | number | Yes | Y coordinate to translate, in px.| 1605 1606**Return value** 1607 1608| Type | Description | 1609| -------------- | --------------------------- | 1610| Promise\<void> | Promise that returns no value.| 1611 1612**Example** 1613 1614```ts 1615import { BusinessError } from '@kit.BasicServicesKit'; 1616import {image} from '@kit.ImageKit'; 1617 1618async function Translate(pixelMap:image.PixelMap) { 1619 let translateX: number = 50.0; 1620 let translateY: number = 10.0; 1621 if (pixelMap != undefined) { 1622 pixelMap.translate(translateX, translateY).then(() => { 1623 console.info('Succeeded in translating pixelmap.'); 1624 }).catch((err: BusinessError) => { 1625 console.error(`Failed to translate pixelmap. code is ${err.code}, message is ${err.message}`); 1626 }) 1627 } 1628} 1629``` 1630 1631## translateSync<sup>12+</sup> 1632 1633translateSync(x: number, y: number): void 1634 1635Translates this image based on given coordinates. This API returns the result synchronously. 1636 1637The size of the translated image is changed to width+X and height+Y. It is recommended that the new width and height not exceed the width and height of the screen. 1638 1639**Atomic service API**: This API can be used in atomic services since API version 12. 1640 1641**System capability**: SystemCapability.Multimedia.Image.Core 1642 1643**Parameters** 1644 1645| Name | Type | Mandatory| Description | 1646| -------- | -------------------- | ---- | ------------------------------- | 1647| x | number | Yes | X coordinate to translate, in px.| 1648| y | number | Yes | Y coordinate to translate, in px.| 1649 1650**Error codes** 1651 1652For details about the error codes, see [Image Error Codes](errorcode-image.md). 1653 1654| ID| Error Message| 1655| ------- | --------------------------------------------| 1656| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed | 1657| 501 | Resource Unavailable | 1658 1659**Example** 1660 1661```ts 1662import { BusinessError } from '@kit.BasicServicesKit'; 1663import {image} from '@kit.ImageKit'; 1664 1665async function TranslateSync(pixelMap:image.PixelMap) { 1666 let translateX : number = 50.0; 1667 let translateY : number = 10.0; 1668 if (pixelMap != undefined) { 1669 pixelMap.translateSync(translateX, translateY); 1670 } 1671} 1672``` 1673 1674## rotate<sup>9+</sup> 1675 1676rotate(angle: number, callback: AsyncCallback\<void>): void 1677 1678Rotates this image based on a given angle. This API uses an asynchronous callback to return the result. 1679 1680> **NOTE** 1681> 1682> The allowable range for image rotation angles is between 0 and 360 degrees. Angles outside this range are automatically adjusted according to the 360-degree cycle. For example, an angle of -100 degrees will produce the same result as 260 degrees. 1683> 1684> If the rotation angle is not an integer multiple of 90 degrees, the image size will change after rotation. 1685 1686**Widget capability**: This API can be used in ArkTS widgets since API version 12. 1687 1688**Atomic service API**: This API can be used in atomic services since API version 11. 1689 1690**System capability**: SystemCapability.Multimedia.Image.Core 1691 1692**Parameters** 1693 1694| Name | Type | Mandatory| Description | 1695| -------- | -------------------- | ---- | ----------------------------- | 1696| angle | number | Yes | Angle to rotate.| 1697| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 1698 1699**Example** 1700 1701```ts 1702import { BusinessError } from '@kit.BasicServicesKit'; 1703import {image} from '@kit.ImageKit'; 1704 1705async function Rotate(pixelMap:image.PixelMap) { 1706 let angle: number = 90.0; 1707 if (pixelMap != undefined) { 1708 pixelMap.rotate(angle, (err: BusinessError) => { 1709 if (err) { 1710 console.error(`Failed to rotate pixelmap. code is ${err.code}, message is ${err.message}`); 1711 return; 1712 } else { 1713 console.info("Succeeded in rotating pixelmap."); 1714 } 1715 }) 1716 } 1717} 1718``` 1719 1720## rotate<sup>9+</sup> 1721 1722rotate(angle: number): Promise\<void> 1723 1724Rotates this image based on a given angle. This API uses a promise to return the result. 1725 1726> **NOTE** 1727> 1728> The allowable range for image rotation angles is between 0 and 360 degrees. Angles outside this range are automatically adjusted according to the 360-degree cycle. For example, an angle of -100 degrees will produce the same result as 260 degrees. 1729> 1730> If the rotation angle is not an integer multiple of 90 degrees, the image size will change after rotation. 1731 1732**Widget capability**: This API can be used in ArkTS widgets since API version 12. 1733 1734**Atomic service API**: This API can be used in atomic services since API version 11. 1735 1736**System capability**: SystemCapability.Multimedia.Image.Core 1737 1738**Parameters** 1739 1740| Name| Type | Mandatory| Description | 1741| ------ | ------ | ---- | ----------------------------- | 1742| angle | number | Yes | Angle to rotate.| 1743 1744**Return value** 1745 1746| Type | Description | 1747| -------------- | --------------------------- | 1748| Promise\<void> | Promise that returns no value.| 1749 1750**Example** 1751 1752```ts 1753import { BusinessError } from '@kit.BasicServicesKit'; 1754import {image} from '@kit.ImageKit'; 1755 1756async function Rotate(pixelMap:image.PixelMap) { 1757 let angle: number = 90.0; 1758 if (pixelMap != undefined) { 1759 pixelMap.rotate(angle).then(() => { 1760 console.info('Succeeded in rotating pixelmap.'); 1761 }).catch((err: BusinessError) => { 1762 console.error(`Failed to rotate pixelmap. code is ${err.code}, message is ${err.message}`); 1763 }) 1764 } 1765} 1766``` 1767 1768## rotateSync<sup>12+</sup> 1769 1770rotateSync(angle: number): void 1771 1772Rotates this image based on a given angle. This API returns the result synchronously. 1773 1774> **NOTE** 1775> 1776> The allowable range for image rotation angles is between 0 and 360 degrees. Angles outside this range are automatically adjusted according to the 360-degree cycle. For example, an angle of -100 degrees will produce the same result as 260 degrees. 1777> 1778> If the rotation angle is not an integer multiple of 90 degrees, the image size will change after rotation. 1779 1780**Atomic service API**: This API can be used in atomic services since API version 12. 1781 1782**System capability**: SystemCapability.Multimedia.Image.Core 1783 1784**Parameters** 1785 1786| Name | Type | Mandatory| Description | 1787| -------- | -------------------- | ---- | ----------------------------- | 1788| angle | number | Yes | Angle to rotate.| 1789 1790**Error codes** 1791 1792For details about the error codes, see [Image Error Codes](errorcode-image.md). 1793 1794| ID| Error Message| 1795| ------- | --------------------------------------------| 1796| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed | 1797| 501 | Resource Unavailable | 1798 1799**Example** 1800 1801```ts 1802import { BusinessError } from '@kit.BasicServicesKit'; 1803 1804async function RotateSync() { 1805 let angle : number = 90.0; 1806 if (pixelMap != undefined) { 1807 pixelMap.rotateSync(angle); 1808 } 1809} 1810``` 1811 1812## flip<sup>9+</sup> 1813 1814flip(horizontal: boolean, vertical: boolean, callback: AsyncCallback\<void>): void 1815 1816Flips this image horizontally or vertically, or both. This API uses an asynchronous callback to return the result. 1817 1818**Widget capability**: This API can be used in ArkTS widgets since API version 12. 1819 1820**Atomic service API**: This API can be used in atomic services since API version 11. 1821 1822**System capability**: SystemCapability.Multimedia.Image.Core 1823 1824**Parameters** 1825 1826| Name | Type | Mandatory| Description | 1827| ---------- | -------------------- | ---- | ----------------------------- | 1828| horizontal | boolean | Yes | Whether to flip the image horizontally. **true** to flip the image horizontally, **false** otherwise. | 1829| vertical | boolean | Yes | Whether to flip the image vertically. **true** to flip the image vertically, **false** otherwise. | 1830| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 1831 1832**Example** 1833 1834```ts 1835import { BusinessError } from '@kit.BasicServicesKit'; 1836import {image} from '@kit.ImageKit'; 1837 1838async function Flip(pixelMap:image.PixelMap) { 1839 let horizontal: boolean = true; 1840 let vertical: boolean = false; 1841 if (pixelMap != undefined) { 1842 pixelMap.flip(horizontal, vertical, (err: BusinessError) => { 1843 if (err) { 1844 console.error(`Failed to flip pixelmap. code is ${err.code}, message is ${err.message}`); 1845 return; 1846 } else { 1847 console.info("Succeeded in flipping pixelmap."); 1848 } 1849 }) 1850 } 1851} 1852``` 1853 1854## flip<sup>9+</sup> 1855 1856flip(horizontal: boolean, vertical: boolean): Promise\<void> 1857 1858Flips this image horizontally or vertically, or both. This API uses a promise to return the result. 1859 1860**Widget capability**: This API can be used in ArkTS widgets since API version 12. 1861 1862**Atomic service API**: This API can be used in atomic services since API version 11. 1863 1864**System capability**: SystemCapability.Multimedia.Image.Core 1865 1866**Parameters** 1867 1868| Name | Type | Mandatory| Description | 1869| ---------- | ------- | ---- | --------- | 1870| horizontal | boolean | Yes | Whether to flip the image horizontally. **true** to flip the image horizontally, **false** otherwise. | 1871| vertical | boolean | Yes | Whether to flip the image vertically. **true** to flip the image vertically, **false** otherwise. | 1872 1873**Return value** 1874 1875| Type | Description | 1876| -------------- | --------------------------- | 1877| Promise\<void> | Promise that returns no value.| 1878 1879**Example** 1880 1881```ts 1882import { BusinessError } from '@kit.BasicServicesKit'; 1883import {image} from '@kit.ImageKit'; 1884 1885async function Flip(pixelMap:image.PixelMap) { 1886 let horizontal: boolean = true; 1887 let vertical: boolean = false; 1888 if (pixelMap != undefined) { 1889 pixelMap.flip(horizontal, vertical).then(() => { 1890 console.info('Succeeded in flipping pixelmap.'); 1891 }).catch((err: BusinessError) => { 1892 console.error(`Failed to flip pixelmap. code is ${err.code}, message is ${err.message}`); 1893 }) 1894 } 1895} 1896``` 1897 1898## flipSync<sup>12+</sup> 1899 1900flipSync(horizontal: boolean, vertical: boolean): void 1901 1902Flips this image horizontally or vertically, or both. This API returns the result synchronously. 1903 1904**Atomic service API**: This API can be used in atomic services since API version 12. 1905 1906**System capability**: SystemCapability.Multimedia.Image.Core 1907 1908**Parameters** 1909 1910| Name | Type | Mandatory| Description | 1911| ---------- | -------------------- | ---- | ----------------------------- | 1912| horizontal | boolean | Yes | Whether to flip the image horizontally. **true** to flip the image horizontally, **false** otherwise. | 1913| vertical | boolean | Yes | Whether to flip the image vertically. **true** to flip the image vertically, **false** otherwise. | 1914 1915**Error codes** 1916 1917For details about the error codes, see [Image Error Codes](errorcode-image.md). 1918 1919| ID| Error Message| 1920| ------- | --------------------------------------------| 1921| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed | 1922| 501 | Resource Unavailable | 1923 1924**Example** 1925 1926```ts 1927import { BusinessError } from '@kit.BasicServicesKit'; 1928import {image} from '@kit.ImageKit'; 1929 1930async function FlipSync(pixelMap:image.PixelMap) { 1931 let horizontal : boolean = true; 1932 let vertical : boolean = false; 1933 if (pixelMap != undefined) { 1934 pixelMap.flipSync(horizontal, vertical); 1935 } 1936} 1937``` 1938 1939## crop<sup>9+</sup> 1940 1941crop(region: Region, callback: AsyncCallback\<void>): void 1942 1943Crops this image based on a given size. This API uses an asynchronous callback to return the result. 1944 1945**Widget capability**: This API can be used in ArkTS widgets since API version 12. 1946 1947**Atomic service API**: This API can be used in atomic services since API version 11. 1948 1949**System capability**: SystemCapability.Multimedia.Image.Core 1950 1951**Parameters** 1952 1953| Name | Type | Mandatory| Description | 1954| -------- | -------------------- | ---- | ----------------------------- | 1955| region | [Region](arkts-apis-image-i.md#region8) | Yes | Size of the image after cropping. The value cannot exceed the width or height of the image.| 1956| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 1957 1958**Example** 1959 1960```ts 1961import { BusinessError } from '@kit.BasicServicesKit'; 1962import {image} from '@kit.ImageKit' 1963 1964async function Crop(pixelMap:image.PixelMap) { 1965 let region: image.Region = { x: 0, y: 0, size: { height: 100, width: 100 } }; 1966 if (pixelMap != undefined) { 1967 await pixelMap.crop(region, (err: BusinessError) => { 1968 if (err) { 1969 console.error(`Failed to crop pixelmap. code is ${err.code}, message is ${err.message}`); 1970 return; 1971 } else { 1972 console.info("Succeeded in cropping pixelmap."); 1973 } 1974 }) 1975 } 1976} 1977``` 1978 1979## crop<sup>9+</sup> 1980 1981crop(region: Region): Promise\<void> 1982 1983Crops this image based on a given size. This API uses a promise to return the result. 1984 1985**Widget capability**: This API can be used in ArkTS widgets since API version 12. 1986 1987**Atomic service API**: This API can be used in atomic services since API version 11. 1988 1989**System capability**: SystemCapability.Multimedia.Image.Core 1990 1991**Parameters** 1992 1993| Name| Type | Mandatory| Description | 1994| ------ | ------------------ | ---- | ----------- | 1995| region | [Region](arkts-apis-image-i.md#region8) | Yes | Size of the image after cropping. The value cannot exceed the width or height of the image.| 1996 1997**Return value** 1998 1999| Type | Description | 2000| -------------- | --------------------------- | 2001| Promise\<void> | Promise that returns no value.| 2002 2003**Example** 2004 2005```ts 2006import { BusinessError } from '@kit.BasicServicesKit'; 2007import {image} from '@kit.ImageKit' 2008 2009async function Crop(pixelMap:image.PixelMap) { 2010 let region: image.Region = { x: 0, y: 0, size: { height: 100, width: 100 } }; 2011 if (pixelMap != undefined) { 2012 pixelMap.crop(region).then(() => { 2013 console.info('Succeeded in cropping pixelmap.'); 2014 }).catch((err: BusinessError) => { 2015 console.error(`Failed to crop pixelmap. code is ${err.code}, message is ${err.message}`); 2016 2017 }); 2018 } 2019} 2020``` 2021 2022## cropSync<sup>12+</sup> 2023 2024cropSync(region: Region): void 2025 2026Crops this image based on a given size. This API returns the result synchronously. 2027 2028**Atomic service API**: This API can be used in atomic services since API version 12. 2029 2030**System capability**: SystemCapability.Multimedia.Image.Core 2031 2032**Parameters** 2033 2034| Name | Type | Mandatory| Description | 2035| -------- | -------------------- | ---- | ----------------------------- | 2036| region | [Region](arkts-apis-image-i.md#region8) | Yes | Size of the image after cropping. The value cannot exceed the width or height of the image.| 2037 2038**Error codes** 2039 2040For details about the error codes, see [Image Error Codes](errorcode-image.md). 2041 2042| ID| Error Message| 2043| ------- | --------------------------------------------| 2044| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed | 2045| 501 | Resource Unavailable | 2046 2047**Example** 2048 2049```ts 2050import { BusinessError } from '@kit.BasicServicesKit'; 2051import {image} from '@kit.ImageKit'; 2052 2053function CropSync(pixelMap:image.PixelMap) { 2054 let region : image.Region = { x: 0, y: 0, size: { height: 100, width: 100 } }; 2055 if (pixelMap != undefined) { 2056 pixelMap.cropSync(region); 2057 } 2058} 2059``` 2060 2061## getColorSpace<sup>10+</sup> 2062 2063getColorSpace(): colorSpaceManager.ColorSpaceManager 2064 2065Obtains the color space of this image. 2066 2067**System capability**: SystemCapability.Multimedia.Image.Core 2068 2069**Return value** 2070 2071| Type | Description | 2072| ----------------------------------- | ---------------- | 2073| [colorSpaceManager.ColorSpaceManager](../apis-arkgraphics2d/js-apis-colorSpaceManager.md#colorspacemanager) | Color space obtained.| 2074 2075**Error codes** 2076 2077For details about the error codes, see [Image Error Codes](errorcode-image.md). 2078 2079| ID| Error Message| 2080| ------- | --------------------------------------------| 2081| 62980101| If the image data abnormal. | 2082| 62980103| If the image data unsupport. | 2083| 62980115| If the image parameter invalid. | 2084 2085**Example** 2086 2087```ts 2088import { BusinessError } from '@kit.BasicServicesKit'; 2089import {image} from '@kit.ImageKit'; 2090 2091function GetColorSpace(pixelMap:image.PixelMap) { 2092 if (pixelMap != undefined) { 2093 let csm = pixelMap.getColorSpace(); 2094 } 2095} 2096``` 2097 2098## setColorSpace<sup>10+</sup> 2099 2100setColorSpace(colorSpace: colorSpaceManager.ColorSpaceManager): void 2101 2102Sets the color space for this image. 2103 2104**System capability**: SystemCapability.Multimedia.Image.Core 2105 2106**Parameters** 2107 2108| Name | Type | Mandatory| Description | 2109| ---------- | ----------------------------------- | ---- | --------------- | 2110| colorSpace | [colorSpaceManager.ColorSpaceManager](../apis-arkgraphics2d/js-apis-colorSpaceManager.md#colorspacemanager) | Yes | Color space to set.| 2111 2112**Error codes** 2113 2114For details about the error codes, see [Image Error Codes](errorcode-image.md). 2115 2116| ID| Error Message| 2117| ------- | --------------------------------------------| 2118| 62980111| The image source data is incomplete. | 2119| 62980115| If the image parameter invalid. | 2120 2121**Example** 2122 2123```ts 2124import { colorSpaceManager } from '@kit.ArkGraphics2D'; 2125import {image} from '@kit.ImageKit'; 2126 2127async function SetColorSpace(pixelMap:image.PixelMap) { 2128 let colorSpaceName = colorSpaceManager.ColorSpace.SRGB; 2129 let csm: colorSpaceManager.ColorSpaceManager = colorSpaceManager.create(colorSpaceName); 2130 if (pixelMap != undefined) { 2131 pixelMap.setColorSpace(csm); 2132 } 2133} 2134``` 2135 2136## applyColorSpace<sup>11+</sup> 2137 2138applyColorSpace(targetColorSpace: colorSpaceManager.ColorSpaceManager, callback: AsyncCallback\<void>): void 2139 2140Performs color space conversion (CSC) on the image pixel color based on a given color space. This API uses an asynchronous callback to return the result. 2141 2142**System capability**: SystemCapability.Multimedia.Image.Core 2143 2144**Parameters** 2145 2146| Name | Type | Mandatory| Description | 2147| -------- | -------------------- | ---- | ----------------------------- | 2148| targetColorSpace | [colorSpaceManager.ColorSpaceManager](../apis-arkgraphics2d/js-apis-colorSpaceManager.md#colorspacemanager) | Yes | Target color space. SRGB, DCI_P3, DISPLAY_P3, and ADOBE_RGB_1998 are supported.| 2149| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 2150 2151**Error codes** 2152 2153For details about the error codes, see [Image Error Codes](errorcode-image.md). 2154 2155| ID| Error Message| 2156| ------- | ------------------------------------------| 2157| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed | 2158| 62980104| Failed to initialize the internal object. | 2159| 62980108| Failed to convert the color space. | 2160| 62980115| Invalid image parameter. | 2161 2162**Example** 2163 2164```ts 2165import { colorSpaceManager } from '@kit.ArkGraphics2D'; 2166import { BusinessError } from '@kit.BasicServicesKit'; 2167import {image} from '@kit.ImageKit'; 2168 2169async function ApplyColorSpace(pixelMap:image.PixelMap) { 2170 let colorSpaceName = colorSpaceManager.ColorSpace.SRGB; 2171 let targetColorSpace: colorSpaceManager.ColorSpaceManager = colorSpaceManager.create(colorSpaceName); 2172 if (pixelMap != undefined) { 2173 try { 2174 await pixelMap.applyColorSpace(targetColorSpace); 2175 } catch (error) { 2176 console.error(`Failed to apply color space for pixelmap object, error code is ${error}`); 2177 return; 2178 } 2179 console.info('Succeeded in applying color space for pixelmap object.'); 2180 } 2181} 2182``` 2183 2184## applyColorSpace<sup>11+</sup> 2185 2186applyColorSpace(targetColorSpace: colorSpaceManager.ColorSpaceManager): Promise\<void> 2187 2188Performs CSC on the image pixel color based on a given color space. This API uses a promise to return the result. 2189 2190**System capability**: SystemCapability.Multimedia.Image.Core 2191 2192**Parameters** 2193 2194| Name| Type | Mandatory| Description | 2195| ------ | ------------------ | ---- | ----------- | 2196| targetColorSpace | [colorSpaceManager.ColorSpaceManager](../apis-arkgraphics2d/js-apis-colorSpaceManager.md#colorspacemanager) | Yes | Target color space. SRGB, DCI_P3, DISPLAY_P3, and ADOBE_RGB_1998 are supported.| 2197 2198**Return value** 2199 2200| Type | Description | 2201| -------------- | --------------------------- | 2202| Promise\<void> | Promise that returns no value.| 2203 2204**Error codes** 2205 2206For details about the error codes, see [Image Error Codes](errorcode-image.md). 2207 2208| ID| Error Message| 2209| ------- | ------------------------------------------| 2210| 401 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed | 2211| 62980104| Failed to initialize the internal object. | 2212| 62980108| Failed to convert the color space. | 2213| 62980115| Invalid image parameter. | 2214 2215**Example** 2216 2217```ts 2218import { colorSpaceManager } from '@kit.ArkGraphics2D'; 2219import { BusinessError } from '@kit.BasicServicesKit'; 2220import {image} from '@kit.ImageKit'; 2221 2222async function ApplyColorSpace(pixelMap:image.PixelMap) { 2223 let colorSpaceName = colorSpaceManager.ColorSpace.SRGB; 2224 let targetColorSpace: colorSpaceManager.ColorSpaceManager = colorSpaceManager.create(colorSpaceName); 2225 if (pixelMap != undefined) { 2226 try { 2227 await pixelMap.applyColorSpace(targetColorSpace); 2228 } catch (error) { 2229 console.error(`Failed to apply color space for pixelmap object, error code is ${error}`); 2230 return; 2231 } 2232 console.info('Succeeded in applying color space for pixelmap object.'); 2233 } 2234} 2235``` 2236 2237## toSdr<sup>12+<sup> 2238 2239toSdr(): Promise\<void> 2240 2241Converts an HDR image into an SDR image. This API uses a promise to return the result. 2242 2243**System capability**: SystemCapability.Multimedia.Image.Core 2244 2245**Return value** 2246 2247| Type | Description | 2248| -------------- | --------------------------- | 2249| Promise\<void> | Promise that returns no value.| 2250 2251**Error codes** 2252 2253For details about the error codes, see [Image Error Codes](errorcode-image.md). 2254 2255| ID| Error Message| 2256| ------- | --------------------------------------------| 2257| 62980137 | Invalid image operation. | 2258 2259**Example** 2260 2261<!--code_no_check--> 2262```ts 2263import { BusinessError } from '@kit.BasicServicesKit'; 2264import { common } from '@kit.AbilityKit'; 2265import { image } from '@kit.ImageKit'; 2266import { resourceManager } from '@kit.LocalizationKit'; 2267 2268// Obtain the context from the component and ensure that the return value of this.getUIContext().getHostContext() is UIAbilityContext. 2269let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 2270// 'hdr.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. 2271let img = context.resourceManager.getMediaContentSync($r('app.media.hdr').id); 2272let imageSource = image.createImageSource(img.buffer.slice(0)); 2273let decodingOptions: image.DecodingOptions = { 2274 desiredDynamicRange: image.DecodingDynamicRange.AUTO 2275}; 2276let pixelmap = imageSource.createPixelMapSync(decodingOptions); 2277if (pixelmap != undefined) { 2278 console.info('Succeeded in creating pixelMap object.'); 2279 pixelmap.toSdr().then(() => { 2280 let imageInfo = pixelmap.getImageInfoSync(); 2281 console.info("after toSdr ,imageInfo isHdr:" + imageInfo.isHdr); 2282 }).catch((err: BusinessError) => { 2283 console.error(`Failed to set sdr. code is ${err.code}, message is ${err.message}`); 2284 }); 2285} else { 2286 console.error('Failed to create pixelMap.'); 2287} 2288``` 2289 2290## getMetadata<sup>12+</sup> 2291 2292getMetadata(key: HdrMetadataKey): HdrMetadataValue 2293 2294Obtains the value of the metadata with a given key in this PixelMap. 2295 2296**System capability**: SystemCapability.Multimedia.Image.Core 2297 2298**Parameters** 2299 2300| Name | Type | Mandatory| Description | 2301| ------------- | -------------------------------- | ---- | ---------------- | 2302| key | [HdrMetadataKey](arkts-apis-image-e.md#hdrmetadatakey12) | Yes | Key of the HDR metadata.| 2303 2304**Return value** 2305 2306| Type | Description | 2307| --------------------------------- | --------------------------------- | 2308| [HdrMetadataValue](arkts-apis-image-t.md#hdrmetadatavalue12) | Value of the metadata with the given key.| 2309 2310**Error codes** 2311 2312For details about the error codes, see [Image Error Codes](errorcode-image.md). 2313 2314| ID| Error Message| 2315| ------- | --------------------------------------------| 2316| 401| Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. | 2317| 501 | Resource unavailable. | 2318| 62980173 | The DMA memory does not exist. | 2319| 62980302 | Memory copy failed. Possibly caused by invalid metadata value. | 2320 2321**Example** 2322 2323<!--code_no_check--> 2324```ts 2325import { image } from '@kit.ImageKit'; 2326import { resourceManager } from '@kit.LocalizationKit'; 2327import { BusinessError } from '@kit.BasicServicesKit'; 2328import { common } from '@kit.AbilityKit'; 2329 2330// Obtain the context from the component and ensure that the return value of this.getUIContext().getHostContext() is UIAbilityContext. 2331let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 2332// Replace 'app.media.test' with a local HDR image. 2333let img = context.resourceManager.getMediaContentSync($r('app.media.test').id); 2334let imageSource = image.createImageSource(img.buffer.slice(0)); 2335let decodingOptions: image.DecodingOptions = { 2336 desiredDynamicRange: image.DecodingDynamicRange.AUTO 2337}; 2338let pixelmap = imageSource.createPixelMapSync(decodingOptions); 2339if (pixelmap != undefined) { 2340 console.info('Succeeded in creating pixelMap object.'); 2341 try { 2342 let staticMetadata = pixelmap.getMetadata(image.HdrMetadataKey.HDR_STATIC_METADATA); 2343 console.info("getmetadata:" + JSON.stringify(staticMetadata)); 2344 } catch (e) { 2345 console.error('pixelmap create failed' + e); 2346 } 2347} else { 2348 console.error('Failed to create pixelMap.'); 2349} 2350``` 2351 2352## setMetadata<sup>12+</sup> 2353 2354setMetadata(key: HdrMetadataKey, value: HdrMetadataValue): Promise\<void> 2355 2356Sets the value for the metadata with a given key in this PixelMap. 2357 2358**System capability**: SystemCapability.Multimedia.Image.Core 2359 2360**Parameters** 2361 2362| Name | Type | Mandatory| Description | 2363| ------------- | -------------------------------- | ---- | ---------------- | 2364| key | [HdrMetadataKey](arkts-apis-image-e.md#hdrmetadatakey12) | Yes | Key of the HDR metadata.| 2365| value | [HdrMetadataValue](arkts-apis-image-t.md#hdrmetadatavalue12) | Yes | Value of the metadata.| 2366 2367**Return value** 2368 2369| Type | Description | 2370| -------------- | --------------------- | 2371| Promise\<void> | Promise that returns no value.| 2372 2373**Error codes** 2374 2375For details about the error codes, see [Image Error Codes](errorcode-image.md). 2376 2377| ID| Error Message| 2378| ------- | --------------------------------------------| 2379| 401| Parameter error. Possible causes: 1.Mandatory parameters are left unspecified. 2.Incorrect parameter types. 3.Parameter verification failed. | 2380| 501 | Resource unavailable. | 2381| 62980173 | The DMA memory does not exist. | 2382| 62980302 | Memory copy failed. Possibly caused by invalid metadata value. | 2383 2384**Example** 2385 2386```ts 2387import image from '@ohos.multimedia.image'; 2388import { BusinessError } from '@kit.BasicServicesKit'; 2389 2390let staticMetadata: image.HdrStaticMetadata = { 2391 displayPrimariesX: [1.1, 1.1, 1.1], 2392 displayPrimariesY: [1.2, 1.2, 1.2], 2393 whitePointX: 1.1, 2394 whitePointY: 1.2, 2395 maxLuminance: 2.1, 2396 minLuminance: 1.0, 2397 maxContentLightLevel: 2.1, 2398 maxFrameAverageLightLevel: 2.1, 2399}; 2400const color: ArrayBuffer = new ArrayBuffer(96); // 96 is the size of the pixel buffer to create. The value is calculated as follows: height * width *4. 2401let opts: image.InitializationOptions = { editable: true, pixelFormat: image.PixelMapFormat.RGBA_8888, size: { height: 4, width: 6 } }; 2402image.createPixelMap(color, opts).then((pixelMap: image.PixelMap) => { 2403 pixelMap.setMetadata(image.HdrMetadataKey.HDR_STATIC_METADATA, staticMetadata).then(() => { 2404 console.info('Succeeded in setting pixelMap metadata.'); 2405 }).catch((error: BusinessError) => { 2406 console.error(`Failed to set the metadata.code ${error.code},message is ${error.message}`); 2407 }) 2408}).catch((error: BusinessError) => { 2409 console.error(`Failed to create the PixelMap.code ${error.code},message is ${error.message}`); 2410}) 2411 2412``` 2413 2414## setTransferDetached<sup>12+<sup> 2415 2416setTransferDetached(detached: boolean): void 2417 2418Sets whether to detach from the original thread when this PixelMap is transmitted across threads. This API applies to the scenario where the PixelMap needs to be released immediately. 2419 2420**System capability**: SystemCapability.Multimedia.Image.Core 2421 2422**Parameters** 2423 2424| Name | Type | Mandatory| Description | 2425| ------- | ------------------ | ---- | ----------------------------- | 2426| detached | boolean | Yes | Whether to detach from the original thread. **true** to detach, **false** otherwise.| 2427 2428**Error codes** 2429 2430For details about the error codes, see [Image Error Codes](errorcode-image.md). 2431 2432| ID| Error Message| 2433| ------- | --------------------------------------------| 2434| 501 | Resource Unavailable | 2435 2436**Example** 2437 2438```ts 2439import { BusinessError } from '@kit.BasicServicesKit'; 2440import { common } from '@kit.AbilityKit'; 2441import image from '@ohos.multimedia.image'; 2442import taskpool from '@ohos.taskpool'; 2443 2444@Concurrent 2445// Child thread method. 2446async function loadPixelMap(rawFileDescriptor: number): Promise<PixelMap> { 2447 // Create an ImageSource instance. 2448 const imageSource = image.createImageSource(rawFileDescriptor); 2449 // Create a pixelMap. 2450 const pixelMap = imageSource.createPixelMapSync(); 2451 // Release the ImageSource instance. 2452 imageSource.release(); 2453 // Disconnect the reference of the original thread after the cross-thread transfer of the pixelMap is complete. 2454 pixelMap.setTransferDetached(true); 2455 // Return the pixelMap to the main thread. 2456 return pixelMap; 2457} 2458 2459@Component 2460struct Demo { 2461 @State pixelMap: PixelMap | undefined = undefined; 2462 // Main thread method. 2463 private loadImageFromThread(): void { 2464 let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 2465 const resourceMgr = context.resourceManager; 2466 // 'example.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. 2467 resourceMgr.getRawFd('example.jpg').then(rawFileDescriptor => { 2468 taskpool.execute(loadPixelMap, rawFileDescriptor).then(pixelMap => { 2469 if (pixelMap) { 2470 this.pixelMap = pixelMap as PixelMap; 2471 console.log('Succeeded in creating pixelMap.'); 2472 // The main thread releases the pixelMap. Because setTransferDetached has been called when the child thread returns pixelMap, the pixelMap can be released immediately. 2473 this.pixelMap.release(); 2474 } else { 2475 console.error('Failed to create pixelMap.'); 2476 } 2477 }); 2478 }); 2479 } 2480 build() { 2481 // ... 2482 } 2483} 2484``` 2485 2486## marshalling<sup>10+</sup> 2487 2488marshalling(sequence: rpc.MessageSequence): void 2489 2490Marshals this PixelMap object and writes it to a MessageSequence object. 2491 2492**System capability**: SystemCapability.Multimedia.Image.Core 2493 2494**Parameters** 2495 2496| Name | Type | Mandatory| Description | 2497| ---------------------- | ------------------------------------------------------ | ---- | ---------------------------------------- | 2498| sequence | [rpc.MessageSequence](../apis-ipc-kit/js-apis-rpc.md#messagesequence9) | Yes | MessageSequence object. | 2499 2500**Error codes** 2501 2502For details about the error codes, see [Image Error Codes](errorcode-image.md). 2503 2504| ID| Error Message| 2505| ------- | --------------------------------------------| 2506| 62980115 | Invalid image parameter. | 2507| 62980097 | IPC error. Possible cause: 1.IPC communication failed. 2. Image upload exception. 3. Decode process exception. 4. Insufficient memory. | 2508 2509**Example** 2510 2511```ts 2512import { image } from '@kit.ImageKit'; 2513import { rpc } from '@kit.IPCKit'; 2514 2515class MySequence implements rpc.Parcelable { 2516 pixel_map: image.PixelMap; 2517 constructor(conPixelMap : image.PixelMap) { 2518 this.pixel_map = conPixelMap; 2519 } 2520 marshalling(messageSequence : rpc.MessageSequence) { 2521 this.pixel_map.marshalling(messageSequence); 2522 console.info('marshalling'); 2523 return true; 2524 } 2525 unmarshalling(messageSequence : rpc.MessageSequence) { 2526 image.createPixelMap(new ArrayBuffer(96), {size: { height:4, width: 6}}).then((pixelParcel: image.PixelMap) => { 2527 pixelParcel.unmarshalling(messageSequence).then(async (pixelMap: image.PixelMap) => { 2528 this.pixel_map = pixelMap; 2529 pixelMap.getImageInfo().then((imageInfo: image.ImageInfo) => { 2530 console.info("unmarshalling information h:" + imageInfo.size.height + "w:" + imageInfo.size.width); 2531 }) 2532 }) 2533 }); 2534 return true; 2535 } 2536} 2537async function Marshalling() { 2538 const color: ArrayBuffer = new ArrayBuffer(96); 2539 let bufferArr: Uint8Array = new Uint8Array(color); 2540 for (let i = 0; i < bufferArr.length; i++) { 2541 bufferArr[i] = 0x80; 2542 } 2543 let opts: image.InitializationOptions = { 2544 editable: true, 2545 pixelFormat: image.PixelMapFormat.BGRA_8888, 2546 size: { height: 4, width: 6 }, 2547 alphaType: image.AlphaType.UNPREMUL 2548 } 2549 let pixelMap: image.PixelMap | undefined = undefined; 2550 await image.createPixelMap(color, opts).then((srcPixelMap: image.PixelMap) => { 2551 pixelMap = srcPixelMap; 2552 }) 2553 if (pixelMap != undefined) { 2554 // Implement serialization. 2555 let parcelable: MySequence = new MySequence(pixelMap); 2556 let data: rpc.MessageSequence = rpc.MessageSequence.create(); 2557 data.writeParcelable(parcelable); 2558 2559 // Implement deserialization to obtain data through the RPC. 2560 let ret: MySequence = new MySequence(pixelMap); 2561 data.readParcelable(ret); 2562 } 2563} 2564``` 2565 2566## unmarshalling<sup>10+</sup> 2567 2568unmarshalling(sequence: rpc.MessageSequence): Promise\<PixelMap> 2569 2570Unmarshals a MessageSequence object to obtain a PixelMap object. 2571 2572To create a PixelMap object in synchronous mode, use [createPixelMapFromParcel](arkts-apis-image-f.md#imagecreatepixelmapfromparcel11). 2573 2574**System capability**: SystemCapability.Multimedia.Image.Core 2575 2576**Parameters** 2577 2578| Name | Type | Mandatory| Description | 2579| ---------------------- | ----------------------------------------------------- | ---- | ---------------------------------------- | 2580| sequence | [rpc.MessageSequence](../apis-ipc-kit/js-apis-rpc.md#messagesequence9) | Yes | MessageSequence object that stores the PixelMap information. | 2581 2582**Return value** 2583 2584| Type | Description | 2585| -------------------------------- | --------------------- | 2586| Promise\<[PixelMap](arkts-apis-image-PixelMap.md)> |Promise used to return the PixelMap object.| 2587 2588**Error codes** 2589 2590For details about the error codes, see [Image Error Codes](errorcode-image.md). 2591 2592| ID| Error Message| 2593| ------- | --------------------------------------------| 2594| 62980115 | Invalid image parameter. | 2595| 62980097 | IPC error. Possible cause: 1.IPC communication failed. 2. Image upload exception. 3. Decode process exception. 4. Insufficient memory. | 2596| 62980096 | The operation failed. Possible cause: 1.Image upload exception. 2. Decoding process exception. 3. Insufficient memory. | 2597 2598**Example** 2599 2600```ts 2601import { image } from '@kit.ImageKit'; 2602import { rpc } from '@kit.IPCKit'; 2603 2604class MySequence implements rpc.Parcelable { 2605 pixel_map: image.PixelMap; 2606 constructor(conPixelMap: image.PixelMap) { 2607 this.pixel_map = conPixelMap; 2608 } 2609 marshalling(messageSequence: rpc.MessageSequence) { 2610 this.pixel_map.marshalling(messageSequence); 2611 console.info('marshalling'); 2612 return true; 2613 } 2614 unmarshalling(messageSequence: rpc.MessageSequence) { 2615 image.createPixelMap(new ArrayBuffer(96), {size: { height:4, width: 6}}).then((pixelParcel : image.PixelMap) => { 2616 pixelParcel.unmarshalling(messageSequence).then(async (pixelMap : image.PixelMap) => { 2617 this.pixel_map = pixelMap; 2618 pixelMap.getImageInfo().then((imageInfo : image.ImageInfo) => { 2619 console.info("unmarshalling information h:" + imageInfo.size.height + "w:" + imageInfo.size.width); 2620 }) 2621 }) 2622 }); 2623 return true; 2624 } 2625} 2626async function Unmarshalling() { 2627 const color: ArrayBuffer = new ArrayBuffer(96); 2628 let bufferArr: Uint8Array = new Uint8Array(color); 2629 for (let i = 0; i < bufferArr.length; i++) { 2630 bufferArr[i] = 0x80; 2631 } 2632 let opts: image.InitializationOptions = { 2633 editable: true, 2634 pixelFormat: image.PixelMapFormat.BGRA_8888, 2635 size: { height: 4, width: 6 }, 2636 alphaType: image.AlphaType.UNPREMUL 2637 } 2638 let pixelMap: image.PixelMap | undefined = undefined; 2639 await image.createPixelMap(color, opts).then((srcPixelMap : image.PixelMap) => { 2640 pixelMap = srcPixelMap; 2641 }) 2642 if (pixelMap != undefined) { 2643 // Implement serialization. 2644 let parcelable: MySequence = new MySequence(pixelMap); 2645 let data : rpc.MessageSequence = rpc.MessageSequence.create(); 2646 data.writeParcelable(parcelable); 2647 2648 // Implement deserialization to obtain data through the RPC. 2649 let ret : MySequence = new MySequence(pixelMap); 2650 data.readParcelable(ret); 2651 } 2652} 2653``` 2654 2655## release<sup>7+</sup> 2656 2657release():Promise\<void> 2658 2659Releases this PixelMap object. This API uses a promise to return the result. 2660 2661ArkTS supports memory reclamation. Even if the application does not call **release()**, the memory of the PixelMap 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. 2662 2663**Widget capability**: This API can be used in ArkTS widgets since API version 12. 2664 2665**Atomic service API**: This API can be used in atomic services since API version 11. 2666 2667**System capability**: SystemCapability.Multimedia.Image.Core 2668 2669**Return value** 2670 2671| Type | Description | 2672| -------------- | ------------------------------- | 2673| Promise\<void> | Promise that returns no value.| 2674 2675**Example** 2676 2677```ts 2678import { BusinessError } from '@kit.BasicServicesKit'; 2679import {image} from '@kit.ImageKit'; 2680 2681async function Release(pixelMap:image.PixelMap) { 2682 if (pixelMap != undefined) { 2683 await pixelMap.release().then(() => { 2684 console.info('Succeeded in releasing pixelmap object.'); 2685 }).catch((error: BusinessError) => { 2686 console.error(`Failed to release pixelmap object. code is ${error.code}, message is ${error.message}`); 2687 }) 2688 } 2689} 2690``` 2691 2692## release<sup>7+</sup> 2693 2694release(callback: AsyncCallback\<void>): void 2695 2696Releases this PixelMap object. This API uses an asynchronous callback to return the result. 2697 2698ArkTS supports memory reclamation. Even if the application does not call **release()**, the memory of the PixelMap 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. 2699 2700**Widget capability**: This API can be used in ArkTS widgets since API version 12. 2701 2702**Atomic service API**: This API can be used in atomic services since API version 11. 2703 2704**System capability**: SystemCapability.Multimedia.Image.Core 2705 2706**Parameters** 2707 2708| Name | Type | Mandatory| Description | 2709| -------- | -------------------- | ---- | ------------------ | 2710| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**; otherwise, **err** is an error object.| 2711 2712**Example** 2713 2714```ts 2715import { BusinessError } from '@kit.BasicServicesKit'; 2716import {image} from '@kit.ImageKit'; 2717 2718async function Release(pixelMap:image.PixelMap) { 2719 if (pixelMap != undefined) { 2720 pixelMap.release((err: BusinessError) => { 2721 if (err) { 2722 console.error(`Failed to release pixelmap object. code is ${err.code}, message is ${err.message}`); 2723 return; 2724 } else { 2725 console.info('Succeeded in releasing pixelmap object.'); 2726 } 2727 }) 2728 } 2729} 2730``` 2731 2732## convertPixelFormat<sup>12+</sup> 2733 2734convertPixelFormat(targetPixelFormat: PixelMapFormat): Promise\<void> 2735 2736Performs a conversion between YUV and RGB formats. Currently, only conversion between NV12/NV21 and RGB888/RGBA8888/RGB565/BGRA8888/RGBAF16 and conversion between YCRCB_P010/YCBCR_P010 and RGBA1010102 are supported. 2737 2738Since API version 18, this API can be used for conversion from ASTC_4x4 to RGBA_8888. 2739 2740> **NOTE** 2741> 2742> Call this API to convert the format from ASTC_4x4 to RGBA_8888 only when you need to access pixels of images in ASTC_4x4 format. The conversion from ASTC_4x4 to RGBA_8888 is slow and is not recommended in other cases. 2743 2744**System capability**: SystemCapability.Multimedia.Image.Core 2745 2746**Parameters** 2747 2748| Name | Type | Mandatory| Description | 2749| -------- | -------------------- | ---- | ------------------ | 2750| targetPixelFormat | [PixelMapFormat](arkts-apis-image-e.md#pixelmapformat7) | Yes | Target pixel format. Currently, only conversion between NV12/NV21 and RGB888/RGBA8888/RGB565/BGRA8888/RGBAF16, conversion between YCRCB_P010/YCBCR_P010 and RGBA1010102, and conversion from ASTC_4x4 to RGBA_8888 are supported.| 2751 2752**Return value** 2753 2754| Type | Description | 2755| -------------- | ------------------------------- | 2756| Promise\<void> | Promise that returns no value.| 2757 2758**Error codes** 2759 2760For details about the error codes, see [Image Error Codes](errorcode-image.md). 2761 2762| ID| Error Message| 2763| ------- | --------------------------------------------| 2764| 62980111 | The image source data is incomplete. | 2765| 62980115 | Invalid input parameter. | 2766| 62980178 | Failed to create the pixelmap. | 2767| 62980274 | The conversion failed | 2768| 62980276 | The type to be converted is an unsupported target pixel format| 2769 2770**Example** 2771 2772```ts 2773import { BusinessError } from '@kit.BasicServicesKit'; 2774 2775if (pixelMap != undefined) { 2776 // Set the target pixel format to NV12. 2777 let targetPixelFormat = image.PixelMapFormat.NV12; 2778 pixelMap.convertPixelFormat(targetPixelFormat).then(() => { 2779 // The pixelMap is converted to the NV12 format. 2780 console.info('PixelMapFormat convert Succeeded'); 2781 }).catch((error: BusinessError) => { 2782 // The pixelMap fails to be converted to the NV12 format. 2783 console.error(`PixelMapFormat convert Failed. code is ${error.code}, message is ${error.message}`); 2784 }) 2785} 2786``` 2787 2788## setMemoryNameSync<sup>13+</sup> 2789 2790setMemoryNameSync(name: string): void 2791 2792Sets a memory name for this PixelMap. 2793 2794**System capability**: SystemCapability.Multimedia.Image.Core 2795 2796**Parameters** 2797 2798| Name | Type | Mandatory| Description | 2799| ------------- | -------------------------------- | ---- | ---------------- | 2800| name | string | Yes | Memory name, which can be set only for a PixelMap with the DMA or ASHMEM memory format. The length ranges from 1 to 31 bits.| 2801 2802**Error codes** 2803 2804For details about the error codes, see [Image Error Codes](errorcode-image.md). 2805 2806| ID| Error Message| 2807| ------- | --------------------------------------------| 2808| 401 | Parameter error. Possible causes: 1.The length of the input parameter is too long. 2.Parameter verification failed. | 2809| 501 | Resource unavailable. | 2810| 62980286 | Memory format not supported. | 2811 2812**Example** 2813 2814```ts 2815import { BusinessError } from '@ohos.base'; 2816import {image} from '@kit.ImageKit'; 2817 2818function SetMemoryNameSync(pixelMap:image.PixelMap) { 2819 if (pixelMap != undefined) { 2820 try { 2821 pixelMap.setMemoryNameSync("PixelMapName Test"); 2822 } catch(e) { 2823 let error = e as BusinessError; 2824 console.error(`setMemoryNameSync error. code is ${error.code}, message is ${error.message}`); 2825 } 2826 } 2827} 2828``` 2829