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. Cannot load qimage from byte array in release mode
QtWS25 Last Chance

Cannot load qimage from byte array in release mode

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 5 Posters 1.8k 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.
  • U Offline
    U Offline
    umriyaev
    wrote on last edited by
    #1

    In my app I receive images in JPEG format from Android device over QTcpSocket as a byte array.

    When I run this app on my dev PC it works like a charm (both in debug and release modes).

    However, I'm not able to load QImage from byte array on another PC where QT is not installed.

    I included all the *.dll files in QT for my platform (Win_x64, msvc_2015_64) and also installed Visual Studio C++ Redistributable 2015. My app starts fine (doesn't give any errors that some *.dll files are missing). But QImage is empty all the time.

    First I thought that byte array wasn't being received at all, so I checked its size and found that it's being received correctly.

    I'm using this code to load QImage from uchar bytes:

    void handleImageBytes(uchar *bytes, qint64 length)
    {
        QImage image;
        image.loadFromData(bytes, length, "JPEG");
        emit frameReady(image);
    }
    

    And I receive those bytes from QTcpSocket:

    void handleReadyRead()
    {
        if(this->incomingFrameLength==0){
            QString data = this->client->readAll();
            this->incomingFrameLength = data.toInt();
            this->client->write("ok");
            qDebug()<<QString("Received frame length: %1").arg(this->incomingFrameLength);
            return;
        }
    
        if(this->incomingFrameLength!=0 && this->client->bytesAvailable()<this->incomingFrameLength)
             return;
    
    
        uchar* data = (uchar*)malloc(this->incomingFrameLength+256);
        qint64 len = this->client->read((char*)data, this->incomingFrameLength+256);
    
        qDebug() << QString("Received %1 bytes").arg(len);
    
        handleImageBytes(data, len);
    
        this->incomingFrameLength=0;
        this->client->write("received");
        qDebug()<<QString("Received frame of length %1").arg(len);
        free(data);
    }
    

    As you see, first I receive the size of the image as a length of the byte array. Then I wait for all the required bytes are received and ready in the QTcpSocket's buffer. Then I create pointer to the byte array with sufficient size, load incoming bytes to that array and call handleImageBytes method from above.

    This all works fine on my dev PC (windows 7 x64, Qt 5.7, msvc_2015_64). However, loading images from byte array doesn't work when I move my release code with all the required *.dll files to other PC where Qt isn't installed.

    How to solve this problem?

    J.HilkJ 1 Reply Last reply
    0
    • raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @umriyaev said in Cannot load qimage from byte array in release mode:

      However, loading images from byte array doesn't work when I move my release code with all the required *.dll files to other PC where Qt isn't installed.

      most probably you forgot to copy also the imageplugin dlls (or coppied them to the wrong sub-directory)?
      Try again with using the windeployqt tool

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #3

        @umriyaev said in Cannot load qimage from byte array in release mode:

        QString data = this->client->readAll();
        [...]
        uchar* data = (uchar*)malloc(this->incomingFrameLength+256);
        qint64 len = this->client->read((char*)data, this->incomingFrameLength+256);
        

        QString stores data in UTF-16. Better not use it for binary JPEG data, especially not for checking it's length. Use QByteArray instead.

        Also, why do you first read the frame length from QString data, but then discard that and define and read into new uchar * data? Looks like you read same data twice for no reason.

        Anyway, that's off-topic. If the other device (Android) also runs Qt, I would suggest using QDataStream to read and write your QImage - then you don't have to parse the data by hand in any way, it will work automatically.

        If that is not the option, then... hm. Do you get any warnings or error messages on the console when you run the application on the other (non-dev) machine? Have you deployed all dependencies (inluding image format plugins)? Try using the deployment tool to ease the process: http://doc.qt.io/qt-5/windows-deployment.html#the-windows-deployment-tool

        (Z(:^

        1 Reply Last reply
        1
        • U umriyaev

          In my app I receive images in JPEG format from Android device over QTcpSocket as a byte array.

          When I run this app on my dev PC it works like a charm (both in debug and release modes).

          However, I'm not able to load QImage from byte array on another PC where QT is not installed.

          I included all the *.dll files in QT for my platform (Win_x64, msvc_2015_64) and also installed Visual Studio C++ Redistributable 2015. My app starts fine (doesn't give any errors that some *.dll files are missing). But QImage is empty all the time.

          First I thought that byte array wasn't being received at all, so I checked its size and found that it's being received correctly.

          I'm using this code to load QImage from uchar bytes:

          void handleImageBytes(uchar *bytes, qint64 length)
          {
              QImage image;
              image.loadFromData(bytes, length, "JPEG");
              emit frameReady(image);
          }
          

          And I receive those bytes from QTcpSocket:

          void handleReadyRead()
          {
              if(this->incomingFrameLength==0){
                  QString data = this->client->readAll();
                  this->incomingFrameLength = data.toInt();
                  this->client->write("ok");
                  qDebug()<<QString("Received frame length: %1").arg(this->incomingFrameLength);
                  return;
              }
          
              if(this->incomingFrameLength!=0 && this->client->bytesAvailable()<this->incomingFrameLength)
                   return;
          
          
              uchar* data = (uchar*)malloc(this->incomingFrameLength+256);
              qint64 len = this->client->read((char*)data, this->incomingFrameLength+256);
          
              qDebug() << QString("Received %1 bytes").arg(len);
          
              handleImageBytes(data, len);
          
              this->incomingFrameLength=0;
              this->client->write("received");
              qDebug()<<QString("Received frame of length %1").arg(len);
              free(data);
          }
          

          As you see, first I receive the size of the image as a length of the byte array. Then I wait for all the required bytes are received and ready in the QTcpSocket's buffer. Then I create pointer to the byte array with sufficient size, load incoming bytes to that array and call handleImageBytes method from above.

          This all works fine on my dev PC (windows 7 x64, Qt 5.7, msvc_2015_64). However, loading images from byte array doesn't work when I move my release code with all the required *.dll files to other PC where Qt isn't installed.

          How to solve this problem?

          J.HilkJ Online
          J.HilkJ Online
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @umriyaev
          hi,
          this looks like a big/little enianness problem.

          Have you checked that its the same for your DevPc and your android device?

          AFAIK Windows is l-Endian, and most android arm devices are as well so this is a small chance.

          But something you should keep in mind any way, because IIRC network byte order is big-endian, it can be helpful to convert any format you plan on using for data interchange to network byte order.


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          0
          • Vinod KuntojiV Offline
            Vinod KuntojiV Offline
            Vinod Kuntoji
            wrote on last edited by
            #5

            @umriyaev ,

            Have you included qjpeg.dll?

            C++, Qt, Qt Quick Developer,
            PthinkS, Bangalore

            1 Reply Last reply
            2

            • Login

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