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. Byte array to QImage
Forum Updated to NodeBB v4.3 + New Features

Byte array to QImage

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 8 Posters 4.6k Views 3 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.
  • C Offline
    C Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on 6 Apr 2019, 05:59 last edited by
    #3

    Do you really think the camera is sending jpeg images?

    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
    Visit the Qt Academy at https://academy.qt.io/catalog

    1 Reply Last reply
    2
    • H hardikgarg19
      5 Apr 2019, 21:29

      I am trying to form a QImage from the Byte array and save the image. Below is the code:
      BYTE* pBuf=(BYTE*)malloc(iBytes*sy);
      I have copied all the image data in this pBuf from the camera (not copying that code)

          QImage img(1200,1920,QImage::Format_Mono);
          img = QImage::fromData((uchar*)pBuf,iBytes*sy,"JPG");
          img.save( "C:\\test_folder\\file.JPG" );
      

      After running this code, a file.JPG is saved but it is not a valid file. When I am trying to open this file the system is displaying the message "it looks like we don't support this format".
      How can I solve this issue?

      B Offline
      B Offline
      beecksche
      wrote on 6 Apr 2019, 06:04 last edited by beecksche 4 Jun 2019, 06:12
      #4

      @hardikgarg19
      Maybe the set format is wrong. Try Format_Grayscale8 or Format_RGB32.

      Or try to leave the format blank, so QImage will detect the reading format by itself.

      QImage::fromData((uchar*)pBuf, iBytes*sy);
      

      All described in the docs

      H 1 Reply Last reply 8 Apr 2019, 17:00
      2
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 6 Apr 2019, 07:02 last edited by
        #5

        Hi,

        Besides the points of my fellows, are you sure about the size of the image ? 1200x1920 seems a bit non standard for a camera.

        On that point, what camera is that ?
        How do you get data from it ?
        How did you configure it ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        H 1 Reply Last reply 8 Apr 2019, 17:51
        4
        • B beecksche
          6 Apr 2019, 06:04

          @hardikgarg19
          Maybe the set format is wrong. Try Format_Grayscale8 or Format_RGB32.

          Or try to leave the format blank, so QImage will detect the reading format by itself.

          QImage::fromData((uchar*)pBuf, iBytes*sy);
          

          All described in the docs

          H Offline
          H Offline
          hardikgarg19
          wrote on 8 Apr 2019, 17:00 last edited by
          #6

          @beecksche
          I have tried the change, but, I am getting the bool value as false and no image is created.

              QImage img(1200,1920,QImage::Format_Grayscale8);
              img = QImage::fromData((uchar*)pBuf,iBytes*sy);
              bool b = img.save( "C:\\test_folder\\file_img.BMP");
          
          1 Reply Last reply
          0
          • C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 8 Apr 2019, 17:02 last edited by
            #7

            You really should try to find out what format the camera is sending - guessing is not the correct solution here.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            H 1 Reply Last reply 8 Apr 2019, 18:02
            2
            • M Offline
              M Offline
              mranger90
              wrote on 8 Apr 2019, 17:13 last edited by
              #8

              I'm curious about the "iBytes * sy" in your code.
              If you are truly grabbing an 8 bit gray scale image from the camera then the size of the data will likely be just "iBytes" which, and I'm going out on a limb here, should be imageWidth * imageHeight.
              If you are multiplying this by some factor, the mysterious "sy", then the image is probably RGB24 (sy == 3 ?) or RGB32 (sy == 4 ?).

              1 Reply Last reply
              3
              • S SGaist
                6 Apr 2019, 07:02

                Hi,

                Besides the points of my fellows, are you sure about the size of the image ? 1200x1920 seems a bit non standard for a camera.

                On that point, what camera is that ?
                How do you get data from it ?
                How did you configure it ?

                H Offline
                H Offline
                hardikgarg19
                wrote on 8 Apr 2019, 17:51 last edited by
                #9

                @SGaist Hi,
                I am using a FPGA based camera (http://isgcameras.com/allegro-usb3-family/).

                Below is the code:

                long sx,sy,iBpp;
                //get the image size
                

                //pMedleyUcam is the camera object
                pMedleyUcam->get_SizeX(&sx);
                pMedleyUcam->get_SizeY(&sy);

                pMedleyUcam->GetBytesPerPixel(&iBpp);
                int iBytes=sx*iBpp; // horizontal image size in bytes

                BYTE* pBuf=(BYTE*)malloc(iBytes*sy);
                
                short v;
                pMedleyUcam->Grab(&v);
                BYTE *ptr=pBuf;
                for(int y=0;y<sy;y++)
                    {
                        pMedleyUcam->GetImagePointer(0, sy-1-y,&var);
                        memcpy(ptr,var.pbVal,iBytes);
                        ptr+=iBytes;
                    }
                

                //form the image using the pBuf
                QImage img(1200,1920,QImage::Format_Grayscale8);
                img = QImage::fromData((uchar*)pBuf,iBytes*sy);
                bool b = img.save( "C:\test_folder\file_img.BMP");

                b is returning false here

                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  hskoglund
                  wrote on 8 Apr 2019, 17:59 last edited by
                  #10

                  Hi, if you try with double slashes

                  bool b = img.save( "C:\\test_folder\\file_img.BMP");
                  

                  is b still returning false?

                  H 1 Reply Last reply 8 Apr 2019, 18:13
                  2
                  • C Christian Ehrlicher
                    8 Apr 2019, 17:02

                    You really should try to find out what format the camera is sending - guessing is not the correct solution here.

                    H Offline
                    H Offline
                    hardikgarg19
                    wrote on 8 Apr 2019, 18:02 last edited by
                    #11

                    @Christian-Ehrlicher
                    The camera allows me to check the available pixel format. Below is the list:
                    Mono8
                    Mono10p
                    Mono12p
                    BayerRGB
                    BayerRGB10p
                    BayerRGB12p
                    RGB8
                    RGB565P
                    BGR8
                    YCbCr422_8
                    YUV400_8
                    YUV8_UYV

                    1 Reply Last reply
                    0
                    • H hskoglund
                      8 Apr 2019, 17:59

                      Hi, if you try with double slashes

                      bool b = img.save( "C:\\test_folder\\file_img.BMP");
                      

                      is b still returning false?

                      H Offline
                      H Offline
                      hardikgarg19
                      wrote on 8 Apr 2019, 18:13 last edited by
                      #12

                      @hskoglund Hi,
                      Yes, it is still returning false.

                      1 Reply Last reply
                      0
                      • H Offline
                        H Offline
                        hskoglund
                        wrote on 8 Apr 2019, 18:41 last edited by hskoglund 4 Aug 2019, 18:46
                        #13

                        Looks like you're only getting the raw pixels from the camera (in the specified pixel format). To save to a file, like a BMP file., you need an header as well.
                        To get one, you could try create an empty QImage with the specified x and y size, and then fill it with those pixels you get from the camera, say something like:

                        QImage img(1200,1920,QImage::Format_Grayscale8);
                        for (int y = 0; (y < 1920); ++y)
                            for (int x = 0; (x < 1200); ++x)
                                img.setPixel(x,y,((uchar*)pBuf) + x + 1200 * y);
                        H 1 Reply Last reply 8 Apr 2019, 21:07
                        4
                        • F Offline
                          F Offline
                          fcarney
                          wrote on 8 Apr 2019, 18:51 last edited by
                          #14

                          According to this SO question you can do a line at a time:
                          https://stackoverflow.com/questions/14563180/raw-data-to-qimage

                          C++ is a perfectly valid school of magic.

                          H 1 Reply Last reply 8 Apr 2019, 21:38
                          1
                          • H hskoglund
                            8 Apr 2019, 18:41

                            Looks like you're only getting the raw pixels from the camera (in the specified pixel format). To save to a file, like a BMP file., you need an header as well.
                            To get one, you could try create an empty QImage with the specified x and y size, and then fill it with those pixels you get from the camera, say something like:

                            QImage img(1200,1920,QImage::Format_Grayscale8);
                            for (int y = 0; (y < 1920); ++y)
                                for (int x = 0; (x < 1200); ++x)
                                    img.setPixel(x,y,((uchar*)pBuf) + x + 1200 * y);
                            H Offline
                            H Offline
                            hardikgarg19
                            wrote on 8 Apr 2019, 21:07 last edited by
                            #15

                            @hskoglund Hi,
                            Yes, that was the problem. Thank you so much.

                            1 Reply Last reply
                            0
                            • F fcarney
                              8 Apr 2019, 18:51

                              According to this SO question you can do a line at a time:
                              https://stackoverflow.com/questions/14563180/raw-data-to-qimage

                              H Offline
                              H Offline
                              hardikgarg19
                              wrote on 8 Apr 2019, 21:38 last edited by
                              #16

                              @fcarney Hi,
                              Thank you so much for sharing the link. I have used the QImage.scanLine and it worked.

                              1 Reply Last reply
                              0

                              12/16

                              8 Apr 2019, 18:13

                              • Login

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