peco
peco

Reputation: 351

Display Camera created in C++ on VideoOutput in QML

I have problem displaying the QCamera created in C++ to VideoOutput that is in QML. If i use this way where camera is in QML, everything is fine i get the video output:

Item{
        VideoOutput
        {
            id: videoOutput
            anchors.fill: parent
            source: camera
        }
        Camera
        {
            id: camera
        }
}

But in my case camera is not in QML. I am creating it in C++. I have tried to create it in C++ and set it as contextProperty and therefore use it in source in VideoOutput in qml. So this is my main.cpp.

QCamera* camera;

    QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
    foreach (const QCameraInfo &cameraInfo, cameras)
    {
        qDebug() << cameraInfo.description();

        camera = new QCamera(cameraInfo);
    }

    if(camera)
    {
        qDebug() << "setContextProperty  camera ";
        engine.rootContext()->setContextProperty("mCamera", camera);
    }

And everything is fine camera is detected and i am using it in QML like this:

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    Item
    {
        id: cameraView
        height: 230
        width: 300
        anchors.centerIn: parent

        VideoOutput
        {
            id: videoOutput
            anchors.fill: parent
            source: mCamera
        }
    }

But there is no video output in this way. Is this possible to achive? Thanks in advance.

Upvotes: 1

Views: 1921

Answers (2)

AD1170
AD1170

Reputation: 418

Just in case someone is still interested in.
You can use the Camera->setViewfinder() function.

I have a wrapper class for the QCamera the do some other stuff. Among others, there is a public slot to set the cameras viewfinder from QML.

void MyCamClass::setViewFinder(QObject *vf)
{
  if(Camera)
    Camera->setViewfinder(qobject_cast<QAbstractVideoSurface *>(vf));
}

On QML side, you will have a

VideoOutput {
  id: vOutput
}

If you want to activate your camera, you can call:

mCamera->setviewFinder(vOutput.videoSurface);

mCamera is a object of MyCamClass class and registered to QML environment.
Make sure to start your camera before set the viewfinder.

Upvotes: 2

GrecKo
GrecKo

Reputation: 7150

You can't directly assign a QCamera to the source of a VideoOutput.

What you can do is set the deviceId of the QML Camera to match the one from your QCamera:

In your C++ :

engine.rootContext()->setContextProperty("deviceId", cameraInfo.deviceName);

and in your QML :

Camera {
    id: camera
    deviceId: cameraDeviceId
}

VideoOutput {
    id: videoOutput
    anchors.fill: parent
    source: camera
}

Upvotes: 1

Related Questions