QT实现多摄像头监控

工具使用方法:

1、在add camera后面输入对应摄像头的IP后,点击add会自动布局显示。

2、在del camera后选择一个对应IP后,点击del会自动删除对应摄像头的显示,且整体布局会自动调整。
工具使用场景

测试摄像头的好坏。支持windows和linux系统。
部分代码实例

mplayer.h

cpp 复制代码
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include "mplayer.h"
#include <QPushButton>
#include <QComboBox>
#include <QLineEdit>
#include <QGridLayout>
#include <QVBoxLayout>

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();

private:
    QVector<MPlayer*> m_videos;
    QStringList m_videoUrls;
    QWidget* m_btnWid = nullptr;
    QWidget* m_videoWid = nullptr;
    QPushButton* m_addBtn = nullptr;
    QPushButton* m_delBtn = nullptr;
    QLineEdit* m_urlEdit = nullptr;
    QComboBox* m_urlBox = nullptr;
};

#endif // WIDGET_H

mplayer.cpp

cpp 复制代码
#include "widget.h"
#include "uheartthread.h"
#include <QLabel>

Widget::Widget(QWidget *parent) :
    QWidget(parent)
{    
    this->resize(1280,800);

    m_btnWid = new QWidget(this);
    m_videoWid = new QWidget(this);
    QGridLayout* glay = new QGridLayout(this);
    glay->addWidget(m_btnWid,0,0,1,1);
    glay->addWidget(m_videoWid,1,0,10,1);
    glay->setMargin(0);
    this->setLayout(glay);

    QLabel* lab1 = new QLabel("add camera:",m_btnWid);
    QLabel* lab2 = new QLabel("del camera:",m_btnWid);
    m_addBtn = new QPushButton("add",m_btnWid);
    m_delBtn = new QPushButton("del",m_btnWid);
    m_urlEdit = new QLineEdit(m_btnWid);
    m_urlBox = new QComboBox(m_btnWid);
    QGridLayout* glay1 = new QGridLayout(m_btnWid);
    glay1->addWidget(lab1,0,0,1,1);
    glay1->addWidget(m_urlEdit,0,1,1,3);
    glay1->addWidget(m_addBtn,0,4,1,1);
    glay1->addWidget(lab2,1,0,1,1);
    glay1->addWidget(m_urlBox,1,1,1,3);
    glay1->addWidget(m_delBtn,1,4,1,1);
    glay1->setMargin(0);
    m_btnWid->setLayout(glay1);
    m_urlEdit->setText("192.168.1.216");
    m_addBtn->setFixedHeight(60);
    m_delBtn->setFixedHeight(60);
    m_urlEdit->setFixedHeight(60);
    m_urlBox->setFixedHeight(60);

    QGridLayout* glay2 = new QGridLayout(m_videoWid);
    glay2->setMargin(0);
    m_videoWid->setLayout(glay2);

    //初始化摄像头检测
    UHeartThread* thread = new UHeartThread;
    thread->start();
    connect(m_addBtn,&QPushButton::clicked,this,[=](){
        //摄像头
        MPlayer* video = new MPlayer(m_videoWid);
        QString url = QString("rtsp://admin:abcd-1234@%1:554/h264/ch1/main/av_stream").arg(m_urlEdit->text());
        video->setUrl(url);
        video->stopAudio();

        m_videos.append(video);
        for (int i = 0; i < m_videos.size(); ++i) {
            glay2->addWidget(m_videos[i],i/2,i%2,1,1);
        }
        m_videoUrls.append(url);
        m_urlBox->addItem(m_urlEdit->text());
        thread->m_urllist.append(m_urlEdit->text());
    });
    connect(m_delBtn,&QPushButton::clicked,this,[=](){
        if (m_videos.size() <= 0)return;
        for (int i = 0; i < m_videos.size(); ++i) {
            glay2->removeWidget(m_videos[i]);
        }

        MPlayer* video = m_videos[m_urlBox->currentIndex()];
        m_videos.removeOne(video);
        for (int i = 0; i < m_videos.size(); ++i) {
            glay2->addWidget(m_videos[i],i/2,i%2,1,1);
        }
        glay2->removeWidget(video);
        m_videoUrls.removeAt(m_urlBox->currentIndex());
        thread->m_urllist.removeAt(m_urlBox->currentIndex());
        m_urlBox->removeItem(m_urlBox->currentIndex());
        video->stop();
        delete video;
        this->update();
    });

    connect(thread,&UHeartThread::sigCamera1IsOn,this,[=](int i,bool ok){
        if (m_videos.size() > i)
        {
            if (ok)
            {
                if (!m_videos[i]->getVaild())
                {
                    m_videos[i]->play();
                    m_videos[i]->setVaild(true);
                }
            }
            else
                m_videos[i]->setVaild(false);
        }
    });
}

Widget::~Widget()
{
}

uheartthread.h

cpp 复制代码
#ifndef UHEARTTHREAD_H
#define UHEARTTHREAD_H

#include <QThread>

class UHeartThread : public QThread
{
    Q_OBJECT
public:
    UHeartThread();

    virtual void run();

    QStringList m_urllist;
signals:
    void sigCamera1IsOn(int i,bool ok);
};

#endif // UHEARTTHREAD_H

uheartthread.cpp

cpp 复制代码
#include "uheartthread.h"
#include <QDebug>
UHeartThread::UHeartThread()
{
}

QString SystemCmdRet(const char* cmd)
{
    static char buf[1024];

    QString ret = QString();
    memset(buf, 0x0, sizeof(buf));
    FILE* fp = popen(cmd, "r");
    if (fp == nullptr)
    {
        perror(QString("##Err: GetSystemCmdRet->").arg(cmd).toLatin1().constData());
        return QString();
    }
    fread(buf, sizeof(char), sizeof(buf), fp);
    ret = QString(buf);
    pclose(fp);

    return ret;
}

void UHeartThread::run()
{
    static int index = 0;
    while (1) {
        for (int i = 0; i < m_urllist.size(); ++i) {
            QString pingtest = SystemCmdRet(QString("ping -c 1 -W 1 %1 &").arg(m_urllist[i]).toUtf8().data());
            bool ok = pingtest.contains("1 packets transmitted, 1 received, 0% packet loss,");
            if (!ok)
                qDebug()<<i<<pingtest;
            emit sigCamera1IsOn(i,ok);
            sleep(2);
        }

        index++;
    }
}

详细代码实例

相关推荐
学习路上_write5 分钟前
FPGA/Verilog,Quartus环境下if-else语句和case语句RT视图对比/学习记录
单片机·嵌入式硬件·qt·学习·fpga开发·github·硬件工程
远望清一色15 分钟前
基于MATLAB边缘检测博文
开发语言·算法·matlab
何曾参静谧23 分钟前
「Py」Python基础篇 之 Python都可以做哪些自动化?
开发语言·python·自动化
Prejudices27 分钟前
C++如何调用Python脚本
开发语言·c++·python
我狠狠地刷刷刷刷刷40 分钟前
中文分词模拟器
开发语言·python·算法
wyh要好好学习43 分钟前
C# WPF 记录DataGrid的表头顺序,下次打开界面时应用到表格中
开发语言·c#·wpf
AitTech44 分钟前
C#实现:电脑系统信息的全面获取与监控
开发语言·c#
qing_0406031 小时前
C++——多态
开发语言·c++·多态
孙同学_1 小时前
【C++】—掌握STL vector 类:“Vector简介:动态数组的高效应用”
开发语言·c++
froginwe111 小时前
XML 编辑器:功能、选择与使用技巧
开发语言