Flutter图像处理插件image_magick_q16_hdri的使用
Flutter图像处理插件image_magick_q16_hdri的使用
目录
贡献者
特别感谢Piero512作为“FFI大师”,为这个插件提供了很多帮助。
ImageMagickFFI插件
该插件将ImageMagick C库MagickWand带到dart中使用。
感受原生
像在C语言中一样与底层的ImageMagick C API交互(当然不是用指针)。
能做什么
以下是一些你可以使用该插件做的事情,以及可以用来完成这些事情的函数名称。
查看下面的使用部分获得更多见解。
变体
该插件仅提供ImageMagick的Q16-HDRI变体。如果你想使用其他变体,则需要使用相应的包。
Windows
支持Windows x64 (32位) 和 Windows x86 (32位)。
Android
目前仅支持arm64-v8a (64位)。如果你希望添加对armeabi-v7a (32位)的支持,请参阅这里。
请注意,某些操作可能需要从系统获取写入权限,例如写入图像。
Linux
即将推出。
MacOS
欢迎提供二进制文件。
iOS
欢迎提供二进制文件。
使用
在Flutter应用中
初始化MagickWand
@override
void initState() {
_wand = MagickWand.newMagickWand(); // 创建一个MagickWand来编辑图像
// 设置一个回调,当图像处理进度改变时调用
WidgetsBinding.instance.addPostFrameCallback(
(timeStamp) async => await _wand.magickSetProgressMonitor(
(info, offset, size, clientData) => setState(() =>
status = '[${info.split('/').first}, $offset, $size, $clientData]'),
),
);
super.initState();
}
使用MagickWand
// 读取图像,对其进行一些操作,然后保存
Future<String> _handlePress() async {
try {
setState(() => isLoading = true);
String? result;
await _wand.magickReadImage(_inputFile!.path); // 读取图像
_throwWandExceptionIfExists(_wand);
///////////////////////// 对Wand进行一些操作 /////////////////////////
// 调整图像大小
await _wand.magickAdaptiveResizeImage(1200, 800);
_throwWandExceptionIfExists(_wand);
// 翻转图像
await _wand.magickFlipImage();
_throwWandExceptionIfExists(_wand);
// 增强图像
await _wand.magickEnhanceImage();
_throwWandExceptionIfExists(_wand);
// 向图像添加噪声
await _wand.magickAddNoiseImage(NoiseType.GaussianNoise, 1.5);
_throwWandExceptionIfExists(_wand);
/////////////////////////////////////////////////////////////////////////////////
String outputFilePath = _getOutputFilePath();
await _wand.magickWriteImage(outputFilePath); // 将图像写入文件
_throwWandExceptionIfExists(_wand);
_outputFile = File(outputFilePath);
isLoading = false;
return result ?? 'Operation Successful!';
} catch (e) {
_outputFile = null;
isLoading = false;
return 'Error: ${e.toString()}';
}
}
String _getOutputFilePath() {
final String ps = Platform.pathSeparator;
final String inputFileNameWithoutExtension =
_inputFile!.path.split(ps).last.split('.').first;
final String outputFilePath =
'${_outputDirectory!.path}${ps}out_$inputFileNameWithoutExtension.png';
return outputFilePath;
}
void _throwWandExceptionIfExists(MagickWand wand) {
MagickGetExceptionResult e = _wand.magickGetException(); // 获取异常
if (e.severity != ExceptionType.UndefinedException) {
throw e;
}
}
释放MagickWand和插件
@override
dispose() {
_wand.destroyMagickWand(); // 我们已经完成了Wand的操作
disposeImageMagick(); // 我们已经完成了整个插件的操作
super.dispose();
}
在纯Dart应用中
- 在
pubspec.yaml
中依赖插件,就像依赖其他包一样。 - 你还需要手动将依赖文件(.lib文件,.dll文件)复制到与可执行文件相同的路径下(现在这是在Dart中必须这样做的方式)。你可以通过构建一个Flutter应用,然后从那里复制依赖文件来获取这些文件。
然后你就可以正常使用插件了,例如:
import 'dart:io';
import 'package:image_magick_q16_hdri/image_magick_q16_hdri.dart';
Future<void> main(List<String> arguments) async {
final File inputFile1 = File("D:\\magick\\Screenshot.png");
final File inputFile2 = File("D:\\magick\\fayruz_love.png");
final File inputFile3 = File("D:\\magick\\untitled.png");
print('Magick Dart App Started!');
initializeImageMagick(); // 初始化插件
MagickWand wand1 = MagickWand.newMagickWand(); // 创建一个MagickWand
MagickWand wand2 = MagickWand.newMagickWand(); // 创建一个MagickWand
await wand1.magickReadImage(inputFile3.path);
_throwWandExceptionIfExists(wand1);
await wand2.magickReadImage(inputFile2.path);
_throwWandExceptionIfExists(wand2);
Stopwatch stopwatch = Stopwatch()..start();
///////////////////////////////// 使用MagickWand /////////////////////////////////
final imagePage = wand1.magickGetImagePage(); // 获取图像的尺寸
_throwWandExceptionIfExists(wand1);
final int width = imagePage!.width;
final int height = imagePage.height;
final int x = 0;
final int y = 0;
final cropWand = await wand1.magickGetImageRegion(
width: width ~/ 2,
height: height ~/ 2,
x: x,
y: y,
); // 从图像中裁剪出一个新的Wand
await cropWand!.magickWriteImage(getOutputFilePath(inputFile1.path));
_throwWandExceptionIfExists(wand2);
///////////////////////////////// 使用MagickWand /////////////////////////////////
print('elapsed time: ${stopwatch.elapsedMilliseconds} millis');
await wand1.destroyMagickWand(); // 释放MagickWand
await wand2.destroyMagickWand(); // 释放MagickWand
await cropWand.destroyMagickWand(); // 释放MagickWand
disposeImageMagick(); // 释放插件
print('Magick Dart App Ended!');
}
String getOutputFilePath(String inputFilePath) {
final String outputFilePath = inputFilePath.replaceAll(
RegExp(r'\.(png|jpg|jpeg|gif|bmp|tiff|tif|webp|pdf|ps|eps|svg|ico)$'),
'_output.png');
return outputFilePath;
}
Future<void> _throwWandExceptionIfExists(MagickWand wand) {
final exception = wand.magickGetException();
if (exception.severity != ExceptionType.UndefinedException) {
throw Exception(
'An exception occurred with the wand: ${exception.description}');
}
}
更多关于Flutter图像处理插件image_magick_q16_hdri的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter图像处理插件image_magick_q16_hdri的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
image_magick_q16_hdri
是一个基于 ImageMagick 的 Flutter 插件,允许你在 Flutter 应用中进行高级图像处理。ImageMagick 是一个功能强大的图像处理库,支持多种图像格式的读取、写入和操作。image_magick_q16_hdri
插件提供了 Flutter 与 ImageMagick 的绑定,使得你可以在移动端应用中使用 ImageMagick 的功能。
安装
首先,你需要在 pubspec.yaml
文件中添加 image_magick_q16_hdri
插件的依赖:
dependencies:
flutter:
sdk: flutter
image_magick_q16_hdri: ^0.0.1
然后运行 flutter pub get
来安装插件。
基本用法
以下是如何在 Flutter 中使用 image_magick_q16_hdri
插件进行图像处理的基本步骤。
-
导入包: 在你的 Dart 文件中导入
image_magick_q16_hdri
包:import 'package:image_magick_q16_hdri/image_magick_q16_hdri.dart';
-
加载图像: 使用
ImageMagick
类加载图像文件:final imageMagick = ImageMagick(); final imagePath = 'path/to/your/image.jpg'; final image = await imageMagick.loadImage(imagePath);
-
图像处理: 你可以使用 ImageMagick 提供的各种方法来处理图像,例如调整大小、裁剪、应用滤镜等。以下是一个简单的示例,展示如何调整图像大小:
final resizedImage = await imageMagick.resizeImage(image, width: 200, height: 200);
-
保存图像: 处理完图像后,你可以将其保存到文件中:
final outputPath = 'path/to/save/resized_image.jpg'; await imageMagick.saveImage(resizedImage, outputPath);
示例代码
以下是一个完整的示例,展示如何使用 image_magick_q16_hdri
插件加载图像、调整大小并保存:
import 'package:flutter/material.dart';
import 'package:image_magick_q16_hdri/image_magick_q16_hdri.dart';
void main() async {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: ImageProcessingScreen(),
);
}
}
class ImageProcessingScreen extends StatefulWidget {
[@override](/user/override)
_ImageProcessingScreenState createState() => _ImageProcessingScreenState();
}
class _ImageProcessingScreenState extends State<ImageProcessingScreen> {
String _processedImagePath;
[@override](/user/override)
void initState() {
super.initState();
_processImage();
}
Future<void> _processImage() async {
final imageMagick = ImageMagick();
final imagePath = 'path/to/your/image.jpg';
final image = await imageMagick.loadImage(imagePath);
final resizedImage = await imageMagick.resizeImage(image, width: 200, height: 200);
final outputPath = 'path/to/save/resized_image.jpg';
await imageMagick.saveImage(resizedImage, outputPath);
setState(() {
_processedImagePath = outputPath;
});
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Image Processing'),
),
body: Center(
child: _processedImagePath != null
? Image.file(_processedImagePath)
: CircularProgressIndicator(),
),
);
}
}