获取Flutter图片的宽度和高度
最编程
2024-01-03 19:17:12
...
- 本地图片宽高获取
- 网络图片宽高获取
原理
通过在其ImageProvider
上调用resolve
来读取ImageStream
1.本地图片宽高获取
以IM发送本地图片为例
sendImage: (String path) async {
IMImageMsgModel m = IMImageMsgModel(localUrl: path);
Image image = Image.file(File.fromUri(Uri.parse(path)));
// 预先获取图片信息
image.image.resolve(new ImageConfiguration()).addListener(
new ImageStreamListener((ImageInfo info, bool _) {
m.width = info.image.width;
m.height = info.image.height;
this.sendMsg(m);
}));
},
2.网络图片宽高获取
自己返回的Message格式:
{timestamp: 1579076501, msgType: Image, message: {imageFormat: 255, imageList: [], level: 1, path: https://ae80-1400284328-1256635546.cos.ap-shanghai.myqcloud.com/e1696b47832dd6e336a6db38b51cd3fd/23a2ad6539b63787dee8909f9bdb60b9.jpg, taskId: 0, type: Image}, msgId: 862520433, seq: 13163, rand: 862520433, isSelf: true, uniqueId: 6782081930539631729, isRead: true, status: 2, timMessageLocator: {rand: 862520433, seq: 13163, timestamp: 1579076501, sid: null, isSelf: true}, sender: e1696b47832dd6e336a6db38b51cd3fd, peer: 5d2806e96205f82dcf692370de0c7e07, customData: null, customInt: 0, customStr: , type: Image}
对方返回的Message格式:
{timestamp: 1579077317, msgType: Image, message: {imageFormat: 1, imageList: [{height: 2160, size: 1185181, type: Original, url: https://ae80-1400284328-1256635546.cos.ap-shanghai.myqcloud.com/5d2806e96205f82dcf692370de0c7e07/0f7b0c259f53770c6eab0cbba550e605.jpg, uuid: 1400284328_5d2806e96205f82dcf692370de0c7e07_0f7b0c259f53770c6eab0cbba550e605.jpg, width: 1080}, {height: 396, size: 0, type: Thumb, url: https://ae80-1400284328-1256635546.picsh.myqcloud.com/5d2806e96205f82dcf692370de0c7e07/0f7b0c259f53770c6eab0cbba550e605.jpg?imageView2/3/w/198/h/198, uuid: 1400284328_5d2806e96205f82dcf692370de0c7e07_0f7b0c259f53770c6eab0cbba550e605.jpg, width: 198}, {height: 1440, size: 0, type: Large, url: https://ae80-1400284328-1256635546.picsh.myqcloud.com/5d2806e96205f82dcf692370de0c7e07/0f7b0c259f53770c6eab0cbba550e605.jpg?imageView2/3/w/720/h/720, uuid: 1400284328_5d2806e96205f82dcf692370de0c7e07_0f7b0c259f53770c6eab0cbba550e605.jpg, width: 720}], level: 1, path: , taskId: 0, type: Image}
当IM回调图片不带宽高时,通过以下方法获取网络图片宽高
IMImageMsgModel model;
Image image = Image.network(this.detail.message['path'] ?? '');
image.image
.resolve(new ImageConfiguration())
.addListener(new ImageStreamListener(
(ImageInfo info, bool _) {
model = IMImageMsgModel(
url: this.detail.message['path'],
width: info.image.width,
height: info.image.height,
);
print('model.width======${model.width}');
},
));
效果如下
图片宽高为:66-800
上一篇: 在CentOS 7中实现腾讯云COS对象存储挂载的教程
下一篇: tencent cloud
推荐阅读
-
Android 实现了图片宽度 100% 与 ImageView 宽度和高度成比例的自动缩放
-
HarmonyOS Hands-on - 文本组件宽度和高度的三个值的书写和颜色属性
-
微信 "扫一扫 "物联网,全面揭秘 "扫一扫 "背后的扫盲技术!-1.1 扫一扫感知物体是做什么的? 1.1 微信扫一扫是做什么的? 扫一扫识物是指以图片或视频(商品图片:鞋/包/美妆/服饰/家电/玩具/图书/食品/珠宝/家具/其他商品)为输入媒介,挖掘微信内容生态中的有价值信息(电商+百科+资讯,如图1所示),并展示给用户。这里的电商基本涵盖了微信小程序覆盖上亿SKU的全量优质电商,可以支持用户货比N家并直接下单购买,百科和资讯则聚合了微信内的头部自媒体如搜狗、搜搜、百度等,向用户展示和分享拍摄商品相关的内容资讯。 图 1 扫一扫识别功能示意图 欢迎大家更新iOS新版微信→扫一扫→识货,亲自体验,也欢迎大家通过识货界面的反馈按钮向我们提交反馈意见。 扫一扫识物实景图展示 1.2 扫一扫识物有哪些使用场景? 扫一扫识物的目的是为用户访问微信内部生态内容开辟一个新窗口,以用户扫图片为输入形式,为用户提供微信生态内容中的百科、资讯、电商等作为展示页面。除了用户熟悉的扫一扫操作外,我们还将进一步拓展长按操作,让用户更方便地进行扫一扫操作。"扫一扫知事 "的落地场景主要涵盖三大部分: a. 科普知识: a.科普知识。用户通过扫一扫,可以在微信生态圈中获取该对象的百科、资讯等常识或趣闻,帮助用户更好地了解该对象; b.购物场景。同样的搜索功能支持用户看到喜欢的商品立即检索到微信小程序电商中的同款商品,支持用户即扫即购; c.广告场景。扫一扫识别物体可以辅助公众号文章、视频更好地理解其中蕴含的图片信息,从而更好地投放匹配广告,提高点击率。 1.3 Sweep Sense 为 Sweep 家族带来了哪些新技术? 对于扫一扫来说,大家耳熟能详的应该就是扫一扫二维码、扫一扫小程序码、扫一扫条形码、扫一扫翻译了。无论是各种形式的编码还是文字字符,都可以看作是图片的一种特定编码形式,而物的识别则是对自然场景图片的识别,这对于扫一扫家族来说是一个质的飞跃,我们希望从物的识别入手,进一步拓展扫一扫对自然场景图片的理解能力,比如扫酒、扫车、扫植物、扫人脸等服务,如下图3所示。 图 3 Sweep 家族
-
如何在 php 中设置表格的宽度和高度
-
使用 jquery 等比例控制图片宽度和高度的具体实现方法
-
如何在 html5 视频标记中设置视频的宽度和高度
-
安卓屏幕硬件宽度、高度和当前视图显示区域的宽度,Kotlin-...
-
设置 Img 标签的高度和宽度属性
-
js 获取浏览器高度和宽度值(多浏览器)
-
Android 开发中 nodpi、xhdpi、hdpi、mdpi、ldpi 的概念 - 术语和概念 屏幕尺寸 屏幕的物理尺寸,基于屏幕的对角线长度(如 2.8 英寸、3.5 英寸)。 简而言之,安卓系统将所有屏幕尺寸简化为三大类:大、普通和小。 程序可以为这三种屏幕尺寸提供三种不同的布局选项,然后系统会以合适的方式将布局选项呈现到相应的屏幕上,这个过程不需要程序员用代码进行干预。 屏幕纵横比 屏幕的物理长度与物理宽度之比。程序只需使用系统提供的资源分类器 long(长)和 notlong(不长),就能为具有特定长宽比的屏幕提供配制材料。 分辨率 屏幕的像素总数。请注意,分辨率并不意味着长宽比,尽管在大多数情况下,分辨率表示为 "宽度 x 长度"。在安卓系统中,程序一般不直接处理分辨率。 密度 根据屏幕分辨率,沿屏幕宽度和长度排列的像素数量。 密度较低的屏幕在长度和宽度方向上的像素都相对较少,而密度较高的屏幕通常会在同一区域内排列很多甚至非常非常多的像素。屏幕的密度非常重要;例如,一个界面元素(如按钮)的长度和宽度以像素为单位,在低密度屏幕上会显得很大,但在高密度屏幕上就会显得很小。 独立于密度的像素(DIP)是指程序用来定义界面元素的抽象意义上的像素。它作为一个与实际密度无关的单位,帮助程序员构建布局方案(界面元素的宽度、高度和位置)。 与密度无关的像素在逻辑上与像素密度为 160 DPI 的屏幕上的像素大小相同,而 160 DPI 是安卓平台默认的显示设备。在运行时,平台会以目标屏幕的密度为基准,"透明 "地处理所有所需的 DIP 缩放操作。要将与密度无关的像素转换为屏幕像素,可以使用一个简单的公式:像素 = DIP * (密度 / 160)。例如,在 240 DPI 的屏幕上,1 个 DIP 等于 1.5 个物理像素。强烈建议使用 DIP 来定义程序界面的布局,因为这样可以确保用户界面在所有分辨率的屏幕上都能正常显示。 为了简化程序员在面对各种分辨率时的麻烦,也为了让各种分辨率的平台都能直接运行这些程序,Android 平台将所有屏幕以密度和分辨率作为分类方式,分别分为三类:- 三大尺寸:大、普通、小;- 三种不同密度:高(hdpi)、中(mdpi)和低(ldpi)。DPI 表示 "每英寸点数",即每英寸的像素数。如果需要,程序可以为不同的屏幕尺寸提供不同的资源(主要是布局),为不同的屏幕密度提供不同的资源(主要是位图)。除此之外,程序无需对屏幕尺寸或密度进行任何额外处理。执行时,平台会根据屏幕本身的尺寸和密度特性自动加载相应的资源,并将其从逻辑像素(DIP,用于定义界面布局)转换为屏幕上的物理像素。