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