I am trying to implement it with QQuickImageProvider but I am not understanding why it is not working. My code is below. If you could help me.
.h
#ifndef MANIPULAIMAGEM_H
#define MANIPULAIMAGEM_H
#include <QObject>
#include <QImage>
#include <QQuickImageProvider>
#include <QQmlEngine>
#include <QQmlContext>
class manipulaImagem
: public QObject,
public QQuickImageProvider
{
Q_OBJECT
public slots:
public:
manipulaImagem
(QObject *parent
= 0);
private:
};
#endif // MANIPULAIMAGEM_H
#ifndef MANIPULAIMAGEM_H
#define MANIPULAIMAGEM_H
#include <QObject>
#include <QImage>
#include <QQuickImageProvider>
#include <QQmlEngine>
#include <QQmlContext>
class manipulaImagem : public QObject, public QQuickImageProvider
{
Q_OBJECT
public slots:
QString recortarFotoPerfil(const QString &imagem, QRect rectRecorte);
public:
manipulaImagem(QObject *parent = 0);
QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize);
private:
void alocaImagem(const QString &imagem, QRect rectRecorte);
QImage imagemEditada;
};
#endif // MANIPULAIMAGEM_H
To copy to clipboard, switch view to plain text mode
.cpp
#include "manipulaimagem.h"
#include <QDebug>
manipulaImagem
::manipulaImagem(QObject *parent
) : QQuickImageProvider
(QQmlImageProviderBase
::Image){
}
{
if(imagemEditada.isNull())
{
qDebug() << "Request image: (image is null)";
}
else
{
qDebug() << "Request image: image is OK";
}
return imagemEditada;
}
void manipulaImagem
::alocaImagem(const QString &imagem,
QRect rectRecorte
) {
QUrl caminhoImagem
(imagem
);
QQmlEngine *engine = QQmlEngine::contextForObject(this)->engine();
QQmlImageProviderBase *imageProviderBase = engine->imageProvider(caminhoImagem.host());
QQuickImageProvider *imageProvider = static_cast<QQuickImageProvider*>(imageProviderBase);
QString imageId
= caminhoImagem.
path().
remove(0,
1);
imagemEditada = imageProvider->requestImage(imageId, &imageSize, imageSize);
if(imagemEditada.isNull())
{
qDebug() << "Loading image failed";
}
else
{
qDebug() << "Loading image OK";
}
}
{
this->alocaImagem(imagem, rectRecorte);
QString a
= "image://ProvedorImagens/imagemEditada";
if(imagemEditada.isNull())
{
qDebug() << "Imagem is null";
}
else
{
qDebug() << "Imagem is loaded";
}
return a;
}
#include "manipulaimagem.h"
#include <QDebug>
manipulaImagem::manipulaImagem(QObject *parent) : QQuickImageProvider(QQmlImageProviderBase::Image)
{
}
QImage manipulaImagem::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
{
if(imagemEditada.isNull())
{
qDebug() << "Request image: (image is null)";
}
else
{
qDebug() << "Request image: image is OK";
}
return imagemEditada;
}
void manipulaImagem::alocaImagem(const QString &imagem, QRect rectRecorte)
{
QUrl caminhoImagem(imagem);
QQmlEngine *engine = QQmlEngine::contextForObject(this)->engine();
QQmlImageProviderBase *imageProviderBase = engine->imageProvider(caminhoImagem.host());
QQuickImageProvider *imageProvider = static_cast<QQuickImageProvider*>(imageProviderBase);
QSize imageSize;
QString imageId = caminhoImagem.path().remove(0, 1);
imagemEditada = imageProvider->requestImage(imageId, &imageSize, imageSize);
if(imagemEditada.isNull())
{
qDebug() << "Loading image failed";
}
else
{
qDebug() << "Loading image OK";
}
}
QString manipulaImagem::recortarFotoPerfil(const QString &imagem, QRect rectRecorte)
{
this->alocaImagem(imagem, rectRecorte);
QString a = "image://ProvedorImagens/imagemEditada";
if(imagemEditada.isNull())
{
qDebug() << "Imagem is null";
}
else
{
qDebug() << "Imagem is loaded";
}
return a;
}
To copy to clipboard, switch view to plain text mode
.qml
ManipulaImagem {
id: manipulaImagem
}
Camera {
id: camera
captureMode: Camera.CaptureStillImage
imageCapture {
onImageCaptured: {
previewImage.source = manipulaImagem.recortarFotoPerfil(preview, viewfinder.mapRectToSource(Qt.rect(viewfinder.x, viewfinder.y, viewfinder.width, viewfinder.height)));
}
}
}
Rectangle {
id: previewRectangle
visible: false
anchors.fill: parent
Image {
id: previewImage
fillMode: Image.PreserveAspectFit
anchors.top: parent.top
width: parent.width
height: parent.width
}
ManipulaImagem {
id: manipulaImagem
}
Camera {
id: camera
captureMode: Camera.CaptureStillImage
imageCapture {
onImageCaptured: {
previewImage.source = manipulaImagem.recortarFotoPerfil(preview, viewfinder.mapRectToSource(Qt.rect(viewfinder.x, viewfinder.y, viewfinder.width, viewfinder.height)));
}
}
}
Rectangle {
id: previewRectangle
visible: false
anchors.fill: parent
Image {
id: previewImage
fillMode: Image.PreserveAspectFit
anchors.top: parent.top
width: parent.width
height: parent.width
}
To copy to clipboard, switch view to plain text mode
The output of this code is:
Loading image OK
Imagem is loaded
Request image: (image is null)
QML Image: Failed to get image from provider: image://provedorimagens/imagemEditada
What happens is that when I call the functions the image is not null, but when I try to return the QImage using the provider it cant return the image. I dont know why but for the image provider the image is null.
How could I solve that?
Bookmarks