ubuntu系统下,使用qt creater,使图片输出成灰度图、降低分辨率的图片的main.cpp和mainwindow.cpp的例程,以及对应的.pro文件
时间: 2024-05-07 13:20:41 浏览: 128
以下是一个简单的例程,可以将图像转换为灰度图,并降低分辨率。需要在.pro文件中添加QT += widgets和QT += gui。
main.cpp
```c++
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
```
mainwindow.h
```c++
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QLabel>
#include <QImage>
#include <QSpinBox>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void openImage();
void convertToGray();
void reduceResolution();
private:
QLabel *imageLabel;
QImage image;
QSpinBox *spinBox;
};
#endif // MAINWINDOW_H
```
mainwindow.cpp
```c++
#include "mainwindow.h"
#include <QFileDialog>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
imageLabel = new QLabel;
imageLabel->setBackgroundRole(QPalette::Base);
imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
imageLabel->setScaledContents(true);
QWidget *centralWidget = new QWidget(this);
setCentralWidget(centralWidget);
QVBoxLayout *mainLayout = new QVBoxLayout(centralWidget);
QHBoxLayout *buttonLayout = new QHBoxLayout;
QPushButton *openButton = new QPushButton(tr("Open"), this);
QPushButton *grayButton = new QPushButton(tr("Convert to Gray"), this);
QPushButton *reduceButton = new QPushButton(tr("Reduce Resolution"), this);
spinBox = new QSpinBox(this);
spinBox->setRange(1, 10);
spinBox->setValue(1);
buttonLayout->addWidget(openButton);
buttonLayout->addWidget(grayButton);
buttonLayout->addWidget(reduceButton);
buttonLayout->addWidget(spinBox);
mainLayout->addWidget(imageLabel);
mainLayout->addLayout(buttonLayout);
connect(openButton, SIGNAL(clicked()), this, SLOT(openImage()));
connect(grayButton, SIGNAL(clicked()), this, SLOT(convertToGray()));
connect(reduceButton, SIGNAL(clicked()), this, SLOT(reduceResolution()));
}
MainWindow::~MainWindow()
{
}
void MainWindow::openImage()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Image"), ".", tr("Image Files (*.png *.jpg *.bmp)"));
if (fileName.isEmpty())
return;
image.load(fileName);
if (image.isNull())
{
QMessageBox::information(this, tr("Error"), tr("Cannot load %1.").arg(fileName));
return;
}
imageLabel->setPixmap(QPixmap::fromImage(image));
}
void MainWindow::convertToGray()
{
if (image.isNull())
return;
image = image.convertToFormat(QImage::Format_Grayscale8);
imageLabel->setPixmap(QPixmap::fromImage(image));
}
void MainWindow::reduceResolution()
{
if (image.isNull())
return;
int factor = spinBox->value();
image = image.scaled(image.width() / factor, image.height() / factor);
imageLabel->setPixmap(QPixmap::fromImage(image));
}
```
.pro文件
```c++
QT += widgets gui
TARGET = imageConverter
TEMPLATE = app
SOURCES += main.cpp \
mainwindow.cpp
HEADERS += mainwindow.h
```
这个例程中,使用了Qt的几个基本控件,如QLabel、QSpinBox、QPushButton等,并且利用QImage类进行图像处理。openImage()函数用于打开图片,convertToGray()函数将图片转换为灰度图,reduceResolution()函数将图片降低分辨率。
阅读全文