1

我正在使用 Qt 库,创建 QImages。

我可以使用这个构造函数

QImage image("example.jpg");

但是我在使用这个静态函数时遇到了问题:

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

Qt 向控制台吐出的地方:

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

以上不完全是我所拥有的,但这是唯一重要的区别。(我需要能够从缓冲区读取,因为我正在打开 zip 存档中的图像。)

4

1 回答 1

4

Tnx 从 irc.freenode.net 上的#qt 到 peppe:

解决方案是显式包含缓冲区长度。忽略一些类型unsigned char转换char和其他细节,我应该使用的是类似于:

QImage image = QImage::fromData(buffer, sizeOfFile);
于 2010-02-23T02:46:31.987 回答