QT+OPENCV实现监控
mianwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <opencv2/videoio.hpp>
#include <QTimer>
#include <opencv2/opencv.hpp>
# include <QDate>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
void get_camera_hash();
~MainWindow();
private slots:
void on_startButton_clicked();
void importFrame();
void on_stopButton_clicked();
void choose_video_dir();
void choose_camera();
void saveVideo();
private:
Ui::MainWindow *ui;
cv::VideoCapture cap;
QTimer *timer;
QTimer *timer2;
cv::Mat frame;
cv::VideoWriter write;
int counter=0;
int video_width=0;
int video_height=0;
int deviceID = 0;
QString file_name = QDate::currentDate().toString("yyyy-MM-dd") + ".avi";
QString file_path = QString("./%1_%2").arg(deviceID).arg(file_name);
bool flag=false;
QHash<int, QString> camera_hash;
};
#endif // MAINWINDOW_H
mianwindow.cpp
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <QFileDialog>
#include <QCameraDevice>
#include <QMediaDevices>
#include "mainwindow.h"
#include "./ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QRect rect = ui->video_label->geometry();
video_width = rect.width();
video_height = rect.height();
this->get_camera_hash();
timer = new QTimer(this);
timer2 = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(importFrame()));
connect(timer2, SIGNAL(timeout()), this, SLOT(saveVideo()));
connect(ui->action, SIGNAL(triggered()), this, SLOT(choose_video_dir()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_startButton_clicked()
{
cap.open(deviceID);
flag = true;
timer->start(30);
timer2->start(30);
}
void MainWindow::on_stopButton_clicked()
{
flag = false;
cap.release(); //释放相机捕获对象
timer->stop();
timer2->stop();
ui->video_label->clear();
write.release();
}
void MainWindow::importFrame()
{
counter++;
cap >> frame;
cv::flip(frame,frame,180);
cvtColor(frame, frame, cv::COLOR_BGR2RGB); //only RGB of Qt
QImage srcQImage = QImage((uchar*)(frame.data), frame.cols, frame.rows, QImage::Format_RGB888);
ui->video_label->setPixmap(QPixmap::fromImage(srcQImage));
ui->video_label->show();
}
void MainWindow::choose_video_dir(){
QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"), ".", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
if (!dir.isEmpty()){
file_path = QString("%1/%2_%3").arg(dir).arg(deviceID).arg(file_name);
}
}
void MainWindow::saveVideo(){
int fourcc = cv::VideoWriter::fourcc('M', 'J', 'P', 'G');
double fps = 30.0;
cv::Size frameSize(640, 480);
write.open(file_path.toStdString(), fourcc, fps, frameSize, true);
if (!cap.isOpened()) {
cap.open(deviceID);
}
cv::Mat frame;
while (flag) {
cap >> frame;
if (!frame.empty()) {
write.write(frame);
}
qApp->processEvents();
}
}
void MainWindow::choose_camera(){
QHash<QString,QVariant> data = qobject_cast<QAction*>(sender())->data().toHash();
int index = data.find("index")->toInt();
deviceID = index;
}
void MainWindow::get_camera_hash(){
QList<QAction *> childActionList;
QMenu *childMenu = new QMenu();
const QList<QCameraDevice> cameras = QMediaDevices::videoInputs();
for(int i=0;i<cameras.size();i++){
QString description = cameras[i].description();
camera_hash.insert(i,description);
QAction *child_camera_action = new QAction(childMenu);
child_camera_action->setText(description);
QHash<QString, QVariant> item;
item.insert("index",i);
item.insert("desc",description);
QVariant variant(item);
child_camera_action->setData(variant);
connect(child_camera_action,SIGNAL(triggered()), this, SLOT(choose_camera()));
childActionList<<child_camera_action;
}
childMenu->addActions(childActionList);
ui->camera_action->setMenu(childMenu);
}
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>800</width>
<height>600</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>20</x>
<y>10</y>
<width>68</width>
<height>22</height>
</rect>
</property>
<property name="text">
<string>实时动画</string>
</property>
</widget>
<widget class="QPushButton" name="startButton">
<property name="geometry">
<rect>
<x>60</x>
<y>500</y>
<width>92</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>开启摄像头</string>
</property>
</widget>
<widget class="QLabel" name="video_label">
<property name="geometry">
<rect>
<x>20</x>
<y>40</y>
<width>751</width>
<height>441</height>
</rect>
</property>
<property name="text">
<string/>
</property>
</widget>
<widget class="QPushButton" name="stopButton">
<property name="geometry">
<rect>
<x>680</x>
<y>500</y>
<width>92</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>关闭摄像头</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>27</height>
</rect>
</property>
<widget class="QMenu" name="menu">
<property name="title">
<string>配置</string>
</property>
<addaction name="action"/>
<addaction name="camera_action"/>
</widget>
<addaction name="menu"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<action name="action">
<property name="text">
<string>设置视频存放路径</string>
</property>
</action>
<action name="camera_action">
<property name="text">
<string>设置摄像头</string>
</property>
</action>
</widget>
<resources/>
<connections/>
</ui>
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}