I tried to compile one my application with Qt5. Application was using QML camera and sharing image to C++ code for further processing.
Following is sample code, it works with Qt5 and Qt Multimedia 5.
Lets start with ImageProcessor class, the C++ class which is called from QML to further image processing.
Following is header file for ImageProcessor class, it declares processImage() slot which can be invoked from QML code.
Following is sample code, it works with Qt5 and Qt Multimedia 5.
Lets start with ImageProcessor class, the C++ class which is called from QML to further image processing.
Following is header file for ImageProcessor class, it declares processImage() slot which can be invoked from QML code.
#ifndef IMAGEPROCESSOR_H
#define IMAGEPROCESSOR_H
#include <QObject>
class ImageProcessor : public QObject
{
Q_OBJECT
public:
explicit ImageProcessor(QObject *parent = 0);
public slots:
void processImage( const QString& image);
};
#endif // IMAGEPROCESSOR_H
Following is cpp file for ImageProcessor class. processImage() function retrieves Image from camera image provider. Once we have valid image, we can process it further.#include "imageprocessor.h"
#include <QtQml/QmlEngine>
#include <QtQml/QmlContext>
#include <QtQuick/QQuickImageProvider>
#include <QDebug>
ImageProcessor::ImageProcessor(QObject *parent)
: QObject(parent)
{}
void ImageProcessor::processImage( const QString& path)
{
QUrl imageUrl(path);
QQmlEngine* engine = QQmlEngine::contextForObject(this)->engine();
QQmlImageProviderBase* imageProviderBase = engine->imageProvider(
imageUrl.host());
QQuickImageProvider* imageProvider = static_cast<QQuickImageProvider>
(imageProviderBase);
QSize imageSize;
QString imageId = imageUrl.path().remove(0,1);
QImage image = imageProvider->requestImage(imageId, &imageSize, imageSize);
if( !image.isNull()) {
//process image
}
}
Now we need to register ImageProcessor class with QML. so that we can use it from QML code. This can be done by using qmlRegisterType global function.#include <QtGui/QGuiApplication>
#include <QQmlEngine>
#include <QQmlComponent>
#include <QtQuick/QQuickView>
#include "imageprocessor.h"
int main(int argc, char *argv[])
{
qmlRegisterType<ImageProcessor>("ImageProcessor", 1, 0, "ImageProcessor");
QGuiApplication app(argc, argv);
QQuickView view;
QObject::connect(view.engine(),SIGNAL(quit()),&app,SLOT(quit()));
view.setSource(QUrl::fromLocalFile("qml/main.qml"));
view.show();
return app.exec();
}
That's all from C++ side, QML code is event easier. Following how you can use ImageProcess class from QML code.import QtQuick 2.0
import QtMultimedia 5.0
import ImageProcessor 1.0
Rectangle {
width: 360
height: 360
//shows live preview from camera
VideoOutput {
source: camera
anchors.fill: parent
focus : visible
}
//shows captured image
Image {
id: photoPreview
anchors.fill: parent
fillMode: Image.PreserveAspectFit
}
Camera {
id: camera
imageProcessing.whiteBalanceMode: CameraImageProcessing.WhiteBalanceFlash
captureMode: Camera.CaptureStillImage
exposure {
exposureCompensation: -1.0
exposureMode: Camera.ExposurePortrait
}
flash.mode: Camera.FlashRedEyeReduction
imageCapture {
onImageCaptured: {
photoPreview.source = preview
imageProcessor.processImage(preview);
}
}
}
MouseArea{
anchors.fill: parent
onClicked: {
camera.imageCapture.capture();
}
}
//image processor for further image processing
ImageProcessor{
id: imageProcessor
}
}






























