如何在 Dart 中上传时压缩/减小图像大小?我正在尝试上传一张 5MB 的图片,但我想在上传时减小它的大小。我正在使用 Dart 和 Flutter。
最佳答案
有一个image manipulation package on 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 - 在 Dart 中上传时如何压缩/减小图像大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50543060/