实现逻辑:将一张张的截图文件数据,写入视频文件
main函数
#include "mainwindow.h"
#include <QApplication>
#include <QTextCodec>
#include <Windows.h>
using namespace std;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//编码格式:设备端不一定能识别GBK,一劳永逸(UTF-8)
QTextCodec * codec = QTextCodec::codecForName("GBK");
QTextCodec::setCodecForLocale(codec);
//调试窗口
AllocConsole();
freopen("CONOUT$","w",stdout);
//设置程序的CPU的处理优先级
SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS );
//主窗口
MainWindow w;
w.show();
return a.exec();
}
头文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QList>
#include <QDateTime>
#include <QCamera>
#include <QCameraInfo>
#include <QUrl>
#include <Windows.h>
#include <QTimer>
#include <QImage>
#include <opencv2/opencv.hpp>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButton_ScanDevice_clicked();
void on_pushButton_ConnectDevice_clicked();
void on_pushButton_clicked();
void on_pushButton_2_clicked();
public slots:
void ReadFrame();
private:
Ui::MainWindow *ui;
//用来截图的控件
cv::VideoCapture capture;
//定时器:刷新频率
QTimer *timer;
//存放一帧图像
cv::Mat frame;
private:
cv::VideoWriter videowriter;
QString fileInfo ;//视频文件的文件名
bool isRecorder;//控制是否将图像写入视频文件
private://用来扫描本地有多少摄像头
QCamera*m_Camera;
QList<QCameraInfo>m_CameraList;
};
#endif // MAINWINDOW_H
源文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
using namespace cv;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_Camera=NULL;
m_CameraList.clear();
isRecorder = false;
timer=new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(ReadFrame()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_ScanDevice_clicked()
{
//扫描本地设备
ui->comboBox->clear();
m_CameraList = QCameraInfo::availableCameras();
for (int i = 0; i<m_CameraList.size() ;i++ ) {
QString devName = m_CameraList.at(i).description();
ui->comboBox->addItem(devName);
}
}
void MainWindow::on_pushButton_ConnectDevice_clicked()
{
bool b = capture.open(ui->comboBox->currentIndex());
if(b == false ){return;}
timer->start(20);//开始计时
}
void MainWindow::ReadFrame()
{
//取一帧图像
capture>>frame;
//将视频显示在label上
QImage image=QImage((const uchar*)frame.data,frame.cols,frame.rows,QImage::Format_RGB888).rgbSwapped();
ui->label->setPixmap(QPixmap::fromImage(image));
//将取到的帧写入视频文件
if(isRecorder){
videowriter.write(frame);
}
}
void MainWindow::on_pushButton_clicked()
{
QString filePath = QString("C:/Users/38912/Desktop/");
QString fileName =QDateTime::currentDateTime().toString("yyyy_MM_dd_HH_mm_ss");
QString fileSuffic= QString(".mp4");
fileInfo = filePath+fileName+fileSuffic;
bool b = videowriter.open(fileInfo.toStdString(),1,30,cv::Size(frame.cols,frame.rows));
if(b == false){return;}//摄像头打开失败
isRecorder = true;
}
void MainWindow::on_pushButton_2_clicked()
{
isRecorder = false;
videowriter.release();
}
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>602</width>
<height>442</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QWidget" name="widget_2" native="true">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="pushButton_ScanDevice">
<property name="text">
<string>扫描设备</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="comboBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_ConnectDevice">
<property name="text">
<string>连接设备</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QWidget" name="widget_3" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>开始录像</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_2">
<property name="text">
<string>停止录像</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QLabel" name="label">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<pointsize>34</pointsize>
</font>
</property>
<property name="text">
<string>图像显示区</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>602</width>
<height>23</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>