Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Reading image from memory
QtWS25 Last Chance

[Solved] Reading image from memory

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 9.5k Views
  • 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.
  • L Offline
    L Offline
    lloydqt
    wrote on last edited by
    #1

    Hi,

    I have a image file (jpeg,bmp...) in QByteArray. I want to display it. For this I am using QLable. I tried loading it with QImage::loadFromData(), and QPixMap::LoadFromData(), but both of them fails! But if I use QImage::Load("filename") the file is shown correctly. What is the right method to display an image from buffer? Also I don't know the image type in buffer, is there any method to check the type of image?

    Thanks,
    Lloyd

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      I would assume that you are facing a formatting issue. Typically any format has some meta information about the object stored. For an image it would be size, color scheme and similar stuff. You need to refer to "the documentation":http://qt-project.org/doc/qt-4.8/qimage.html#image-information and find in which format the image is.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • U Offline
        U Offline
        utcenter
        wrote on last edited by
        #3

        Different image formats have different header data in the beginning, you have to set your format straight so that QImage knows where is the dimensions, color depth and so on so it can locate and process image data properly.

        You don't know the image type in buffer? Well, who put it there? I am not aware if there is build in functionality for this in Qt but most image formats begin with a specific magic number, it gives the image type and thus also the information on the header and so on. Or you could just do a little struct or class to contain the buffer together with format information when the buffer is being filled with an image.

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lloydqt
          wrote on last edited by
          #4

          I think my problem description is not clear enough. I receive the image file from a network socket. So I have no clue about the image format.

          Is QImage is the class that I need to depend on displaying images?

          The only method I could find to load an image from buffer is "QImage::loadFromData()". the second argument for this is "const char * format". I assume it should be something similar to "JPG,JPEG,BMP (Documentation does not say anything about what a"format" is in this function). In my case the image construction phase itself is failing.

          Am I understanding the concepts in a wrong way?

          Thanks,
          Lloyd

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lloydqt
            wrote on last edited by
            #5

            What surprises me is QImage::load("filename") works fine, but a QByteArray containing the same data cannot be loaded correctly using QImage::loadFromData()

            1 Reply Last reply
            0
            • K Offline
              K Offline
              koahnig
              wrote on last edited by
              #6

              If you are receiving your image through a QTcpSocket, you may want to have a look on the "QImageReader":http://qt-project.org/doc/qt-4.8/qimagereader.html#QImageReader-2 I never used it, but it looks suitable for your purpose.

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              0
              • L Offline
                L Offline
                lloydqt
                wrote on last edited by
                #7

                I have written a code block as shown below @
                QByteArray ba;
                //Append image data to ba
                //...
                //...
                QBuffer qbuff(&ba);
                QImageReader qimg;
                qimg.setDecideFormatFromContent(true);
                qimg.setDevice(&qbuff);
                QImage img=qimg.read();
                imageView->setPixmap(QPixmap::fromImage(img));
                @

                But it is not showing the image! Am I doing something wrong?

                Thanks,
                Lloyd

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  koahnig
                  wrote on last edited by
                  #8

                  You could check with "canRead":http://qt-project.org/doc/qt-4.8/qimagereader.html#canRead method and also "error()":http://qt-project.org/doc/qt-4.8/qimagereader.html#error is supported.

                  Vote the answer(s) that helped you to solve your issue(s)

                  1 Reply Last reply
                  0
                  • L Offline
                    L Offline
                    lloydqt
                    wrote on last edited by
                    #9

                    Thanks koahnig, the above methods works

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      koahnig
                      wrote on last edited by
                      #10

                      Glad to hear that your problem has been solved. Please mark the thread with [Solved] in the header line then.

                      Vote the answer(s) that helped you to solve your issue(s)

                      1 Reply Last reply
                      0
                      • L Offline
                        L Offline
                        lloydqt
                        wrote on last edited by
                        #11

                        I would be glad if there is an easy way to mark the thread as solved (using a button or anything like that)

                        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