当前位置:网站首页>【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 博客中,希望自己学习的同时,也可以帮助更多的人。
边栏推荐
- Programming examples of stm32f1 and stm32subeide infrared receiving and decoding of NEC protocol
- Insufficient free space after clearing expired cache entries - consider increasing the maximum cache space
- Challenges faced by operation and maintenance? Intelligent operation and maintenance management system to help you
- The programmer of Beipiao posted a post for help late at night: I am lonely when my girlfriend is gone
- How to draw a product architecture diagram?
- Jena基于OWL的默认推理查询
- Operation and maintenance management system, humanized operation experience
- 【LINGO】求七个城市最小连线图,使天然气管道价格最低
- STM32F1与STM32CubeIDE编程实例-NEC协议红外接收与解码
- ESP32 - ULP 协处理器在低功耗模式下读片内霍尔传感器HALL SENSOR
猜你喜欢

Are there any practical skills for operation and maintenance management
![C language implementation [minesweeping game] full version (implementation source code)](/img/70/60f9a61bd99fa5fb5fab679a32528e.png)
C language implementation [minesweeping game] full version (implementation source code)

Mysql与Redis一致性解决方案

ctfshow-web355,356(SSRF)

The game is real! China software cup releases a new industrial innovation competition, and schools and enterprises can participate in it jointly

【LINGO】求七个城市最小连线图,使天然气管道价格最低

【深圳IO】精确食品称(汇编语言的一些理解)

Système de gestion de l'exploitation et de l'entretien, expérience d'exploitation humanisée

8 figures | analyze Eureka's first synchronization registry

Esp32 - ULP coprocessor reading Hall sensor in low power mode
随机推荐
北漂程序员深夜emo发帖求助:女朋友走了我很孤独 ......
Esp32 - ULP coprocessor reading Hall sensor in low power mode
rclone中文文档:常用命令大全
5G Massive MIMO的概念和优点总结
记一次线上接口慢查询问题排查
Ctfhub port scan (SSRF)
The programmer of Beipiao posted a post for help late at night: I am lonely when my girlfriend is gone
SQL language learning record I
Problem: officeexception: failed to start and connect (II)
Will Internet talents be scarce in the future? Which technology directions are popular?
ctfshow-web351(SSRF)
1286_FreeRTOS的任务优先级设置实现分析
【推荐技术】基于协同过滤的网络信息推荐技术matlab仿真
DC-4 target
运维管理系统,人性化操作体验
運維管理系統,人性化操作體驗
Record an online interface slow query problem troubleshooting
[lingo] find the shortest path problem of undirected graph
ctfshow-web354(SSRF)
Rclone access web interface