How to convert QImage to QByteArray?

I am trying to create a QByteArray from QImage, however, although I tried many variations, I could not handle it.

What am I doing:

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, the error code is invalid data. I think the conversion of QImage to QByteArray is wrong. Please, could you help me how to convert to QByteArray?

+6
source share
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 this if you need it for serialization, you better use a QDataStream .

+5
source

Try it:

QByteArray arr = QByteArray::fromRawData((const char*)img.bits(), img.byteCount());
0
source

All Articles