如何在 Dart 中上传时压缩/减小图像大小?

Ami*_*ani 9 dart flutter

如何在 Dart 中上传时压缩/减小图像大小?我正在尝试上传 5MB 的图像,但我想在上传时减小其大小。我正在使用 Dart 和 Flutter。

ABa*_*bin 6

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)

  • 这导致应用程序冻结。 (3认同)