当前位置:网站首页>【Flutter 問題系列第 71 篇】Flutter 中 Uint8List 和 Image 之間的相互轉換
【Flutter 問題系列第 71 篇】Flutter 中 Uint8List 和 Image 之間的相互轉換
2022-06-28 22:59: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 博客中,希望自己學習的同時,也可以幫助更多的人。
边栏推荐
- Multiomics single cell data integration and regulatory reasoning based on graph linked embedding
- Differences among CPU, GPU, TPU and NPU
- Complex nested object pool (4) -- manage the object pool of multi class instances and multi-stage instances
- 论文解读(DCN)《Towards K-means-friendly Spaces: Simultaneous Deep Learning and Clustering》
- 自媒体行业内卷严重:企业自媒体应该何去何从
- 【Try to Hack】nmap
- 长投学堂帮忙开证券账户是安全靠谱的吗?个人如何开
- Oracle set password complexity and timeout exit function
- 深入虚拟内存(Virtual Memory,VM)
- Detailed steps for MySQL to recover data through IBD files
猜你喜欢

稳!上千微服务如何快速接入 Zadig(Helm Chart 篇)

Windows mysql5.7 enable binlog log

leetCode-栈类型详解
利用Redis实现点赞功能的示例代码

Ansible production environment usage scenario (7): batch deployment of elk clients

Flowable boundary timer

Wechat red envelope cover making tutorial and use guide with link jump

On the necessity and solution of building a campus online teaching video convergence platform

Websocket for im instant messaging development: concept, principle and common sense of mistakes

k线图基础知识图解——单根K线的含义
随机推荐
Lecun predicts AgI: big model and reinforcement learning are both ramps! My world model is the new way
【Try to Hack】nmap
Heavyweight! CDA certification test preparation Q & A Online
Online linear programming: Dual convergence, new algorithms, and regret bounds
How powerful is the Zadig build? Practice together
论文解读(DCN)《Towards K-means-friendly Spaces: Simultaneous Deep Learning and Clustering》
Fanuc robot_ Introduction to Karel programming (2)_ Usage of general IO signal
Zadig + sonarqube, ensuring the safety of the development process
在QT进行cin(全网最清晰教程)
Panxiaoming, senior vice president of IC nansha|amd and President of Greater China: process, architecture and platform optimization break through the computing boundary
Can we still enter the "pit" data analysis now? Look at the hot jobs of data analysis in 2022!
一文搞懂shell脚本
Wechat red envelope cover making tutorial and use guide with link jump
go语言插件平台的实现思路
Oracle删除归档日志及添加定时任务
Ansible production environment usage scenario (7): batch deployment of elk clients
Complex nested object pool (4) -- manage the object pool of multi class instances and multi-stage instances
Considerations on the construction of operation and maintenance system - stability
Is it safe to open a stock account by mobile phone?
How to solve the problem of desktop without sound