爲了儘快學會使用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();
}
這一篇博客好水啊。。。