Pass Camera preview from QML to C++
-
Hey,
I got a problem with passing Camera preview from QML to C++, I need to process it there. Here is my code, anyone knows what I am doing wrong?
Test.cpp
@#include "test.h"Test::Test(QObject *rootObject, QObject *parent) : QObject(parent)
{
QObject *camera = rootObject->findChild<QObject *>(QString("camera"));
setCamera(camera);
}void Test::processPreview(int, const QImage& image)
{
qDebug() << "PROCESS IMAGE" << image;
}void Test::setCamera(QObject *cameraObject)
{
QVariant cameraVariant = cameraObject->property("mediaObject");
QCamera camera = qvariant_cast<QCamera>(cameraVariant);
QCameraImageCapture *imageCapture = new QCameraImageCapture(camera);
connect(imageCapture, SIGNAL(imageCaptured(int, const QImage&)),
this, SLOT(processPreview(int, const QImage&)));
}void Test::setCamera2(QObject *cameraObject)
{
QVariant cameraVariant = cameraObject->property("mediaObject");
QCamera camera = qvariant_cast<QCamera>(cameraVariant);
QMediaService *service = camera->service();
QMediaControl *control = service->requestControl(QCameraImageCaptureControl_iid);
QCameraImageCaptureControl captureControl = qobject_cast<QCameraImageCaptureControl>(control);
connect(captureControl, SIGNAL(imageCaptured(int, const QImage&)),
this, SLOT(processPreview(int, const QImage&)));
}void Test::imageCapture(QString url)
{
qDebug() << url;
}Test::~Test()
{}@
and main.qml
@import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2
import QtMultimedia 5.4ApplicationWindow {
title: qsTr("Hello World")
width: 640
height: 480
visible: trueItem { anchors.fill: parent Camera { objectName: "camera" id: camera imageProcessing.whiteBalanceMode: CameraImageProcessing.WhiteBalanceFlash exposure { exposureCompensation: -1.0 exposureMode: Camera.ExposurePortrait } flash.mode: Camera.FlashRedEyeReduction imageCapture { onImageCaptured: { photoPreview.source = preview // Show the preview in an Image } } } VideoOutput { source: camera anchors.fill: parent focus : visible // to receive focus and capture key events when visible } Image { id: photoPreview onSourceChanged: imageChanged(source) } }
}
@I tried both setCamera and setCamera2 but they doesnt work.
I can see the preview on Android tablet, which I am using for testing.Thanks in advance
-
Hi,
what exactly went wrong ?
I used C++ QCamera(and all related) from QML and everything is working properly, but I didn't try to find camera object from qml root.
Instead I create a property in C++ class, expose it to qml, and from qml assign the camera object to that C++ property.