当前位置:网站首页>【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 博客中,希望自己学习的同时,也可以帮助更多的人。
边栏推荐
- Leetcode detailed explanation of stack type
- 直击产业落地 | 飞桨重磅推出业界首个模型选型工具
- go语言插件平台的实现思路
- How to use London gold to draw support resistance line
- 在线文本过滤小于指定长度工具
- 论文解读(DCN)《Towards K-means-friendly Spaces: Simultaneous Deep Learning and Clustering》
- How to use the apipost script - global variables
- Is it safe to open a stock account online?
- Wave picking of WMS warehouse management system module
- Considerations on the construction of operation and maintenance system - stability
猜你喜欢
![LeetCode 324 擺動排序 II[排序 雙指針] HERODING的LeetCode之路](/img/41/b8ba8d771b7224eac1cc8c54fe9d29.png)
LeetCode 324 擺動排序 II[排序 雙指針] HERODING的LeetCode之路

Online text filter less than specified length tool

The love digital smart 2022 summit opens, sharing data strategy and building data-driven organization methodology

This simple little function saves 213 hours for our production research team in half a year

2022-06-28:以下golang代码输出什么?A:true;B:false;C:panic;D:编译失败。 package main import “fmt“ func main() {

After crossing, she said that the multiverse really exists

微搭低代码中实现二维码生成

分享im即时通讯开发之WebSocket:概念、原理、易错常识

Production environment sonarqube installation

全面掌握const的用法《一》
随机推荐
Online sql to htmltable tool
The technology giants set up the meta universe standard forum to open up or build a besieged city?
Considerations on the construction of operation and maintenance system - stability
Mono's execution process
Complex nested object pool (4) -- manage the object pool of multi class instances and multi-stage instances
Simple understanding of counting and sorting
Online text filter less than specified length tool
Mathematical knowledge: finding combinatorial number I - finding combinatorial number
Leetcode detailed explanation of stack type
Database basic notes
在线文本过滤小于指定长度工具
分享im即时通讯开发之WebSocket:概念、原理、易错常识
Zadig officially launched vs code plug-in, making local development more efficient
Linux Installation mysql5.7 (centos7.6) tutorial
台式机没声音怎么样才能解决
Embedded dynamic Arabic string conversion LCD display string [thanks for Jianguo ambition]
The Best of Many Worlds_ Dual Mirror Descent for Online Allocation Problems
How many stages did the development and evolution of data analysis go through?
一文读懂,WMS仓储管理系统与ERP有什么区别
Ansible production environment usage scenario (7): batch deployment of elk clients