plot pictures from char buffer
-
Hello All. I hope you all are doing well.
I have a raspberry pi and with openCV and a webcam it captures pictures, processes it and sends it using socket. In the socket server side, there is an application created by Qt. I got the data but Im having a problem to plot the image.
Please, remember that as I using socket in armbian system, it sends the data as a char data type flux.This is what I tried (first sending a grayscale images)
char buffer [307200]; //its big but the data that server gets takes packets with 1000 bytes size (308 packets to form an image of 640*480 QImage *img; img = new QImage ((uchar *)buffer, 640, 480, QImage::Format_Grayscale8); ui->label->setPixmap(QPixmap::fromImage(*img));
But it always shown a black pictures.
-
from http://doc.qt.io/qt-5/qimage.html#QImage-3:
data must be 32-bit aligned, and each scanline of data in the image must also be 32-bit aligned.
You are basically missing an argument to use http://doc.qt.io/qt-5/qimage.html#QImage-5
QImage img(reinterpret_cast<uchar*>(buffer), 640, 480, 640,QImage::Format_Grayscale8);
The below is an example showing white noise:
int main(int argc, char *argv[]) { QApplication a(argc,argv); uchar buffer [648*480]; std::default_random_engine generator; std::uniform_int_distribution<quint16> distribution(0,0xff); for(auto i=std::begin(buffer);i!=std::end(buffer);++i) *i=distribution(generator); QImage testImg(buffer,640,480,640,QImage::Format_Grayscale8); QLabel result; result.setPixmap(QPixmap::fromImage(testImg)); result.show(); return a.exec(); }