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:
qDebug()<<"File was opened";
if (image.loadFromData(bytes , "JPG")){
qDebug()<<"Image was loaded";
}else{
qDebug()<<"Image was not loaded";
}
if (image.save(filename, "JPG")){
qDebug()<<"Image was saved";
}else{
qDebug()<<"Image was not saved";
}
}
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";
}
}
To copy to clipboard, switch view to plain text mode
I've tried both bytes and base64Data for QImage::LoadFromData method but still can't load image properly.
This service 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:
qDebug()<<"Picture was opened";
if (!image.
loadFromData(QByteArray::fromBase64(raw
),
"JPG")) qDebug()<<"Not loaded";
if (!image.save("output_pic.jpg", "JPG"))
qDebug()<<"Not saved";
}
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";
}
To copy to clipboard, switch view to plain text mode
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)
data.zip
Bookmarks