我目前正在构建一个使用某个相机的Qt应用程序。 在这个应用程序中使用捕捉图像,然后它们自动保存在特定的文件夹中。一切都很好。保存图像,然后将它们显示在QLabel中
现在,当点击“库”按钮时,我想要读取所有图像(JPEG文件)并显示在QLabel
中逐个拍摄的所有图像。
我找不到任何教程,只找到教程,并使用argv
参数,这对我来说并不好,因为在我的应用程序中,用户可能会捕获图像,然后在同一运行中显示它们。
如何读取文件列表并显示它?
非常感谢你:)
我目前正在构建一个使用某个相机的Qt应用程序。 在这个应用程序中使用捕捉图像,然后它们自动保存在特定的文件夹中。一切都很好。保存图像,然后将它们显示在QLabel中
现在,当点击“库”按钮时,我想要读取所有图像(JPEG文件)并显示在QLabel
中逐个拍摄的所有图像。
我找不到任何教程,只找到教程,并使用argv
参数,这对我来说并不好,因为在我的应用程序中,用户可能会捕获图像,然后在同一运行中显示它们。
如何读取文件列表并显示它?
非常感谢你:)
如果你有一个QLabel
,那么你必须将图像连接起来的一个。我觉得更容易显示QLabel
的List:
auto layout = new QVBoxLayout();
Q_FOREACH (auto imageName, listOfImages) {
QPixmap pixmap(dirPath + "/" + imageName);
if (!pixmap.isNull()) {
auto label = new QLabel();
label->setPixmap(pixmap);
layout->addWidget(label);
}
}
a_wdiget_where_to_show_images->setLayout(layout);
最后一行将取决于你什么时候要放置标签上。我建议使用滚动条的一些小部件。
现在,您要读取目录中的所有图像(上面的listOfImages
变量)。如果你没有的话:
const auto listOfImages = QDir(dirPath).entryList(QStringList("*.jpg"), QDir::Files);
你可能有布局的问题,如果您的图片太大。在这种情况下,如果它们大于给定大小,则应该缩放它们。看看QPixmap::scaled或QPixmap::scaledToWidth。另外,如果图像质量很重要,请指定Qt::SmoothTransformation
作为转换模式。
您可以使用opencv库来读取目录中的所有图像。
vector<String> filenames; // notice here that we are using the Opencv's embedded "String" class
String folder = "Deri-45x45/"; // again we are using the Opencv's embedded "String" class
float sayi = 0;
glob(folder, filenames); // new function that does the job ;-)
float toplam = 0;
for (size_t i = 0; i < filenames.size(); ++i)
{
Mat img = imread(filenames[i],0);
//Display img in QLabel
QImage imgIn = putImage(img);
imgIn = imgIn.scaled(ui->label_15->width(), ui->label_15->height(),Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
ui->label_15->setPixmap(QPixmap::fromImage(imgIn));
}
为了垫类型转换为QImage的,我们使用PUTIMAGE功能:
QImage putImage(const Mat& mat)
{
// 8-bits unsigned, NO. OF CHANNELS=1
if (mat.type() == CV_8UC1)
{
// Set the color table (used to translate colour indexes to qRgb values)
QVector<QRgb> colorTable;
for (int i = 0; i < 256; i++)
colorTable.push_back(qRgb(i, i, i));
// Copy input Mat
const uchar *qImageBuffer = (const uchar*)mat.data;
// Create QImage with same dimensions as input Mat
QImage img(qImageBuffer, mat.cols, mat.rows, mat.step, QImage::Format_Indexed8);
img.setColorTable(colorTable);
return img;
}
// 8-bits unsigned, NO. OF CHANNELS=3
if (mat.type() == CV_8UC3)
{
// Copy input Mat
const uchar *qImageBuffer = (const uchar*)mat.data;
// Create QImage with same dimensions as input Mat
QImage img(qImageBuffer, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
return img.rgbSwapped();
}
else
{
qDebug() << "ERROR: Mat could not be converted to QImage.";
return QImage();
}
}
什么是你有问题?在QLabel中显示图像?阅读文件列表? –
阅读文件列表 –
请看看[这个帮助部分](http://stackoverflow.com/help/how-to-ask)。目前,你的问题并没有显示出太多的研究工作,也没有人可以帮你解决的明确问题。相反,它要求从头开始的链接或完整的代码,这两者都是SO的主题。 – Bowdzone