圖像效果

2024-01-23 13:19 更新

圖像效果提供處理圖像的一些基礎(chǔ)能力,包括對當(dāng)前圖像的亮度調(diào)節(jié)、模糊化、灰度調(diào)節(jié)、智能取色等。

該模塊提供以下圖像效果相關(guān)的常用功能:

  • Filter:效果類,用于添加指定效果到圖像源。
  • Color:顏色類,用于保存取色的結(jié)果。
  • ColorPicker:智能取色器。
說明

本模塊首批接口從API version 9開始支持。后續(xù)版本的新增接口,采用上角標(biāo)單獨(dú)標(biāo)記接口的起始版本。

導(dǎo)入模塊

  1. import effectKit from '@ohos.effectKit';

effectKit.createEffect

createEffect(source: image.PixelMap): Filter

通過傳入的PixelMap創(chuàng)建Filter實(shí)例。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

參數(shù):

參數(shù)名

類型

必填

說明

source

image.PixelMap

image模塊創(chuàng)建的PixelMap實(shí)例??赏ㄟ^圖片解碼或直接創(chuàng)建獲得,具體可見圖片開發(fā)指導(dǎo)。

返回值:

類型

說明

Filter

返回不帶任何效果的Filter鏈表的頭節(jié)點(diǎn),失敗時(shí)返回null。

示例:

  1. import image from "@ohos.multimedia.image";
  2. const color = new ArrayBuffer(96);
  3. let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
  4. image.createPixelMap(color, opts).then((pixelMap) => {
  5. let headFilter = effectKit.createEffect(pixelMap);
  6. })

effectKit.createColorPicker

createColorPicker(source: image.PixelMap): Promise<ColorPicker>

通過傳入的PixelMap創(chuàng)建ColorPicker實(shí)例,使用Promise異步回調(diào)。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

參數(shù):

參數(shù)名

類型

必填

說明

source

image.PixelMap

image模塊創(chuàng)建的PixelMap實(shí)例。可通過圖片解碼或直接創(chuàng)建獲得,具體可見圖片開發(fā)指導(dǎo)

返回值:

類型

說明

Promise<ColorPicker>

Promise對象。返回創(chuàng)建的ColorPicker實(shí)例。

示例:

  1. import image from "@ohos.multimedia.image";
  2. const color = new ArrayBuffer(96);
  3. let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
  4. image.createPixelMap(color, opts).then((pixelMap) => {
  5. effectKit.createColorPicker(pixelMap).then(colorPicker => {
  6. console.info("color picker=" + colorPicker);
  7. }).catch(ex => console.error(".error=" + ex.toString()))
  8. })

effectKit.createColorPicker

createColorPicker(source: image.PixelMap, callback: AsyncCallback<ColorPicker>): void

通過傳入的PixelMap創(chuàng)建ColorPicker實(shí)例,使用callback異步回調(diào)。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

參數(shù):

參數(shù)名

類型

必填

說明

source

image.PixelMap

image模塊創(chuàng)建的PixelMap實(shí)例。可通過圖片解碼或直接創(chuàng)建獲得,具體可見圖片開發(fā)指導(dǎo)

callback

AsyncCallback<ColorPicker>

回調(diào)函數(shù)。返回創(chuàng)建的ColorPicker實(shí)例。

示例:

  1. import image from "@ohos.multimedia.image";
  2. const color = new ArrayBuffer(96);
  3. let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } }
  4. image.createPixelMap(color, opts).then((pixelMap) => {
  5. effectKit.createColorPicker(pixelMap, (error, colorPicker) => {
  6. if (error) {
  7. console.error('Failed to create color picker.');
  8. } else {
  9. console.info('Succeeded in creating color picker.');
  10. }
  11. })
  12. })

Color

顏色類,用于保存取色的結(jié)果。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

名稱

類型

可讀

可寫

說明

red

number

紅色分量值,取值范圍[0x0, 0xFF]。

green

number

綠色分量值,取值范圍[0x0, 0xFF]。

blue

number

藍(lán)色分量值,取值范圍[0x0, 0xFF]。

alpha

number

透明通道分量值,取值范圍[0x0, 0xFF]。

ColorPicker

取色類,用于從一張圖像數(shù)據(jù)中獲取它的主要顏色。在調(diào)用ColorPicker的方法前,需要先通過createColorPicker創(chuàng)建一個(gè)ColorPicker實(shí)例。

getMainColor

getMainColor(): Promise<Color>

讀取圖像主色的顏色值,結(jié)果寫入Color里,使用Promise異步回調(diào)。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

返回值:

類型

說明

Promise<Color>

Promise對象。返回圖像主色對應(yīng)的顏色值,失敗時(shí)返回錯(cuò)誤信息。

示例:

  1. colorPicker.getMainColor().then(color => {
  2. console.info('Succeeded in getting main color.');
  3. console.info(`color[ARGB]=${color.alpha},${color.red},${color.green},${color.blue}`);
  4. }).catch(error => {
  5. console.error('Failed to get main color.');
  6. })

getMainColorSync

getMainColorSync(): Color

讀取圖像主色的顏色值,結(jié)果寫入Color里,使用同步方式返回。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

返回值:

類型

說明

Color

Color實(shí)例,即圖像主色對應(yīng)的顏色值,失敗時(shí)返回null。

示例:

  1. let color = colorPicker.getMainColorSync();
  2. console.info('get main color =' + color);

Filter

圖像效果類,用于將指定的效果添加到輸入圖像中。在調(diào)用Filter的方法前,需要先通過createEffect創(chuàng)建一個(gè)Filter實(shí)例。

blur

blur(radius: number): Filter

將模糊效果添加到效果鏈表中,結(jié)果返回效果鏈表的頭節(jié)點(diǎn)。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

參數(shù):

參數(shù)名

類型

必填

說明

radius

number

模糊半徑,單位是像素。模糊效果與所設(shè)置的值成正比,值越大效果越明顯。

返回值:

類型

說明

Filter

返回已添加的圖像效果。

示例:

  1. import image from "@ohos.multimedia.image";
  2. const color = new ArrayBuffer(96);
  3. let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } };
  4. image.createPixelMap(color, opts).then((pixelMap) => {
  5. let radius = 5;
  6. let headFilter = effectKit.createEffect(pixelMap);
  7. if (headFilter != null) {
  8. headFilter.blur(radius);
  9. }
  10. })

brightness

brightness(bright: number): Filter

將高亮效果添加到效果鏈表中,結(jié)果返回效果鏈表的頭節(jié)點(diǎn)。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

參數(shù):

參數(shù)名

類型

必填

說明

bright

number

高亮程度,取值范圍在0-1之間,取值為0時(shí)圖像保持不變。

返回值:

類型

說明

Filter

返回已添加的圖像效果。

示例:

  1. import image from "@ohos.multimedia.image";
  2. const color = new ArrayBuffer(96);
  3. let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } };
  4. image.createPixelMap(color, opts).then((pixelMap) => {
  5. let bright = 0.5;
  6. let headFilter = effectKit.createEffect(pixelMap);
  7. if (headFilter != null) {
  8. headFilter.brightness(bright);
  9. }
  10. })

grayscale

grayscale(): Filter

將灰度效果添加到效果鏈表中,結(jié)果返回效果鏈表的頭節(jié)點(diǎn)。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

返回值:

類型

說明

Filter

返回已添加的圖像效果。

示例:

  1. import image from "@ohos.multimedia.image";
  2. const color = new ArrayBuffer(96);
  3. let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } };
  4. image.createPixelMap(color, opts).then((pixelMap) => {
  5. let headFilter = effectKit.createEffect(pixelMap);
  6. if (headFilter != null) {
  7. headFilter.grayscale();
  8. }
  9. })

getPixelMap

getPixelMap(): image.PixelMap

獲取已添加鏈表效果的源圖像的image.PixelMap。

系統(tǒng)能力: SystemCapability.Multimedia.Image.Core

返回值:

類型

說明

image.PixelMap

已添加效果的源圖像的image.PixelMap。

示例:

  1. import image from "@ohos.multimedia.image";
  2. const color = new ArrayBuffer(96);
  3. let opts = { editable: true, pixelFormat: 3, size: { height: 4, width: 6 } };
  4. image.createPixelMap(color, opts).then((pixelMap) => {
  5. let pixel = effectKit.createEffect(pixelMap).grayscale().getPixelMap();
  6. })
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號