Dart 图像到本地存储中的文件

Dart Image to File in local storage

我正在使用包 image 中的 Image class。我必须从互联网上下载图像并将其保存在我的本地存储中。然后修改此图像并保存更改。我已经完成了前 2 个步骤,但是当我保存文件时,它已损坏并且图像查看器无法将其识别为图像文件。这是更改后保存图像的代码。

  var response = await client.get(urlToImage);
  var img = await File('./network_image.jpg').writeAsBytes(response.bodyBytes);
  Image image = decodeImage(img.readAsBytesSync())

  var f = await File('./image.jpg').writeAsBytes(image.getBytes()); // this doesnt work
  // var f = await File('./image.jpg').writeAsBytes(image.data); // this doesnt work

File.writeAsBytes 期望 List<int> bytesImage.dataImage.getBytes() returns Uint32ListUint8List 分别。

我没有使用 Flutter,只为命令行程序使用 dart。

图像对象以未编码格式存储图像数据。记住你用 decodeImage 解码了它。它存储图像的原始颜色。您需要重新编码图像数据,或者您的图像查看器需要处理任何图像格式的数据 getBytes returns.

更简单的解决方案可能是重新编码。你似乎想要 JpegEncoder.

var f = await File('./image.jpg').writeAsBytes(JpegEncoder().encodeImage(image));