Solved Black and White BMP QImage distort
-
@Christian-Ehrlicher
Like what i write before, i have two kinds of image data, one is colorful BMP image, this format can be convert back into QImage and show correctly on my server UI.
But the Black and white one, before i send data, i checked the data length,after server recviced the data, i checked the data length before convert it to QImage.And the result is these two length are all the same.But the QImage data is just distort...About the thread question,it`s because i have two camera ,i need to make them send both format data at the same time.And my server need to show these two different image at the same time.
If i dont use threads,there will be a very high delay.Even if i only open just one camera.Now im trying to use char* to trasmit my B&W format img data directly.Later we will know if it works.Wish me successful.
And if you have any other suggestions, plz tell me i`ll learn and give them a try.
Thank you for your advice! -
@JustSoleSole said in Black and White BMP QImage distort:
But the QImage data is just distort...
What does this actually mean? Can you post a good and bad image.
-
@ChrisW67
Normal black and white img:
Distort abnormal one:
-
I tried to give give QImage a format before i read the img data from QByteArray which is transmitted to server.
//Server code //qbImgData is QByteArray that is transmitted from client QBuffer buffer(&qbImgData); QImageReader reader(&buffer, "bmp"); //this line is the one i add before read img, black&white img is 1 bit per pixel. //At the memory 8bits are saved in one byte. QImage img(808, 608, QImage::Format_Indexed8); reader.read(&img);
With this effort.This two pics seem to be more clear and intuitional.
You can see the abnormal one is not only distort but also seems to be cut into two reversed parts.
But now i dont know how to make it more clear. -
Hi,
What is the exact format of your B&W image ? Bitmap ? Raw ? JPEG ?
-
@SGaist
BMP,if i put the data in a string or in a bytearray,its beginning two words are 'B' and 'M',which should stands for the BMP format.
-
And I tried to use QDataStream to convert QImage to QByteArray,in this way when the QByteArray convert back to QImage, it works...The QImage doesnt distort and shows good.But this way consume too much time,dealing with one frame needs about 130ms.
Code:QByteArray qbtMsgArray; QDataStream in(&qbtMsgArray, QIODevice::ReadWrite); in << *qImg; qDebug() << qbtMsgArray; QDataStream out(&qbtMsgArray, QIODevice::ReadWrite); QImage tImg; out >> tImg;
I print the QByteArray and it shows this:
And the faster way:QByteArray qbtImageArray; QBuffer qBuffer(&qbtImageArray); //qImg->save(&qBuffer, "bmp", 100); qDebug() << qbtImageArray; QBuffer buffer(&qbtImageArray); QImageReader reader(&buffer, "bmp"); QImage img(808, 608, QImage::Format_Indexed8); reader.read(&img);
I print the QByteArray and it shows this:
Why are they so different...
-
the source code is here. You check the differences out.
https://code.qt.io/cgit/qt/qtbase.git/tree/src/gui/image/qimage.cpp?h=dev -
@JoeCFD
I`ve already checked yesterday,
I see no much differences between QDataStream operator<<() and QImage save().
And now i find if i use privious saved BMP file as source data, do QImage save and QImageReader read,the new QImage pic doesnt distort.
But if i use real-time img source data like snap by a camera, the it doesnt work.
So im now checking the source data to see if it can help. -
Finally,this problem solved.
I checked the QImage::Format enum,found this:QImage::Format_Indexed8: The image is stored using 8-bit indexes into a colormap.
Feeling something wrong then i found:
QImage::Format_Grayscale8 The image is stored using an 8-bit grayscale format.
This format should be what i need,but not Index8.
So i tried to convert my unsigned char* img data into QImage by this format, and transmit the formed QByteArray data to server and convert it back to QImage ,and it works!!!
The conclusion of create a black and white BMP QImage is use QImage::Format_Grayscale8.
And thank you all for your help.Really appreciate!!!