Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Pass Camera preview from QML to C++

Pass Camera preview from QML to C++

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 2 Posters 1.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kolegs
    wrote on 16 Jan 2015, 20:17 last edited by
    #1

    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.4

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

    Item {
        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

    1 Reply Last reply
    0
    • C Offline
      C Offline
      cincirin
      wrote on 20 Jan 2015, 07:10 last edited by
      #2

      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.

      1 Reply Last reply
      0

      2/2

      20 Jan 2015, 07:10

      • Login

      • Login or register to search.
      2 out of 2
      • First post
        2/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved