当前位置:网站首页>【Flutter 问题系列第 72 篇】在 Flutter 中使用 Camera 插件拍的图片被拉伸问题的解决方案
【Flutter 问题系列第 72 篇】在 Flutter 中使用 Camera 插件拍的图片被拉伸问题的解决方案
2022-07-01 07:07:00 【Allen Su】
这是【Flutter 问题系列第 72 篇】,如果觉得有用的话,欢迎关注专栏。
Flutter SDK:2.5.0,Dart SDK:2.14,Android Studio :2021.2.1 Pathc 1。
一:问题描述
项目中要自定义相机页,所以用到了官方提供的 Camera 插件,构建相机视图的代码如下
/// 构建相机视图
Widget _cameraView() {
return CameraPreview(cameraController);
}
不过在拍摄照片时,却出现了图像被拉伸的问题,如下图所示

经测试,虽然实际拍出来的效果是正常的,但这种像是开了长腿特效的情况并不是我们想要的,怎么解决这种问题呢?
二:解决方案
这里用到了 Transform
组件的 scale 属性,求得相机纵横比与设备像素比的比例,当作 Transform 的缩放比即可,修改后的代码如下所示
/// 构建相机视图
Widget _cameraView() {
// 设备尺寸
final Size size = MediaQuery.of(context).size;
// 设备像素比
final double deviceRatio = size.width / size.height;
// 相机纵横比
final double aspectRatio = cameraController.value.aspectRatio;
return Center(
child: Transform.scale(
scale: aspectRatio / deviceRatio,
child: AspectRatio(
aspectRatio: aspectRatio,
child: Center(child: CameraPreview(cameraController)),
),
),
);
}
修改后的效果图如下

可以看到,图像没有再被拉伸了,good。
你的问题得到解决了吗?欢迎在评论区留言。
赠人玫瑰,手有余香,如果觉得文章不错,希望可以给个一键三连,感谢。
Google 的 Flutter 越来越火,截止 2022年6月29日 GitHub 标星已达 142K,Flutter 毅然是一种趋势,所以作为前端开发者,没有理由不趁早去学习。
结束语
无论你是 Flutter 新手还是已经入门了,不妨先点个关注,后续我会将 Flutter 中的常用组件(含有源码分析、组件的用法及注意事项)以及可能遇到的问题写到 CSDN 博客中,希望自己学习的同时,也可以帮助更多的人。
边栏推荐
- 女生适合学产品经理吗?有什么优势?
- Problem solving: officeexception: failed to start and connect (I)
- 记一次线上接口慢查询问题排查
- Is it safe to buy funds on Alipay? Where can I buy funds
- 解决kaniko push镜像到harbor时报错(代理导致):unexpected status code 503 Service Unavailable
- Jena default inference query based on OWL
- 关于“2022年度网络安全教育线上培训”相关问题的复盘和说明
- 如何进入互联网行业,成为产品经理?没有项目经验如何转行当上产品经理?
- 如何通过cdn方式使用阿里巴巴矢量图字体文件
- 北漂程序员深夜emo发帖求助:女朋友走了我很孤独 ......
猜你喜欢
ctfshow-web351(SSRF)
JSP - paging
热烈祝贺五行和合酒成功挂牌
How to enter the Internet industry and become a product manager? How to become a product manager without project experience?
The game is real! China software cup releases a new industrial innovation competition, and schools and enterprises can participate in it jointly
Paging in servlets and JSPS
[lingo] solve quadratic programming
图像风格迁移 CycleGAN原理
Understand esp32 sleep mode and its power consumption
C语言实现【扫雷游戏】完整版(实现源码)
随机推荐
灰度何以跌下神坛?
Are there any practical skills for operation and maintenance management
Is fixed investment fund a high-risk product?
Understand esp32 sleep mode and its power consumption
Ctfhub port scan (SSRF)
Which securities company is better or safer for mobile phone account opening
Is it safe to do fund fixed investment on Great Wall Securities?
[classification model] Q-type cluster analysis
8 张图 | 剖析 Eureka 的首次同步注册表
Is the account opening of GF Securities safe and reliable? How to open GF Securities Account
buildroot override 机制
JSP - paging
运维面临挑战?智能运维管理系统来帮您
C# 读写自定义的Config文件
【MATLAB】求解非线性规划
Summary of the concept and advantages of 5g massive MIMO
[the path of system analysts] Chapter 5: software engineering of double disk (reverse clean room and Model Driven Development)
[lingo] find the shortest path problem of undirected graph
ctfshow-web354(SSRF)
Operation and maintenance management system, humanized operation experience