PDA

View Full Version : QImage from QByteArray



alexlpn
12th September 2019, 13:45
Hello, I have some problem with loading image from raw bytes.
I've tried several options from this forum and other sources as well, but still stuck. Something really tiny shrinks from my look. Can someone please check it?

So the code:


QFile f("data.txt");
if (f.open(QIODevice::ReadOnly)){
qDebug()<<"File was opened";
QByteArray bytes = f.readAll();

QImage image;
if (image.loadFromData(bytes , "JPG")){
qDebug()<<"Image was loaded";
}else{
qDebug()<<"Image was not loaded";
}
QString filename = "output.jpg";
if (image.save(filename, "JPG")){
qDebug()<<"Image was saved";
}else{
qDebug()<<"Image was not saved";
}
}


I've tried both bytes and base64Data for QImage::LoadFromData method but still can't load image properly.
This service (https://onlinejpgtools.com/convert-base64-to-jpg) can be used to check bytes, so one can see that it is a real photo.

Thanks in advice!



After an hour I've also tried this:


QFile pic("pic.jpg");
if (pic.open(QIODevice::ReadOnly)){
qDebug()<<"Picture was opened";
QByteArray raw = pic.readAll().toBase64();
QImage image;
if (!image.loadFromData(QByteArray::fromBase64(raw), "JPG"))
qDebug()<<"Not loaded";
if (!image.save("output_pic.jpg", "JPG"))
qDebug()<<"Not saved";
}


Here I am taking real jpg file, converting it to raw base64 bytes and trying to decode it back to jpg, but loadFromData gives me false and I can't check why...

ps. file data.txt is in the attachment (put it in zip, so it should be unpacked first)
13256

ChristianEhrlicher
12th September 2019, 16:53
When data.txt contains base64 encoded content, why do you encode it a second time here:


QByteArray base64Data = bytes.toBase64();

And when it's a jpeg - why do you tell QImage::loadFromData() that it's a PNG?

alexlpn
12th September 2019, 18:54
When data.txt contains base64 encoded content, why do you encode it a second time here:

And when it's a jpeg - why do you tell QImage::loadFromData() that it's a PNG?

Thanks for pointing this out. I've fixed that but still the result is false...

ChristianEhrlicher
12th September 2019, 19:14
QImage::loadFromData() needs the raw data, not base64 encoded text.

d_stranz
12th September 2019, 22:22
QImage::loadFromData() needs the raw data, not base64 encoded text.

And so you need to give it the output from QByteArray::fromBase64(), not QByteArray::toBase64().

alexlpn
13th September 2019, 05:52
And so you need to give it the output from QByteArray::fromBase64(), not QByteArray::toBase64().


QImage::loadFromData() needs the raw data, not base64 encoded text.

Thank you mates!
I see I've mixed several things in wrong order) Now it is clear.



QFile f("data.txt");
if (f.open(QIODevice::ReadOnly)){
qDebug()<<"File was opened";
QByteArray base64Data = f.readAll();
QByteArray bytes = QByteArray::fromBase64(base64Data);

QImage image;
if (image.loadFromData(bytes, "JPG")){
qDebug()<<"Image was loaded";
}else{
qDebug()<<"Image was not loaded";
}
QString filename = "output.jpg";
if (image.save(filename, "JPG")){
qDebug()<<"Image was saved";
}else{
qDebug()<<"Image was not saved";
}
}