.h:
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_QtGuiApplication1.h"
#include <QThread>
/*线程类*/
class Thread : public QThread
{
Q_OBJECT
public:
Thread(); //构造函数
signals:
void returnResult(int);//信号
protected:
void run();
};
/*窗口类*/
class QtGuiApplication1 : public QMainWindow
{
Q_OBJECT
public:
QtGuiApplication1(QWidget *parent = Q_NULLPTR);
private slots:
void test();//按钮事件
void displayResult(int);//信号回调
private:
Ui::QtGuiApplication1Class ui;
Thread* thread;
};
.cpp:
#include "QtGuiApplication1.h"
#include "ui_QtGuiApplication1.h"
#include<DVPCamera.h>
#pragma comment(lib, "DVPCamera.lib")
dvpHandle CameraHandle_t;//相机句柄
dvpFrame pFrame;//帧数据
void* pBuffer;//帧地址
QImage g_image;
QtGuiApplication1::QtGuiApplication1(QWidget *parent): QMainWindow(parent)
{
ui.setupUi(this);
thread = new Thread();
connect(thread, SIGNAL(returnResult(int)), this, SLOT(displayResult(int)));//信号槽
dvpUint32 n = 0;//相机数量
dvpRefresh(&n);//刷新相机,返回连接的相机数量
dvpOpen(0, dvpOpenMode(OPEN_NORMAL), &CameraHandle_t);//打开相机
dvpStart(CameraHandle_t);//启动视频流
}
/*按钮事件*/
void QtGuiApplication1::test()
{
thread->start();//启动新线程
}
Thread::Thread()
{
}
/*线程循环*/
void Thread::run()
{
int result=0;
while (1)
{
msleep(50);//延时50ms
emit returnResult(result); //发送信号
}
}
/*信号回调*/
void QtGuiApplication1::displayResult(int result)
{
dvpGetFrame(CameraHandle_t, &pFrame, &pBuffer, 3000);//获取一帧数据
QImage tmpImg((uchar*)pBuffer, pFrame.iWidth, pFrame.iHeight, QImage::Format_Indexed8);//BGR24-Format_BGR888\MONO8、16、RAW8-Format_Indexed8\RAW12-Format_Grayscale16
g_image = tmpImg.scaled(tmpImg.width() * 0.5, tmpImg.height() * 0.5, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);//缩放图像,SmoothTransformation平滑缩放,FastTransformation快速缩放
ui.label->setPixmap(QPixmap::fromImage(g_image));//显示图像到Label
}
/*
帧数据转为QImage格式
多线程发送信号
*/