Image Provider for gif format ?



  • Hi Guys, I have a problem implementing my own QQuickAsyncImageProvider for gif image format.

    QML AnimatedImage: Error Reading Animated Image File image: // db_image_response / 9

    Please, I wait for your answers.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Please provide more details like the Qt version you are using as well as the code you are currently writing.



  • Thanks for the replay, I'm using Qt5.9.1 and a piece of code is the following:
    // QML
    import QtQuick 2.9
    import QtQuick.Layouts 1.3
    import QtQuick.Controls 2.2
    import QtQuick.Controls.Material 2.2
    import QtGraphicalEffects 1.0

    import "../common"
    import org.ingenii.cs.data 1.0

    Page {
    property string name: "HomePage"

    property int myIndex: index
    contentHeight: root.implicitHeight
    
    clip: true
    
    Pane {
        id: root
        anchors.fill: parent
        contentItem: ColumnLayout {
            id: theContent
    
            spacing: 1
            RowLayout{
                anchors.horizontalCenter: parent.horizontalCenter
                Layout.preferredHeight: root.implicitHeight * 0.80
    
                Image {
                    id: img
                    anchors.fill: parent
                    fillMode: Image.PreserveAspectFit
    
                    mipmap: true
                    smooth: false
                    cache: true
                    asynchronous: true
    
                    source: "qrc:/images/test/portada2.jpg"
                }// image
            } // row image
            RowLayout{
                anchors.horizontalCenter: parent.horizontalCenter
                Layout.preferredWidth: root.width * 0.95
                Layout.preferredHeight: root.implicitHeight * 0.10
                SwipeView {
                    id: sloganSwipeView
                    currentIndex: 0
                    anchors.fill: parent
                    
                    Repeater {
                        model: dataManager.sloganPropertyList
                        delegate: Loader {
                            
                            active: SwipeView.isCurrentItem //|| SwipeView.isNextItem || SwipeView.isPreviousItem
                            sourceComponent: Pane {
                                anchors.fill: parent
        
                                Image {
                                    //                                    z: 1
                                    anchors.fill: parent
                                    source: imageResponse + modelData.file
                                    fillMode: Image.PreserveAspectCrop
                                }
                                // FIXME: The asynchronous provider does not work with animated images (gif)
                                //                                AnimatedImage {
                                //                                    id: name
                                //                                    anchors.fill: parent
                                //                                    fillMode: Image.PreserveAspectFit
                                //                                    mipmap: true
                                //                                    asynchronous: true
                                //                                    source: imageResponse + "7"
                                //                                }
                                LabelBody {
                                    //                                    z:1
                                    //                                    anchors.fill: parent
                                    anchors.centerIn: parent
                                    text: modelData.text
                                }
                            }
                        }
                    }// repeater
                } // swipeView
            } // row swipeView
        } // column layout
    }// root pane
    
    // emitting a Signal could be another option
    Component.onDestruction: {
        cleanup()
    }
    
    // called immediately after Loader.loaded
    function init() {
        console.log("Init done from Home Page")
    }
    // called from Component.destruction
    function cleanup() {
        console.log("Cleanup done from Home Page")
    }
    

    }

    // CPP
    class AsyncImageResponse2 : public QQuickImageResponse
    {
    public:
    AsyncImageResponse2(const QString &id, const QSize &requestedSize);

    virtual ~AsyncImageResponse2();
    
    QQuickTextureFactory *textureFactory() const;
    
    QString m_id;
    QSize m_requestedSize;
    QImage m_image;
    
    void run();
    

    private:
    bool loadDefaultImage(QImage& img);
    };

    class DBImageResponseProvider : public QQuickAsyncImageProvider
    {
    public:
    DBImageResponseProvider();

    QQuickImageResponse* requestImageResponse(const QString &id,
                                              const QSize &requestedSize);
    

    private:
    QThreadPool m_pool;
    };

    class DBImageResponseProviderExtensionPlugin : public QQmlExtensionPlugin
    {
    // Q_PLUGIN_METADATA(IID "org.ingenii.ii.IDBImageResponeseProviderPlugin" FILE "mymetadata.json")
    Q_PLUGIN_METADATA(IID "org.ingenii.ii.QDBImageResponeseProviderPlugin")

    public:
    void registerTypes(const char *uri);

    void initializeEngine(QQmlEngine *engine,
                          const char *uri);
    

    };

    //#include "dbimageresponseprovider.moc"

    #endif // DBIMAGERESPONSEPROVIDER_H

    AsyncImageResponse2::AsyncImageResponse2(const QString &id,
    const QSize &requestedSize)
    : QQuickImageResponse(),
    m_id(id),
    m_requestedSize(requestedSize)
    {
    // setAutoDelete(false);
    }

    AsyncImageResponse2::~AsyncImageResponse2()
    {

    }

    QQuickTextureFactory* AsyncImageResponse2::textureFactory() const
    {
    return QQuickTextureFactory::textureFactoryForImage(m_image);
    }

    void AsyncImageResponse2::run()
    {
    DataManagerPtr dataManager = DataManager::instance();

    bool result = false;
    
        File* file = dataManager->findFileById(m_id.toInt());
    
        if (file) {
            File::ImageFormat format;
            format = File::suffixToImageFormat(file->suffix());
            QByteArray formatBytea = File::imageFormatToByteArray(format);
    
            m_image = QImage::fromData(file->conten(), formatBytea);
    
            file->deleteLater();
    
            if(!m_image.isNull())
            {
                qDebug()<<"Loded image "<<m_id;
                result = true;
            }
        }
    
    
    if (!result) {
        qDebug()<<"No Loded image "<<m_id;
        loadDefaultImage(m_image);
    }
    
    emit finished();
    

    }

    bool AsyncImageResponse2::loadDefaultImage(QImage& img)
    {
    bool result = img.load(":/images/test/portada2.jpg", "JPG");

    if(result)
        qDebug()<<"DBImageResponseProvider loded default image";
    else
        qDebug()<<"DBImageResponseProvider no loded default image";
    
    return result;
    

    }

    DBImageResponseProvider::DBImageResponseProvider():
    QQuickAsyncImageProvider()
    {

    }

    QQuickImageResponse* DBImageResponseProvider::
    requestImageResponse(const QString &id,
    const QSize &requestedSize)
    {
    AsyncImageResponse2* response = new AsyncImageResponse2(id, requestedSize);

    QFuture<void> result = QtConcurrent::run(response, &AsyncImageResponse2::run);
    
    Q_UNUSED(result)
    
    return response;
    

    }

    void DBImageResponseProviderExtensionPlugin::registerTypes(const char *uri)
    {
    Q_UNUSED(uri);
    }

    void DBImageResponseProviderExtensionPlugin::initializeEngine(QQmlEngine *engine,
    const char *uri)
    {
    Q_UNUSED(uri);
    engine->addImageProvider("db_image_response", new DBImageResponseProvider);
    }

    Guys I need help, please


  • Lifetime Qt Champion

    Just thinking of something, why not use the AnimatedImage QML Type ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.