Qt for Android中相机拍照的坑

       在使用QtQuick进行移动端开发时,时常会用到相机这一个功能,而原生android中如果拍照的话,如果不想复杂或者特殊需求,就直接使用Intent去调用系统的相机进行拍照,然后 在当前activity中的拿到拍完后的照片的地址进行编辑,在使用QML做移动端开发时,如果想使用相机拍照预览,也有几种实现方式,1)使用c++操控相机,2)直接使用QtQuick中提供的相机元素控件Camera  ,3)间接调用android或者ios的相机进行拍照,但是这个需要相应的应用权限

我这里使用了QtQuick提供QtMultimedia这个模块下的Camera的功能进行拍照预览的实现,拍照前我们需要判断当前设备是否支持拍照的功能进行应用的提升功能,我们可以根据QtMultimedia.availableCameras.length>0是否为真来判断当前设备是否可用,我这里只是个人的用法,希望多多补砖

拍照我们需要一个相机的元素控件

            Camera {
                id: camera
                captureMode: Camera.CaptureStillImage
                imageCapture {
                    id: _capture
                   onImageCaptured: {
                        console.log(preview)
                        photoPreview.source = preview
                    }
                }
                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
            }


 




    Rectangle{
            anchors{ fill: parent; margins: 0 }
            color: l.color.dark
            Camera {
                id: camera
                captureMode: Camera.CaptureStillImage
                imageCapture {
                    id: _capture
//                    onImageCaptured: {
//                        console.log(preview)
//                        photoPreview.source = preview
//                        imageIsShow = true
//                    }
                    onImageSaved: {
                        photoPreview.source="file:///"+path
                        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
            }
            Column{
                anchors.right: parent.right
                anchors.rightMargin: dp(l.margin)
                anchors.verticalCenter: parent.verticalCenter
                spacing: dp(l.margin)
                visible: isCameraAvailable
                Rectangle{
                    width: dp(l.height); height: dp(l.height); radius: width/2
                    color: l.color.translucent
                    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: dp(l.height); height: dp(l.height); radius: width/2
                    color: l.color.translucent
                    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: dp(l.height); height: dp(l.height); radius: width/2
                    color: l.color.translucent
                    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: {
                            containerqml.setAnswer('', _capture.capturedImagePath)
                            _page.pop();
                        }
                    }
                }
            }
            Text{
                anchors.centerIn: parent
                text: "设备不可用"; color: "whitesmoke"
                font.pixelSize: dp(24)
                visible: !isCameraAvailable
            }
        }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值