PDA

View Full Version : convert uchar* to QbyteArray



saman_artorious
11th August 2013, 16:23
I get the screen raw data as uchar* as below:


void MainWindow::getScreenShot()
{
QPixmap originalPixmap = QPixmap::grabWidget(this);
QImage *image = new QImage(originalPixmap.toImage());

QSize imageSize = image->size();
int len = imageSize.width() * imageSize.height() * 3; // ?????????
uchar *rawData = new uchar[len];

emit sendRawDataToClient(rawData);
}


then send it to client, now client needs to convert it to qbytearray, write() get char* and not uchar*, does it make any difference to send it as char* and not uchar*?


void Client::rcvRawDataFromMW(uchar* rawData)
{
QByteArray block((char*)rawData);
tcpSocket->write(block);
tcpSocket->disconnectFromHost();
}

anda_skoa
11th August 2013, 18:16
I would just save it to a buffer, this way you can even make use of a more efficient format, like PNG

http://qt-project.org/doc/qt-4.8/qimage.html#save-2

Or even directly into the TcpSocket, it is a QUIDevice after all :)

Btw, in you example you are leaking the image data twice!
Once because you needlessly create QImage on the heap and forget to delete it and the second time when you forget to delete rawData after emit.

And for calculating "len" see QImage::byteCount()

Cheers,
_

ChrisW67
12th August 2013, 06:58
If sent over the network using Qt mechanisms then the receiver can trivially access the received data as a QByteArray: QIODevice::read().

Nothing of value is being sent as your sendRawDataToClient() signal parameter. At line 8 you allocate a chunk of memory containing 'len' unsigned chars and send a pointer to that chunk of memory to any slot connected to the signal. Nothing initialises the memory and nothing connects it in any way to the image you are manipulating. Qt will dutifully send those bytes through the network assuming you don't make other mistakes handling it in the connected slot (we cannot see). The other end might receive this but it will not be useful.

saman_artorious
13th August 2013, 10:03
I would just save it to a buffer, this way you can even make use of a more efficient format, like PNG


bool QImage::save ( QIODevice * device, const char * format = 0, int quality = -1 ) const

I need raw data in QByteArray and not in any image format. As I can guess, the default value of format would save it in raw data without any format wouldn't it?

Cheers,

Added after 4 minutes:


If sent over the network using Qt mechanisms then the receiver can trivially access the received data as a QByteArray: QIODevice::read().

Nothing of value is being sent as your sendRawDataToClient() signal parameter. At line 8 you allocate a chunk of memory containing 'len' unsigned chars and send a pointer to that chunk of memory to any slot connected to the signal. Nothing initialises the memory and nothing connects it in any way to the image you are manipulating. Qt will dutifully send those bytes through the network assuming you don't make other mistakes handling it in the connected slot (we cannot see). The other end might receive this but it will not be useful.

Yes, you are correct. Even it would be worse if I delete the image variable memory space. The good way is to define the variable inside the class.

wysota
13th August 2013, 11:51
I need raw data in QByteArray and not in any image format.

QImage::bits() returns the raw data in format specified by QImage::format()

ChrisW67
14th August 2013, 04:54
I need raw data in QByteArray and not in any image format.
What is "raw data"? The bytes representing the image must be arranged in some known way, a "format", in order to be at all useful.