【发布时间】:2018-07-11 13:44:11
【问题描述】:
我有一张带有.jpg 扩展名的图像,但它以png 格式存储。当我使用QImage 加载该图像时,它返回一个无效的QImage:
QImage image (path);
if (image.isNull())
{
//it enters here
}
在加载图像之前我必须一一检查格式:
auto readImage = [](const QString & path)
{
std::string formats[] = {
"PNG",
"JPG",
"GIF",
"JPEG",
};
for (auto && format : formats)
{
QImageReader img(path, format.c_str());
if (img.canRead())
{
return QImage(path, format.c_str());
}
}
return QImage(path);
};
QImage image = readImage(path);
if (image.isNull())
{
//it does not enter here this time
}
有没有更好的方法来加载未知格式的图片?
使用QImage的load方法更新,还是一样的结果:
QImage image;
image.load(path);
if (image.isNull())
{
//it enters herer
}
【问题讨论】:
-
if (image.isNull())或if(!image.isNull())? -
@eyllanesc 请在评论前仔细阅读问题。目前的方法没有任何问题。但是将来如果有新的格式,我必须将它添加到格式列表中。
if (image.isNull())当然。 -
如果有新格式,那么你必须创建一个插件并将其嵌入到 Qt 中。
-
如果有新的图像格式并且Qt可以读取它,你应该在以下列表中:doc.qt.io/qt-5/qimagereader.html#supportedImageFormats