qml在摄像机图像捕获之前更改分辨率

时间:2016-11-23 06:08:35

标签: qt camera qml

我想用特定的分辨率捕捉图像。 我使用了这段代码,但是从具有最后分辨率的相机捕获的图像和捕获的图像的分辨率不会改变为大小(1280,720)。我想在捕获图像之前更改分辨率。

imageCapture {
 resolution: Qt.size(1280, 720)
 onImageCaptured: {
 photoPreview.source = preview  
 }

2 个答案:

答案 0 :(得分:3)

在许多情况下,QML Camera的行为很奇怪,并且一些依赖关系没有很好地记录(但是)。

无论如何,以下代码适用于我:

import QtQuick 2.6
import QtQuick.Window 2.2
import QtQuick.Layouts 1.3
import QtQuick.Controls 1.4
import QtMultimedia 5.6

Window {
    visible: true

    width: 1280
    height: 960

    GridLayout {
        id: grid
        rows: 2
        columns: 2

        Item {
            Layout.row: 0
            Layout.column: 0
            Layout.minimumWidth: 80
            Layout.minimumHeight: 30

            Button {
                id: button
                text: "capture"
                onClicked: {
                    camera.stop();
                    camera.viewfinder.resolution = "640x480";
                    camera.start();
                }
            }
        }

        Camera {
            id: camera
            captureMode: Camera.CaptureViewfinder

            viewfinder.resolution: "160x120"

            imageCapture {
                id: cameracapture

                onImageCaptured: {
                    photoPreview.source = preview  // Show the preview in an Image
                    console.log( "capture size: ", photoPreview.sourceSize );
                    timerHelper.restart();
                }

            }

            onCameraStateChanged: {
                console.log("camera state changed to: ", cameraState );
                if ( cameraState == Camera.ActiveState && viewfinder.resolution == Qt.size(640,480) ) {
                    cameracapture.capture();
                }
            }

            function cameraHelper() {
                console.log( "Stopping cam..." );
                camera.stop();
                viewfinder.resolution = "160x120";
                camera.start();
            }
        }

        Timer {
            id: timerHelper
            interval: 1
            onTriggered: camera.cameraHelper();
        }

        Item {
            width: 640
            height: 480

            Layout.row: 1
            Layout.column: 0
            Layout.minimumWidth: 640
            Layout.minimumHeight: 480

            Image {

                width: 640
                height: 480

                id: photoPreview
            }
        }

        Item {
            width: 640
            height: 480

            Layout.row: 1
            Layout.column: 1
            Layout.minimumWidth: 640
            Layout.minimumHeight: 480

            VideoOutput {
                source: camera
                anchors.fill: parent
                focus : visible // to receive focus and capture key events when visible
            }
        }
    }
}

如果您想成功切换分辨率,则必须stop()start() Camera

如果您尝试将分辨率切换回(160,120)中的onImageCaptured,它会冻结,因此我使用Timer获取某种QueuedConnection

答案 1 :(得分:1)

在我的案例代码中: camera.stop(); camera.viewfinder.resolution = "640x480"; camera.start();

不起作用。当调用start()时出现错误: CameraBin错误:“设备'/ dev / video0'忙” CameraBin错误:“无法协商格式” 看来我自己的应用无法释放设备,并且我无法更改分辨率。 就我而言,解决方案是在被调用的camera.setCameraState(Camera.UnloadedState)之前添加start()