Base64 string to QPixmap
-
Hi,
@CloudL said in Base64 string to QPixmap:
ret.loadFromData(QByteArray::fromBase64(base64ImageString.toUtf8().toBase64()))
Aren't you doing a double conversion here ? Your base64ImageString variable is likely already containing base64 data. If that's not the case, then you have a naming issue.
As an additional note, why are you converting your database data to a QString to then make it a QByteArray again ?
-
Hi,
@CloudL said in Base64 string to QPixmap:
ret.loadFromData(QByteArray::fromBase64(base64ImageString.toUtf8().toBase64()))
Aren't you doing a double conversion here ? Your base64ImageString variable is likely already containing base64 data. If that's not the case, then you have a naming issue.
As an additional note, why are you converting your database data to a QString to then make it a QByteArray again ?
@SGaist said in Base64 string to QPixmap:
As an additional note, why are you converting your database data to a QString to then make it a QByteArray again
Hi,
thanks for your help.I get the data from a server inside a json message.
Then i save the base64 string from the json message into my local database.Later i want to load the data from my local database and display the pixmap...
Now i tried the following but still not working:
const QByteArray& base64ImageString(query.value(0).toByteArray()); QPixmap ret; if(!ret.loadFromData(base64ImageString)) { qCritical() << "getPilotAvatar: couldn't create png image from db binary data."; return defaultAvatar; } -
@SGaist said in Base64 string to QPixmap:
As an additional note, why are you converting your database data to a QString to then make it a QByteArray again
Hi,
thanks for your help.I get the data from a server inside a json message.
Then i save the base64 string from the json message into my local database.Later i want to load the data from my local database and display the pixmap...
Now i tried the following but still not working:
const QByteArray& base64ImageString(query.value(0).toByteArray()); QPixmap ret; if(!ret.loadFromData(base64ImageString)) { qCritical() << "getPilotAvatar: couldn't create png image from db binary data."; return defaultAvatar; } -
@eyllanesc
The output is:"data:image/png;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAQCAw.........mYiTApS0RcY3Z/9k=" -
So you are saving the html content in your database not the base64 encoded image. That's is your first issue. Drop the stuff up to the and including
base64,. This is information of theimgtag. -
@eyllanesc
The output is:"data:image/png;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAQCAw.........mYiTApS0RcY3Z/9k=" -
@eyllanesc said in Base64 string to QPixmap:
still not working ...
QByteArray base64ImageString(query.value(0).toByteArray()); base64ImageString = base64ImageString.remove(0, 22); //base64ImageString = base64ImageString.replace("data:image/png;base64,", ""); qInfo() << base64ImageString; QPixmap ret; if(!ret.loadFromData(base64ImageString, "PNG")) { qCritical() << "getPilotAvatar: couldn't create png image from db binary data."; return defaultAvatar; } -
@eyllanesc said in Base64 string to QPixmap:
still not working ...
QByteArray base64ImageString(query.value(0).toByteArray()); base64ImageString = base64ImageString.remove(0, 22); //base64ImageString = base64ImageString.replace("data:image/png;base64,", ""); qInfo() << base64ImageString; QPixmap ret; if(!ret.loadFromData(base64ImageString, "PNG")) { qCritical() << "getPilotAvatar: couldn't create png image from db binary data."; return defaultAvatar; } -
@CloudL Your string is not base64 of a png, if so then it should start with "iVBO". I think you have not saved the image in base64.
You are right. It was no png. Now i changed it, but still not working...
data looks like:
"iVBORw0KGgoAAAANSUhEUgAAANwAAADcCAYAAAAbWs+BAAAAAXNSR0IArs4c6QAAIABJREFUeF7tfQmYHFW1/zlVM5OQbdj3RZZMVycPENw3NkUQ3IFJV4dFUPNkCemaAKK8p8HtPYF0dQjiH1FRJF3NuAPi9gSfPjdkE0m6OgFRCQRQhIQEkpmpOv/v9nRParnVtXT1Pvf7+Pgyfe+55557T917zz3ndxCmy7QEpiXQNAlg03qa7qimBGj0LFF/Ze9d+3aZuc/E.........." QByteArray base64ImageString(query.value(0).toByteArray()); // base64ImageString = base64ImageString.remove(0, 22); base64ImageString = base64ImageString.replace("data:image/png;base64,", ""); qInfo() << base64ImageString; QPixmap ret; if(!ret.loadFromData(base64ImageString, "PNG")) { qCritical() << "getPilotAvatar: couldn't create png image from db binary data."; return defaultAvatar; }Sorry, i have no more ideas...
By the way. In your profile your e-mail is wrong i think.. It should be gmail ? not gmal... ?
-
You are right. It was no png. Now i changed it, but still not working...
data looks like:
"iVBORw0KGgoAAAANSUhEUgAAANwAAADcCAYAAAAbWs+BAAAAAXNSR0IArs4c6QAAIABJREFUeF7tfQmYHFW1/zlVM5OQbdj3RZZMVycPENw3NkUQ3IFJV4dFUPNkCemaAKK8p8HtPYF0dQjiH1FRJF3NuAPi9gSfPjdkE0m6OgFRCQRQhIQEkpmpOv/v9nRParnVtXT1Pvf7+Pgyfe+55557T917zz3ndxCmy7QEpiXQNAlg03qa7qimBGj0LFF/Ze9d+3aZuc/E.........." QByteArray base64ImageString(query.value(0).toByteArray()); // base64ImageString = base64ImageString.remove(0, 22); base64ImageString = base64ImageString.replace("data:image/png;base64,", ""); qInfo() << base64ImageString; QPixmap ret; if(!ret.loadFromData(base64ImageString, "PNG")) { qCritical() << "getPilotAvatar: couldn't create png image from db binary data."; return defaultAvatar; }Sorry, i have no more ideas...
By the way. In your profile your e-mail is wrong i think.. It should be gmail ? not gmal... ?
@CloudL change to:
QByteArray data(query.value(0).toByteArray()); QByteArray base64 = data.replace(QByteArray("data:image/png;base64,"), QByteArray("")); QByteArray rawImage = QByteArray::fromBase64(base64); QPixmap ret; if(!ret.loadFromData(rawImage, "PNG")) { }