0

I'm new at qml. I'm working in windows 10, Qt creator 6.0.0, using Mingw64 as compiler and Qt 6.2.2. I'm trying to take a picture clicking on the screen.

import QtQuick
import QtCore
import QtMultimedia

Window {
    id: main_window
    width: Screen.desktopAvailableWidth
    height: Screen.desktopAvailableHeight
    visible: true
    CaptureSession {
        id:captureSession
        videoOutput: videoOutput

        Component.onCompleted: camera.start()

        camera: Camera {
            cameraDevice: MediaDevices.defaultVideoInput
        }
        imageCapture: ImageCapture {
            onErrorOccurred: {
                console.log("Error occurred\n")
            }
            onImageCaptured: {
                console.log("Image captured\n")
            }
            onImageSaved: {
                console.log("Image saved\n")
            }
        }
    }
    VideoOutput {
        id:videoOutput;
        anchors.fill: parent;
    }
    MouseArea {
        anchors.fill: parent;
        onClicked: captureSession.imageCapture.capture();
    }
}

My main.cpp file is the default file of QtQuick application template.

I checked the default path for pictures and its file:///C:/Users/myname/Pictures and never found the picture.

The only output I'm getting is Image captured, so I guess the image is being saved. What colud the problem be? thank you in advance

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
rain 183
  • 11
  • 3

1 Answers1

1

The capture method does not save any file, it only takes the image and saves it in a QImage that can be used to display in an Item Image through the preview property. If you want to save the image in a specific path then use captureToFile, you also have other errors.

import QtCore
import QtQuick
import QtMultimedia

Window {
    id: main_window

    width: Screen.desktopAvailableWidth
    height: Screen.desktopAvailableHeight
    visible: true

    MediaDevices {
        id: mediaDevices
    }

    CaptureSession {
        id: captureSession

        videoOutput: videoOutput
        Component.onCompleted: camera.start()

        camera: Camera {
            cameraDevice: mediaDevices.defaultVideoInput
        }

        imageCapture: ImageCapture {
            onErrorOccurred: function(requestId, error, message) {
                console.log("Error occurred", requestId, error, message);
            }
            onImageCaptured: function(requestId, previewImage) {
                console.log("Image captured", requestId, previewImage);
            }
            onImageSaved: function(requestId, path) {
                console.log("Image saved", requestId, path);
            }
        }

    }

    VideoOutput {
        id: videoOutput

        anchors.fill: parent
    }

    MouseArea {
        anchors.fill: parent
        onClicked: function() {
            captureSession.imageCapture.captureToFile("C:/Users/myname/Pictures");
        }
    }

}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Ok then I have two question? why `MediaDivices { id:mediaDevices; }` as child of `captureSession` give me error `Cannot assign to non-existent default property`? why is better `onClicked: function(...parameters...) { ...instructions...}` then `onClicked: { ...instructions...}`? – rain 183 Dec 13 '21 at 12:06
  • 1. I don't know, but in your comment there is a typo `MediaDivices`. 2. It is more verbose, for example you can see its advantage in onErrorOccurred where you can indicate the arguments of the signal. – eyllanesc Dec 13 '21 at 14:36