openharmony 鸿蒙 arkts-apis-graphics-drawing-ImageFilter

2026-08-25 浏览 (1)

Class (ImageFilter)

Implements an image filter.

NOTE

  • The initial APIs of this module are supported since API version 11. Newly added APIs will be marked with a superscript to indicate their earliest API version.

  • The initial APIs of this class are supported since API version 12.

  • This module uses the physical pixel unit, px.

  • This module operates under a single-threaded model. The caller needs to manage thread safety and context state transitions.

Modules to Import

import { drawing } from '@kit.ArkGraphics2D';

createBlurImageFilter12+

static createBlurImageFilter(sigmaX: number, sigmaY: number, tileMode: TileMode, imageFilter?: ImageFilter|null ): ImageFilter

Creates an image filter with a given blur effect.

System capability: SystemCapability.Graphics.Drawing

Parameters

NameTypeMandatoryDescription
sigmaXnumberYesStandard deviation of the Gaussian blur along the X axis. The value must be a floating point number greater than 0.
sigmaYnumberYesStandard deviation of the Gaussian blur along the Y axis. The value must be a floating point number greater than 0.
tileModeTileModeYesTile mode to apply to the edges.
imageFilterImageFilter |nullNoFilter to which the image filter will be applied. The default value is null, indicating that the image filter is directly applied to the original image.

Returns

TypeDescription
ImageFilterImage filter created.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error.Possible causes:1.Mandatory parameters are left unspecified;2.Incorrect parameter types;3.Parameter verification failed.

Example

import { drawing } from '@kit.ArkGraphics2D';

let imgFilter = drawing.ImageFilter.createBlurImageFilter(5, 10, drawing.TileMode.CLAMP);

createFromImage20+

static createFromImage(pixelmap: image.PixelMap, srcRect?: common2D.Rect|null, dstRect?: common2D.Rect|null): ImageFilter

Creates an image filter from a given image. You are advised not to use the function for the canvas of the capture type because it affects the performance.

System capability: SystemCapability.Graphics.Drawing

Parameters

NameTypeMandatoryDescription
pixelmapimage.PixelMapYesImage object.
srcRectcommon2D.Rect |nullNo(Optional) Pixel area of the image to be applied to the filter. This parameter is left empty by default, which means that the entire PixelMap area is applied.
dstRectcommon2D.Rect |nullNo(Optional) Area to be rendered. This parameter is left empty by default, which means that the value is the same as that of srcRect.

Returns

TypeDescription
ImageFilterImage filter created.

Example

import { RenderNode } from '@kit.ArkUI';
import { image } from '@kit.ImageKit';
import { common2D, drawing } from '@kit.ArkGraphics2D';

class DrawingRenderNode extends RenderNode {
  draw(context: DrawContext) {
    const width = 1000;
    const height = 1000;
    const bufferSize = width * height * 4;
    const color: ArrayBuffer = new ArrayBuffer(bufferSize);

    const colorData = new Uint8Array(color);
    for (let i = 0; i < colorData.length; i += 4) {
      colorData[i] = 255;
      colorData[i+1] = 156;
      colorData[i+2] = 0;
      colorData[i+3] = 255;
    }

    let opts: image.InitializationOptions = {
      editable: true,
      pixelFormat: 3,
      size: { height, width }
    }

    let pixelMap: image.PixelMap = image.createPixelMapSync(color, opts);
    let srcRect: common2D.Rect = {
      left: 10,
      top: 10,
      right: 80,
      bottom: 80
    };
    let dstRect: common2D.Rect = {
      left: 200,
      top: 200,
      right: 400,
      bottom: 400
    };
    if (pixelMap != null) {
      let filter = drawing.ImageFilter.createFromImage(pixelMap, srcRect, dstRect);
    }
  }
}

createBlendImageFilter20+

static createBlendImageFilter(mode: BlendMode, background: ImageFilter, foreground: ImageFilter): ImageFilter

Creates a filter by blending two existing filters in a certain way.

System capability: SystemCapability.Graphics.Drawing

Parameters

NameTypeMandatoryDescription
modeBlendModeYesBlend mode.
backgroundImageFilterYesFilter that serves as the destination color in blend mode.
foregroundImageFilterYesFilter that serves as the source color in blend mode.

Returns

TypeDescription
ImageFilterImage filter created.

Error codes

For details about the following error code, see Drawing and Display Error Codes.

IDError Message
25900001Parameter error.Possible causes: Incorrect parameter range.

Example

import { drawing } from '@kit.ArkGraphics2D';

let dx = 15.0;
let dy = 10.0;
let offsetFilter1 = drawing.ImageFilter.createOffsetImageFilter(dx, dy, null);
let x = 15.0;
let y = 30.0;
let offsetFilter2 = drawing.ImageFilter.createOffsetImageFilter(x, y, null);
let blendImageFilter = drawing.ImageFilter.createBlendImageFilter(drawing.BlendMode.SRC_IN, offsetFilter1, offsetFilter2);

createComposeImageFilter20+

static createComposeImageFilter(cOuter: ImageFilter, cInner: ImageFilter): ImageFilter

Cascades two image filters to create a new image filter. The first filter's output becomes the second filter's input. The second filter then processes this input to produce the final result.

System capability: SystemCapability.Graphics.Drawing

Parameters

NameTypeMandatoryDescription
cOuterImageFilterYesThe second filter in the cascade, which processes the first filter's output. If the second filter is empty and the first filter is not empty, the final result is the first filter's output. The two filters cannot be empty at the same time.
cInnerImageFilterYesThe first filter in the cascade, which directly processes the original image content. If the first filter is empty and the second filter is not empty, the final result is the second filter's output. The two filters cannot be empty at the same time.

Returns

TypeDescription
ImageFilterImage filter created.

Example

import { drawing } from '@kit.ArkGraphics2D';

let blurSigmaX = 10.0;
let blurSigmaY = 10.0;
let blurFilter = drawing.ImageFilter.createBlurImageFilter(blurSigmaX, blurSigmaY, drawing.TileMode.CLAMP, null);
let colorMatrix:Array<number> = [
  0, 0, 0, 0, 0,
  0, 1, 0, 0, 0,
  0, 0, 1, 0, 0,
  0, 0, 0, 1, 0
];
let redRemovalFilter = drawing.ColorFilter.createMatrixColorFilter(colorMatrix);
let colorFilter = drawing.ImageFilter.createFromColorFilter(redRemovalFilter, null);
let composedImageFilter = drawing.ImageFilter.createComposeImageFilter(colorFilter, blurFilter);

createFromColorFilter12+

static createFromColorFilter(colorFilter: ColorFilter, imageFilter?: ImageFilter|null): ImageFilter

Creates an image filter object with a given color filter effect.

System capability: SystemCapability.Graphics.Drawing

Parameters

NameTypeMandatoryDescription
colorFilterColorFilterYesColor filter.
imageFilterImageFilter |nullNoFilter to which the image filter will be applied. The default value is null, indicating that the image filter is directly applied to the original image.

Returns

TypeDescription
ImageFilterImage filter created.

Error codes

For details about the error codes, see Universal Error Codes.

IDError Message
401Parameter error.Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types.

Example

import { drawing } from '@kit.ArkGraphics2D';

let imgFilter = drawing.ImageFilter.createBlurImageFilter(5, 10, drawing.TileMode.CLAMP);
let colorFilter = drawing.ColorFilter.createSRGBGammaToLinear();
let imgFilter1 = drawing.ImageFilter.createFromColorFilter(colorFilter, imgFilter);

createOffsetImageFilter20+

static createOffsetImageFilter(dx: number, dy: number, input?: ImageFilter|null): ImageFilter

Creates an offset filter to translate the input filter based on the specified vector.

System capability: SystemCapability.Graphics.Drawing

Parameters

NameTypeMandatoryDescription
dxnumberYesHorizontal translation distance. The value is a floating point number.
dynumberYesVertical translation distance. The value is a floating point number.
inputImageFilter |nullNoFilter to be translated. This parameter is left empty by default, which means that the drawing result without the filtering effect is translated.

Returns

TypeDescription
ImageFilterImage filter created.

Example

import { drawing } from '@kit.ArkGraphics2D';

let dx = 15.0;
let dy = 10.0;
let offsetFilter = drawing.ImageFilter.createOffsetImageFilter(dx, dy, null);

createFromShaderEffect20+

static createFromShaderEffect(shader: ShaderEffect): ImageFilter

Creates an ImageFilter object based on a shader.

System capability: SystemCapability.Graphics.Drawing

Parameters

NameTypeMandatoryDescription
shaderShaderEffectYesShader effect to be applied to the image.

Returns

TypeDescription
ImageFilterImage filter created.

Example

import { drawing } from '@kit.ArkGraphics2D';

let shaderEffect = drawing.ShaderEffect.createColorShader(0xFF00FF00);
let renderEffect = drawing.ImageFilter.createFromShaderEffect(shaderEffect);

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 capi-drawing-gpu-context-h

openharmony 鸿蒙 capi-drawing-nativepixelmap-

openharmony 鸿蒙 capi-nativevsync-oh-nativevsync-expectedraterange

openharmony 鸿蒙 capi-drawing-oh-drawing-brush

openharmony 鸿蒙 capi-drawing-oh-drawing-image-info

openharmony 鸿蒙 capi-drawing-text-linetypography-h

openharmony 鸿蒙 capi-oh-nativeimage-oh-nativeimage

openharmony 鸿蒙 capi-drawing-typeface-h

openharmony 鸿蒙 capi-drawing-brush-h

openharmony 鸿蒙 capi-nativewindow

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/j04Dnwx8