Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert QImage to QByteArray?

I am trying to create QByteArray from QImage, however although I tried lots of varient, I couldn't handle it.

What I am doing is :

QImage img_enrll; // <--- There is an image coming from another function. 

QByteArray arr((char*)img_enrll.bits(),img_enrll.byteCount());  // <-- convertion but I am not sure it is true or not. 

funcCheck((unsigned char*)arr.data(), arr.size(), 0, &sam, 1, &n);


virtual Error funcCheck (const uint8_t    src[],
                           size_t           src_len,
                           size_t           tout_ms,
                           IRawSample*      dst[],
                           size_t           dst_len,
                           size_t*          dst_n )

However Error code is return Invalid Data. I think that converting QImage to QByteArray is wrong. Please could you kindly help me how to convert to QByteArray?

like image 314
goGud Avatar asked Dec 07 '14 14:12

goGud


2 Answers

You could do this:

QImage img_enrll;
QByteArray arr;
QBuffer buffer(&arr);
buffer.open(QIODevice::WriteOnly);
img_enrll.save(&buffer, "yourformat");

Having written that, if you need this for serialization, you are better of with QDataStream.

like image 98
lpapp Avatar answered Sep 16 '22 22:09

lpapp


Try this:

QByteArray arr = QByteArray::fromRawData((const char*)img.bits(), img.byteCount());
like image 32
Tarek.Mh Avatar answered Sep 19 '22 22:09

Tarek.Mh