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. Transferring an image from server to client (raw data of a QImage ?)
Forum Updated to NodeBB v4.3 + New Features

Transferring an image from server to client (raw data of a QImage ?)

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.2k 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.
  • D Offline
    D Offline
    duarna
    wrote on last edited by
    #1

    Hi all!

    I'm trying to send an image from a server to a client through a QJsonObject.

    I figured out a way to create again the image (client-side) with this method:

    QImage(uchar *data, int width, int height, QImage::Format format,)
    

    But on the server side, I can't get how to extract the raw data of a QImage.
    How to use the method bits() ? How to encapsulate these bits in a QJsonObject ?

    Thank you for your help!

    raven-worxR 1 Reply Last reply
    0
    • D duarna

      Hi all!

      I'm trying to send an image from a server to a client through a QJsonObject.

      I figured out a way to create again the image (client-side) with this method:

      QImage(uchar *data, int width, int height, QImage::Format format,)
      

      But on the server side, I can't get how to extract the raw data of a QImage.
      How to use the method bits() ? How to encapsulate these bits in a QJsonObject ?

      Thank you for your help!

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @duarna said in Transferring an image from server to client (raw data of a QImage ?):

      How to use the method bits()

      better use constBits(). It returns the bytes of the raw image data.
      Use the bytesPerLine() / width() / height() or sizeInBytes() methods in a for-loop and store it in a QByteArray.

      To store the data in a JSON format you should convert the QByteArray to a base64 string.

      --- 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
      4
      • D Offline
        D Offline
        duarna
        wrote on last edited by
        #3

        Thank you @raven-worx for your quick reply.

        I'm sorry but can you provide me a short example on how to fulfil a QByteArray with uchar* and how to get back the uchar* from it ?
        I'm lost

        Thank you

        raven-worxR 1 Reply Last reply
        0
        • D duarna

          Thank you @raven-worx for your quick reply.

          I'm sorry but can you provide me a short example on how to fulfil a QByteArray with uchar* and how to get back the uchar* from it ?
          I'm lost

          Thank you

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @duarna
          This will create a QByteArray which is valid as long as also the QImage data is valid. If QImage gets deleted and you still try to use the QByteArray afterwards it will crash. But the benefit of this is it is faster of course.

          QByteArray data = QByteArray::fromRawData((const char*)image.constBits(), image.byteCount());
          

          Alternatice to make a deep copy of the data you can use the QByteArray constructor:

          QByteArray data = QByteArray((const char*)image.constBits(), image.byteCount());
          

          --- 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
          2
          • D Offline
            D Offline
            duarna
            wrote on last edited by duarna
            #5

            Ok got it, thank you for your help.
            here is my updated solution, because you were using old-style cast and obsolete method:

            On server side:

            QImage img("pathToImage"); 
            QByteArray data = QByteArray::fromRawData(reinterpret_cast<const char*>(img.constBits()), static_cast<int>(img.sizeInBytes()));
            QString strContent = data.toBase64(); //convert to a mqtt-compliant string
            

            And on the client side, assuming that the data of the image comes with the value "data":

            QString strContentB64 = p_image.value("data").toString(""); //get the raw string
            QByteArray baContentB64; baContentB64.append(strContentB64); //convert to QByteArray
            QByteArray baContent = QByteArray::fromBase64(baContentB64); //convert from Base64
            
            QImage img = QImage(reinterpret_cast<const uchar*>(baContent.constData()), width, height, static_cast<QImage::Format>(format));
            

            Again, thanks a lot!

            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