当前位置:网站首页>【Flutter 问题系列第 71 篇】Flutter 中 Uint8List 和 Image 之间的相互转换
【Flutter 问题系列第 71 篇】Flutter 中 Uint8List 和 Image 之间的相互转换
2022-06-28 22:58:00 【Allen Su】
这是【Flutter 问题系列第 71 篇】,如果觉得有用的话,欢迎关注专栏。
Flutter 版本:2.5.0,Dart 版本:2.14
因为最近一直在处理照相机、裁剪图片和 OCR 图片识别的功能,所以频繁的和 Image、Uint8List 打交道。
这篇博客把工作中用到的两者相互转换的方法总结下,方便大家查看。
需要先引入
- import ‘dart:ui’ as ui;
- import ‘dart:typed_data’;
一:Uint8List 转 Image
Uint8List 转 Image 提供了两种方式,如下代码所示
/// Uint8List 转 Image 方式一
ui.Image _fun1(Uint8List list) {
late ui.Image image;
ui.decodeImageFromList(list, (ui.Image callBack) {
image = callBack;
});
return image;
}
/// Uint8List 转 Image 方式二
Future<ui.Image> _fun2(Uint8List list) async {
ui.Codec codec = await ui.instantiateImageCodec(list);
ui.FrameInfo frame = await codec.getNextFrame();
return frame.image;
}
二:Image 转 Uint8List
如下代码所示
Future<Uint8List?> _fun(ui.Image image) async {
ByteData? byteData = await image.toByteData();
if (byteData != null) {
Uint8List bytes = byteData.buffer.asUint8List();
return bytes;
}
return null;
}
拿走直接用就行了,没什么好说的。
你的问题得到解决了吗?欢迎在评论区留言。
赠人玫瑰,手有余香,如果觉得文章不错,希望可以给个一键三连,感谢。
Google 的 Flutter 越来越火,截止 2022年6月28日 GitHub 标星已达 142K,Flutter 毅然是一种趋势,所以作为前端开发者,没有理由不趁早去学习。
结束语
无论你是 Flutter 新手还是已经入门了,不妨先点个关注,后续我会将 Flutter 中的常用组件(含有源码分析、组件的用法及注意事项)以及可能遇到的问题写到 CSDN 博客中,希望自己学习的同时,也可以帮助更多的人。
边栏推荐
- How powerful is the Zadig build? Practice together
- Qtcreater5.15.0 source code compilation process record
- Is it safe to open a stock account online?
- 如何使用伦敦金画出支撑阻力线
- With the development of industrial Internet as the starting point, the industry can enter a new stage of development
- 浅析搭建校园在线教学视频汇聚平台的必要性及解决方案
- WMS仓库管理系统模块之波次拣货
- Go language - reflect
- Steady! How thousands of micro services can quickly access Zadig (helm chart)
- 云计算的迷路者
猜你喜欢

00 後雲原生工程師:用 Zadig 為思創科技(廣州公交)研發開源節流

论文解读(DCN)《Towards K-means-friendly Spaces: Simultaneous Deep Learning and Clustering》

Online sql to htmltable tool

Windows mysql5.7 enable binlog log

Considerations on the construction of operation and maintenance system - stability

Jointly explore digital technology and information security, and the fourth China Russia Digital Forum was successfully held

如何使用伦敦金画出支撑阻力线

LeCun预言AGI:大模型和强化学习都是斜道!我的世界模型才是新路

Interpretation of papers (DCN) towards k-means-friendly spaces: simultaneous deep learning and clustering

CS5463代码模块解析(包含下载链接)
随机推荐
Is it safe and reliable to open a securities account in changtou school?
见丰知夏|国漫鼻祖丰子恺,数字藏品独家发售
Panxiaoming, senior vice president of IC nansha|amd and President of Greater China: process, architecture and platform optimization break through the computing boundary
hiredis的代码示例
Google Earth engine (GEE) -- crop extraction and analysis using sentinel-2 data
微搭低代码中实现二维码生成
Ansible production environment usage scenario (7): batch deployment of elk clients
TCP three handshakes and four waves
How powerful is the Zadig build? Practice together
在QT进行cin(全网最清晰教程)
Complex nested object pool (4) -- manage the object pool of multi class instances and multi-stage instances
After crossing, she said that the multiverse really exists
On the necessity and solution of building a campus online teaching video convergence platform
torch.nn.Transformer导入失败
LeetCode 324 擺動排序 II[排序 雙指針] HERODING的LeetCode之路
Zadig officially launched vs code plug-in, making local development more efficient
【HackTheBox】 meow
[kotlin] beautiful pop-up box, custom pop-up box (dialog box), extension function, chrysanthemum waiting bar, message prompt box
邂逅阿维塔 11:强产品力下久违的新鲜感
论文解读(DCN)《Towards K-means-friendly Spaces: Simultaneous Deep Learning and Clustering》