1# @ohos.effectKit (Image Effects) 2 3The **EffectKit** module provides basic image processing capabilities, including brightness adjustment, blurring, grayscale adjustment, and color picker. 4 5This module provides the following classes: 6 7- [Filter](#filter): a class that adds a specified effect to the image source. 8- [Color](#color): a class used to store the color picked. 9- [ColorPicker](#colorpicker): a smart color picker. 10 11> **NOTE** 12> 13> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version. 14 15## Modules to Import 16 17```ts 18import { effectKit } from "@kit.ArkGraphics2D"; 19``` 20 21## effectKit.createEffect 22createEffect(source: image.PixelMap): Filter 23 24Creates a **Filter** instance based on a pixel map. 25 26**Widget capability**: This API can be used in ArkTS widgets since API version 12. 27 28**Atomic service API**: This API can be used in atomic services since API version 12. 29 30**System capability**: SystemCapability.Multimedia.Image.Core 31 32**Parameters** 33 34| Name | Type | Mandatory | Description | 35| ------- | ----------------- | ---- | -------- | 36| source | [image.PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7) | Yes | **PixelMap** instance created by the image module. An instance can be obtained by decoding an image or directly created. For details, see [Image Overview](../../media/image/image-overview.md). | 37 38**Return value** 39 40| Type | Description | 41| -------------------------------- | -------------- | 42| [Filter](#filter) | Head node of the filter linked list without any effect. If the operation fails, **null** is returned. | 43 44**Example** 45 46```ts 47import { image } from "@kit.ImageKit"; 48import { effectKit } from "@kit.ArkGraphics2D"; 49 50const color = new ArrayBuffer(96); 51let opts : image.InitializationOptions = { 52 editable: true, 53 pixelFormat: 3, 54 size: { 55 height: 4, 56 width: 6 57 } 58} 59image.createPixelMap(color, opts).then((pixelMap) => { 60 let headFilter = effectKit.createEffect(pixelMap); 61}) 62``` 63 64## effectKit.createColorPicker 65 66createColorPicker(source: image.PixelMap): Promise\<ColorPicker> 67 68Creates a **ColorPicker** instance based on a pixel map. This API uses a promise to return the result. 69 70**Widget capability**: This API can be used in ArkTS widgets since API version 12. 71 72**Atomic service API**: This API can be used in atomic services since API version 12. 73 74**System capability**: SystemCapability.Multimedia.Image.Core 75 76**Parameters** 77 78| Name | Type | Mandatory | Description | 79| -------- | ----------- | ---- | -------------------------- | 80| source | [image.PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7) | Yes | **PixelMap** instance created by the image module. An instance can be obtained by decoding an image or directly created. For details, see [Image Overview](../../media/image/image-overview.md). | 81 82**Return value** 83 84| Type | Description | 85| ---------------------- | -------------- | 86| Promise\<[ColorPicker](#colorpicker)> | Promise used to return the **ColorPicker** instance created. | 87 88**Error codes** 89 90For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 91 92| ID | Error Message | 93| -------- | ------------------------------ | 94| 401 | Input parameter error. | 95 96**Example** 97 98```ts 99import { image } from "@kit.ImageKit"; 100import { effectKit } from "@kit.ArkGraphics2D"; 101import { BusinessError } from "@kit.BasicServicesKit"; 102 103const color = new ArrayBuffer(96); 104let opts : image.InitializationOptions = { 105 editable: true, 106 pixelFormat: 3, 107 size: { 108 height: 4, 109 width: 6 110 } 111} 112 113image.createPixelMap(color, opts).then((pixelMap) => { 114 effectKit.createColorPicker(pixelMap).then(colorPicker => { 115 console.info("color picker=" + colorPicker); 116 }).catch( (reason : BusinessError) => { 117 console.error("error=" + reason.message); 118 }) 119}) 120``` 121 122## effectKit.createColorPicker<sup>10+</sup> 123 124createColorPicker(source: image.PixelMap, region: Array\<number>): Promise\<ColorPicker> 125 126Creates a **ColorPicker** instance for the selected region based on a pixel map. This API uses a promise to return the result. 127 128**Widget capability**: This API can be used in ArkTS widgets since API version 12. 129 130**Atomic service API**: This API can be used in atomic services since API version 12. 131 132**System capability**: SystemCapability.Multimedia.Image.Core 133 134**Parameters** 135 136| Name | Type | Mandatory | Description | 137| -------- | ----------- | ---- | -------------------------- | 138| source | [image.PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7) | Yes | **PixelMap** instance created by the image module. An instance can be obtained by decoding an image or directly created. For details, see [Image Overview](../../media/image/image-overview.md). | 139| region | Array\<number> | Yes | Region of the image from which the color is picked.<br>The array consists of four elements, representing the left, top, right, and bottom positions of the image, respectively. The value of each element must be in the range [0, 1]. The leftmost and topmost positions of the image correspond to 0, and the rightmost and bottom positions correspond to 1. In the array, the third element must be greater than the first element, and the fourth element must be greater than the second element.| 140 141**Return value** 142 143| Type | Description | 144| ---------------------- | -------------- | 145| Promise\<[ColorPicker](#colorpicker)> | Promise used to return the **ColorPicker** instance created. | 146 147**Error codes** 148 149For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 150 151| ID | Error Message | 152| -------- | ------------------------------ | 153| 401 | Input parameter error. | 154 155**Example** 156 157```ts 158import { image } from "@kit.ImageKit"; 159import { effectKit } from "@kit.ArkGraphics2D"; 160import { BusinessError } from "@kit.BasicServicesKit"; 161 162const color = new ArrayBuffer(96); 163let opts : image.InitializationOptions = { 164 editable: true, 165 pixelFormat: 3, 166 size: { 167 height: 4, 168 width: 6 169 } 170} 171 172image.createPixelMap(color, opts).then((pixelMap) => { 173 effectKit.createColorPicker(pixelMap, [0, 0, 1, 1]).then(colorPicker => { 174 console.info("color picker=" + colorPicker); 175 }).catch( (reason : BusinessError) => { 176 console.error("error=" + reason.message); 177 }) 178}) 179``` 180 181## effectKit.createColorPicker 182 183createColorPicker(source: image.PixelMap, callback: AsyncCallback\<ColorPicker>): void 184 185Creates a **ColorPicker** instance based on a pixel map. This API uses an asynchronous callback to return the result. 186 187**Widget capability**: This API can be used in ArkTS widgets since API version 12. 188 189**Atomic service API**: This API can be used in atomic services since API version 12. 190 191**System capability**: SystemCapability.Multimedia.Image.Core 192 193**Parameters** 194 195| Name | Type | Mandatory | Description | 196| -------- | ------------------ | ---- | -------------------------- | 197| source | [image.PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7) | Yes |**PixelMap** instance created by the image module. An instance can be obtained by decoding an image or directly created. For details, see [Image Overview](../../media/image/image-overview.md). | 198| callback | AsyncCallback\<[ColorPicker](#colorpicker)> | Yes | Callback used to return the **ColorPicker** instance created. | 199 200**Error codes** 201 202For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 203 204| ID | Error Message | 205| -------- | ------------------------------ | 206| 401 | Input parameter error. | 207 208**Example** 209 210```ts 211import { image } from "@kit.ImageKit"; 212import { effectKit } from "@kit.ArkGraphics2D"; 213 214const color = new ArrayBuffer(96); 215let opts : image.InitializationOptions = { 216 editable: true, 217 pixelFormat: 3, 218 size: { 219 height: 4, 220 width: 6 221 } 222} 223image.createPixelMap(color, opts).then((pixelMap) => { 224 effectKit.createColorPicker(pixelMap, (error, colorPicker) => { 225 if (error) { 226 console.error('Failed to create color picker.'); 227 } else { 228 console.info('Succeeded in creating color picker.'); 229 } 230 }) 231}) 232``` 233 234## effectKit.createColorPicker<sup>10+</sup> 235 236createColorPicker(source: image.PixelMap, region:Array\<number>, callback: AsyncCallback\<ColorPicker>): void 237 238Creates a **ColorPicker** instance for the selected region based on a pixel map. This API uses an asynchronous callback to return the result. 239 240**Widget capability**: This API can be used in ArkTS widgets since API version 12. 241 242**Atomic service API**: This API can be used in atomic services since API version 12. 243 244**System capability**: SystemCapability.Multimedia.Image.Core 245 246**Parameters** 247 248| Name | Type | Mandatory | Description | 249| -------- | ------------------ | ---- | -------------------------- | 250| source | [image.PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7) | Yes |**PixelMap** instance created by the image module. An instance can be obtained by decoding an image or directly created. For details, see [Image Overview](../../media/image/image-overview.md). | 251| region | Array\<number> | Yes | Region of the image from which the color is picked.<br>The array consists of four elements, representing the left, top, right, and bottom positions of the image, respectively. The value of each element must be in the range [0, 1]. The leftmost and topmost positions of the image correspond to 0, and the rightmost and bottom positions correspond to 1. In the array, the third element must be greater than the first element, and the fourth element must be greater than the second element.| 252| callback | AsyncCallback\<[ColorPicker](#colorpicker)> | Yes | Callback used to return the **ColorPicker** instance created. | 253 254**Error codes** 255 256For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 257 258| ID | Error Message | 259| -------- | ------------------------------ | 260| 401 | Input parameter error. | 261 262**Example** 263 264```ts 265import { image } from "@kit.ImageKit"; 266import { effectKit } from "@kit.ArkGraphics2D"; 267 268const color = new ArrayBuffer(96); 269let opts : image.InitializationOptions = { 270 editable: true, 271 pixelFormat: 3, 272 size: { 273 height: 4, 274 width: 6 275 } 276} 277image.createPixelMap(color, opts).then((pixelMap) => { 278 effectKit.createColorPicker(pixelMap, [0, 0, 1, 1], (error, colorPicker) => { 279 if (error) { 280 console.error('Failed to create color picker.'); 281 } else { 282 console.info('Succeeded in creating color picker.'); 283 } 284 }) 285}) 286``` 287 288## Color 289 290A class that stores the color picked. 291 292**Widget capability**: This API can be used in ArkTS widgets since API version 12. 293 294**Atomic service API**: This API can be used in atomic services since API version 12. 295 296**System capability**: SystemCapability.Multimedia.Image.Core 297 298| Name | Type | Readable | Writable | Description | 299| ------ | ----- | ---- | ---- | ---------------- | 300| red | number | Yes | No | Value of the red component. The value range is [0x0, 0xFF]. | 301| green | number | Yes | No | Value of the green component. The value range is [0x0, 0xFF]. | 302| blue | number | Yes | No | Value of the blue component. The value range is [0x0, 0xFF]. | 303| alpha | number | Yes | No | Value of the alpha component. The value range is [0x0, 0xFF]. | 304 305## ColorPicker 306 307A class used to obtain the color from an image. Before calling any method of **ColorPicker**, use [createColorPicker](#effectkitcreatecolorpicker) to create a **ColorPicker** instance. 308 309### getMainColor 310 311getMainColor(): Promise\<Color> 312 313Obtains the main color from the image and writes the result to a [Color](#color) instance. This API uses a promise to return the result. 314 315**Widget capability**: This API can be used in ArkTS widgets since API version 12. 316 317**Atomic service API**: This API can be used in atomic services since API version 12. 318 319**System capability**: SystemCapability.Multimedia.Image.Core 320 321**Return value** 322 323| Type | Description | 324| :------------- | :---------------------------------------------- | 325| Promise\<[Color](#color)> | Promise used to return the color value of the main color. If the operation fails, an error message is returned. | 326 327**Example** 328 329```ts 330import { image } from "@kit.ImageKit"; 331import { effectKit } from "@kit.ArkGraphics2D"; 332 333const color = new ArrayBuffer(96); 334let opts: image.InitializationOptions = { 335 editable: true, 336 pixelFormat: 3, 337 size: { 338 height: 4, 339 width: 6 340 } 341} 342image.createPixelMap(color, opts).then((pixelMap) => { 343 effectKit.createColorPicker(pixelMap, (error, colorPicker) => { 344 if (error) { 345 console.error('Failed to create color picker.'); 346 } else { 347 console.info('Succeeded in creating color picker.'); 348 colorPicker.getMainColor().then(color => { 349 console.info('Succeeded in getting main color.'); 350 console.info(`color[ARGB]=${color.alpha},${color.red},${color.green},${color.blue}`); 351 }) 352 } 353 }) 354}) 355``` 356 357### getMainColorSync 358 359getMainColorSync(): Color 360 361Obtains the main color from the image and writes the result to a [Color](#color) instance. This API returns the result synchronously. 362 363**Widget capability**: This API can be used in ArkTS widgets since API version 12. 364 365**Atomic service API**: This API can be used in atomic services since API version 12. 366 367**System capability**: SystemCapability.Multimedia.Image.Core 368 369**Return value** 370 371| Type | Description | 372| :------- | :----------------------------------- | 373| [Color](#color) | Color value of the main color. If the operation fails, **null** is returned. | 374 375**Example** 376 377```ts 378import { image } from "@kit.ImageKit"; 379import { effectKit } from "@kit.ArkGraphics2D"; 380 381const color = new ArrayBuffer(96); 382let opts : image.InitializationOptions = { 383 editable: true, 384 pixelFormat: 3, 385 size: { 386 height: 4, 387 width: 6 388 } 389} 390image.createPixelMap(color, opts).then((pixelMap) => { 391 effectKit.createColorPicker(pixelMap, (error, colorPicker) => { 392 if (error) { 393 console.error('Failed to create color picker.'); 394 } else { 395 console.info('Succeeded in creating color picker.'); 396 let color = colorPicker.getMainColorSync(); 397 console.info('get main color =' + color); 398 } 399 }) 400}) 401``` 402 403 404### getLargestProportionColor<sup>10+</sup> 405 406getLargestProportionColor(): Color 407 408Obtains the color with the largest proportion from the image and writes the result to a [Color](#color) instance. This API returns the result synchronously. 409 410**Widget capability**: This API can be used in ArkTS widgets since API version 12. 411 412**Atomic service API**: This API can be used in atomic services since API version 12. 413 414**System capability**: SystemCapability.Multimedia.Image.Core 415 416**Return value** 417 418| Type | Description | 419| :------------- | :---------------------------------------------- | 420| [Color](#color) | Color value of the color with the largest proportion. If the operation fails, **null** is returned. | 421 422**Example** 423 424```ts 425import { image } from "@kit.ImageKit"; 426import { effectKit } from "@kit.ArkGraphics2D"; 427 428const color = new ArrayBuffer(96); 429let opts : image.InitializationOptions = { 430 editable: true, 431 pixelFormat: 3, 432 size: { 433 height: 4, 434 width: 6 435 } 436} 437image.createPixelMap(color, opts).then((pixelMap) => { 438 effectKit.createColorPicker(pixelMap, (error, colorPicker) => { 439 if (error) { 440 console.error('Failed to create color picker.'); 441 } else { 442 console.info('Succeeded in creating color picker.'); 443 let color = colorPicker.getLargestProportionColor(); 444 console.info('get largest proportion color =' + color); 445 } 446 }) 447}) 448``` 449 450 451### getTopProportionColors<sup>12+</sup> 452 453getTopProportionColors(colorCount: number): Array<Color | null> 454 455Obtains a given number of colors with the top proportions in the image. This API returns the result synchronously. 456 457**Widget capability**: This API can be used in ArkTS widgets since API version 12. 458 459**Atomic service API**: This API can be used in atomic services since API version 12. 460 461**System capability**: SystemCapability.Multimedia.Image.Core 462 463**Parameters** 464| Name | Type | Mandatory | Description | 465| ---------- | ------ | ---- | ------------------------------------------- | 466| colorCount | number | Yes | Number of colors to obtain. The value range is [1, 10]. If a non-integer is passed in, the value will be rounded down. | 467 468**Return value** 469 470| Type | Description | 471| :--------------------------------------- | :---------------------------------------------- | 472| Array<[Color](#color) \| null> | Array of colors, sorted by proportion.<br>- If the number of colors obtained is less than the value of **colorCount**, the array size is the actual number obtained.<br>- If the color fails to be obtained, an empty array is returned.<br>- If the value of **colorCount** is less than 1, **[null]** is returned.<br>- If the value of **colorCount** is greater than 10, an array holding the first 10 colors with the top proportions is returned. | 473 474**Example** 475 476```js 477import { image } from "@kit.ImageKit"; 478import { effectKit } from "@kit.ArkGraphics2D"; 479 480const color = new ArrayBuffer(96); 481let opts : image.InitializationOptions = { 482 editable: true, 483 pixelFormat: 3, 484 size: { 485 height: 4, 486 width: 6 487 } 488} 489image.createPixelMap(color, opts).then((pixelMap) => { 490 effectKit.createColorPicker(pixelMap, (error, colorPicker) => { 491 if (error) { 492 console.error('Failed to create color picker.'); 493 } else { 494 console.info('Succeeded in creating color picker.'); 495 let colors = colorPicker.getTopProportionColors(2); 496 for(let index = 0; index < colors.length; index++) { 497 if (colors[index]) { 498 console.info('get top proportion colors: index ' + index + ', color ' + colors[index]); 499 } 500 } 501 } 502 }) 503}) 504``` 505 506 507### getHighestSaturationColor<sup>10+</sup> 508 509getHighestSaturationColor(): Color 510 511Obtains the color with the highest saturation from the image and writes the result to a [Color](#color) instance. This API returns the result synchronously. 512 513**Widget capability**: This API can be used in ArkTS widgets since API version 12. 514 515**Atomic service API**: This API can be used in atomic services since API version 12. 516 517**System capability**: SystemCapability.Multimedia.Image.Core 518 519**Return value** 520 521| Type | Description | 522| :------------- | :---------------------------------------------- | 523| [Color](#color) | Color value of the color with the highest saturation. If the operation fails, **null** is returned. | 524 525**Example** 526 527```ts 528import { image } from "@kit.ImageKit"; 529import { effectKit } from "@kit.ArkGraphics2D"; 530 531const color = new ArrayBuffer(96); 532let opts: image.InitializationOptions = { 533 editable: true, 534 pixelFormat: 3, 535 size: { 536 height: 4, 537 width: 6 538 } 539} 540image.createPixelMap(color, opts).then((pixelMap) => { 541 effectKit.createColorPicker(pixelMap, (error, colorPicker) => { 542 if (error) { 543 console.error('Failed to create color picker.'); 544 } else { 545 console.info('Succeeded in creating color picker.'); 546 let color = colorPicker.getHighestSaturationColor(); 547 console.info('get highest saturation color =' + color); 548 } 549 }) 550}) 551``` 552 553 554### getAverageColor<sup>10+</sup> 555 556getAverageColor(): Color 557 558Obtains the average color from the image and writes the result to a [Color](#color) instance. This API returns the result synchronously. 559 560**Widget capability**: This API can be used in ArkTS widgets since API version 12. 561 562**Atomic service API**: This API can be used in atomic services since API version 12. 563 564**System capability**: SystemCapability.Multimedia.Image.Core 565 566**Return value** 567 568| Type | Description | 569| :------------- | :---------------------------------------------- | 570| [Color](#color) | Average color value. If the operation fails, **null** is returned. | 571 572**Example** 573 574```ts 575import { image } from "@kit.ImageKit"; 576import { effectKit } from "@kit.ArkGraphics2D"; 577 578const color = new ArrayBuffer(96); 579let opts: image.InitializationOptions = { 580 editable: true, 581 pixelFormat: 3, 582 size: { 583 height: 4, 584 width: 6 585 } 586} 587image.createPixelMap(color, opts).then((pixelMap) => { 588 effectKit.createColorPicker(pixelMap, (error, colorPicker) => { 589 if (error) { 590 console.error('Failed to create color picker.'); 591 } else { 592 console.info('Succeeded in creating color picker.'); 593 let color = colorPicker.getAverageColor(); 594 console.info('get average color =' + color); 595 } 596 }) 597}) 598``` 599 600 601### isBlackOrWhiteOrGrayColor<sup>10+</sup> 602 603isBlackOrWhiteOrGrayColor(color: number): boolean 604 605Checks whether a color is black, white, and gray. 606 607**Widget capability**: This API can be used in ArkTS widgets since API version 12. 608 609**Atomic service API**: This API can be used in atomic services since API version 12. 610 611**System capability**: SystemCapability.Multimedia.Image.Core 612 613**Parameters** 614 615| Name | Type | Mandatory | Description | 616| -------- | ----------- | ---- | -------------------------- | 617| color| number | Yes | Color to check. The value range is [0x0, 0xFFFFFFFF]. | 618 619**Return value** 620 621| Type | Description | 622| :------------- | :---------------------------------------------- | 623| boolean | Returns **true** if the image is black, white, and gray; returns **false** otherwise. | 624 625**Example** 626 627```ts 628import { image } from "@kit.ImageKit"; 629import { effectKit } from "@kit.ArkGraphics2D"; 630 631const color = new ArrayBuffer(96); 632let opts: image.InitializationOptions = { 633 editable: true, 634 pixelFormat: 3, 635 size: { 636 height: 4, 637 width: 6 638 } 639} 640image.createPixelMap(color, opts).then((pixelMap) => { 641 effectKit.createColorPicker(pixelMap, (error, colorPicker) => { 642 if (error) { 643 console.error('Failed to create color picker.'); 644 } else { 645 console.info('Succeeded in creating color picker.'); 646 let bJudge = colorPicker.isBlackOrWhiteOrGrayColor(0xFFFFFFFF); 647 console.info('is black or white or gray color[bool](white) =' + bJudge); 648 } 649 }) 650}) 651``` 652 653## Filter 654 655A class used to add a specified effect to an image. Before calling any method of **Filter**, use [createEffect](#effectkitcreateeffect) to create a **Filter** instance. 656 657### blur 658 659blur(radius: number): Filter 660 661Adds the blur effect to the filter linked list, and returns the head node of the linked list. 662 663> **NOTE** 664> 665> This API provides the blur effect for static images. To provide the real-time blur effect for components, use [dynamic blur](../../ui/arkts-blur-effect.md). 666 667**Widget capability**: This API can be used in ArkTS widgets since API version 12. 668 669**Atomic service API**: This API can be used in atomic services since API version 12. 670 671**System capability**: SystemCapability.Multimedia.Image.Core 672 673**Parameters** 674 675| Name | Type | Mandatory | Description | 676| ------ | ----------- | ---- | ------------------------------------------------------------ | 677| radius | number | Yes | Blur radius, in pixels. The blur effect is proportional to the configured value. A larger value indicates a more obvious effect. | 678 679**Return value** 680 681| Type | Description | 682| :------------- | :---------------------------------------------- | 683| [Filter](#filter) | Final image effect. | 684 685**Example** 686 687```ts 688import { image } from "@kit.ImageKit"; 689import { effectKit } from "@kit.ArkGraphics2D"; 690 691const color = new ArrayBuffer(96); 692let opts : image.InitializationOptions = { 693 editable: true, 694 pixelFormat: 3, 695 size: { 696 height: 4, 697 width: 6 698 } 699}; 700image.createPixelMap(color, opts).then((pixelMap) => { 701 let radius = 5; 702 let headFilter = effectKit.createEffect(pixelMap); 703 if (headFilter != null) { 704 headFilter.blur(radius); 705 } 706}) 707``` 708 709 710### invert<sup>12+</sup> 711 712invert(): Filter 713 714Adds the inversion effect to the filter linked list, and returns the head node of the linked list. 715 716**System capability**: SystemCapability.Multimedia.Image.Core 717 718**Return value** 719 720| Type | Description | 721| :------------- | :---------------------------------------------- | 722| [Filter](#filter) | Final image effect. | 723 724**Example** 725 726```ts 727import { image } from "@kit.ImageKit"; 728import { effectKit } from "@kit.ArkGraphics2D"; 729 730const color = new ArrayBuffer(96); 731let opts : image.InitializationOptions = { 732 editable: true, 733 pixelFormat: 3, 734 size: { 735 height: 4, 736 width: 6 737 } 738}; 739image.createPixelMap(color, opts).then((pixelMap) => { 740 let headFilter = effectKit.createEffect(pixelMap); 741 if (headFilter != null) { 742 headFilter.invert(); 743 } 744}) 745``` 746 747 748### setColorMatrix<sup>12+</sup> 749 750setColorMatrix(colorMatrix: Array\<number>): Filter 751 752Adds a custom effect to the filter linked list, and returns the head node of the linked list. 753 754**System capability**: SystemCapability.Multimedia.Image.Core 755 756**Parameters** 757 758| Name | Type | Mandatory | Description | 759| ------ | ----------- | ---- | ------------------------------------------------------------ | 760| colorMatrix | Array\<number> | Yes | Custom color matrix.<br>A 5 x 4 matrix can be created. The value range of the matrix element is [0, 1], where **0** indicates that the color channel is not involved in the calculation, and **1** indicates that the color channel is involved in the calculation and retains the original weight. | 761 762**Return value** 763 764| Type | Description | 765| :------------- | :---------------------------------------------- | 766| [Filter](#filter) | Final image effect. | 767 768**Error codes** 769 770For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 771 772| ID | Error Message | 773| -------- | ------------------------------ | 774| 401 | Input parameter error. | 775 776**Example** 777 778```ts 779import { image } from "@kit.ImageKit"; 780import { effectKit } from "@kit.ArkGraphics2D"; 781 782const color = new ArrayBuffer(96); 783let opts : image.InitializationOptions = { 784 editable: true, 785 pixelFormat: 3, 786 size: { 787 height: 4, 788 width: 6 789 } 790}; 791image.createPixelMap(color, opts).then((pixelMap) => { 792 let colorMatrix:Array<number> = [ 793 0.2126,0.7152,0.0722,0,0, 794 0.2126,0.7152,0.0722,0,0, 795 0.2126,0.7152,0.0722,0,0, 796 0,0,0,1,0 797 ]; 798 let headFilter = effectKit.createEffect(pixelMap); 799 if (headFilter != null) { 800 headFilter.setColorMatrix(colorMatrix); 801 } 802}) 803``` 804 805### brightness 806 807brightness(bright: number): Filter 808 809Adds the brightness effect to the filter linked list, and returns the head node of the linked list. 810 811**Widget capability**: This API can be used in ArkTS widgets since API version 12. 812 813**Atomic service API**: This API can be used in atomic services since API version 12. 814 815**System capability**: SystemCapability.Multimedia.Image.Core 816 817**Parameters** 818 819| Name | Type | Mandatory | Description | 820| ------ | ----------- | ---- | ------------------------------------------------------------ | 821| bright | number | Yes | Brightness value, ranging from 0 to 1. When the value is **0**, the image brightness remains unchanged. | 822 823**Return value** 824 825| Type | Description | 826| :------------- | :---------------------------------------------- | 827| [Filter](#filter) | Final image effect. | 828 829**Example** 830 831```ts 832import { image } from "@kit.ImageKit"; 833import { effectKit } from "@kit.ArkGraphics2D"; 834 835const color = new ArrayBuffer(96); 836let opts : image.InitializationOptions = { 837 editable: true, 838 pixelFormat: 3, 839 size: { 840 height: 4, 841 width: 6 842 } 843}; 844image.createPixelMap(color, opts).then((pixelMap) => { 845 let bright = 0.5; 846 let headFilter = effectKit.createEffect(pixelMap); 847 if (headFilter != null) { 848 headFilter.brightness(bright); 849 } 850}) 851``` 852 853 854### grayscale 855 856grayscale(): Filter 857 858Adds the grayscale effect to the filter linked list, and returns the head node of the linked list. 859 860**Widget capability**: This API can be used in ArkTS widgets since API version 12. 861 862**Atomic service API**: This API can be used in atomic services since API version 12. 863 864**System capability**: SystemCapability.Multimedia.Image.Core 865 866**Return value** 867 868| Type | Description | 869| :------------- | :---------------------------------------------- | 870| [Filter](#filter) | Final image effect. | 871 872**Example** 873 874```ts 875import { image } from "@kit.ImageKit"; 876import { effectKit } from "@kit.ArkGraphics2D"; 877 878const color = new ArrayBuffer(96); 879let opts : image.InitializationOptions = { 880 editable: true, 881 pixelFormat: 3, 882 size: { 883 height: 4, 884 width: 6 885 } 886}; 887image.createPixelMap(color, opts).then((pixelMap) => { 888 let headFilter = effectKit.createEffect(pixelMap); 889 if (headFilter != null) { 890 headFilter.grayscale(); 891 } 892}) 893``` 894 895 896### getEffectPixelMap<sup>11+</sup> 897 898getEffectPixelMap(): Promise<image.PixelMap> 899 900Obtains **image.PixelMap** of the source image to which the filter linked list is added. This API uses a promise to return the result. 901 902**Widget capability**: This API can be used in ArkTS widgets since API version 12. 903 904**Atomic service API**: This API can be used in atomic services since API version 12. 905 906**System capability**: SystemCapability.Multimedia.Image.Core 907 908**Return value** 909 910| Type | Description | 911| ---------------------- | -------------- | 912| Promise\<image.PixelMap> | Promise used to return **image.PixelMap** of the source image. | 913 914 915**Example** 916 917```ts 918import { image } from "@kit.ImageKit"; 919import { effectKit } from "@kit.ArkGraphics2D"; 920 921const color = new ArrayBuffer(96); 922let opts : image.InitializationOptions = { 923 editable: true, 924 pixelFormat: 3, 925 size: { 926 height: 4, 927 width: 6 928 } 929}; 930image.createPixelMap(color, opts).then((pixelMap) => { 931 effectKit.createEffect(pixelMap).grayscale().getEffectPixelMap().then(data => { 932 console.info('getPixelBytesNumber = ', data.getPixelBytesNumber()); 933 }) 934}) 935``` 936 937### getPixelMap<sup>(deprecated)</sup> 938 939getPixelMap(): image.PixelMap 940 941Obtains **image.PixelMap** of the source image to which the filter linked list is added. 942 943> **NOTE** 944> 945> This API is supported since API version 9 and deprecated since API version 11. You are advised to use [getEffectPixelMap](#geteffectpixelmap11) instead. 946 947**System capability**: SystemCapability.Multimedia.Image.Core 948 949**Return value** 950 951| Type | Description | 952| :------------- | :---------------------------------------------- | 953| [image.PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7) | **image.PixelMap** of the source image. | 954 955**Example** 956 957```ts 958import { image } from "@kit.ImageKit"; 959import { effectKit } from "@kit.ArkGraphics2D"; 960 961const color = new ArrayBuffer(96); 962let opts : image.InitializationOptions = { 963 editable: true, 964 pixelFormat: 3, 965 size: { 966 height: 4, 967 width: 6 968 } 969}; 970image.createPixelMap(color, opts).then((pixelMap) => { 971 let pixel = effectKit.createEffect(pixelMap).grayscale().getPixelMap(); 972 console.info('getPixelBytesNumber = ', pixel.getPixelBytesNumber()); 973}) 974``` 975