Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to save an image from QML Image element?
Forum Updated to NodeBB v4.3 + New Features

How to save an image from QML Image element?

Scheduled Pinned Locked Moved QML and Qt Quick
27 Posts 12 Posters 28.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    ngocketit
    wrote on 20 Feb 2011, 11:43 last edited by
    #4

    Something like this:

    @
    class ImageSaver : public QObject
    {
    Q_OBJECT
    public:
    explicit ImageSaver(QObject *parent = 0);

    public slots:
    void save(QObject *item, const QString &path);

    };

    void ImageSaver::save(QObject *imageObj, const QString &path)
    {
    QGraphicsObject item = qobject_cast<QGraphicsObject>(imageObj);

    if (!item) {
        qDebug() << "Item is NULL";
        return;
    }
    
    QImage img(item->boundingRect().size().toSize(), QImage::Format_RGB32);
    img.fill(QColor(255, 255, 255).rgb());
    QPainter painter(&img);
    QStyleOptionGraphicsItem styleOption;
    item->paint(&painter, &styleOption);
    img.save(path);
    

    }
    @

    And then in QML:
    @
    import Qt 4.7

    Rectangle {
    width: 640
    height: 480
    color: "white"

    Image {
        id: imgItem
        fillMode: Image.PreserveAspectFit
        source: "test.jpg"
    }
    
    MouseArea {
        anchors.fill: parent
        onClicked: {
            imageSaver.save(imgItem, "/home/meego/saved-test.jpg");
        }
    }
    

    }
    @

    with imageSaver is an instance of ImageSaver and set as context property.

    Cheers
    Phi

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kibsoft
      wrote on 21 Feb 2011, 16:09 last edited by
      #5

      ngocketit, thanks! It works very nice!

      1 Reply Last reply
      0
      • C Offline
        C Offline
        ciffa
        wrote on 17 Mar 2011, 12:46 last edited by
        #6

        Hi

        I'm have a problem with this code. I can't get it to work... When I try to paint the QGraphicsObject onto the QImage, I end up with the color (white in this case) that was filled in the image before the paint job. I'm using the code just as shown above. I would therefore guess that the paint method does not work. Is there any difference between doing this on the Desktop platform or the Symbian platform? Is there anything else that could make my situation different?

        @ QGraphicsObject item = qobject_cast<QGraphicsObject>(imageObj);

        if (!item) {
            qDebug() << "Item is NULL";
            return false;
        }
        
        QImage img(item->boundingRect().size().toSize(), QImage::Format_RGB32);
        img.fill(QColor(255, 255, 255).rgb());
        QPainter painter(&img);
        QStyleOptionGraphicsItem styleOption;
        item->paint(&painter, &styleOption);
        
        // Then I add the image to a database, but that should be irrelevant
        

        @

        I really, really need some help! ...I'm drawing a complete blank on this one (pun intended).

        1 Reply Last reply
        0
        • C Offline
          C Offline
          ciffa
          wrote on 23 Mar 2011, 11:54 last edited by
          #7

          Yay. I figured it out!

          The problem was that I was calling this save method when Image.onProgressChanged() fired and Image.progress == 1. This proved however to be before the Image was actually drawn onto the screen. The solution was to call the method when Image.onPaintedGeometryChanged() fired and Image.progress == 1.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            cmer4
            wrote on 25 Oct 2011, 12:53 last edited by
            #8

            Hi folks,

            The snippet works but I am unsure how the path variable works on Symbian?
            What drive the image saves to when it says "saved".

            Is there a way to set a proper folder like e:\folder on symbian^3?

            1 Reply Last reply
            0
            • S Offline
              S Offline
              simonpena
              wrote on 28 Oct 2011, 19:59 last edited by
              #9

              Hey @ngocketit, thanks a lot! It worked great :)

              My Harmattan applications:

              • Butaca: cinema information - http://bit.ly/nJGBg0
              • Meneamigo: a Meneame client - http://bit.ly/qD4Bss
              1 Reply Last reply
              0
              • D Offline
                D Offline
                DonRico
                wrote on 17 Jun 2014, 12:49 last edited by
                #10

                Hi

                Little help please. That piece of code probably works on Qt 4.7 but in 5.3 it doesn't. -The problem lays in qobject_cast -ing since it requires the source to inherit QObject, but QML Image is based on QtObject class.-

                My bad QtObject inherits QObject but I'm still getting empty object after casting. By debugging the source imageobject seems to be normal QQuickImage object. Any ideas?

                Since I'm really inexperienced in Qt c++ part, could anyone help with that problem.

                Thanx.

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  Djay96
                  wrote on 17 Jun 2014, 13:09 last edited by
                  #11

                  QDeclarativeView *view = new QDeclarativeView();
                  QPixmap::grabWidget(view).save(outputFile, 0, 100);

                  create view and grab image file in outputFile

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    DonRico
                    wrote on 17 Jun 2014, 13:34 last edited by
                    #12

                    Sorry, I'm not quite sure how to implement your solution. Also QDeclarativeView isn't used in Qt5 and Pixmap::grapWidget() also depricated.

                    But thnx anyways :)

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      Djay96
                      wrote on 19 Jun 2014, 10:24 last edited by
                      #13

                      sorry I am using older version (As per requirement ) I appreciate your reply

                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        DonRico
                        wrote on 27 Jun 2014, 13:27 last edited by
                        #14

                        Hi

                        Does anybody have an idea why I can't pass the Image object to the save function. The path comes over nicely but "imageObj" pointer in save() function turns out empty?

                        1 Reply Last reply
                        0
                        • Q Offline
                          Q Offline
                          qomg
                          wrote on 28 Jun 2014, 02:52 last edited by
                          #15

                          Hi

                          I also have an empty pointer. I do not know where to look to solve this problem.

                          1 Reply Last reply
                          0
                          • jeremy_kJ Online
                            jeremy_kJ Online
                            jeremy_k
                            wrote on 28 Jun 2014, 03:09 last edited by
                            #16

                            For Qt 5 and later, "Canvas":http://qt-project.org/doc/qt-5/qml-qtquick-canvas.html has loadImage(url) and save(filename) functions.

                            Asking a question about code? http://eel.is/iso-c++/testcase/

                            1 Reply Last reply
                            0
                            • V Offline
                              V Offline
                              Vincent007
                              wrote on 28 Jun 2014, 03:24 last edited by
                              #17

                              wait for QQuickRenderControl

                              1 Reply Last reply
                              0
                              • Q Offline
                                Q Offline
                                qomg
                                wrote on 28 Jun 2014, 03:38 last edited by
                                #18

                                I tried to use the Canvas, and also I could not save.
                                I'll try again tomorrow ..

                                1 Reply Last reply
                                0
                                • jeremy_kJ Online
                                  jeremy_kJ Online
                                  jeremy_k
                                  wrote on 28 Jun 2014, 04:54 last edited by
                                  #19

                                  Image loading and saving via Canvas:

                                  @import QtQuick 2.2
                                  import QtQuick.Window 2.1

                                  Window {
                                  width: imgLoader.sourceSize.width
                                  height: imgLoader.sourceSize.height
                                  visible: true
                                  property string loadUrl: "file://tmp/image.png"
                                  property string saveUrl: "/tmp/i2.png"

                                  Canvas {
                                      id: root
                                      anchors.fill: parent
                                      onPaint: {
                                          var ctx = getContext("2d")
                                          ctx.drawImage(imgLoader, 0, 0)
                                      }
                                  }
                                  MouseArea {
                                      anchors.fill: parent
                                      onClicked: {
                                          if (root.save(saveUrl)) {
                                              console.log("save succeeded")
                                          }
                                          else {
                                              console.log("save failed")
                                          }
                                      }
                                  }
                                  Image {
                                      id: imgLoader
                                      visible: false
                                      source: loadUrl
                                  }
                                  

                                  }@

                                  Note that the parameter to Canvas::save() is a filename, not a URL. It fails when "file://tmp/i2.png" is used.

                                  Asking a question about code? http://eel.is/iso-c++/testcase/

                                  1 Reply Last reply
                                  0
                                  • Q Offline
                                    Q Offline
                                    qomg
                                    wrote on 28 Jun 2014, 14:21 last edited by
                                    #20

                                    jeremy_k thanks it works great.

                                    1 Reply Last reply
                                    0
                                    • B Offline
                                      B Offline
                                      beemaneni
                                      wrote on 1 Aug 2014, 07:57 last edited by
                                      #21

                                      Hi
                                      does the above code really draw an image in the given path?
                                      For me the file gets created but nothing in it... i have given save url as "/storage/sdcard0/a1.png".

                                      Can u help me ?

                                      Thanks in Advance,,

                                      Bala B
                                      Infinite Computer systems
                                      Chennai

                                      1 Reply Last reply
                                      0
                                      • D Offline
                                        D Offline
                                        DonRico
                                        wrote on 1 Aug 2014, 08:12 last edited by
                                        #22

                                        Hi

                                        I decided to solve this a bit differently. I created a new QML type (c++ class) that takes in image url (local or remote) and spits out local url. In case of beginning http:// or https:// class checks if image is already downloaded from the remote server and in that case spits the local url right out w/o wasting the precious mobile data bandwidth.And then there a QML component that fills out the Image type source property when the local url is returned from my so called "Caching class". If anybody's interest I can strip it out of my project and put it up to github with decent comments.

                                        1 Reply Last reply
                                        0
                                        • B Offline
                                          B Offline
                                          beemaneni
                                          wrote on 1 Aug 2014, 08:44 last edited by
                                          #23

                                          Hi DonRico
                                          If u can post that code that would be an great help for me...can u?

                                          Thanks

                                          Bala B
                                          Infinite Computer systems
                                          Chennai

                                          1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved