步骤一:
Qt+OpenCV环境配置
步骤二:
创建视频播放线程
核心代码如下
视频播放线程.h文件
#include#include using namespace cv; class PlayerVideoThread : public QThread { Q_OBJECT public: PlayerVideoThread(char *videoPath); void run(); private: VideoCapture cap; Mat frame; signals: void frameVideo(Mat frame); };
视频播放线程.cpp源文件
#include "playervideothread.h" #includePlayerVideoThread::PlayerVideoThread(char *videoPath) { if(cap.open(videoPath)) { qDebug()<<"open video"; } } void PlayerVideoThread::run() { while(cap.read(frame)) { emit frameVideo(frame); } }
视频显示窗口.h文件
class VideoWidget : public QWidget
{
Q_OBJECT
public:
explicit VideoWidget(QWidget *parent = 0);
QLabel *label;
VideoCapture cap;
Mat frame;
PlayerVideoThread *plaverVideoThread;
void paintEvent(QPaintEvent *e);
signals:
public slots:
void updateImage(Mat img);
};
视频显示窗口.cpp文件
VideoWidget::VideoWidget(QWidget *parent) : QWidget(parent)
{
this->resize(400,400);
label = new QLabel(this);
label->resize(400,400);
this->plaverVideoThread = new PlayerVideoThread("carMove.mp4");
connect(this->plaverVideoThread,SIGNAL(frameVideo(Mat)),this,SLOT(updateImage(Mat)),Qt::BlockingQueuedConnection);
this->plaverVideoThread->start();
}
void VideoWidget::paintEvent(QPaintEvent *e)
{
QImage q_image = QImage(frame.data,frame.cols,frame.rows,QImage::Format_RGB888);
label->setPixmap(QPixmap::fromImage(q_image));
label->setScaledContents(true);
}
void VideoWidget::updateImage(Mat img)
{
this->frame = img.clone();
this->update();
}
执行效果



