flutter - Flutter:如何将字节转换为文件以便将文件上传到 Firestore
问题描述
我正在尝试用颤动的文本为图像加水印,然后需要将该加水印的图像上传到 Firestore。当我尝试为字节添加水印并将字节存储到文件时,我收到了一个错误。
File: '����
请帮我解决这个问题。谢谢!
这是代码部分。
_originalImage = ui.decodeImage(croppedFile.readAsBytesSync());
ui.drawString(_originalImage, ui.arial_24, 100, 120, 'Hello World');
// Store the watermarked image to a File
List<int> resImage = ui.encodeJpg(_originalImage);
print(resImage);
setState((){
_watermarkedImage = File.fromRawPath(Uint8List.fromList(wmImage));
print(_watermarkedImage);
});
imageFile = _watermarkedImage;
当我在将字节存储到文件之前打印结果时,我得到了以下字节值。
255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 255, 219, 0, 132, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 255, 192, 0, 17, 8, 2, 37, 2, 47, 3, 1, 17, 0, 2, 17, 1, 3, 17, 1, 255, 196, 1, 162, 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 16, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 125, 1, 2, 3, 0, 4, 17, 5, 18, 33, 49, 65, 6, 19, 81, 97, 7, 34, 113, 20, 50, 129, 145, 161, 8, 35, 66, 177, 193, 21, 82, 209, 240, 36, 51, 98, 114, 130, 9, 10, 22, 23, 24, 25, 26, 37, 38, 39, 40, 41, 42, 52, 53, 54, 55, 56, 57, 58, 67, 68, 69, 70, 71, 72, 73, 74, 83, 84, 85, 86, 87, 88, 89, 90, 99, 100, 101, 102, 10
解决方案
正如 Antonin 所建议的,您需要加载文件路径。使用它将您的字节存储在临时文件中,然后加载文件
final directory = await getTemporaryDirectory();
final filepath = "abc.png";
File imgFile = File(filepath);
imgFile.writeAsBytes(bytes); //your image bytes
现在使用该文件
推荐阅读
- javascript - 如何创建可滚动的覆盖内容?
- matplotlib - 跨文件循环以将来自不同数据集的轮廓叠加到一张 pcolor 地图上
- javascript - 剑道网格插入带有自定义类的新行
- r - 使用 unlist() 后,我的有 63 个观察值的列表变成了一个有 65 个观察值的向量
- typescript - 设置对象各个属性的函数的正确类型?
- arrays - Arrays.deepToString 和 Scala 数组
- reactjs - 使用 React 创建搜索栏的问题
- django - 在 aws-ec2 上部署 django 应用程序后无法加载静态文件
- ios - 在 ScrollView 中居中大视图以避免拆分视图
- machine-learning - 如何从 Fastai 学习器语言模型中的编码器获取句子嵌入