Dart 图像库使服务器和 Web 应用程序能够加载、操作和保存多种格式的图像。它支持常见的操作,如调整大小、裁剪、颜色调整和格式转换,使其非常适合在不依赖原生平台工具的情况下进行图像处理任务。该库轻量且专为在 Android、iOS、Web、macOS、Windows 和 Linux 上的 Flutter 应用中使用而设计。它特别适用于后端图像转换或动态生成缩略图。
适合场景
图像调整大小和裁剪
缩略图生成
图像格式转换
服务器端图像处理
网页应用中的动态图像操作
主要特点
支持多种图像格式
跨平台兼容性
轻便且快速
内置图像滤镜
可扩展的图像处理流水线
适合对象
后端图像处理
基于网络的图像工具
需要图像处理的Flutter应用
自动化图像工作流程
开发图像编辑功能的开发者
使用建议
未针对实时UI渲染进行优化
对高级图像效果的支持有限
需要手动处理大文件
无内置缓存
最适合非交互式图像任务
原始 README
以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。
Dart Image Library
Overview
The Dart Image Library provides the ability to load, save, and
manipulate images
in a variety of image file formats.
The library can be used with both dart:io and dart:html, for command-line, Flutter, and
web applications.
NOTE: 4.0 is a major revision from the previous version of the library.
Create an image, set pixel values, save it to a PNG.
import'dart:io';
import'package:image/image.dart'as img;
voidmain() async {
// Create a 256x256 8-bit (default) rgb (default) image.final image = img.Image(width: 256, height: 256);
// Iterate over its pixelsfor (var pixel in image) {
// Set the pixels red value to its x position value, creating a gradient.
pixel..r = pixel.x
// Set the pixels green value to its y position value.
..g = pixel.y;
}
// Encode the resulting image to the PNG image format.final png = img.encodePng(image);
// Write the PNG formatted data to a file.awaitFile('image.png').writeAsBytes(png);
}
To asynchronously load an image file, resize it, and save it as a thumbnail:
import'package:image/image.dart'as img;
voidmain(List<String> args) async {
final path = args.isNotEmpty ? args[0] : 'test.png';
final cmd = img.Command()
// Decode the image file at the given path
..decodeImageFile(path)
// Resize the image to a width of 64 pixels and a height that maintains the aspect ratio of the original.
..copyResize(width: 64)
// Write the image to a PNG file (determined by the suffix of the file path).
..writeToFile('thumbnail.png');
// On platforms that support Isolates, execute the image commands asynchronously on an isolate thread.// Otherwise, the commands will be executed synchronously.await cmd.executeThread();
}