QFile to char[]

  • Hi,

    I found a sample code in libexif-0.6.21.zip
    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);


        QFile fileIn("D:/myfile.jpg");
        QByteArray ba = fileIn.readAll();
        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?

  • 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.


  • Moderators

    @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?

  • Qt Champions 2017

    @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."

  • @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"?

  • @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.

  • @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?


    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.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.