androidqmlqt6

How to Save qml ImageCapture preview Image to file system on Android


I have Tried the following code , I am never able to save the ImageCapture preview to file system on Android , I am using imageCapture.saveToFile() function , I see the following error on the console though:

W app   : QFSFileEngine::open: No file name specified
W app   : QFile::remove: Empty or null file name

and this is my simple code:

 Item {
    id: camItem
    width: parent.width
    height: parent.height - 12 - 24
    anchors{
        top: goBack.bottom
        topMargin: 20
        horizontalCenter: parent.horizontalCenter
    }
    MediaDevices {
        id: mediaDevices
    }
    CaptureSession {
        imageCapture : ImageCapture {
            id: imageCapture
            onImageCaptured: {
                imageCapture.saveToFile("1.png")
            }
            onImageSaved: {
                console.log("image saved", fileName)
            }
        }

        camera: Camera {
            id: camera
            cameraDevice: mediaDevices.videoInputs[0]

        }


        videoOutput: videoOutput
    }

    VideoOutput {
        id: videoOutput
        anchors.fill: parent

        MouseArea {
            anchors.fill: parent;
            onClicked: {
                photoPreview.visible = true
                imageCapture.capture();

            }
        }
    }

    Image {
        id: photoPreview
        visible: false
        anchors.fill: parent
        source: imageCapture.preview // always shows the last captured image
    }
}

Solution

  • I could do it with videooutput and C++ QObject casted to QVideoSink : So in QML I had:

    Button{
        id: saveButton
        onReleased: {
            cpp.cppCapture(videoOutput.videoSink)
        }
    
    }
    

    and C++ function:

    CPP::cppCapture(QObject *qmlPreview)
    {
        auto vs = qobject_cast<QVideoSink*>(qmlPreview);
        if(!vs) return false;
        QImage image{vs->videoFrame().toImage()};
    
        //  save image
    image.save("123.jpeg")