如何在Flutter中将图像转换为Base64

时间:2020-04-23 08:50:45

标签: flutter dart base64

我正在尝试将图像转换为base64,但结果无法作为图像查看。
这是我的下面的代码。

File image = File(path);
final bytes = image.readAsBytesSync();
print(base64Encode(bytes));

结果:

/9j/4Q+WRXhpZgAATU0AKgAAAAgACgEPAAIAAAAFAAAAhgEQAAIAAAAKAAAAjAEaAAUAAAABAAAAlgEbAAUAAAABAAAAngEoAAMAAAABAAIAAAExAAIAAABAAAAApgEyAAIAAAAUAAAA5gITAAMAAAABAAEAAIdpAAQAAAABAAAA+oglAAQAAAABAAAC3gAAAvh2aXZvAAB2aXZvIDE3MjMAAAAASAAAAAEAAABIAAAAAW1zbTg5NTNfNjQtdXNlciA5IFBLUTEuMTkwMTE4LjAwMSBjb21waWxlcjA0MDEyMDI1IHJlbGVhc2Uta2V5cwAyMDIwOjA0OjIzIDE1OjM3OjMyAAAcgpoABQAAAAEAAAJQgp0ABQAAAAEAAAJYiCIAAwAAAAEAAAAAiCcAAwAAAAEClgAAkAAABwAAAAQwMjIwkAMAAgAAABQAAAJgkAQAAgAAABQAAAJ0kQEABwAAAAQBAgMAkgEACgAAAAEAAAKIkgIABQAAAAEAAAKQkgMACgAAAAEAAAKYkgcAAwAAAAEAAQAAkgkAAwAAAAEAAAAAkgoABQAAAAEAAAKgkpAAAgAAAAcAAAKokpEAAgAAAAcAAAKwkpIAAgAAAAcAAAK4oAAABwAAAAQwMTAwoAEAAwAAAAEAAQAAoAIABAAAAAEAAAHgoAMABAAAAAEAAAKAoAUABAAAAAEAAAK/ohcAAwAAAAEAAgAAowEABwAAAAEBAAAApAIAAwAAAAEAAAAApAMAAwAAAAEAAAAApAUAAwAAAAEAHQAApAYAAwAAAAEAAAAAAAAAAAAAAAoAAAD6AAAAyAAAAGQyMDIwOjA0OjIzIDE1OjM3OjMyADIwMjA6MDQ6MjMgMTU6Mzc6MzIAAAASIwAAA+gAAADIAAAAZAAAAAAAAABkAAABfgAAAGQ0OTc4OTYAADQ5Nzg5NgA6NDk3ODk2AAACAAEAAgAAAARSOTgAAAIABwAAAAQwMTAwAAAAAAAAAQAFAAUAAAABAAAC8AAAAAAAAAD

但是上面的结果无法显示为图像。
我用这些网站来检查
codebeautify.org
base64decode

这个问题有解决方案吗?

2 个答案:

答案 0 :(得分:2)

Print函数不会打印所有内容
如果您需要复制此长字符串,则可以设置断点并在Variables中查看
您可以在

下看到图片

enter image description here

答案 1 :(得分:1)

您如何尝试查看图像?

base-64字符串不是有效的图像格式。

您可以使用Uri.dataFromBytes(bytes, mimeType: "image/jpg")创建包含图像的data: URI。 URI的有效负载将为您进行base-64编码。 如果您将该URI用作浏览器中src元素的img,则应将其显示为图像。