在flutter / dart-ui中将UInt8List转换为Image [英] Convert a UInt8List to Image in flutter/dart-ui

查看:1538
本文介绍了在flutter / dart-ui中将UInt8List转换为Image的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种情况下,我必须从这样的图像中获取UInt8List:

I have this scenario where I have to get a UInt8List from an image like that:

List stuff = image.toByteData().buffer.asUInt8List()

进行一些操作并返回到图片

Do some manipulations and get back to a Image.

我尝试了以下操作:

List stuff = image.toByteData().buffer.asUInt8List()
ui.decodeImageFromList(stuff, (image){
  // do stuff with image
});

但我一直收到此异常:

E/flutter (10201): [ERROR:flutter/lib/ui/painting/codec.cc(97)] Failed decoding image. Data is either invalid, or it is encoded using an unsupported format.
E/flutter (10201): [ERROR:flutter/shell/common/shell.cc(186)] Dart Error: Unhandled exception:
...

请注意,即使List中没有任何更改,也会引发异常。如何使列表具有可编码的格式?

Take notice that even without any changes in the List the exception is thrown. How can I make the list have a encodable format?

推荐答案

ui.Image 可以将自身转换为RGBA位图(或PNG),但不能将其自身从位图转换回去。 (这样做的原因是,没有任何办法可以告诉图像编解码器诸如颜色深度,几何形状等的信息。)解决方案是在位图的前面添加一个BMP文件头,您可以在其中描述这些内容丢失的东西,然后将其传递给 instantiateImageCodec 。参见此答案,但请注意,在这种情况下,所讨论的位图具有奇怪的压缩色图。在您使用32位RGBA的情况下,标头会更简单。

ui.Image can turn itself into an RGBA bitmap (or PNG), but cannot convert itself back from a bitmap. (The reason for this is that there isn't any way to tell the image codec things like the color depth, geometry, etc.) The solution is to add a BMP file header onto the front of your bitmap, where you can describe those missing things, then pass that to instantiateImageCodec. See this answer, but note that in that case the bitmap in question had a strange packed color map. In your case of 32 bit RGBA, the header would be even simpler.

这篇关于在flutter / dart-ui中将UInt8List转换为Image的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆