stamaimer
stamaimer

Reputation: 6475

corrupt data in decode base64 to image in Qt

I write a program decode the base64 string to image. I wrote a sample:

    QFile file("./image.jpg");

    if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        return;
    }

    QByteArray raw = file.readAll().toBase64();

    QImage = image;

    image.loadFromData(QByteArray::fromBase64(raw), "JPG");

    image.save("output.jpg", "JPG");

The output of the program is:

Corrupt JPEG data: 65 extraneous bytes before marker 0xc0 Quantization table 0x01 was not defined

I can't find something useful with google. I only read image file, and encode it with base64, then decode it. Could you tell me what's wrong with my code?

Upvotes: 1

Views: 620

Answers (1)

stamaimer
stamaimer

Reputation: 6475

I have figured out what's wrong with my code. When i open a image file, i use the QIODevice::Text open mode. But the image is a binary file, so i should remove the QIODevice::Text option. After do that, the code run well.

Upvotes: 1

Related Questions