Flutter Package:图片处理库 Image

Auth:老猿𝕏𝕏       Date:2024/06/3       Cat:编程       Word:共15152字

image 是一个用于加载、保存和操作各种图像文件格式的Flutter图像库,写作本文时的版本为:4.2.0,下面我们演示每个功能的使用,API的解释采用的机翻,具体的 API 文档点击这里查看

支持的格式

# 读/写
JPEG / PNG + 动画 APNG / GIF + 动画 GIF / TIFF / BMP / TGA / ICO / PVRTC 

# 只读
WebP + 动画 WebP / Photoshop PSD / OpenEXR  /PNM (PBM, PGM, PPM) / PNM(PBM、PGM、PPM)

# 只写
CUR 

加载图像

decodeBmp: 解码 BMP 格式的图像。
decodeBmpFile: 从文件中解码 BMP 格式的图像。
decodeExr: 解码 OpenEXR 格式的图像。 EXR 是一种高动态范围格式。
decodeExrFile: 从文件中解码 EXR 格式的图像。
decodeGif: 解码 GIF 格式的图像。
decodeGifFile: 从文件中解码 GIF 格式的图像。
decodeIco: 解码 ICO 图像。
decodeIcoFile: 从文件中解码 ICO 格式的图像。
decodeJpg: 解码 JPG 格式的图像。
decodeJpgExif: 仅解码 JPEG 文件中的 ExifData。
decodeJpgFile: 从文件中解码 JPG 格式的图像。
decodePng: 解码 PNG 格式的图像。
decodePngFile: 从文件中解码 PNG 格式的图像。
decodePnm: 解码 PNM 格式的图像。
decodePnmFile: 从文件中解码 PNM 格式的图像。
decodePsd: 解码 Photoshop PSD 格式的图像。
decodePsdFile: 从文件中解码 PSD 格式的图像。
decodePvr: 解码 PVR 图像。
decodePvrFile: 从文件中解码 PVR 格式的图像。
decodeTga: 解码 TGA 格式的图像。
decodeTgaFile: 从文件中解码 TGA 格式的图像。
decodeTiff: 解码 TIFF 格式的图像。
decodeTiffFile: 从文件中解码 TIFF 格式的图像。
decodeWebP: 解码 WebP 格式的图像
decodeWebPFile: 从文件中解码 WebP 格式的图像。

decodeImage: 通过首先识别文件的格式并使用该解码器将文件解码为单帧图像来解码给定的图像文件字节。
decodeImageFile: 从文件路径解码图像。
decodeNamedImage: 解码给定的图像文件字节,使用文件扩展名来确定解码器。



保存图像

示例

final image = decodeJpg(File('test.jpg').readAsBytesSync());
final thumbnail = copyResize(image, width: 120);
encodeJpgFile('out/thumbnail-test.png', thumbnail);

API

encodeBmp: 将图像编码为 BMP 格式。
encodeBmpFile: 将 image 编码为给定 path 处的 TIFF 文件。
encodeCur: 将图像编码为 CUR 格式。
encodeCurFile: 将 image 编码为给定 path 处的 CUR 文件。
encodeGif: 将图像编码为 GIF 格式。
encodeGifFile: 将 image 编码为给定 path 处的 GIF 文件。
encodeIco: 将图像编码为 ICO 格式。
encodeIcoFile: 将 image 编码为给定 path 处的 ICO 文件。
encodeJpg: 将 image 编码为 JPEG 格式。
encodeJpgFile: 将 image 编码为给定 path 处的 JPG 文件。
encodePng: 将图像编码为 PNG 格式。
encodePngFile: 将 image 编码为给定 path 处的 PNG 文件。
encodePvr: 将图像编码为 PVR 格式。
encodePvrFile: 将 image 编码为给定 path 处的 PVR 文件。
encodeTga: 将图像编码为 TGA 格式。
encodeTgaFile: 将 image 编码为给定 path 处的 TGA 文件。
encodeTiff: 将图像编码为 TIFF 格式。
encodeTiffFile: 将 image 编码为给定 path 处的 TIFF 文件。

encodeImageFile: 将 image 编码到给定 path 处的文件中。图像文件的格式由文件的扩展名决定。如果图像成功写入文件,则返回 true,否则返回 false。对于不支持 dart:io 的平台,将返回 false。
encodeNamedImage: 将 image 编码为由 path 文件扩展名确定的格式。如果无法识别格式,将返回 null。否则将返回图像的编码格式字节。

在单独的隔离线程中处理图像

示例

await (
  img.Command()
    ..decodeImageFile('test.png')
    ..copyResize(width: 120)
    ..gaussianBlur(radius: 5)
    ..writeToFile('thumbnail.png')
)
.executeThread();

API

Command: 创建、加载、操作和保存图像的命令的基类。在调用execute 或executeThread 方法之前,不会执行命令。

直接使用 Decoder 类来解码图像,并访问其附加信息

示例

final decoder = PngDecoder();
decoder.isValidFile(fileBytes);
Image? image = decoder.decode(fileBytes);

DecodeInfo? info = decoder.startDecode(fileBytes);
if (info != null) {
  int width = info.width;
  int height = info.height;
  int numFrames = info.numFrames;
  final pngInfo = info as PngInfo;
  double? gamma = pngInfo.gamma;
  int bits = pngInfo.bits;
}
int numFrames = decoder.numFrames;
Image? frame0 = decoder.decodeFrame(0);

API

Decoder: 图像格式解码器的基类
BmpDecoder  Bmp解码器
DibDecoder  迪布解码器
ExrDecoder: 解码 OpenEXR 格式的图像。
GifDecoder: GIF 图像格式的解码器。它支持单帧和动画 GIF 文件以及透明度。
IcoDecoder: 解码 ICO 格式的图像。请注意,ICO 文件始终解码为 rgba8 32 位图像,以支持它们编码透明度的方式。
JpegDecoder: 解码 jpeg 编码图像。
PngDecoder: 解码 PNG 编码图像。
PnmDecoder: 解码 PNM 图像。
PsdDecoder: 解码 Photoshop PSD 图像。
PvrDecoder: 解码 PVR 图像。
TgaDecoder: 解码 TGA 图像。这只支持24位和32位未压缩格式。
TiffDecoder: Tiff解码器
WebPDecoder: 解码 WebP 格式的图像。这支持无损 (vp8l)、有损 (vp8)、有损+alpha 和动画 WebP 图像。

创建图像

示例

final rgb8 = Image(width: 256, height: 256);

API

实例

Image: 创建具有给定尺寸和格式的图像
Image.empty(): 创建一个空图像
Image.from: 创建给定图像的副本
Image.fromBytes: 根据 bytes 中的原始数据创建图像
Image.fromResized: 根据给定的宽度和高度创建图像

属性

backgroundColor: 建议用于清除画布的背景颜色。
bitsPerChannel: 每个颜色通道的位数。
buffer: 图像存储数据的ByteBuffer。
data: 图像的 exif 元数据。如果尚未为图像创建 ExifData,则会添加一个。
extraChannels: 该图像使用的命名非颜色通道。
first: 第一个元素。
format: 图像像素的格式。
formatType: 格式的一般类型,无论是 Uint 数据、Int 数据还是 Float 数据(无论精度如何)。
frameDuration: 该帧应显示多长时间(以毫秒为单位)。持续时间为 0 表示没有延迟,并且将尽快绘制下一帧。
frameIndex: 该图像在父动画帧列表中的索引。
frames: 图像的子帧列表(如果是动画)。如果图像具有多个帧,则该图像被视为动画,因为第一帧将是图像本身。
frameType: 框架应该如何解释?如果为 FrameType.animation,则帧是动画序列的一部分。如果是 FrameType.page,则框架是文档的页面。
hasAlpha: 如果图像有 Alpha 通道,则为 True。
hasAnimation: 如果图像具有多个帧,则该图像被视为动画,因为帧列表中的第一个图像是图像本身。
hasExif: 如果图像具有 exif 元数据,则为 True。
hashCode: 该对象的哈希码。
hasPalette: 如果图像有调色板,则为 true。
height: 图像的高度(以像素为单位)。
iccProfile: 
isEmpty: 该集合是否没有元素。
isHdrFormat: 如果图像是高动态范围图像,则为 true。
isLdrFormat: 如果图像格式是低动态范围(常规)图像,则为 true。
isNotEmpty: 该集合是否至少有一个元素。
isValid: 如果图像有效并且有数据,则为 true。
iterator: 返回一个像素迭代器,用于迭代图像中的所有像素。
last: 最后一个元素。
length: 其中的元素数量。
lengthInBytes: 图像数据缓冲区的长度(以字节为单位)。
loopCount: 动画应该循环多少次(0 表示永远)?
maxChannelValue: 像素通道的最大值,基于图像的格式。如果图像有调色板,则这将是调色板颜色通道的最大值。浮点格式图像的 maxChannelValue 为 1.0,但它们的值可以高于该值。
maxIndexValue: 调色板索引的最大值,基于图像的格式。这与 maxChannelValue 的不同之处在于它不会受到调色板格式的影响。
numChannels: 图像的颜色通道数。
numFrames: 该图像中的帧数。一张图像本身至少有一个帧,因此如果它有多个帧,它就会被认为是动画的。
palette: 如果图像有调色板,则为 null。
rowStride: 图像缓冲区中一行像素的长度(以字节为单位)。
single: 检查此可迭代对象是否只有一个元素,并返回该元素。
supportsPalette: 如果此图像格式支持使用调色板,则为 true。
textData: 
文本数据↔映射<字符串,字符串>?
width: 图像的宽度(以像素为单位)。

方法

addFrame: 向该图像的动画添加一个帧。
addTextData: 将文本元数据添加到图像。
clear: 将图像中的所有像素设置为给定的 color 。如果没有提供颜色,图像将初始化为 0。
clone: 创建该图像的副本。
contains: 集合是否包含等于 element 的元素。
convert: 将此图像转换为新的 format 或通道数 numChannels 。如果新的通道数为 4 并且当前图像没有 Alpha 通道,则给定的 alpha 值将用于设置新的 Alpha 通道。如果未提供 alpha ,则 maxChannelValue 将用于设置 alpha。如果 withPalette 为 true,并且为目标格式且 numChannels 的颜色少于 256 种,则新图像将转换为使用调色板。
elementAt: 返回第 index 个元素。
every: 检查此迭代的每个元素是否满足 test 。
expand: 将此 Iterable 的每个元素扩展为零个或多个元素。
firstWhere: 满足给定谓词 test 的第一个元素。
fold: 通过迭代地将集合的每个元素与现有值组合,将集合减少为单个值
followedBy: 创建此可迭代对象和 other 的惰性串联。
forEach: 按迭代顺序调用此可迭代对象的每个元素上的 action 。
getBytes: 与 toUint8List 类似,但会将图像像素的通道转换为给定的 order 。如果发生这种情况,返回的字节将是副本,而不是图像数据的直接视图。如果 order 所需的通道数与图像所具有的通道数不同,则字节将来自转换后的图像。如果转换后的图像需要添加 Alpha 通道,则可以使用 alpha 参数指定添加的 Alpha 通道的值。
getColor: 使用图像的格式和通道数创建一个 Color 对象。
getExtraChannel: 
getFrame: 从此图像中获取一个框架。如果该图像不是动画的,则返回该图像;否则将返回特定帧图像。
getPixel: 返回给定坐标处的像素。如果提供了 pixel ,它将被更新并返回,而不是分配新的像素。
getPixelClamped: 从给定的 x 、 y 坐标获取像素。如果像素坐标超出图像范围,它们将被限制为分辨率。
getPixelCubic: 使用非整数像素坐标的三次插值获取像素。
getPixelIndex: 
getPixelInterpolate: 使用给定的 interpolation 类型获取非整数像素坐标的像素。
getPixelLinear: 使用非整数像素坐标的线性插值获取像素。
getPixelSafe: 从给定的 x 、 y 坐标获取像素。如果像素坐标超出范围,则返回 PixelUndefine。
getRange: 返回一个像素迭代器,用于迭代图像中矩形范围的像素。
hasExtraChannel: 
isBoundsSafe: 如果给定的像素坐标在图像的尺寸范围内,则返回 true。
join: 
lastWhere: 满足给定谓词 test 的最后一个元素。
map: 此可迭代对象的当前元素由 toElement 修改。
noSuchMethod: 当访问不存在的方法或属性时调用。
reduce: 通过使用提供的函数迭代组合集合的元素,将集合减少为单个值。
remapChannels: 将颜色通道重新映射到给定的 order 。通常,4 通道图像的图像颜色通道以 rgba 顺序存储,3 通道图像以 rgb 顺序存储。此方法允许您就地重新排列颜色通道,而无需克隆图像来准备图像数据以供需要备用通道排序的外部使用。
setExtraChannel: 
setPixel: 将给定坐标处的像素颜色设置为给定 Color c 的颜色。
setPixelIndex: 设置调色板图像的索引值,否则设置红色通道。
setPixelR: 设置像素的红色(或索引)颜色通道。
setPixelRgb: 将给定坐标处的像素颜色设置为给定颜色值 r 、 g 、 b 。
setPixelRgba: 将给定坐标处的 Pixel 颜色设置为给定颜色值 r 、 g 、 b 和 a 。
singleWhere: 满足 test 的单个元素。
skip: 创建一个提供除第一个 count 元素之外的所有元素的 Iterable。
skipWhile: 创建一个在满足 test 时跳过前导元素的 Iterable 。
take: 创建此可迭代对象的 count 第一个元素的惰性可迭代对象。
takeWhile: 创建满足 test 的前导元素的惰性迭代。
toList: 创建一个包含此 Iterable 元素的 List。
toSet: 创建一个包含与此迭代相同元素的 Set。
toString: 图像的字符串表示。
toUint8List: 获取图像存储数据的 Uint8List 视图。
where: 使用满足谓词 test 的所有元素创建一个新的惰性 Iterable。



图像处理

adjustColor: 调整颜色
billboard: 广告牌
bleachBypass: 漂白旁路
bulgeDistortion: 凸起失真
bumpToNormal: 
chromaticAberration: 色差
colorHalftone: 颜色半色调
colorOffset: 颜色偏移
contrast: 对比
convolution: 卷积
ditherImage: 抖动图像
dotScreen: 点屏
dropShadow: 阴影
edgeGlow: 边缘辉光
emboss: 浮雕
gamma: 伽马
gaussianBlur: 高斯模糊
grayscale: 灰度
hdrToLdr: hdr 至 Ldr
hexagonPixelate: 六边形像素化
invert: 反转
luminanceThreshold: 亮度阙值
monochrome: 单色
noise: 噪音
normalize: 正常化
pixelate: 像素化
quantize: 量化
reinhardTonemap: reinhard色调映射
remapColors: 重新映射颜色
scaleRgba:  
separableConvolution: 可分离卷积
sepia: 棕褐色
sketch: 草图
smooth: 光滑的
sobel: 索贝尔
SolarizeMode: 曝光过度
stretchDistortion: 拉伸失真
vignette: 小插图



图像变换

copyCrop: 复制裁剪
copyCropCircle: 复制圆形裁剪
copyFlip: 复制翻转
copyExpandCanvas: 复制扩展画布
copyRectify: 复制矫正
copyResize: 复制调整大小
copyResizeCropSquare: 复制方形裁剪
copyRotate: 复制旋转
bakeOrientation: 旋转图像
flip: 翻转图像
trim: 剪切图像
findTrim: 寻找图像的边缘

copyCrop: 复制裁剪

返回src的裁剪后的副本。

Image copyCrop(
  Image src, 
  {
    required int x,
    required int y,
    required int width,
    required int height,
    num radius = 0,
    bool antialias = true,
  }
)
image = img.copyCrop(
  image,
  x: 0,
  y: 0,
  width: 200,
  height: 200,
  radius: 50,
);

copyCropCircle: 复制圆形裁剪

返回src的圆形裁剪副本,以centerX和centerY居中,并具有给定的半径。

如果没有提供半径,则使用填充图像的半径。如果未提供centerX,则将使用图像的水平中点。如果未提供centerY,则将使用图像的垂直中点。

Image copyCropCircle(
  Image src, 
  {
    int? radius,
    int? centerX,
    int? centerY,
    bool antialias = true,
  }
)
image = img.copyCropCircle(
  image,
  centerX: 400,
  centerY: 400,
  radius: 100,
);

copyFlip: 复制翻转

返回src图像的副本,按给定方向翻转。

Image copyFlip(
  Image src, 
  {
    required FlipDirection direction,
  }
)
image = img.copyFlip(
  image,
  // direction: img.FlipDirection.horizontal,
  // direction: img.FlipDirection.vertical,
  direction: img.FlipDirection.both,
);

copyExpandCanvas: 复制扩展画布

返回src图像的副本,其中原始图像被放置在指定位置的指定大小的新画布上,并且画布的其余部分用指定的颜色填充,如果没有提供颜色,则使用透明填充。

Image copyExpandCanvas(
  Image src, 
  {
    int? newWidth,
    int? newHeight,
    int? padding, // 宽高和padding不能同时设置
    ExpandCanvasPosition position = ExpandCanvasPosition.center,
    Color? backgroundColor,
    Image? toImage,
  }
)
image = img.copyExpandCanvas(
  image,
  // newWidth: 1000,
  // newHeight: 1000,
  padding: 100,
  backgroundColor: img.ColorRgb8(150, 100, 55),
);

copyRectify: 复制矫正

返回src图像的副本,其中给定的矩形已映射到完整的图像。

Image copyRectify(
  Image src, 
  {
    required Point topLeft,
    required Point topRight,
    required Point bottomLeft,
    required Point bottomRight,
    Interpolation interpolation = Interpolation.nearest,
    Image? toImage,
  }
)
image = img.copyRectify(
  image,
  topLeft: img.Point(50, 50),
  topRight: img.Point(500, 0),
  bottomRight: img.Point(700, 600),
  bottomLeft: img.Point(0, 200),
);

copyResize: 复制调整大小

返回src图像的调整大小的副本。

如果没有指定高度,那么它将由src和width的长宽比决定。如果没有指定宽度,那么它将由src和height的宽高比决定。

Image copyResize(
  Image src, 
  {
    int? width,
    int? height,
    bool? maintainAspect, // 是否保持宽高比
    Color? backgroundColor,
    Interpolation interpolation = Interpolation.nearest, // 插值
  }
)
image = img.copyResize(
  image,
  width: 200,
  height: 100,
  maintainAspect: true,
  backgroundColor: img.ColorRgb8(150, 100, 55),
);

copyResizeCropSquare: 复制方形裁剪

返回大小为size的src图像的调整大小和正方形裁剪的副本。

Image copyResizeCropSquare(
  Image src, 
  {
    required int size,
    Interpolation interpolation = Interpolation.nearest,
    num radius = 0,
    bool antialias = false,
  }
)
image = img.copyResizeCropSquare(
  image,
  size: 200,
  radius: 100,
);

copyRotate: 复制旋转

返回src图像的副本,按角度旋转。

Image copyRotate(
  Image src, 
  {
    required num angle,
    Interpolation interpolation = Interpolation.nearest,
  }
)
image = img.copyRotate(
  image,
  angle: 45,
);

bakeOrientation: 旋转图像

如果图像的exif数据中有一个方向值,这将旋转图像,使其在物理上与其方向匹配。这可以用于为不支持exif数据的图像格式烘烤图像的方向。

Image bakeOrientation( Image image )

flip: 翻转图像

使用给定的方向翻转src图像,这可以是: FlipDirection.horizontal, FlipDirection.vertical, FlipDirection.both.

Image flip(
  Image src, 
  {
    required FlipDirection direction,
  }
)
img.flip(
  image,
  // direction: img.FlipDirection.horizontal,
  // direction: img.FlipDirection.vertical,
  direction: img.FlipDirection.both,
);

trim: 剪切图像

通过找到符合模式标准(不透明或不同颜色)的图像角来自动裁剪图像。

mode为 TrimMode.transparent, TrimMode.topLeftColor , TrimMode.bottomRightColor.

sides可用于控制图像的哪些边被修剪,并且可以是Trim的任意组合。Trim.top, Trim.bottom, Trim.left, Trim.right.

Image trim(
  Image src, 
  {
    TrimMode mode = TrimMode.topLeftColor,
    Trim sides = Trim.all,
  }
)
img.trim(
  image,
  mode: img.TrimMode.bottomRightColor,
  sides: img.Trim.right,
);

findTrim: 寻找图像的边缘

找到修剪函数要使用的裁剪区域。返回坐标[x, y, width, height]。您可以将这些坐标传递给copyCrop函数来裁剪图像。

List<int> findTrim(
  Image src, 
  {
    TrimMode mode = TrimMode.transparent,
    Trim sides = Trim.all,
  }
)
img.findTrim(
  image,
  mode: img.TrimMode.bottomRightColor,
  sides: img.Trim.right,
);

绘制函数

compositeImage: 图像合成
drawChar: 绘制字符
drawCircle: 绘制圆
drawLine: 绘制线
drawPixel: 绘制像素
drawPolygon: 绘制多边形
drawRect: 绘制矩形
drawString: 绘制字符串
fill: 填充
fillCircle: 填充圆
fillFlood: 漫水填充
fillPolygon: 填充多边形
fillRect: 填充矩形

compositeImage: 图像合成

将图像 src 合成到图像 dst 上

在位置(srcX,srcY)处从src取一个宽度为srcW,高度为srcH的矩形区域,并将其放置在位置(dstX,dstY)处宽度为dstW,高度为dstH的矩形区域中。

如果源和目标坐标、宽度和高度不一致,将对图像片段进行适当的拉伸或收缩。坐标是指左上角。此函数可用于复制同一图像中的区域(如果dst与src相同),但如果区域重叠,则结果将不可预测。

如果center为true, src 将以 dst 居中。

Image compositeImage(
  Image dst,
  Image src,
  {
    int? dstX,
    int? dstY,
    int? dstW,
    int? dstH,
    int? srcX,
    int? srcY,
    int? srcW,
    int? srcH,
    BlendMode blend = BlendMode.alpha,      // 混合模式
    bool linearBlend = false,               // 线性混合
    bool center = false,                    // 是否居中
    Image? mask,                            // 蒙版,只有白色部分会被渲染
    Channel maskChannel = Channel.luminance // 蒙版通道
  }
)
img.compositeImage(
  image,
  watermark,
  dstX: 50,
  dstY: 50,
  dstW: 100,
  dstH: 50,
  // srcW: 100,
  // srcH: 100,
  // srcX: 50,
  // srcY: 50,
  // center: true,
  // blend: img.BlendMode.softLight,
  // linearBlend: true,
  // mask: mask,
);

drawChar: 绘制字符

用给定的颜色水平地绘制一个字符到位置x,y的图像。

Image drawChar(
  Image image,
  String char,
  {
    required BitmapFont font,
    required int x,
    required int y,
    Color? color,
    Image? mask,
    Channel maskChannel = Channel.luminance,
  }
)
img.drawChar(
  image,
  'A',
  x: 50,
  y: 50,
  font: arial24
);
img.drawChar(
  image,
  'B',
  x: 100,
  y: 100,
  font: arial24
);
img.drawChar(
  image,
  'C',
  x: 150,
  y: 150,
  font: arial24
);

drawCircle: 绘制圆

在图像中画一个圆心为x,y和给定半径和颜色的圆。

Image drawCircle(
  Image image, 
  {
    required int x,
    required int y,
    required int radius,
    required Color color,
    bool antialias = false, // 抗锯齿
    Image? mask,
    Channel maskChannel = Channel.luminance,
  }
)
img.drawCircle(
  image,
  x: 300,
  y: 300,
  radius: 300,
  color: img.ColorRgb8(255, 0, 0),
);

drawLine: 绘制线

在图像上画一条线。

如果抗锯齿为 true,那么线条的边缘就会平滑。厚度决定线条的粗细,以像素为单位。

Image drawLine(
  Image image, 
  {
    required int x1,
    required int y1,
    required int x2,
    required int y2,
    required Color color,
    bool antialias = false, // 抗锯齿
    num thickness = 1,
    Image? mask,
    Channel maskChannel = Channel.luminance,
  }
)
img.drawLine(
  image,
  x1: 50,
  y1: 50,
  x2: 500,
  y2: 500,
  thickness: 10,
  color: img.ColorRgb8(255, 0, 0),
);

drawPixel: 绘制像素

在图像中绘制单个像素,应用alpha和不透明度混合。

如果提供了过滤器,则颜色c将按过滤器颜色缩放。

如果提供了alpha,它将被用来代替颜色alpha,作为标准化的颜色值[0,1]。

Image drawPixel(
  Image image,
  int x,
  int y,
  Color c, 
  {
    Color? filter,
    num? alpha,
    BlendMode blend = BlendMode.alpha,
    bool linearBlend = false,
    Image? mask,
    Channel maskChannel = Channel.luminance,
  }
)
img.drawPixel(
  image,
  50,
  50,
  img.ColorRgb8(255, 0, 0),
);
img.drawPixel(
  image,
  60,
  60,
  img.ColorRgb8(255, 0, 0),
);
img.drawPixel(
  image,
  70,
  70,
  img.ColorRgb8(255, 0, 0),
);

drawPolygon: 绘制多边形

填充由给定顶点定义的多边形。

Image drawPolygon(
  Image src, 
  {
    required List<Point> vertices,
    required Color color,
    bool antialias = false,
    num thickness = 1,
    Image? mask,
    Channel maskChannel = Channel.luminance,
  }
)
img.drawPolygon(
  image,
  vertices: [
    img.Point(50, 50),
    img.Point(300, 50),
    img.Point(300, 200),
  ],
  color: img.ColorRgb8(255, 0, 0),
  thickness: 10,
);

drawRect: 绘制矩形

用颜色在图片中画一个矩形。

Image drawRect(
  Image dst, 
  {
    required int x1,
    required int y1,
    required int x2,
    required int y2,
    required Color color,
    num thickness = 1,
    num radius = 0, // 设置了圆角半径之后,thickness 将被重置为1
    Image? mask,
    Channel maskChannel = Channel.luminance,
  }
)
img.drawRect(
  image,
  x1: 50,
  y1: 50,
  x2: 500,
  y2: 500,
  color: img.ColorRgb8(255, 0, 0),
  thickness: 50,
  radius: 50,
);

drawString: 绘制字符串

用给定的颜色在图像x,y位置水平地绘制一个字符串。如果未指定x,则字符串将水平居中。如果未指定y,则字符串将垂直居中。

您可以加载自己的字体,或者使用现有的字体之一,如:arial14、arial24或arial48。字体可以创建的工具,如:https://ttf2fnt.com/

Image drawString(
  Image image,
  String string, 
  {
    required BitmapFont font,
    int? x,
    int? y,
    Color? color,
    bool rightJustify = false,
    bool wrap = false,
    Image? mask,
    Channel maskChannel = Channel.luminance,
  }
)
img.drawString(
  image,
  'This is a test string, it should be wrapped.',
  // x: 50,
  // y: 50,
  font: img.arial48,
  color: img.ColorRgb8(255, 255, 255),
  // rightJustify: true,
  wrap: true,
);

fill: 填充

将图像的所有像素设置为给定的颜色。

Image fill(
  Image image, 
  {
    required Color color,
    Image? mask,
    Channel maskChannel = Channel.luminance,
  }
)
img.fill(
  image,
  color: img.ColorRgb8(255, 255, 255),
  mask: mask,
);

fillCircle: 填充圆

Image fillCircle(
  Image image, 
  {
    required int x,
    required int y,
    required int radius,
    required Color color,
    bool antialias = false,
    Image? mask,
    Channel maskChannel = Channel.luminance,
  }
)
img.fillCircle(
  image,
  x: 300,
  y: 300,
  radius: 300,
  color: img.ColorRgb8(255, 255, 255),
);

fillFlood: 漫水填充

用给定的颜色填充图像src中包含x,y的4连通形状。

Image fillFlood(
  Image src, 
  {
    required int x,
    required int y,
    required Color color,
    num threshold = 0.0,
    bool compareAlpha = false,
    Image? mask,
    Channel maskChannel = Channel.luminance,
  }
)
img.fillFlood(
  image,
  x: 30,
  y: 30,
  color: img.ColorRgb8(255, 0, 0),
  threshold: 35,
);

fillPolygon: 填充多边形

填充由给定顶点定义的多边形。

Image fillPolygon(
  Image src, 
  {
    required List<Point> vertices,
    required Color color,
    Image? mask,
    Channel maskChannel = Channel.luminance,
  }
)
img.fillPolygon(
  image,
  vertices: [
    img.Point(50, 50),
    img.Point(300, 50),
    img.Point(300, 200),
  ],
  color: img.ColorRgb8(255, 0, 0),
);

fillRect: 填充矩形

Image fillRect(
  Image src, 
  {
    required int x1,
    required int y1,
    required int x2,
    required int y2,
    required Color color,
    num radius = 0,
    bool alphaBlend = true,
    Image? mask,
    Channel maskChannel = Channel.luminance,
  }
)
img.fillRect(
  image,
  x1: 50,
  y1: 50,
  x2: 500,
  y2: 500,
  color: img.ColorRgb8(255, 0, 0),
  radius: 50,
);

BlendMode 混合模式

direct: 直接
alpha: 
lighten: 变亮
screen: 屏幕
dodge: 闪避
addition: 加法
darken: 变暗
multiply: 乘法
burn: 刻录
overlay: 覆盖
softLight: 柔光
hardLight: 硬光
difference: 差异
subtract: 减去
divide: 除法

...未完待续...

《Flutter Package:图片处理库 Image》留言数:0

发表留言