I'm trying to get the image type from a QImage as QString, here is the code I use:
QImageReader reader(filename);
QByteArray filetype_ba = reader.format();
QString filetype_qs(filetype_ba);
it works. But when I rename the file, for example from image.png to image.jpg, it returns empty values. Does it mean QImageReader reads from the filename and not the fileheader? I want to get the image type from header so even when I rename the file(not convert), it will still give me the right type.
QImageReader documentation specify the following steps:
It seems that a handler match the file extension, and say "hey I can totally read that" without checking the content, as you assumed. And by (good) design, the jpeg handler is supposed to handle only jpeg and nothing else.
The thing is QImage behaves differently, as it looks directly into the file data stream to guess the format. It seems like you should base your detection on
Qimage
mechanism alone.Note: This describe the default behavior, which is is the same as
QImageReader::setAutoDetectImageFormat(true)
. false means you, the programmer, should set the image type yourself using the second argument or another method.