在使用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
}
}