【问题标题】:Saving images and then display them in a QLabel保存图像,然后在 QLabel 中显示它们
【发布时间】:2017-04-04 08:15:35
【问题描述】:

我目前正在构建一个使用相机的 Qt 应用程序。 在此应用程序中,使用捕获图像,然后将它们自动保存在特定文件夹中。一切都很好。

现在,当单击“库”按钮时,我想读取所有图像(JPEG 文件)并在QLabel 中一一显示所有拍摄的图像。

我找不到任何教程,只找到教程并使用 argv 参数,这对我没有好处,因为在我的应用程序中,用户可能会捕获图像,然后在同一次运行中显示它们。

如何读取文件列表并显示出来?

非常感谢:)

【问题讨论】:

  • 您有什么问题?在 QLabel 中显示图像?读取文件列表?
  • 读取文件列表
  • 请看this help section。目前,您的问题没有显示出太多的研究工作,也没有人可以帮助您解决的明确问题。相反,它从头开始请求链接或完整代码,这在 SO 上都是无关紧要的。
  • 看看QDir

标签: c++ qt qimage


【解决方案1】:

如果您有一个QLabel,那么您必须将图像连接在一起。我发现显示QLabels 列表更容易:

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::scaledQPixmap::scaledToWidth。此外,如果图像质量很重要,请将Qt::SmoothTransformation 指定为转换模式。

【讨论】:

    【解决方案2】:

    您可以使用 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));
        }
    

    为了将 Mat 类型转换为 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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-16
      • 1970-01-01
      • 2018-10-05
      • 1970-01-01
      • 1970-01-01
      • 2016-09-17
      • 1970-01-01
      • 2012-06-14
      相关资源
      最近更新 更多