如何在 Dart 中上传时压缩/减小图像大小?我正在尝试上传 5MB 的图像,但我想在上传时减小其大小。我正在使用 Dart 和 Flutter。
pub 上有一个图像处理包。它甚至还包含一个确切说明您想要做什么的示例。
import 'dart:io' as Io;
import 'package:image/image.dart';
void main() {
// Read an image from file (webp in this case).
// decodeImage will identify the format of the image and use the appropriate
// decoder.
Image image = decodeImage(new Io.File('test.webp').readAsBytesSync());
// Resize the image to a 120x? thumbnail (maintaining the aspect ratio).
Image thumbnail = copyResize(image, 120);
// Save the thumbnail as a PNG.
new Io.File('thumbnail.png')
..writeAsBytesSync(encodePng(thumbnail));
}
对于 Dart
使用图片包:
使用此包,您可以加载图像、调整其大小并将其另存为 png:
首先,将
image
添加为 pubspec.yaml 文件中的依赖项。
用法:
import 'dart:io';
import 'package:image/image.dart';
void main() {
// Read an image from file (webp in this case).
// decodeImage will identify the format of the image and use the appropriate
// decoder.
Image image = decodeImage(File('test.webp').readAsBytesSync());
// Resize the image to a 120x? thumbnail (maintaining the aspect ratio).
Image thumbnail = copyResize(image, width: 120);
// Save the thumbnail as a PNG.
File('thumbnail.png')..writeAsBytesSync(encodePng(thumbnail));
}
如果您想在 flutter 中使用此图像,请不要使用它。使用其他解决方案,例如 flutter_image_compress 或 ResizeImage 类。
Q:Dart 已经有图像压缩库了。为什么要使用原生?
A:由于未知的原因,Dart 语言中的图像压缩效率不高,即使在发布版本中也是如此。使用隔离并不能解决问题 问题。
我做了这个功能来缩小图像尺寸,希望这对你们有帮助
import 'package:image/image.dart' as IMG;
Future<Uint8List> ReduceImageQualityAndSize(Uint8List image) async {
int maxInBytes = 20000;
Uint8List resizedData = Uint8List.fromList(image);
IMG.Image? img = await IMG.decodeImage(image);
int size = await image.lengthInBytes;
int quality = 100;
print("size max: " + maxInBytes.toString());
print("size before: " + size.toString() + " bytes");
while (size > maxInBytes && quality > 10) {
// reduce image size about 10% of image, until the size is less than the maximum limit
quality = (quality - (quality * 0.1)).toInt();
int width = img!.width - (img.width * 0.1).toInt();
IMG.Image resized = await IMG.copyResize(img, width: width);
resizedData =
await Uint8List.fromList(IMG.encodeJpg(resized, quality: quality));
size = await resizedData.lengthInBytes;
img = resized;
}
print("size after: " + size.toString() + " bytes");
return resizedData;
}
此函数会导致 UI 冻结,因此使用 IsolateWorker
是最佳选择import 'package:isolated_worker/isolated_worker.dart';
await IsolatedWorker()
.run(ReduceImageQualityAndSize, imageUint8List)
.then((value) {
imageUint8List = value;
});
您可以尝试使用 flutter_native_image 包。您可以轻松压缩图像
示例:
File compressedFile = await FlutterNativeImage.compressImage(file!.path,
quality: 20, percentage: 60);
我曾经使用 dio 将文件上传到服务器上
套餐:
flutter_native_image:^0.0.6+1
dio:^4.0.6
代码->
File? compressedFile = profileImage.value == null
? null
: await FlutterNativeImage.compressImage(
profileImage.value?.path ?? '',
quality: 20,
percentage: 60);
然后
if (compressedFile != null)
'profile_pic': await dio.MultipartFile.fromFile(compressedFile.path),
灵感来自Jordan Kotiadis
有一种方法可以确保图片不会太大, 将图片放入小部件中,在应用程序中截图,然后保存,对我来说效果很好
希望它也适合你