Connecting QQuickImageProvider
-
Can you describe your use case ?
-
Answering my own question
Problem solved. Here is the solution step by step:1 - Create a
classthat inherits fromQQuickImageProviderandQObjectand inside it create aImagemember(QImage)that is the image to be provided.class provedorImagem : public QObject, public QQuickImageProviderImplement the
virtual requestImagemethod. This is the method that will return the image to QmlQImage requestImage(const QString &id, QSize *size, const QSize &requestedSize)Create a method to load the provider’s image to return
void provedorImagem::carregaImagem(QImage imagemRecebida) { imagem = imagemRecebida; }Now set it as the engine image provider in the
main.cppfileprovedorImagem *provedorImg = new provedorImagem; engine.rootContext()->setContextProperty("ProvedorImagem", provedorImg);2 - Create another
classthat inherits fromQObject.class processaImagem : public QObjectInside this class you must implement a method that will get the image from
cameraprovider, perform the image modifications and return the modified image.
PS: Thep_caminhoImagemis apropertythat I created inside theprocessaImagem classthat receives thecamera preview path.QImage processaImagem::carregaImagem() { QUrl caminhoImagem(p_caminhoImagem); 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); QImage imagem = imageProvider->requestImage(imageId, &imageSize, imageSize); if(imagem.isNull()) { imagem = QImage(); } else { //Perform the modifications } return imagem; }3 - Now is the main part. The image
requestImageprovider method must receive the modified image from theprocessaImagem classto provide it to QML. To do it the providerclass pointermust be accessible to the QML file, so, in themain.cppfile just make the pointer available to QML as apropertyengine.rootContext()->setContextProperty("ProvedorImagem", provedorImg);and register the
processaImagem classas a QML typeqmlRegisterType<processaImagem>("ProcessaImagemQml", 1, 0, "ProcessaImagem");Now we link it inside the QML file
ProvedorImagem.carregaImagem(processaImagem.carregaImagem());4 - It is done. Now just request the image from the provider:
imagemPreview.source = "image://provedor/imagemEditada_" + camera.numeroImagem.toString(); -
But why not do the processing directly in the image provider ?
-
Why not grab the image in the image provider ?
-
Unless I'm mistaken all what your processaImagem does is get the image from the image provider, apply some modification and set it again on the image provider, right ?
-
Are you using the Camera QML type to grab the image from the camera ?
-
In that case, why not use a ShaderEffect to "cut" your image square ?
-
@SGaist
Because I didnt know that ShaderEffect can "cut" an image. I read the documentation and I could not find how to do it with ShaderEffect.Now I am interested about how to perform the image modifications directly in the provider. How can I pass the image without other class to do it?
Another thing, the modifications performed in the image include rotate the image in case of wrong image orientation.