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

详细代码实例

相关推荐
西猫雷婶8 分钟前
python学opencv|读取图像(二十一)使用cv2.circle()绘制圆形进阶
开发语言·python·opencv
kiiila9 分钟前
【Qt】对象树(生命周期管理)和字符集(cout打印乱码问题)
开发语言·qt
小_太_阳35 分钟前
Scala_【2】变量和数据类型
开发语言·后端·scala·intellij-idea
直裾37 分钟前
scala借阅图书保存记录(三)
开发语言·后端·scala
唐 城1 小时前
curl 放弃对 Hyper Rust HTTP 后端的支持
开发语言·http·rust
码银3 小时前
【python】银行客户流失预测预处理部分,独热编码·标签编码·数据离散化处理·数据筛选·数据分割
开发语言·python
从善若水3 小时前
【2024】Merry Christmas!一起用Rust绘制一颗圣诞树吧
开发语言·后端·rust
黄金右肾3 小时前
Qt之数据库使用(十四)
sql·qt·sqlite·database
2401_858286114 小时前
115.【C语言】数据结构之排序(希尔排序)
c语言·开发语言·数据结构·算法·排序算法
Jelena技术达人4 小时前
Java爬虫获取1688关键字 item_search接口返回值详细解析
java·开发语言·爬虫