如何在 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));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21322 次 |
| 最近记录: |