[SOLVED] How to show image from byte array ?
-
hi everyone ,
I want to get byte data from Textedit, and convert to JPG format to show image on Label
(or another one can recommend me what component is good show image ?)My GUI interface like below:
GUIMy code like this,
void MainWindow::on_pushButton_4_clicked() { QString txt = ui->textEdit->toPlainText(); QByteArray *data = new QByteArray(); *data = txt.toUtf8(); QPixmap *mpixmap = new QPixmap(); mpixmap->loadFromData(*data,"JPG"); ui->label->setPixmap(*mpixmap); //ui->label->show(); }
what's going on, why my code not to show image , where's wrong?
thank you in advance
Best regards -
Why do you use so many pointers?
Try it like this:QByteArray data = txt.toUtf8(); QPixmap mpixmap; mpixmap.loadFromData(data,"JPG"); ui->label->setPixmap(mpixmap);
And check what mpixmap.loadFromData(data,"JPG"); returns. If it returns false then it could not load your data.
-
One more note: what you have in your text edit is text! Even if you call toUtf8() it is still text represented as byte array. What you need to do is to convert the hex-codes in your text editor into bytes. For example: FF -> is a byte with value 255
-
One more note: what you have in your text edit is text! Even if you call toUtf8() it is still text represented as byte array. What you need to do is to convert the hex-codes in your text editor into bytes. For example: FF -> is a byte with value 255
@jsulm
hi dear jsulm,
it's still not show up image, I think you are right , it just a string not a hex.the single string to hex is below:
QString str= "FF"; bool ok= false; uint nHex = sValue.toUInt(&ok,16);
but how i do it , convert string(array) to hex(array).
I think the QPixmap::loadFromData need binary format data to show up image. -
You should do something like this (from documentation):
QByteArray ba; ba.resize(5); ba[0] = 0x3c; ba[1] = 0xb8; ba[2] = 0x64; ba[3] = 0x18; ba[4] = 0xca;
So, you iterate over the string, take always two characters and convert them from hex to quint8 and put that quint8 into the byte array.
-
Hi and welcome to devnet,
Out of curiosity, how do you know that your data in your textEdit is a valid JPEG image ?
-
You should do something like this (from documentation):
QByteArray ba; ba.resize(5); ba[0] = 0x3c; ba[1] = 0xb8; ba[2] = 0x64; ba[3] = 0x18; ba[4] = 0xca;
So, you iterate over the string, take always two characters and convert them from hex to quint8 and put that quint8 into the byte array.
@jsulm
hi dear jsulm,
I finished it, it can work now.
your tips is help me a lotthank you vary much a lot :)
the flow is below :
step 1: get string from textedit
step 2: change QString to QByteArray
step 3: transform QByteArray string data to hex data
step 4: using QPixmap component , and put hex data into the QPixmap
step 5: show up on label compoent -
Hi and welcome to devnet,
Out of curiosity, how do you know that your data in your textEdit is a valid JPEG image ?
@SGaist
I hava rs232 camera , I just copy data form serial port terminal
copy data , and paste to textedit