• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 '@ohos.effectKit';
19```
20
21## effectKit.createEffect
22createEffect(source: image.PixelMap): Filter
23
24Creates a **Filter** instance based on a pixel map.
25
26**System capability**: SystemCapability.Multimedia.Image.Core
27
28**Parameters**
29
30| Name   | Type              | Mandatory| Description    |
31| ------- | ----------------- | ---- | -------- |
32| source  | [image.PixelMap](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-overview.md).  |
33
34**Return value**
35
36| Type                            | Description          |
37| -------------------------------- | -------------- |
38| [Filter](#filter) | Head node of the filter linked list without any effect. If the operation fails, **null** is returned.|
39
40**Example**
41
42```ts
43import image from "@ohos.multimedia.image";
44import effectKit from "@ohos.effectKit";
45
46const color = new ArrayBuffer(96);
47let opts : image.InitializationOptions = {
48  editable: true,
49  pixelFormat: 3,
50  size: {
51    height: 4,
52    width: 6
53  }
54}
55image.createPixelMap(color, opts).then((pixelMap) => {
56  let headFilter = effectKit.createEffect(pixelMap);
57})
58```
59
60## effectKit.createColorPicker
61
62createColorPicker(source: image.PixelMap): Promise\<ColorPicker>
63
64Creates a **ColorPicker** instance based on a pixel map. This API uses a promise to return the result.
65
66**System capability**: SystemCapability.Multimedia.Image.Core
67
68**Parameters**
69
70| Name    | Type        | Mandatory| Description                      |
71| -------- | ----------- | ---- | -------------------------- |
72| source   | [image.PixelMap](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-overview.md).|
73
74**Return value**
75
76| Type                  | Description          |
77| ---------------------- | -------------- |
78| Promise\<[ColorPicker](#colorpicker)>  | Promise used to return the **ColorPicker** instance created.|
79
80**Example**
81
82```ts
83import image from "@ohos.multimedia.image";
84import effectKit from "@ohos.effectKit";
85import { BusinessError } from "@ohos.base";
86
87const color = new ArrayBuffer(96);
88let opts : image.InitializationOptions = {
89  editable: true,
90  pixelFormat: 3,
91  size: {
92    height: 4,
93    width: 6
94  }
95}
96
97image.createPixelMap(color, opts).then((pixelMap) => {
98  effectKit.createColorPicker(pixelMap).then(colorPicker => {
99    console.info("color picker=" + colorPicker);
100  }).catch( (reason : BusinessError) => {
101    console.error("error=" + reason.message);
102  })
103})
104```
105
106## effectKit.createColorPicker<sup>10+</sup>
107
108createColorPicker(source: image.PixelMap, region: Array\<number>): Promise\<ColorPicker>
109
110Creates a **ColorPicker** instance for the selected region based on a pixel map. This API uses a promise to return the result.
111
112**System capability**: SystemCapability.Multimedia.Image.Core
113
114**Parameters**
115
116| Name    | Type        | Mandatory| Description                      |
117| -------- | ----------- | ---- | -------------------------- |
118| source   | [image.PixelMap](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-overview.md).|
119| 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.<br>If no value is passed, the default value [0, 0, 1, 1] is used, indicating that the color region is the entire image.|
120
121**Return value**
122
123| Type                  | Description          |
124| ---------------------- | -------------- |
125| Promise\<[ColorPicker](#colorpicker)>  | Promise used to return the **ColorPicker** instance created.|
126
127**Example**
128
129```ts
130import image from "@ohos.multimedia.image";
131import effectKit from "@ohos.effectKit";
132import { BusinessError } from "@ohos.base";
133
134const color = new ArrayBuffer(96);
135let opts : image.InitializationOptions = {
136  editable: true,
137  pixelFormat: 3,
138  size: {
139    height: 4,
140    width: 6
141  }
142}
143
144image.createPixelMap(color, opts).then((pixelMap) => {
145  effectKit.createColorPicker(pixelMap).then(colorPicker => {
146    console.info("color picker=" + colorPicker);
147  }).catch( (reason : BusinessError) => {
148    console.error("error=" + reason.message);
149  })
150})
151```
152
153## effectKit.createColorPicker
154
155createColorPicker(source: image.PixelMap, callback: AsyncCallback\<ColorPicker>): void
156
157Creates a **ColorPicker** instance based on a pixel map. This API uses an asynchronous callback to return the result.
158
159**System capability**: SystemCapability.Multimedia.Image.Core
160
161**Parameters**
162
163| Name    | Type               | Mandatory| Description                      |
164| -------- | ------------------ | ---- | -------------------------- |
165| source   | [image.PixelMap](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-overview.md). |
166| callback | AsyncCallback\<[ColorPicker](#colorpicker)> | Yes | Callback used to return the **ColorPicker** instance created.|
167
168**Example**
169
170```ts
171import image from "@ohos.multimedia.image";
172import effectKit from "@ohos.effectKit";
173
174const color = new ArrayBuffer(96);
175let opts : image.InitializationOptions = {
176  editable: true,
177  pixelFormat: 3,
178  size: {
179    height: 4,
180    width: 6
181  }
182}
183image.createPixelMap(color, opts).then((pixelMap) => {
184  effectKit.createColorPicker(pixelMap, (error, colorPicker) => {
185    if (error) {
186      console.error('Failed to create color picker.');
187    } else {
188      console.info('Succeeded in creating color picker.');
189    }
190  })
191})
192```
193
194## effectKit.createColorPicker<sup>10+</sup>
195
196createColorPicker(source: image.PixelMap, region:Array\<number>, callback: AsyncCallback\<ColorPicker>): void
197
198Creates a **ColorPicker** instance for the selected region based on a pixel map. This API uses an asynchronous callback to return the result.
199
200**System capability**: SystemCapability.Multimedia.Image.Core
201
202**Parameters**
203
204| Name    | Type               | Mandatory| Description                      |
205| -------- | ------------------ | ---- | -------------------------- |
206| source   | [image.PixelMap](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-overview.md). |
207| 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.<br>If no value is passed, the default value [0, 0, 1, 1] is used, indicating that the color region is the entire image.|
208| callback | AsyncCallback\<[ColorPicker](#colorpicker)> | Yes | Callback used to return the **ColorPicker** instance created.|
209
210**Example**
211
212```ts
213import image from "@ohos.multimedia.image";
214import effectKit from "@ohos.effectKit";
215
216const color = new ArrayBuffer(96);
217let opts : image.InitializationOptions = {
218  editable: true,
219  pixelFormat: 3,
220  size: {
221    height: 4,
222    width: 6
223  }
224}
225image.createPixelMap(color, opts).then((pixelMap) => {
226  effectKit.createColorPicker(pixelMap, (error, colorPicker) => {
227    if (error) {
228      console.error('Failed to create color picker.');
229    } else {
230      console.info('Succeeded in creating color picker.');
231    }
232  })
233})
234```
235
236## Color
237
238A class that stores the color picked.
239
240**System capability**: SystemCapability.Multimedia.Image.Core
241
242| Name  | Type  | Readable| Writable| Description             |
243| ------ | ----- | ---- | ---- | ---------------- |
244| red   | number | Yes  | No  | Value of the red component. The value range is [0x0, 0xFF].          |
245| green | number | Yes  | No  | Value of the green component. The value range is [0x0, 0xFF].          |
246| blue  | number | Yes  | No  | Value of the blue component. The value range is [0x0, 0xFF].          |
247| alpha | number | Yes  | No  | Value of the alpha component. The value range is [0x0, 0xFF].      |
248
249## ColorPicker
250
251A class used to obtain the color from an image. Before calling any method of **ColorPicker**, use [createColorPicker](#effectkitcreatecolorpicker) to create a **ColorPicker** instance.
252
253### getMainColor
254
255getMainColor(): Promise\<Color>
256
257Obtains the main color from the image and writes the result to a [Color](#color) instance. This API uses a promise to return the result.
258
259**System capability**: SystemCapability.Multimedia.Image.Core
260
261**Return value**
262
263| Type          | Description                                           |
264| :------------- | :---------------------------------------------- |
265| Promise\<[Color](#color)> | Promise used to return the color value of the main color. If the operation fails, an error message is returned.|
266
267**Example**
268
269```ts
270import image from "@ohos.multimedia.image";
271import effectKit from "@ohos.effectKit";
272
273export function test06(): void {
274  const color = new ArrayBuffer(96);
275  let opts: image.InitializationOptions = {
276    editable: true,
277    pixelFormat: 3,
278    size: {
279      height: 4,
280      width: 6
281    }
282  }
283  image.createPixelMap(color, opts).then((pixelMap) => {
284    effectKit.createColorPicker(pixelMap, (error, colorPicker) => {
285      if (error) {
286        console.error('Failed to create color picker.');
287      } else {
288        console.info('Succeeded in creating color picker.');
289        colorPicker.getMainColor().then(color => {
290          console.info('Succeeded in getting main color.');
291          console.info(`color[ARGB]=${color.alpha},${color.red},${color.green},${color.blue}`);
292        })
293      }
294    })
295  })
296}
297```
298
299### getMainColorSync
300
301getMainColorSync(): Color
302
303Obtains the main color from the image and writes the result to a [Color](#color) instance. This API returns the result in synchronous mode.
304
305**System capability**: SystemCapability.Multimedia.Image.Core
306
307**Return value**
308
309| Type    | Description                                 |
310| :------- | :----------------------------------- |
311| [Color](#color)    | Color value of the main color. If the operation fails, **null** is returned.|
312
313**Example**
314
315```ts
316import image from "@ohos.multimedia.image";
317import effectKit from "@ohos.effectKit";
318
319const color = new ArrayBuffer(96);
320let opts : image.InitializationOptions = {
321  editable: true,
322  pixelFormat: 3,
323  size: {
324    height: 4,
325    width: 6
326  }
327}
328image.createPixelMap(color, opts).then((pixelMap) => {
329  effectKit.createColorPicker(pixelMap, (error, colorPicker) => {
330    if (error) {
331      console.error('Failed to create color picker.');
332    } else {
333      console.info('Succeeded in creating color picker.');
334      let color = colorPicker.getMainColorSync();
335      console.info('get main color =' + color);
336    }
337  })
338})
339```
340![en-us_image_Main_Color.png](figures/en-us_image_Main_Color.png)
341
342### getLargestProportionColor<sup>10+</sup>
343
344getLargestProportionColor(): Color
345
346Obtains the color with the largest proportion from the image and writes the result to a [Color](#color) instance. This API returns the result in synchronous mode.
347
348**System capability**: SystemCapability.Multimedia.Image.Core
349
350**Return value**
351
352| Type          | Description                                           |
353| :------------- | :---------------------------------------------- |
354| [Color](#color)       | Color value of the color with the largest proportion. If the operation fails, **null** is returned.|
355
356**Example**
357
358```ts
359import image from "@ohos.multimedia.image";
360import effectKit from "@ohos.effectKit";
361
362const color = new ArrayBuffer(96);
363let opts : image.InitializationOptions = {
364  editable: true,
365  pixelFormat: 3,
366  size: {
367    height: 4,
368    width: 6
369  }
370}
371image.createPixelMap(color, opts).then((pixelMap) => {
372  effectKit.createColorPicker(pixelMap, (error, colorPicker) => {
373    if (error) {
374      console.error('Failed to create color picker.');
375    } else {
376      console.info('Succeeded in creating color picker.');
377      let color = colorPicker.getLargestProportionColor();
378      console.info('get largest proportion color =' + color);
379    }
380  })
381})
382```
383![en-us_image_Largest_Proportion_Color.png](figures/en-us_image_Largest_Proportion_Color.png)
384
385### getHighestSaturationColor<sup>10+</sup>
386
387getHighestSaturationColor(): Color
388
389Obtains the color with the highest saturation from the image and writes the result to a [Color](#color) instance. This API returns the result in synchronous mode.
390
391**System capability**: SystemCapability.Multimedia.Image.Core
392
393**Return value**
394
395| Type          | Description                                           |
396| :------------- | :---------------------------------------------- |
397| [Color](#color)       | Color value of the color with the highest saturation. If the operation fails, **null** is returned.|
398
399**Example**
400
401```ts
402import image from "@ohos.multimedia.image";
403import effectKit from "@ohos.effectKit";
404
405const color = new ArrayBuffer(96);
406let opts: image.InitializationOptions = {
407  editable: true,
408  pixelFormat: 3,
409  size: {
410    height: 4,
411    width: 6
412  }
413}
414image.createPixelMap(color, opts).then((pixelMap) => {
415  effectKit.createColorPicker(pixelMap, (error, colorPicker) => {
416    if (error) {
417      console.error('Failed to create color picker.');
418    } else {
419      console.info('Succeeded in creating color picker.');
420      let color = colorPicker.getHighestSaturationColor();
421      console.info('get highest saturation color =' + color);
422    }
423  })
424})
425```
426![en-us_image_Highest_Saturation_Color.png](figures/en-us_image_Highest_Saturation_Color.png)
427
428### getAverageColor<sup>10+</sup>
429
430getAverageColor(): Color
431
432Obtains the average color from the image and writes the result to a [Color](#color) instance. This API returns the result in synchronous mode.
433
434**System capability**: SystemCapability.Multimedia.Image.Core
435
436**Return value**
437
438| Type          | Description                                           |
439| :------------- | :---------------------------------------------- |
440| [Color](#color)       | Average color value. If the operation fails, **null** is returned.|
441
442**Example**
443
444```ts
445import image from "@ohos.multimedia.image";
446import effectKit from "@ohos.effectKit";
447
448const color = new ArrayBuffer(96);
449let opts: image.InitializationOptions = {
450  editable: true,
451  pixelFormat: 3,
452  size: {
453    height: 4,
454    width: 6
455  }
456}
457image.createPixelMap(color, opts).then((pixelMap) => {
458  effectKit.createColorPicker(pixelMap, (error, colorPicker) => {
459    if (error) {
460      console.error('Failed to create color picker.');
461    } else {
462      console.info('Succeeded in creating color picker.');
463      let color = colorPicker.getAverageColor();
464      console.info('get average color =' + color);
465    }
466  })
467})
468```
469![en-us_image_Average_Color.png](figures/en-us_image_Average_Color.png)
470
471### isBlackOrWhiteOrGrayColor<sup>10+</sup>
472
473isBlackOrWhiteOrGrayColor(color: number): boolean
474
475Checks whether a color is black, white, and gray.
476
477**System capability**: SystemCapability.Multimedia.Image.Core
478
479**Parameters**
480
481| Name    | Type        | Mandatory| Description                      |
482| -------- | ----------- | ---- | -------------------------- |
483| color| number | Yes  |  Color to check. The value range is [0x0, 0xFFFFFFFF].|
484
485**Return value**
486
487| Type          | Description                                           |
488| :------------- | :---------------------------------------------- |
489| boolean              | Returns **true** if the image is black, white, and gray; returns **false** otherwise.|
490
491**Example**
492
493```ts
494import image from "@ohos.multimedia.image";
495import effectKit from "@ohos.effectKit";
496
497const color = new ArrayBuffer(96);
498let opts: image.InitializationOptions = {
499  editable: true,
500  pixelFormat: 3,
501  size: {
502    height: 4,
503    width: 6
504  }
505}
506image.createPixelMap(color, opts).then((pixelMap) => {
507  effectKit.createColorPicker(pixelMap, (error, colorPicker) => {
508    if (error) {
509      console.error('Failed to create color picker.');
510    } else {
511      console.info('Succeeded in creating color picker.');
512      let bJudge = colorPicker.isBlackOrWhiteOrGrayColor(0xFFFFFFFF);
513      console.info('is black or white or gray color[bool](white) =' + bJudge);
514    }
515  })
516})
517```
518
519## Filter
520
521A class used to add a specified effect to an image. Before calling any method of **Filter**, use [createEffect](#effectkitcreateeffect) to create a **Filter** instance.
522
523### blur
524
525blur(radius: number): Filter
526
527Adds the blur effect to the filter linked list, and returns the head node of the linked list.
528
529**System capability**: SystemCapability.Multimedia.Image.Core
530
531**Parameters**
532
533| Name| Type       | Mandatory| Description                                                        |
534| ------ | ----------- | ---- | ------------------------------------------------------------ |
535|  radius   | number | Yes  | Blur radius, in pixels. The blur effect is proportional to the configured value. A larger value indicates a more obvious effect.|
536
537**Return value**
538
539| Type          | Description                                           |
540| :------------- | :---------------------------------------------- |
541| [Filter](#filter) | Final image effect.|
542
543**Example**
544
545```ts
546import image from "@ohos.multimedia.image";
547import effectKit from "@ohos.effectKit";
548
549const color = new ArrayBuffer(96);
550let opts : image.InitializationOptions = {
551  editable: true,
552  pixelFormat: 3,
553  size: {
554    height: 4,
555    width: 6
556  }
557};
558image.createPixelMap(color, opts).then((pixelMap) => {
559  let radius = 5;
560  let headFilter = effectKit.createEffect(pixelMap);
561  if (headFilter != null) {
562    headFilter.blur(radius);
563  }
564})
565```
566![en-us_image_Add_Blur.png](figures/en-us_image_Add_Blur.png)
567
568### brightness
569
570brightness(bright: number): Filter
571
572Adds the brightness effect to the filter linked list, and returns the head node of the linked list.
573
574**System capability**: SystemCapability.Multimedia.Image.Core
575
576**Parameters**
577
578| Name| Type       | Mandatory| Description                                                        |
579| ------ | ----------- | ---- | ------------------------------------------------------------ |
580|  bright   | number | Yes  | Brightness value, ranging from 0 to 1. When the value is **0**, the image brightness remains unchanged.|
581
582**Return value**
583
584| Type          | Description                                           |
585| :------------- | :---------------------------------------------- |
586| [Filter](#filter) | Final image effect.|
587
588**Example**
589
590```ts
591import image from "@ohos.multimedia.image";
592import effectKit from "@ohos.effectKit";
593
594const color = new ArrayBuffer(96);
595let opts : image.InitializationOptions = {
596  editable: true,
597  pixelFormat: 3,
598  size: {
599    height: 4,
600    width: 6
601  }
602};
603image.createPixelMap(color, opts).then((pixelMap) => {
604  let bright = 0.5;
605  let headFilter = effectKit.createEffect(pixelMap);
606  if (headFilter != null) {
607    headFilter.brightness(bright);
608  }
609})
610```
611![en-us_image_Add_Brightness.png](figures/en-us_image_Add_Brightness.png)
612
613### grayscale
614
615grayscale(): Filter
616
617Adds the grayscale effect to the filter linked list, and returns the head node of the linked list.
618
619**System capability**: SystemCapability.Multimedia.Image.Core
620
621**Return value**
622
623| Type          | Description                                           |
624| :------------- | :---------------------------------------------- |
625| [Filter](#filter) | Final image effect.|
626
627**Example**
628
629```ts
630import image from "@ohos.multimedia.image";
631import effectKit from "@ohos.effectKit";
632
633const color = new ArrayBuffer(96);
634let opts : image.InitializationOptions = {
635  editable: true,
636  pixelFormat: 3,
637  size: {
638    height: 4,
639    width: 6
640  }
641};
642image.createPixelMap(color, opts).then((pixelMap) => {
643  let headFilter = effectKit.createEffect(pixelMap);
644  if (headFilter != null) {
645    headFilter.grayscale();
646  }
647})
648```
649![en-us_image_Add_Grayscale.png](figures/en-us_image_Add_Grayscale.png)
650
651### getPixelMap<sup>(deprecated)</sup>
652
653getPixelMap(): image.PixelMap
654
655Obtains **image.PixelMap** of the source image to which the filter linked list is added.
656
657> **NOTE**
658>
659> This API is supported since API version 9 and deprecated since API version 10. You are advised to use [getEffectPixelMap](#geteffectpixelmap11) instead.
660
661**System capability**: SystemCapability.Multimedia.Image.Core
662
663**Return value**
664
665| Type          | Description                                           |
666| :------------- | :---------------------------------------------- |
667| [image.PixelMap](js-apis-image.md#pixelmap7) | **image.PixelMap** of the source image.|
668
669**Example**
670
671```ts
672import image from "@ohos.multimedia.image";
673import effectKit from "@ohos.effectKit";
674
675const color = new ArrayBuffer(96);
676let opts : image.InitializationOptions = {
677  editable: true,
678  pixelFormat: 3,
679  size: {
680    height: 4,
681    width: 6
682  }
683};
684image.createPixelMap(color, opts).then((pixelMap) => {
685  let pixel = effectKit.createEffect(pixelMap).grayscale().getPixelMap();
686  console.info('getPixelBytesNumber = ', pixel.getPixelBytesNumber());
687})
688```
689
690### getEffectPixelMap<sup>11+</sup>
691
692getEffectPixelMap(): Promise<image.PixelMap>
693
694Obtains **image.PixelMap** of the source image to which the filter linked list is added. This API uses a promise to return the result.
695
696**System capability**: SystemCapability.Multimedia.Image.Core
697
698**Return value**
699
700| Type                  | Description          |
701| ---------------------- | -------------- |
702| Promise\<image.PixelMap>  | Promise used to return **image.PixelMap** of the source image.|
703
704
705**Example**
706
707```ts
708import image from "@ohos.multimedia.image";
709import effectKit from "@ohos.effectKit";
710
711const color = new ArrayBuffer(96);
712let opts : image.InitializationOptions = {
713  editable: true,
714  pixelFormat: 3,
715  size: {
716    height: 4,
717    width: 6
718  }
719};
720image.createPixelMap(color, opts).then((pixelMap) => {
721  effectKit.createEffect(pixelMap).grayscale().getEffectPixelMap().then(data => {
722    console.info('getPixelBytesNumber = ', data.getPixelBytesNumber());
723  })
724})
725```
726