一、创建一个线程,名字随意,继承QThread(比如)
注:再次之前要将opcv和工业相机的ployn在vs中的环境配置好!!!
//线程头文件
//根据自己的需要传参到线程里
#ifndef MYTHREAD_H
#define MYTHREAD_H
#include
#include
#include
#include
#include
#include
#include
class GigeCamera;//这是另一个线程,所以相机的操作倒在这里面。
class MyThread : public QThread
{
Q_OBJECT
public:
explicit MyThread(QLabel *imgLabel,GigeCamera *gigeCamera);
~MyThread();
void closeThread();
public slots:
void pauses();
void resume();
protected:
virtual void run();
private:
QLabel *imgLabelM = nullptr; //图片显示控件指针
GigeCamera *gigeCameraM = nullptr; //相机对象指针
QMutex pause;
QImage *imgM; //图片指针
};
#endif // MYTHREAD_H
//.cpp文件
#include “MyThread.h”
#include
#include
#include
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/video.hpp>
#include"SampleView.h"
#include
#include “GigeCamera.h”
#include
#include
MyThread::MyThread(QLabel *imgLabel, GigeCamera *gigeCamera)
{
imgLabelM = imgLabel;
gigeCameraM = gigeCamera;
imgM = new QImage();
}
MyThread::~MyThread()
{
if (imgM)
delete imgM;
}
void MyThread::pauses()
{
this->pause.lock();
}
void MyThread::resume()
{
this->pause.unlock();
}
void MyThread::run()
{
while (true)
{
pause.lock();
gigeCameraM->SavePhoto(imgM);
imgLabelM->setPixmap(QPixmap::fromImage(imgM)/.scaled(imgLabelM->size(), Qt::KeepAspectRatio)/);
imgLabelM->setAlignment(Qt::AlignCenter); //设置图片的位置为居中
msleep(20);
pause.unlock();
}
}
二、调用线程
1.在使用线程的.h文件中引入线程:class MyThread;
2.在使用线程的.cpp文件中引入线程的头文件
3.实例化线程
5.使用一个按钮来控制线程的开启和关闭
相机线程看下一篇