QT industrial camera interface development example

Article directory


foreword

The development of QT industrial camera interface is a very important work. Its main purpose is to display the images collected by industrial cameras in real time on the computer screen, and provide a series of functions for manipulating images, such as image processing, dimension measurement, image capturing and saving , etc.

1. Image acquisition module

   该模块的主要任务是通过工业相机进行图像采集,并实现采集参数的设置,例如曝光时间、增益等。

2. Display module

 该模块的主要任务是将采集到的图像实时显示在计算机屏幕上,并提供图像缩放、移动和旋转等操作。

3. Control module

该模块的主要任务是实现工业相机的控制操作,例如启动、停止、暂停、恢复等。

4. Operation function module

 该模块的主要任务是提供一系列图像处理和操作功能,例如色彩调整、灰度处理、图像拍摄和保存等。

The following takes a computer camera as an example to demonstrate how to implement a complex interface.

First, you need to create a QT project, and then add a QLabel control and a QPushButton control to the window.

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
    
    
    ui->setupUi(this);

    //添加图像显示的QLabel控件
    imageLabel = new QLabel;
    imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    imageLabel->setScaledContents(true);
    ui->scrollArea->setWidget(imageLabel);

    //添加拍照按钮的QPushButton控件
    ui->takeImageButton->setEnabled(false);
    ui->saveImageButton->setEnabled(false);
    ui->takeImageButton->setText(tr("&Take Image"));
    connect(ui->takeImageButton, SIGNAL(clicked()), this, SLOT(takeImage()));

    //添加保存按钮的QPushButton控件
    ui->saveImageButton->setText(tr("&Save Image"));
    connect(ui->saveImageButton, SIGNAL(clicked()), this, SLOT(saveImage()));

    //初始化计时器
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(displayImage()));
}

Next, we need to implement some signal and slot functions. For example, when the camera button is clicked, the takeImage() slot function needs to be called.

void MainWindow::takeImage()
{
    
    
    //停止计时器
    timer->stop();

    //通过QCamera实现图像采集
    QCamera camera;
    camera.setCaptureMode(QCamera::CaptureStillImage);
    camera.start();
    camera.searchAndLock();
    QImage image = QImage(camera.grab());
    camera.unlock();

    //将采集到的图像显示在界面上
    showImage(image);
    ui->takeImageButton->setEnabled(false);
    ui->saveImageButton->setEnabled(true);
}

The showImage() function is used to display the collected image on the interface.

void MainWindow::showImage(const QImage &image)
{
    
    
    //缩放、旋转和平移图像
    scaleFactor = 1.0;
    QPixmap pixmap = QPixmap::fromImage(image);
    pixmap = pixmap.scaled(imageLabel->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
    pixmap = pixmap.transformed(QTransform().rotate(rotation), Qt::SmoothTransformation);
    imageLabel->setPixmap(pixmap);
    ui->scrollArea->setBackgroundRole(QPalette::Dark);
    imageLabel->adjustSize();
    ui->takeImageButton->setEnabled(true);
}

Next, you need to implement the function of saving pictures, the code is as follows:

void MainWindow::saveImage()
{
    
    
    //弹出文件对话框
    QString fileName = QFileDialog::getSaveFileName(this, tr("保存"), ".", tr("图像文件(*.bmp *.jpg *.gif *.png)"));
  
    //保存图像
    if (!fileName.isEmpty())
    {
    
    
        QFile file(fileName);
        file.open(QIODevice::WriteOnly);
        imageLabel->pixmap()->save(&file, "JPG");
    }
    ui->saveImageButton->setEnabled(false);
    timer->start(1000/fps);
}

Finally, it is necessary to implement the function of displaying images in real time, the code is as follows:

void MainWindow::displayImage()
{
    
    
    QCamera camera;
    camera.setCaptureMode(QCamera::CaptureStillImage);
    camera.start();
    camera.searchAndLock();
    QImage image = QImage(camera.grab());
    camera.unlock();
    showImage(image);
}

Although the development of QT industrial camera interface is difficult, as long as you master the use of basic modules and signal slot functions , you can realize various types of interfaces. For the industrial automation industry, the development of QT industrial camera interface is an indispensable technology and will play an important role in many practical applications.

Guess you like

Origin blog.csdn.net/qq_41454577/article/details/130836397