【问题标题】:How to convert Image object to File object in Flutter?如何在 Flutter 中将 Image 对象转换为 File 对象?
【发布时间】:2021-08-05 00:07:19
【问题描述】:

这是图像对象:

PickedFile imageFile = await ImagePicker().getImage(source: ImageSource.gallery,maxWidth: 1000,imageQuality: 100);
Image rrimage = Image(image: ResizeImage(FileImage(File(imageFile.path)),height: 224,width: 224));

我想将 Image 对象 (rrimage) 转换为 File 对象。该怎么做?

【问题讨论】:

  • 这能回答你的问题吗? How to convert Image to File in Flutter?
  • @AlexHartford 在这个问题中不,图像是一种资产。就我而言,它不是资产。请检查我编辑的代码。
  • 为什么不使用image 包:pub.dev/packages/image 头版上的示例似乎是您想要做的:读取文件,调整大小,将其作为文件存储回来。跨度>
  • @RichardHeap 是的,我确实尝试过使用该方法。但是由于某种原因,copyResize 函数会降低图像的质量。 ResizeImage 类可以完美地调整大小。并且这个 ResizeImage 对象不能被强制转换为这个图像包的 Image 类。只能转换为原生 Image 小部件类的 Image 类。

标签: flutter dart


【解决方案1】:

更新

File('out/thumbnail-test.png') ..writeAsBytesSync(encodePng(rrimage));

【讨论】:

  • 是的,但我也想使用 ResizeImage 这就是问题
  • 为什么要精确调整图像大小?
  • @ShowTime 我需要 224x224 图像才能在机器学习模型上运行它
  • 那么在这种情况下,您可以在运行模型之前在 python 中调整图像大小。这对你有用吗?
  • 我试过了,但它抛出了一个错误。对于 encodePng 方法,我们必须导入“image”包。问题中使用的 rrimage 是 Image 小部件的一个实例。而 encodePng 函数期望输入是图像包的 Image 对象。
猜你喜欢
  • 2016-11-25
  • 2020-09-30
  • 2017-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-14
  • 2012-09-15
  • 2021-12-03
相关资源
最近更新 更多