QML Camera设置分辨率方式
直接上测试代码,该代码在Qt5.5.1版本测试通过。
示例代码的主要功能是:点击窗口就可以截取摄像头中图片,在控制台上打印支持的分辨率。
import QtQuick 2.5
import QtQuick.Window 2.2
import QtQuick.Layouts 1.2
import QtQuick.Controls 1.4
import QtMultimedia 5.5
Item {
width: 640
height: 360
Camera {
id: camera
// 本函数用于打印支持的分辨率
function getSupportResulution()
{
var res = camera.supportedViewfinderResolutions()
for(var index in res){
console.info(index+":"+res[index].width+"====="+res[index].height);
}
}
// 这里是关键
captureMode: Camera.CaptureViewfinder;
// 设置自己想要的分辨率(上面分辨率中的一个)
viewfinder.resolution: Qt.size(160,120);
// 下面的代码是套话
imageProcessing.whiteBalanceMode: CameraImageProcessing.WhiteBalanceFlash
exposure {
exposureCompensation: -1.0
exposureMode: Camera.ExposurePortrait
}
flash.mode: Camera.FlashRedEyeReduction
imageCapture {
onImageCaptured: {
photoPreview.source = preview // Show the preview in an Image
// 调用函数,在控制台上看支持的分辨率
camera.getSupportResulution();
}
}
}
VideoOutput {
source: camera
anchors.fill: parent
focus : visible // to receive focus and capture key events when visible
}
Image {
id: photoPreview
}
// 点击窗口就会截取一帧画面
MouseArea{
anchors.fill: parent;
onClicked: {
var imgPath = "C:\\test";
camera.imageCapture.captureToLocation(imgPath)
}
}
}
设置分辨率是工作中遇到的一个问题,根据参考文档的思路,但是这个参考文档等示例没有执行成功,后来在qt帮助文档中找到示例和思路。
参考文档:
qt帮助 -> Camera -> Qt Multimedia 5.5
https://stackoverflow.com/questions/40757220/qml-change-the-resolution-before-camera-image-capture