0

I captured an Image with camera in my qt quick application.I want to send the path to my c++ code and load that Image in c++ QImage.But the Path is image://camera/preview_1 and I don't know How work with that path?

Camera {
    id: camera

    imageCapture {
        onImageCaptured: {
            console.log("Preview = "+preview);
            photoPreview.source = preview
            console.log("CapturedImagePath => "+camera.imageCapture.capturedImagePath);
            up.loadImage(preview);
        }
}

c++ class

void UserProfile::loadImage(QString path)
{    
    QUrl imageUrl(path);
    qWarning()<<"imageUrl.host()=>"<<imageUrl.host();
    qWarning()<<"imageUrl.path()=>"<<imageUrl.path();
    qWarning()<<"imageUrl.toLocalFile()=>"<<imageUrl.toLocalFile();
    bool isOpend= m_image.load(path); //m_image is an QImage object
    qWarning()<<"Image loaded=> "<<isOpend;
}

Application output

D MyApp: qml: Preview = image://camera/preview_1
D MyApp: qml: CapturedImagePath =>
W MyApp: imageUrl.host()=> "camera"
W MyApp: imageUrl.path()=> "/preview_1"
W MyApp: imageUrl.toLocalFile()=> ""
W MyApp: Image loaded=> false
mohsen
  • 1,763
  • 3
  • 17
  • 55
  • I suspect you really want [captureToLocation](https://doc.qt.io/qt-5/qml-qtmultimedia-cameracapture.html#captureToLocation-method) and the [imageSaved](https://doc.qt.io/qt-5/qml-qtmultimedia-cameracapture.html#imageSaved-signal) event – selbie Dec 02 '19 at 08:04
  • @selbie I seaid in [my another question](https://stackoverflow.com/questions/59117481/camera-not-saving-the-captured-image).the imageSaved event don't work – mohsen Dec 02 '19 at 08:11
  • 1
    It's not common practice for someone answering your question to know what you've asked before. Every question you ask needs to be complete with all relevant information. Basically, we need [mcve] – selbie Dec 02 '19 at 08:19

1 Answers1

3

The URL image://camera/preview_1 means that the image data is living in a QQuickImageProvider instance. Probably, it's a QQuickImageProvider instance created by Camera.

As the UserProfile instance is living in the same QQmlEngine as the camera lives, you can

void UserProfile::loadImage(const QString &path)
{
    auto myQmlEngine = qmlEngine(this);
    if(myQmlEngine==nullptr)
        return;

    QUrl imageUrl(path);
    auto provider = reinterpret_cast<QQuickImageProvider*>( myQmlEngine->imageProvider(imageUrl.host()));

    if (provider->imageType()==QQuickImageProvider::Image){
        QImage img = provider->requestImage(imageUrl.path().remove(0,1),nullptr,QSize());
        // do whatever you want with the image
    } 
}

Be careful with the reinterpret_cast. You also have to make sure that the QQuickImageProvider::imageType() is returning QQmlImageProviderBase::Image.

and you could also use capturedImagePath instead of the preview URL to prevent this complexity if it could be an option for your usecase.

Soheil Armin
  • 2,725
  • 1
  • 6
  • 18
  • your answer worked,but after chaning `imageUrl.host().remove(0,1)` to `imageUrl.path().remove(0,1)` – mohsen Dec 04 '19 at 07:21