1
votes

I want to capture image with the specific resolution. I used this code but the image captured from camera with last resolution and the resolution of image captured does not change to size(1280, 720). I want to change the resolution before capturing the image.

imageCapture {
 resolution: Qt.size(1280, 720)
 onImageCaptured: {
 photoPreview.source = preview  
 }
2
What is last resolution? Does that mean you change the resolution before capturing the image? Please post appropriate code.folibis

2 Answers

3
votes

In many cases, the behavior of QML Camera is strange and some dependencies are not well documented(, yet).

Anyway, following code works for me:

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
            }
        }
    }
}

If you want to switch the resolution successfully, you have to stop() and start() the Camera.

It freezes, if you try to switch the resolution back to (160,120) in onImageCaptured, so I used a Timer to obtain some kind of QueuedConnection.

1
votes

In my case code: camera.stop(); camera.viewfinder.resolution = "640x480"; camera.start();

doesn't work. When start() called i have error: CameraBin error: "Device '/dev/video0' is busy" CameraBin error: "Could not negotiate format" Looks like my own app doesn't free device and i can't change resolution. In my case the solution is to add camera.setCameraState(Camera.UnloadedState) before start() called.