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++;
    }
}

详细代码实例

相关推荐
Tech Synapse1 分钟前
Java根据前端返回的字段名进行查询数据的方法
java·开发语言·后端
乌啼霜满天2499 分钟前
JDBC编程---Java
java·开发语言·sql
色空大师22 分钟前
23种设计模式
java·开发语言·设计模式
Bruce小鬼34 分钟前
QT文件基本操作
开发语言·qt
2202_7544215440 分钟前
生成MPSOC以及ZYNQ的启动文件BOOT.BIN的小软件
java·linux·开发语言
我只会发热1 小时前
Java SE 与 Java EE:基础与进阶的探索之旅
java·开发语言·java-ee
懷淰メ1 小时前
PyQt飞机大战游戏(附下载地址)
开发语言·python·qt·游戏·pyqt·游戏开发·pyqt5
hummhumm1 小时前
第 22 章 - Go语言 测试与基准测试
java·大数据·开发语言·前端·python·golang·log4j
宁静@星空1 小时前
006-自定义枚举注解
java·开发语言
hummhumm1 小时前
第 28 章 - Go语言 Web 开发入门
java·开发语言·前端·python·sql·golang·前端框架