开发者

QImage from datastream

开发者 https://www.devze.com 2022-12-21 10:17 出处:网络
I\'m using the Qt library, creating QImages. I\'m able to use this constructor: QImage image(\"example.jpg\");

I'm using the Qt library, creating QImages.

I'm able to use this constructor:

QImage image("example.jpg");

But I'm having trouble with this static function:

char buffer[sizeOfFile];
ifstream inFile("example.jpg");
inFile.read(buffer, sizeOfFile);
QImage image = QImage::fromData(buffer); // error here
// but 开发者_JS百科there's nothing wrong with the buffer
ofstream outFile("bufferOut.jpg");
outFile.write(buffer, sizeOfFile);

Where Qt spits out to console:

Corrupt JPEG data: 1 extraneous bytes before marker 0xd9
JPEG datastream contains no image

The above isn't exactly what I have, but it's the only important difference. (I need to be able to read from a buffer because I'm opening images that are inside a zip archive.)


Tnx to peppe from #qt on irc.freenode.net:

The solution is to explicitly include the buffer length. Ignoring a few unsigned char to char typecasting and other details, what I should have used is something akin to:

QImage image = QImage::fromData(buffer, sizeOfFile);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号