harmony 鸿蒙@ohos.effectKit (图像效果)

  • 2025-06-12
  • 浏览 (4)

@ohos.effectKit (图像效果)

图像效果模块提供了处理图像的基础能力,包括亮度调节、模糊化、灰度调节和智能取色等。effectKit用于离线处理图像(如pixelmap、png、jpeg)以获得视觉效果,而uiEffect则实时接入渲染服务,针对屏幕帧缓存进行处理以获得动态视觉效果。

该模块提供以下图像效果相关的常用功能:

  • Filter:效果类,用于添加指定效果到图像源。
  • Color:颜色类,用于保存取色的结果。
  • ColorPicker:智能取色器。

说明:

本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

导入模块

import { effectKit } from "@kit.ArkGraphics2D";

effectKit.createEffect

createEffect(source: image.PixelMap): Filter

通过传入的PixelMap创建Filter实例。

卡片能力: 从API version 12开始,该接口支持在ArkTS卡片中使用。

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名 类型 必填 说明
source image.PixelMap image模块创建的PixelMap实例。可通过图片解码或直接创建获得,具体可见图片开发指导

返回值:

类型 说明
Filter 返回不带任何效果的Filter链表头节点,失败时返回null。

示例:

import { image } from "@kit.ImageKit";
import { effectKit } from "@kit.ArkGraphics2D";

const color = new ArrayBuffer(96);
let opts : image.InitializationOptions = {
  editable: true,
  pixelFormat: 3,
  size: {
    height: 4,
    width: 6
  }
}
image.createPixelMap(color, opts).then((pixelMap) => {
  let headFilter = effectKit.createEffect(pixelMap);
})

effectKit.createColorPicker

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

通过传入的PixelMap创建ColorPicker实例,使用Promise异步回调。

卡片能力: 从API version 12开始,该接口支持在ArkTS卡片中使用。

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名 类型 必填 说明
source image.PixelMap image模块创建的PixelMap实例。可通过图片解码或直接创建获得,具体可见图片开发指导

返回值:

类型 说明
Promise<ColorPicker> Promise对象。返回创建的ColorPicker实例。

错误码:

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
401 Input parameter error.

示例:

import { image } from "@kit.ImageKit";
import { effectKit } from "@kit.ArkGraphics2D";
import { BusinessError } from "@kit.BasicServicesKit";

const color = new ArrayBuffer(96);
let opts : image.InitializationOptions = {
  editable: true,
  pixelFormat: 3,
  size: {
    height: 4,
    width: 6
  }
}

image.createPixelMap(color, opts).then((pixelMap) => {
  effectKit.createColorPicker(pixelMap).then(colorPicker => {
    console.info("color picker=" + colorPicker);
  }).catch( (reason : BusinessError) => {
    console.error("error=" + reason.message);
  })
})

effectKit.createColorPicker10+

createColorPicker(source: image.PixelMap, region: Array<number>): Promise<ColorPicker>

通过传入的PixelMap创建选定取色区域的ColorPicker实例,使用Promise异步回调。

卡片能力: 从API version 12开始,该接口支持在ArkTS卡片中使用。

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名 类型 必填 说明
source image.PixelMap image模块创建的PixelMap实例。可通过图片解码或直接创建获得,具体可见图片开发指导
region Array<number> 指定图片的取色区域。
数组元素个数为4,取值范围为[0, 1],分别表示图片区域的左、上、右、下位置,图片最左侧和最上侧对应位置0,最右侧和最下侧对应位置1。数组第三个元素需大于第一个元素,第四个元素需大于第二个元素。

返回值:

类型 说明
Promise<ColorPicker> Promise对象。返回创建的ColorPicker实例。

错误码:

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
401 Input parameter error.

示例:

import { image } from "@kit.ImageKit";
import { effectKit } from "@kit.ArkGraphics2D";
import { BusinessError } from "@kit.BasicServicesKit";

const color = new ArrayBuffer(96);
let opts : image.InitializationOptions = {
  editable: true,
  pixelFormat: 3,
  size: {
    height: 4,
    width: 6
  }
}

image.createPixelMap(color, opts).then((pixelMap) => {
  effectKit.createColorPicker(pixelMap, [0, 0, 1, 1]).then(colorPicker => {
    console.info("color picker=" + colorPicker);
  }).catch( (reason : BusinessError) => {
    console.error("error=" + reason.message);
  })
})

effectKit.createColorPicker

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

通过传入的PixelMap创建ColorPicker实例,使用callback异步回调。

卡片能力: 从API version 12开始,该接口支持在ArkTS卡片中使用。

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名 类型 必填 说明
source image.PixelMap image模块创建的PixelMap实例。可通过图片解码或直接创建获得,具体可见图片开发指导
callback AsyncCallback<ColorPicker> 回调函数。返回创建的ColorPicker实例。

错误码:

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
401 Input parameter error.

示例:

import { image } from "@kit.ImageKit";
import { effectKit } from "@kit.ArkGraphics2D";

const color = new ArrayBuffer(96);
let opts : image.InitializationOptions = {
  editable: true,
  pixelFormat: 3,
  size: {
    height: 4,
    width: 6
  }
}
image.createPixelMap(color, opts).then((pixelMap) => {
  effectKit.createColorPicker(pixelMap, (error, colorPicker) => {
    if (error) {
      console.error('Failed to create color picker.');
    } else {
      console.info('Succeeded in creating color picker.');
    }
  })
})

effectKit.createColorPicker10+

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

通过传入的PixelMap创建选定取色区域的ColorPicker实例,使用callback异步回调。

卡片能力: 从API version 12开始,该接口支持在ArkTS卡片中使用。

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名 类型 必填 说明
source image.PixelMap image模块创建的PixelMap实例。可通过图片解码或直接创建获得,具体可见图片开发指导
region Array<number> 指定图片的取色区域。
数组元素个数为4,取值范围为[0, 1],数组元素分别表示图片区域的左、上、右、下位置,图片最左侧和最上侧对应位置0,最右侧和最下侧对应位置1。数组第三个元素需大于第一个元素,第四个元素需大于第二个元素。
callback AsyncCallback<ColorPicker> 回调函数。返回创建的ColorPicker实例。

错误码:

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
401 Input parameter error.

示例:

import { image } from "@kit.ImageKit";
import { effectKit } from "@kit.ArkGraphics2D";

const color = new ArrayBuffer(96);
let opts : image.InitializationOptions = {
  editable: true,
  pixelFormat: 3,
  size: {
    height: 4,
    width: 6
  }
}
image.createPixelMap(color, opts).then((pixelMap) => {
  effectKit.createColorPicker(pixelMap, [0, 0, 1, 1], (error, colorPicker) => {
    if (error) {
      console.error('Failed to create color picker.');
    } else {
      console.info('Succeeded in creating color picker.');
    }
  })
})

Color

颜色类,用于保存取色的结果。

卡片能力: 从API version 12开始,该接口支持在ArkTS卡片中使用。

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Multimedia.Image.Core

名称 类型 可读 可写 说明
red number 红色分量值,取值范围[0x0, 0xFF]。
green number 绿色分量值,取值范围[0x0, 0xFF]。
blue number 蓝色分量值,取值范围[0x0, 0xFF]。
alpha number 透明通道分量值,取值范围[0x0, 0xFF]。

TileMode14+

着色器效果平铺模式的枚举。

系统能力: SystemCapability.Multimedia.Image.Core

名称 说明
CLAMP 0 如果着色器效果超出其原始边界,剩余区域使用着色器的边缘颜色填充。
REPEAT 1 在水平和垂直方向上重复着色器效果。
MIRROR 2 在水平和垂直方向上重复着色器效果,交替镜像图像,以便相邻图像始终接合。
DECAL 3 仅在其原始边界内渲染着色器效果。

ColorPicker

取色类,用于从一张图像数据中获取它的主要颜色。在调用ColorPicker的方法前,需要先通过createColorPicker创建一个ColorPicker实例。

getMainColor

getMainColor(): Promise<Color>

读取图像主色的颜色值,结果写入Color里,使用Promise异步回调。该接口通过图像缩放算法,根据周围像素的加权计算,将原图缩小到1个像素以得到主色。

卡片能力: 从API version 12开始,该接口支持在ArkTS卡片中使用。

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Multimedia.Image.Core

返回值:

类型 说明
Promise<Color> Promise对象。返回图像主色对应的颜色值,失败时返回错误信息。

示例:

import { image } from "@kit.ImageKit";
import { effectKit } from "@kit.ArkGraphics2D";

const color = new ArrayBuffer(96);
let opts: image.InitializationOptions = {
  editable: true,
  pixelFormat: 3,
  size: {
    height: 4,
    width: 6
  }
}
image.createPixelMap(color, opts).then((pixelMap) => {
  effectKit.createColorPicker(pixelMap, (error, colorPicker) => {
    if (error) {
      console.error('Failed to create color picker.');
     } else {
       console.info('Succeeded in creating color picker.');
       colorPicker.getMainColor().then(color => {
        console.info('Succeeded in getting main color.');
         console.info(`color[ARGB]=${color.alpha},${color.red},${color.green},${color.blue}`);
      })
    }
  })
})

getMainColorSync

getMainColorSync(): Color

读取图像主色的颜色值,结果写入Color里,使用同步方式返回。该接口通过图像缩放算法,根据周围像素的加权计算,将原图缩小到1个像素以得到主色。

卡片能力: 从API version 12开始,该接口支持在ArkTS卡片中使用。

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Multimedia.Image.Core

返回值:

类型 说明
Color Color实例,即图像主色对应的颜色值,失败时返回null。

示例:

import { image } from "@kit.ImageKit";
import { effectKit } from "@kit.ArkGraphics2D";

const color = new ArrayBuffer(96);
let opts : image.InitializationOptions = {
  editable: true,
  pixelFormat: 3,
  size: {
    height: 4,
    width: 6
  }
}
image.createPixelMap(color, opts).then((pixelMap) => {
  effectKit.createColorPicker(pixelMap, (error, colorPicker) => {
    if (error) {
      console.error('Failed to create color picker.');
    } else {
      console.info('Succeeded in creating color picker.');
      let color = colorPicker.getMainColorSync();
      console.info('get main color =' + color);
    }
  })
})

zh-ch_image_Main_Color.png

getLargestProportionColor10+

getLargestProportionColor(): Color

读取图像中占比最多的颜色值,结果写入Color里,使用同步方式返回。该接口使用中位切分算法划分颜色空间,获取占比最多的颜色空间的平均颜色。

卡片能力: 从API version 12开始,该接口支持在ArkTS卡片中使用。

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Multimedia.Image.Core

返回值:

类型 说明
Color Color实例,即图像占比最多的颜色值,失败时返回null。

示例:

import { image } from "@kit.ImageKit";
import { effectKit } from "@kit.ArkGraphics2D";

const color = new ArrayBuffer(96);
let opts : image.InitializationOptions = {
  editable: true,
  pixelFormat: 3,
  size: {
    height: 4,
    width: 6
  }
}
image.createPixelMap(color, opts).then((pixelMap) => {
  effectKit.createColorPicker(pixelMap, (error, colorPicker) => {
    if (error) {
      console.error('Failed to create color picker.');
    } else {
      console.info('Succeeded in creating color picker.');
      let color = colorPicker.getLargestProportionColor();
      console.info('get largest proportion color =' + color);
    }
  })
})

zh-ch_image_Largest_Proportion_Color.png

getTopProportionColors12+

getTopProportionColors(colorCount: number): Array

读取图像占比靠前的颜色值,个数由colorCount指定,结果写入Color的数组里,使用同步方式返回。

卡片能力: 从API version 12开始,该接口支持在ArkTS卡片中使用。

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Multimedia.Image.Core

参数: |参数名 |类型 |必填|说明 | |———-|——|—-|——————————————-| |colorCount|number|是 |需要取主色的个数,取值范围为[1, 10],向下取整。 |

返回值:

类型 说明
Array<Color |null> Color数组,即图像占比前colorCount的颜色值数组,按占比排序。
- 当实际读取的特征色个数小于colorCount时,数组大小为实际特征色个数。
- 取色失败或取色个数小于1返回[null]
- 取色个数大于10视为取前10个。

示例:

import { image } from "@kit.ImageKit";
import { effectKit } from "@kit.ArkGraphics2D";

const color = new ArrayBuffer(96);
let opts : image.InitializationOptions = {
  editable: true,
  pixelFormat: 3,
  size: {
    height: 4,
    width: 6
  }
}
image.createPixelMap(color, opts).then((pixelMap) => {
  effectKit.createColorPicker(pixelMap, (error, colorPicker) => {
    if (error) {
      console.error('Failed to create color picker.');
    } else {
      console.info('Succeeded in creating color picker.');
      let colors = colorPicker.getTopProportionColors(2);
      for(let index = 0; index < colors.length; index++) {
        if (colors[index]) {
          console.info('get top proportion colors: index ' + index + ', color ' + colors[index]);
        }
      }
    }
  })
})

zh-ch_image_Top_Proportion_Colors.png

getHighestSaturationColor10+

getHighestSaturationColor(): Color

读取图像饱和度最高的颜色值,结果写入Color里,使用同步方式返回。

卡片能力: 从API version 12开始,该接口支持在ArkTS卡片中使用。

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Multimedia.Image.Core

返回值:

类型 说明
Color Color实例,即图像饱和度最高的颜色值,失败时返回null。

示例:

import { image } from "@kit.ImageKit";
import { effectKit } from "@kit.ArkGraphics2D";

const color = new ArrayBuffer(96);
let opts: image.InitializationOptions = {
  editable: true,
  pixelFormat: 3,
  size: {
    height: 4,
    width: 6
  }
}
image.createPixelMap(color, opts).then((pixelMap) => {
  effectKit.createColorPicker(pixelMap, (error, colorPicker) => {
    if (error) {
      console.error('Failed to create color picker.');
    } else {
      console.info('Succeeded in creating color picker.');
      let color = colorPicker.getHighestSaturationColor();
      console.info('get highest saturation color =' + color);
    }
  })
})

zh-ch_image_Highest_Saturation_Color.png

getAverageColor10+

getAverageColor(): Color

读取图像平均的颜色值,结果写入Color里,使用同步方式返回。

卡片能力: 从API version 12开始,该接口支持在ArkTS卡片中使用。

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Multimedia.Image.Core

返回值:

类型 说明
Color Color实例,即图像平均的颜色值,失败时返回null。

示例:

import { image } from "@kit.ImageKit";
import { effectKit } from "@kit.ArkGraphics2D";

const color = new ArrayBuffer(96);
let opts: image.InitializationOptions = {
  editable: true,
  pixelFormat: 3,
  size: {
    height: 4,
    width: 6
  }
}
image.createPixelMap(color, opts).then((pixelMap) => {
  effectKit.createColorPicker(pixelMap, (error, colorPicker) => {
    if (error) {
      console.error('Failed to create color picker.');
    } else {
      console.info('Succeeded in creating color picker.');
      let color = colorPicker.getAverageColor();
      console.info('get average color =' + color);
    }
  })
})

zh-ch_image_Average_Color.png

isBlackOrWhiteOrGrayColor10+

isBlackOrWhiteOrGrayColor(color: number): boolean

判断图像是否为黑白灰颜色,返回true或false。

卡片能力: 从API version 12开始,该接口支持在ArkTS卡片中使用。

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名 类型 必填 说明
color number 需要判断是否黑白灰色的颜色值,取值范围[0x0, 0xFFFFFFFF]。

返回值:

类型 说明
boolean 如果图像为黑白灰颜色,则返回true;否则返回false。

示例:

import { image } from "@kit.ImageKit";
import { effectKit } from "@kit.ArkGraphics2D";

const color = new ArrayBuffer(96);
let opts: image.InitializationOptions = {
  editable: true,
  pixelFormat: 3,
  size: {
    height: 4,
    width: 6
  }
}
image.createPixelMap(color, opts).then((pixelMap) => {
  effectKit.createColorPicker(pixelMap, (error, colorPicker) => {
    if (error) {
      console.error('Failed to create color picker.');
    } else {
      console.info('Succeeded in creating color picker.');
      let bJudge = colorPicker.isBlackOrWhiteOrGrayColor(0xFFFFFFFF);
      console.info('is black or white or gray color[bool](white) =' + bJudge);
    }
  })
})

Filter

图像效果类,用于将指定的效果添加到输入图像中。在调用Filter的方法前,需要先通过createEffect创建一个Filter实例。

blur

blur(radius: number): Filter

将模糊效果添加到效果链表中,返回链表的头节点。

说明:

该接口为静态模糊接口,为静态图像提供模糊化效果,如果要对组件进行实时渲染的模糊,可以使用动态模糊

卡片能力: 从API version 12开始,该接口支持在ArkTS卡片中使用。

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名 类型 必填 说明
radius number 模糊半径,单位是像素。模糊效果与所设置的值成正比,值越大效果越明显。

返回值:

类型 说明
Filter 返回已添加的图像效果。

示例:

import { image } from "@kit.ImageKit";
import { effectKit } from "@kit.ArkGraphics2D";

const color = new ArrayBuffer(96);
let opts : image.InitializationOptions = {
  editable: true,
  pixelFormat: 3,
  size: {
    height: 4,
    width: 6
  }
};
image.createPixelMap(color, opts).then((pixelMap) => {
  let radius = 5;
  let headFilter = effectKit.createEffect(pixelMap);
  if (headFilter != null) {
    headFilter.blur(radius);
  }
})

zh-ch_image_Add_Blur.png

blur14+

blur(radius: number, tileMode: TileMode): Filter

将模糊效果添加到效果链表中,返回链表的头节点。

说明:

该接口为静态模糊接口,为静态图像提供模糊化效果,如果要对组件进行实时渲染的模糊,可以使用动态模糊

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名 类型 必填 说明
radius number 模糊半径,单位是像素。模糊效果与所设置的值成正比,值越大效果越明显。
tileMode TileMode 着色器效果平铺模式。影响图像边缘的模糊效果。目前仅支持CPU渲染,所以目前着色器平铺模式仅支持DECAL。

返回值:

类型 说明
Filter 返回已添加的图像效果。

示例:

import { image } from "@kit.ImageKit";
import { effectKit } from "@kit.ArkGraphics2D";

const color = new ArrayBuffer(96);
let opts : image.InitializationOptions = {
  editable: true,
  pixelFormat: 3,
  size: {
    height: 4,
    width: 6
  }
};
image.createPixelMap(color, opts).then((pixelMap) => {
  let radius = 30;
  let headFilter = effectKit.createEffect(pixelMap);
  if (headFilter != null) {
    headFilter.blur(radius, effectKit.TileMode.DECAL);
  }
})

zh-ch_image_Add_Blur_With_TileMode.png

invert12+

invert(): Filter

将反转效果添加到效果链表中,返回链表的头节点。

系统能力: SystemCapability.Multimedia.Image.Core

返回值:

类型 说明
Filter 返回已添加的图像效果。

示例:

import { image } from "@kit.ImageKit";
import { effectKit } from "@kit.ArkGraphics2D";

const color = new ArrayBuffer(96);
let opts : image.InitializationOptions = {
  editable: true,
  pixelFormat: 3,
  size: {
    height: 4,
    width: 6
  }
};
image.createPixelMap(color, opts).then((pixelMap) => {
  let headFilter = effectKit.createEffect(pixelMap);
  if (headFilter != null) {
    headFilter.invert();
  }
})

zh-ch_image_Add_Invert.png

setColorMatrix12+

setColorMatrix(colorMatrix: Array<number>): Filter

将自定义效果添加到效果链表中,返回链表的头节点。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名 类型 必填 说明
colorMatrix Array<number> 自定义颜色矩阵。
用于创建效果滤镜的 5x4 大小的矩阵,矩阵元素取值范围为[0, 1],0和1代表的是矩阵中对应位置的颜色通道的权重,0代表该颜色通道不参与计算,1代表该颜色通道参与计算并保持原始权重。

返回值:

类型 说明
Filter 返回已添加的图像效果。

错误码:

以下错误码的详细介绍请参见通用错误码

错误码ID 错误信息
401 Input parameter error.

示例:

import { image } from "@kit.ImageKit";
import { effectKit } from "@kit.ArkGraphics2D";

const color = new ArrayBuffer(96);
let opts : image.InitializationOptions = {
  editable: true,
  pixelFormat: 3,
  size: {
    height: 4,
    width: 6
  }
};
image.createPixelMap(color, opts).then((pixelMap) => {
  let colorMatrix:Array<number> = [
    0.2126,0.7152,0.0722,0,0,
    0.2126,0.7152,0.0722,0,0,
    0.2126,0.7152,0.0722,0,0,
    0,0,0,1,0
  ];
  let headFilter = effectKit.createEffect(pixelMap);
  if (headFilter != null) {
    headFilter.setColorMatrix(colorMatrix);
  }
})

brightness

brightness(bright: number): Filter

将高亮效果添加到效果链表中,返回链表的头节点。

卡片能力: 从API version 12开始,该接口支持在ArkTS卡片中使用。

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Multimedia.Image.Core

参数:

参数名 类型 必填 说明
bright number 高亮程度,取值范围在0-1之间,取值为0时图像保持不变。

返回值:

类型 说明
Filter 返回已添加的图像效果。

示例:

import { image } from "@kit.ImageKit";
import { effectKit } from "@kit.ArkGraphics2D";

const color = new ArrayBuffer(96);
let opts : image.InitializationOptions = {
  editable: true,
  pixelFormat: 3,
  size: {
    height: 4,
    width: 6
  }
};
image.createPixelMap(color, opts).then((pixelMap) => {
  let bright = 0.5;
  let headFilter = effectKit.createEffect(pixelMap);
  if (headFilter != null) {
    headFilter.brightness(bright);
  }
})

zh-ch_image_Add_Brightness.png

grayscale

grayscale(): Filter

将灰度效果添加到效果链表中,返回链表的头节点。

卡片能力: 从API version 12开始,该接口支持在ArkTS卡片中使用。

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Multimedia.Image.Core

返回值:

类型 说明
Filter 返回已添加的图像效果。

示例:

import { image } from "@kit.ImageKit";
import { effectKit } from "@kit.ArkGraphics2D";

const color = new ArrayBuffer(96);
let opts : image.InitializationOptions = {
  editable: true,
  pixelFormat: 3,
  size: {
    height: 4,
    width: 6
  }
};
image.createPixelMap(color, opts).then((pixelMap) => {
  let headFilter = effectKit.createEffect(pixelMap);
  if (headFilter != null) {
    headFilter.grayscale();
  }
})

zh-ch_image_Add_Grayscale.png

getEffectPixelMap11+

getEffectPixelMap(): Promise

获取已添加链表效果的源图像的image.PixelMap,使用Promise异步回调。

卡片能力: 从API version 12开始,该接口支持在ArkTS卡片中使用。

原子化服务API: 从API version 12开始,该接口支持在原子化服务中使用。

系统能力: SystemCapability.Multimedia.Image.Core

返回值:

类型 说明
Promise<image.PixelMap> Promise对象。返回已添加链表效果的源图像的image.PixelMap。

示例:

import { image } from "@kit.ImageKit";
import { effectKit } from "@kit.ArkGraphics2D";

const color = new ArrayBuffer(96);
let opts : image.InitializationOptions = {
  editable: true,
  pixelFormat: 3,
  size: {
    height: 4,
    width: 6
  }
};
image.createPixelMap(color, opts).then((pixelMap) => {
  effectKit.createEffect(pixelMap).grayscale().getEffectPixelMap().then(data => {
    console.info('getPixelBytesNumber = ', data.getPixelBytesNumber());
  })
})

getPixelMap(deprecated)

getPixelMap(): image.PixelMap

获取已添加链表效果的源图像的image.PixelMap。

说明:

此接口从API version 9开始支持,从API version 11开始废弃,推荐使用getEffectPixelMap

系统能力: SystemCapability.Multimedia.Image.Core

返回值:

类型 说明
image.PixelMap 已添加效果的源图像的image.PixelMap。

示例:

import { image } from "@kit.ImageKit";
import { effectKit } from "@kit.ArkGraphics2D";

const color = new ArrayBuffer(96);
let opts : image.InitializationOptions = {
  editable: true,
  pixelFormat: 3,
  size: {
    height: 4,
    width: 6
  }
};
image.createPixelMap(color, opts).then((pixelMap) => {
  let pixel = effectKit.createEffect(pixelMap).grayscale().getPixelMap();
  console.info('getPixelBytesNumber = ', pixel.getPixelBytesNumber());
})

你可能感兴趣的鸿蒙文章

harmony 鸿蒙ArkGraphics 2D(方舟2D图形服务)

harmony 鸿蒙buffer_common.h

harmony 鸿蒙buffer_handle.h

harmony 鸿蒙BufferHandle

harmony 鸿蒙ColorSpacePrimaries

harmony 鸿蒙DisplaySoloist_ExpectedRateRange

harmony 鸿蒙drawing_bitmap.h

harmony 鸿蒙drawing_brush.h

harmony 鸿蒙drawing_canvas.h

harmony 鸿蒙drawing_color_filter.h

0  赞