当前位置:网站首页>【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 博客中,希望自己学习的同时,也可以帮助更多的人。
边栏推荐
- Automated test platform (13): interface automation framework and platform comparison, application scenario analysis and design ideas sharing
- 运维管理系统,人性化操作体验
- ctfshow-web354(SSRF)
- 开源了!文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
- 【推荐系统】美团外卖推荐场景的深度位置交互网络DPIN的突破与畅想
- SQL language learning record I
- Which securities company is better or safer for mobile phone account opening
- Problem: officeexception: failed to start and connect (III)
- 为什么这么多人转行产品经理?产品经理发展前景如何?
- Operation and maintenance management system, humanized operation experience
猜你喜欢

比赛即实战!中国软件杯发布全新产业创新赛项,校企可联合参赛

【剑指offer&牛客101】中那些高频笔试,面试题——链表篇

ctfshow-web352,353(SSRF)

Product learning (II) - competitive product analysis

Introduction to spark (one article is enough)

JSP - paging

代码实战——从零开始搭建自己的Diffusion models/Score-based generative models

发现了一个 MySQL 的巨坑:update 更新别再用影响行数做判断了!!!

JAX的深度学习和科学计算

Are there any practical skills for operation and maintenance management
随机推荐
广发证券开户是安全可靠的么?怎么开广发证券账户
灰度何以跌下神坛?
[lingo] solve quadratic programming
MySQL table partition creation method
Figure out the difference between event coordinates screenx, clientx, pagex and offsetx
Is it safe to buy funds on Alipay? Where can I buy funds
Are there any practical skills for operation and maintenance management
kdtree(kd树)笔记
rclone配置minio及基本操作
[matlab] solve nonlinear programming
手机开户选哪个证券公司比较好,哪个更安全
北漂程序员深夜emo发帖求助:女朋友走了我很孤独 ......
Solve the problem that the class defined in meta-inf.services cannot be read
如何进入互联网行业,成为产品经理?没有项目经验如何转行当上产品经理?
Inventory the six second level capabilities of Huawei cloud gaussdb (for redis)
图像风格迁移 CycleGAN原理
8 张图 | 剖析 Eureka 的首次同步注册表
运维面临挑战?智能运维管理系统来帮您
Is fixed investment fund a high-risk product?
go-etcd