1、新建一个Qt Widgets Application,工程配置文件(.pro文件)内容如下:
#-------------------------------------------------
#
# Project created by QtCreator 2018-12-11T12:57:12
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = QtOpenVideo
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
INCLUDEPATH += C:\opencv\build\include \
C:\opencv\build\include\opencv \
C:\opencv\build\include\opencv2
CONFIG(debug, debug|release):
{
LIBS += -LC:\opencv\build\x86\vc11\lib\
-lopencv_calib3d249d \
-lopencv_contrib249d \
-lopencv_core249d \
-lopencv_features2d249d \
-lopencv_flann249d \
-lopencv_gpu249d \
-lopencv_highgui249d \
-lopencv_imgproc249d \
-lopencv_legacy249d \
-lopencv_ml249d \
-lopencv_nonfree249d \
-lopencv_objdetect249d \
-lopencv_ocl249d \
-lopencv_photo249d \
-lopencv_stitching249d \
-lopencv_superres249d \
-lopencv_ts249d \
-lopencv_video249d \
-lopencv_videostab249d
}
2、main.cpp内容如下:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
3、mainwiodow.h内容如下
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QString>
#include <QTimer>
#include <QImage>
#include <QFileDialog>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void ReadFrame();
void on_btnOpenVideo_clicked();
void on_btnCloseVideo_clicked();
private:
Ui::MainWindow *ui;
QTimer *timer;
VideoCapture capture;
Mat frame;
Mat result_frame;
};
#endif // MAINWINDOW_H
4、mainwindow.cpp内容如下
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(ReadFrame()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::ReadFrame()
{
if(capture.isOpened())
{
capture >> frame;
if(!frame.empty())
{
cvtColor(frame, result_frame, CV_BGR2RGB); // OpenCV中Mat读入的图像是BGR格式,要转换为RGB格式
cv::resize(result_frame, result_frame, Size(640, 480));
// 将抓取到的帧,转换为QImage格式。QImage::Format_RGB888不同的摄像头用不同的格式。
QImage image((const uchar*)result_frame.data, result_frame.cols, result_frame.rows, QImage::Format_RGB888);
ui->label->setPixmap(QPixmap::fromImage(image)); // 将图片显示到label上
ui->label->resize( ui->label->pixmap()->size()); // 将label控件resize到fame的尺寸
}
}
}
void MainWindow::on_btnOpenVideo_clicked()
{
QString file_name = QFileDialog::getOpenFileName(this, tr("Open Video"), ".", tr("Video File(*.avi *.mp4 *.h264)"));
capture.open(file_name.toStdString());
timer->start(25); // 开始计时,每隔25毫秒更新一次,超时则发出timeout()信号
}
void MainWindow::on_btnCloseVideo_clicked()
{
timer->stop(); // 停止读取数据。
// 释放内存;
capture.release();
frame.release();
}
5、mainwindow.ui内容如下
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>660</width>
<height>565</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>640</width>
<height>480</height>
</rect>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
<widget class="QWidget" name="">
<property name="geometry">
<rect>
<x>9</x>
<y>500</y>
<width>621</width>
<height>25</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="btnOpenVideo">
<property name="text">
<string>打开视频</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="btnCloseVideo">
<property name="text">
<string>关闭视频</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>660</width>
<height>23</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
版权声明:本文为mj412828668原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。