Qt for Android中拍照预览的bug及解决
2017-02-24 10:58
288 查看
在使用使用QtQuick进行移动端开发时,时常会用到相机这一个功能,而原生android中如果拍照的话,如果不想复杂或者特殊需求,就直接使用Intent去调用系统的相机进行拍照,然后 在当前activity中的拿到拍完后的照片的地址进行编辑,在使用QML做移动端开发时,如果想使用相机拍照预览,也有几种实现方式,
1. 使用c++操控相机,
2. 直接使用QtQuick中提供的相机元素控件Camera ,
3. 间接调用android或者ios的相机进行拍照,但是这个需要相应的应用权限
我这里使用了QtQuick提供QtMultimedia这个模块下的Camera的功能进行拍照预览的实现,拍照前我们需要判断当前设备是否支持拍照的功能进行应用的提升功能,我们可以根据QtMultimedia.availableCameras.length>0是否为真来判断当前设备是否可用,我这里只是个人的用法,在拍照前,先声明两个变量来进行布局的控制
//判断当前设备是否可以拍照
property bool isCameraAvailable: QtMultimedia.availableCameras.length > 0
//判断是否拍照完成
property bool imageIsShow: false
拍照我们需要一个相机的元素控件和一个拍照后预览的容器Image元素和一个拍照显示的容器VideoOutput控件
“
上边这些设置在Qt的帮助文档中都有对应的解释就不累赘了,拍照还需要有几个按钮控件来进行控制拍照或者预览的作用
这就是一个拍照的完整功能,但是在使用过程中却发现一个问题,拍照预览时,如果快速的拍照使用,会出现预览的照片总是预览的是上一次拍的照片,这个问题纠结了很久,于是就去查了下帮助文档,发现imageCapture下还有另外一个方法 onImageSaved,于是就尝试了一下,把照片的预览的事件写到了onImageSaved方法里面,发现问题尽然解决了,代码如下,其他不变
但是在有的配置低的设备上这个过程比较慢,可能是拍照完成后去读取本地照片速度有的慢的问题,在使用过程中发现有时候发现拍完照片后,旋转终端设备会出现前面是预览照片,后面是当前画面的情况解决方法是在VideoOutput 中添加visible: !imageIsShow 即可解决。如果问题欢迎留言。
1. 使用c++操控相机,
2. 直接使用QtQuick中提供的相机元素控件Camera ,
3. 间接调用android或者ios的相机进行拍照,但是这个需要相应的应用权限
我这里使用了QtQuick提供QtMultimedia这个模块下的Camera的功能进行拍照预览的实现,拍照前我们需要判断当前设备是否支持拍照的功能进行应用的提升功能,我们可以根据QtMultimedia.availableCameras.length>0是否为真来判断当前设备是否可用,我这里只是个人的用法,在拍照前,先声明两个变量来进行布局的控制
//判断当前设备是否可以拍照
property bool isCameraAvailable: QtMultimedia.availableCameras.length > 0
//判断是否拍照完成
property bool imageIsShow: false
拍照我们需要一个相机的元素控件和一个拍照后预览的容器Image元素和一个拍照显示的容器VideoOutput控件
Camera { id: camera captureMode: Camera.CaptureStillImage imageCapture { id: _capture onImageCaptured: { photoPreview.source = preview imageIsShow = true } } imageProcessing.whiteBalanceMode: CameraImageProcessing.WhiteBalanceFlash imageProcessing.colorFilter: CameraImageProcessing.ColorFilterNone exposure.exposureMode: Camera.ExposureAuto flash.mode: Camera.FlashRedEyeReduction focus.focusMode: Camera.FocusAuto focus.focusPointMode: Camera.FocusPointCenter position: Camera.BackFace } VideoOutput { id: videoout; anchors.fill: parent source: camera; visible: true autoOrientation: true } Image { id: photoPreview; anchors.fill: parent visible: status == Image.Ready fillMode: Image.PreserveAspectFit }
“
上边这些设置在Qt的帮助文档中都有对应的解释就不累赘了,拍照还需要有几个按钮控件来进行控制拍照或者预览的作用
Column{ anchors.right: parent.right anchors.rightMargin: 20 anchors.verticalCenter: parent.verticalCenter spacing: 20 visible: isCameraAvailable Rectangle{ width: 80; height: 80; radius: width/2 color: “black” visible: imageIsShow Text{ text: "丢弃" anchors.fill: parent horizontalAlignment:Text.AlignHCenter; verticalAlignment: Text.AlignVCenter font.pixelSize: height * 0.3 color: "white" } MouseArea{ anchors.fill: parent onClicked: { imageIsShow = false photoPreview.source = '' } } } Rectangle{ width: 80; height: 80; radius: width/2 color: “black” visible: !imageIsShow Text{ text: "拍照" anchors.fill: parent horizontalAlignment:Text.AlignHCenter; verticalAlignment: Text.AlignVCenter font.pixelSize: height * 0.3 color: "white" } MouseArea{ anchors.fill: parent onClicked: { camera.imageCapture.capture(); } } } Rectangle{ width: 80; height: 80; radius: width/2 color: “black” visible: imageIsShow Text{ text: "使用" anchors.fill: parent horizontalAlignment:Text.AlignHCenter; verticalAlignment: Text.AlignVCenter font.pixelSize: height * 0.3 color: "white" } MouseArea{ anchors.fill: parent onClicked: { 在使用照片,并退出界面 } } } } Text{ anchors.centerIn: parent text: "设备不可用"; color: "whitesmoke" font.pixelSize: dp(24) visible: !isCameraAvailable } }
这就是一个拍照的完整功能,但是在使用过程中却发现一个问题,拍照预览时,如果快速的拍照使用,会出现预览的照片总是预览的是上一次拍的照片,这个问题纠结了很久,于是就去查了下帮助文档,发现imageCapture下还有另外一个方法 onImageSaved,于是就尝试了一下,把照片的预览的事件写到了onImageSaved方法里面,发现问题尽然解决了,代码如下,其他不变
imageCapture { id: _capture onImageSaved: { photoPreview.source="file:///"+path imageIsShow=true; } }
但是在有的配置低的设备上这个过程比较慢,可能是拍照完成后去读取本地照片速度有的慢的问题,在使用过程中发现有时候发现拍完照片后,旋转终端设备会出现前面是预览照片,后面是当前画面的情况解决方法是在VideoOutput 中添加visible: !imageIsShow 即可解决。如果问题欢迎留言。
相关文章推荐
- QT for android 比较完美解决 全屏问题
- 关于Qt for Android 编译时出错/usr/bin/sh: -c: line 1: syntax error: unexpected end of file 时的解决方法
- JS 解决 IOS 中拍照图片预览旋转 90度 BUG
- 解决Qt For Android 识别不到手机 Incompatible devices 问题
- Qt for android 在安卓上面运行app启动闪黑屏的解决方法
- Android之MIUI系统BUG:调用拍照后不返回当前activity解决办法
- Android 8.0 WebView 拍照、简易预览、二维码扫描 各种问题解决
- Qt for Android编译报错提示:Install to device:No Buildfile:build.xml does not exist!解决方法
- JS解决IOS中拍照图片预览旋转90度BUG的问题
- Qt for Android调用原生接口打开相机拍照并存储照片
- 解决android项目Error:Execution failed for task ':app:dexDebug'.ebug'错误
- qt for android搭建碰到问题及解决办法分享。
- Qt for Android遇到的几个错误解决[Win7 + Qt5.6 +jdk 8u91]
- 解决QT5.8 for android 无法连接64位Mysql的问题
- 金融理财终端、股票软件数据剖析: 20、qt for android(c/c++)
- QT for linux 的错误 undefined reference to 'FcFreeTypeQueryFace' 的解决方法
- Qt for Android 配置详细
- 【转】 解决通过Intent调用系统拍照程序,返回图片太小的问题[android]
- 转帖-Qt for Android 配置详细 --看到了就转来了
- Qt for Android 配置详细 (欢迎转载)