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. QFile to char[]

QFile to char[]

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

    Hi,

    I found a sample code in libexif-0.6.21.zip
    https://github.com/wang-bin/libexif-port/blob/master/contrib/examples/write-exif.c
    which saves image to file with Exif information. In order to get image from file, I replaced

    static const unsigned char image_jpg[] = {
      0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01,
      0x01, 0x01, 0x00, 0x48, 0x00, 0x48, 0x00, 0x00, 0xff, 0xdb, 0x00, 0x43,
      ...
      };
    unsigned int image_jpg_len = sizeof(image_jpg);
    

    by

        QFile fileIn("D:/myfile.jpg");
        fileIn.open(QIODevice::ReadOnly);
        QByteArray ba = fileIn.readAll();
        fileIn.close();
        unsigned char* image_jpg = (unsigned char*) ba.data();
        unsigned int image_jpg_len = ba.size();
    

    but the saved image is invalid. Is there some mistake?

    m.sueM jsulmJ JonBJ 3 Replies Last reply
    0
    • S samdol

      Hi,

      I found a sample code in libexif-0.6.21.zip
      https://github.com/wang-bin/libexif-port/blob/master/contrib/examples/write-exif.c
      which saves image to file with Exif information. In order to get image from file, I replaced

      static const unsigned char image_jpg[] = {
        0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01,
        0x01, 0x01, 0x00, 0x48, 0x00, 0x48, 0x00, 0x00, 0xff, 0xdb, 0x00, 0x43,
        ...
        };
      unsigned int image_jpg_len = sizeof(image_jpg);
      

      by

          QFile fileIn("D:/myfile.jpg");
          fileIn.open(QIODevice::ReadOnly);
          QByteArray ba = fileIn.readAll();
          fileIn.close();
          unsigned char* image_jpg = (unsigned char*) ba.data();
          unsigned int image_jpg_len = ba.size();
      

      but the saved image is invalid. Is there some mistake?

      m.sueM Offline
      m.sueM Offline
      m.sue
      wrote on last edited by
      #2

      Hi @samdol

      JPEG is stored big endian. On the usual WINDOWS machine you read files in little endian. So a direct comparison does not work.

      -Michael.

      1 Reply Last reply
      0
      • S samdol

        Hi,

        I found a sample code in libexif-0.6.21.zip
        https://github.com/wang-bin/libexif-port/blob/master/contrib/examples/write-exif.c
        which saves image to file with Exif information. In order to get image from file, I replaced

        static const unsigned char image_jpg[] = {
          0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01,
          0x01, 0x01, 0x00, 0x48, 0x00, 0x48, 0x00, 0x00, 0xff, 0xdb, 0x00, 0x43,
          ...
          };
        unsigned int image_jpg_len = sizeof(image_jpg);
        

        by

            QFile fileIn("D:/myfile.jpg");
            fileIn.open(QIODevice::ReadOnly);
            QByteArray ba = fileIn.readAll();
            fileIn.close();
            unsigned char* image_jpg = (unsigned char*) ba.data();
            unsigned int image_jpg_len = ba.size();
        

        but the saved image is invalid. Is there some mistake?

        jsulmJ Online
        jsulmJ Online
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @samdol said in QFile to char[]:

        but the saved image is invalid

        What is invalid? Did you try to check the file content using an hex editor? Do you mean what you read is different from what you wrote?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @samdol said in QFile to char[]:

          unsigned char* image_jpg = (unsigned char*) ba.data();

          But if the ba contains zeros, what will happen ?

          the docs notes this
          "Note: A QByteArray can store any byte values including '\0's, but most functions that take char * arguments assume that the data ends at the first '\0' they encounter."

          1 Reply Last reply
          1
          • S samdol

            Hi,

            I found a sample code in libexif-0.6.21.zip
            https://github.com/wang-bin/libexif-port/blob/master/contrib/examples/write-exif.c
            which saves image to file with Exif information. In order to get image from file, I replaced

            static const unsigned char image_jpg[] = {
              0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01,
              0x01, 0x01, 0x00, 0x48, 0x00, 0x48, 0x00, 0x00, 0xff, 0xdb, 0x00, 0x43,
              ...
              };
            unsigned int image_jpg_len = sizeof(image_jpg);
            

            by

                QFile fileIn("D:/myfile.jpg");
                fileIn.open(QIODevice::ReadOnly);
                QByteArray ba = fileIn.readAll();
                fileIn.close();
                unsigned char* image_jpg = (unsigned char*) ba.data();
                unsigned int image_jpg_len = ba.size();
            

            but the saved image is invalid. Is there some mistake?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @samdol

            unsigned char* image_jpg = (unsigned char*) ba.data();
            unsigned int image_jpg_len = ba.size();

            What do you actually do with these to make you think "the saved image is invalid"?

            S 1 Reply Last reply
            0
            • JonBJ JonB

              @samdol

              unsigned char* image_jpg = (unsigned char*) ba.data();
              unsigned int image_jpg_len = ba.size();

              What do you actually do with these to make you think "the saved image is invalid"?

              S Offline
              S Offline
              samdol
              wrote on last edited by samdol
              #6

              @JNBarchan
              I found that if I use a file without EXIF info, it works. But if the file already has EXIF info, it did not work. I Could not see the image. Then the solution for file with EXIF would be to get the image char* without EXIF. But I don't know how to do that.

              JonBJ 1 Reply Last reply
              0
              • S samdol

                @JNBarchan
                I found that if I use a file without EXIF info, it works. But if the file already has EXIF info, it did not work. I Could not see the image. Then the solution for file with EXIF would be to get the image char* without EXIF. But I don't know how to do that.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

                @samdol
                So if, for whatever reason, you are saying you need to remove EXIF information from your JPEG file to make it "acceptable", since you quoted the source at https://github.com/wang-bin/libexif-port/blob/master/contrib/examples/write-exif.c for adding the EXIF info into the file, you can look at it and "reverse engineer" how to remove it, can't you?

                https://en.wikipedia.org/wiki/Exif:

                When Exif is employed for JPEG files, the Exif data are stored in one of JPEG's defined utility Application Segments, the APP1 (segment marker 0xFFE1), which in effect holds an entire TIFF file within

                Or, Googling remove exif data gives lots of hits for how to remove it.

                Or, find yourself a "previewer" which does not get confused by the embedded information?

                BTW, https://forum.qt.io/topic/84097/qimagewriter-and-exif shows some stuff to do with getting at EXIF information, might help you.

                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