PDA

View Full Version : Displaying Raw data on QLabel



Seungsoo
21st November 2016, 01:21
Hi, I have some problem in displaying unsigned char* data on QLabel.

Following is my code.

Originally data was "unsigned char*", but it has first converted to "const char*" because I had to write it to QDataStream.

Anyway, i read "const char*" data by "in.readRawData", and converted it to unsigned char*.




QBuffer buffer;
QDataStream in(&buffer);

sharedMemory.create(1920 * 1080);
buffer.setData((char*)sharedMemory.constData(), sharedMemory.size());
buffer.open(QBuffer::ReadOnly);
sharedMemory.unlock();
sharedMemory.detach();

int r_width = 0;
int r_height = 0;
int r_cameraId = 0;
int r_step = 0;
int r_strlen = 0;
in >> r_width >> r_height >> r_step >> r_cameraId >> r_strlen;

char* receive = new char[r_strlen];
in.readRawData(receive, r_strlen);
//unsigned char* r_receive = new unsigned char[r_strlen];
//r_receive = (unsigned char*)receive;

QPixmap backBuffer = QPixmap::fromImage(QImage((unsigned char*)receive, r_width, r_height, r_step, QImage::Format::Format_RGB888));
ui.label->setPixmap(backBuffer.scaled(ui.label->size(), Qt::KeepAspectRatio));
ui.label->show();



But When I ran my code and debugged it, there was some problem in converting data to image.

I think converting unsigned char* to QPixmap like following code is right, but it doesn't work well and also with Format_RGB888, Index8, etc.

(unsigned char* data was made by ffmpeg video.)



QPixmap backBuffer = QPixmap::fromImage(QImage((unsigned char*)receive, r_width, r_height, r_step, QImage::Format::Format_RGB888));


please share your idea!
thank you!

anda_skoa
21st November 2016, 09:32
Which format does the sender use for the pixel data?

Have you verified that the read int values are correct?

Btw, you are leaking the image data and you needlessly convert from pixmap to image and back for scaling.

Cheers,
_