문제

Hi I have a QByteArray of a jpeg image which I obtained from a QNetworkReply. I see everywhere jpeg image read from file and decompressed like this,

FILE * infile;
......
if ((infile = fopen(filename, "rb")) == NULL) 
{
     fprintf(stderr, "can't open %s\n", filename);
     exit(1);
}
jpeg_stdio_src(&cinfo, infile);
jpeg_read_header(&cinfo, 0);
jpeg_start_decompress(&cinfo);

Then

while (scan lines remain to be read)
      jpeg_read_scanlines(...);

But how do I read it from the QByteArray instead of a file/stdio stream?

도움이 되었습니까?

해결책

Use

void jpeg_mem_src(j_decompress_ptr cinfo, unsigned char * inbuffer,
              unsigned long insize);

instead of jpeg_stdio_src

 QByteArray qarr;

 jpeg_decompress_struct cinfo;
 jpeg_mem_src(&cinfo, qarr.data(), qarr.size());
 jpeg_read_header(&cinfo, TRUE);
 jpeg_start_decompress(&cinfo);

 /// etc..

다른 팁

You don't need to use external jpeg library:

QByteArray array;
// read data into array here;

QPixmap image;
image.loadFromData(array);

should be enough. Qt will autodetect the image format. Just remember to distribute Qt jpeg plugin if you compile your application dynamically.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top