Qt 视频播放

为了尽快学会使用Qt框架,看了别人的博客之后直接就用了,也懒得去慢慢原理,反正以后接触多了慢慢就懂了。

写一个QtPlayer,能够播放视频流,并且准备在这个视频播放中导入之前所写的处理代码。
//***********************************
//qtplayer.h
//***********************************
#ifndef QTPLAYER_H
#define QTPLAYER_H

#include "ui_qtplayer.h"

#include <QMainWindow>
#include <QPaintEvent> 
#include <QTimer> 
#include <QPainter> 
#include <QPixmap> 
#include <QLabel> 
#include <QImage> 

#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

class QtPlayer : public QMainWindow
{
     Q_OBJECT

public:
     QtPlayer(QWidget *parent = 0);
     ~QtPlayer();
     public slots:
     void updateImage();
private:

     QTimer theTimer;
     Mat srcImage;
     VideoCapture videoCap;
     QLabel *imageLabel;

     Ui::QtPlayerClass ui;

protected:
     void paintEvent(QPaintEvent *e);
};

#endif // QTPLAYER_H


//***********************************
//qtplayer.cpp
//***********************************
#include "qtplayer.h"
#include <QtGui\QPainter>
#include <QtCore\QPoint>


QtPlayer::QtPlayer(QWidget *parent)
: QMainWindow(parent)
{
     ui.setupUi(this);
     connect(&theTimer, &QTimer::timeout, this, &QtPlayer::updateImage);
     //从摄像头捕获视频 
     if (videoCap.open("D:\\VS Project\\Player\\Resources\\test1.avi"))
     {
          srcImage = Mat::zeros(videoCap.get(CV_CAP_PROP_FRAME_HEIGHT), videoCap.get(CV_CAP_PROP_FRAME_WIDTH), CV_8UC3);
          theTimer.start(30);
     }
     //设置显示视频用的Label 
     imageLabel = new QLabel(this);
     ui.mainToolBar->addWidget(imageLabel);
}

QtPlayer::~QtPlayer()
{

}

void QtPlayer::paintEvent(QPaintEvent *e)
{
     ////显示方法一 
     //QPainter painter(this);
     //QImage image1 = QImage((uchar*)(srcImage.data), srcImage.cols, srcImage.rows, QImage::Format_RGB888);
     //painter.drawImage(QPoint(20, 20), image1);
    
     //显示方法二 
     QImage image2 = QImage((uchar*)(srcImage.data), srcImage.cols, srcImage.rows, QImage::Format_RGB888);
     imageLabel->setPixmap(QPixmap::fromImage(image2));
     imageLabel->resize(image2.size());
     imageLabel->show();
}

void QtPlayer::updateImage()
{
     videoCap >> srcImage;
     if (srcImage.data)
     {
          cvtColor(srcImage, srcImage, CV_BGR2RGB);//Qt中支持的是RGB图像, OpenCV中支持的是BGR 
          this->update();  //发送刷新消息 
     }
}



//***********************************
//main.cpp
//***********************************
#include "qtplayer.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
     QApplication a(argc, argv);

     QtPlayer *player = new QtPlayer();
     player->show();

     return a.exec();
}

这一篇博客好水啊。。。


你可能感兴趣的:(C++,qt,视频流)