工具使用方法:
1、在add camera后面输入对应摄像头的IP后,点击add会自动布局显示。
2、在del camera后选择一个对应IP后,点击del会自动删除对应摄像头的显示,且整体布局会自动调整。
工具使用场景:
测试摄像头的好坏。支持windows和linux系统。
部分代码实例:
mplayer.h
#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_OBJECTpublic: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
#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);}}elsem_videos[i]->setVaild(false);}});
}Widget::~Widget()
{
}
uheartthread.h
#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
#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++;}
}
详细代码实例