Qt / C++ - Converting raw binary data and display it as an image (i.e. QImage)

扶醉桌前 提交于 2019-12-04 14:08:19

问题


I have a C++ Open GL application that renders an animation display, and captures the frame-buffer contents using glReadPixels(), which is then stored as a 1D char array.

I can get the buffer contents and save it to a char array, as follow:

char * values = (char *) malloc(width * height * 4 * sizeof(char));
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, values);

I then send these raw data over the network using socket, which is not an issue for me. I have a Qt Desktop Client (which communicates with the OpenGL application using socket)

This client program can successfully receive the raw binary data sent by the Open GL application.

How can I render an image from these binary data in my client application? Does Qt has any built-in function / library to draw an image from binary data (which is generated from glReadPixels)?

Do I need to encode / decode these raw binary data as base64 type prior to send them via sockets? Why or why not?


回答1:


QImage has a nice constructor that takes an existing buffer, a width, a height and a pixel format. That seems like an excellent place to start.

And if you create the QImage on the server, you can use Qt's built-in streaming to send a QImage to your clients (QDataStreams can wrap just about any IO device, including network sockets), so there's no need to do any encoding and decoding yourself - Qt will take care of everything for you.

Hope that helps!



来源:https://stackoverflow.com/questions/9797862/qt-c-converting-raw-binary-data-and-display-it-as-an-image-i-e-qimage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!