摄像机搜索模块是后面新增加的,这个模块很有必要,一开始做视频监控系统的时候,那时候还没有研究出来一套纯Qt底层通信的onvif类,也就前几个月搞出来了,支持搜索设备信息和云台控制,所以马上加到了监控系统中,大部分的监控系统都支持两种搜索,一种是自家私有协议即SDK的方式搜索设备,只能搜索到自家的设备,另外一种就是用国际标准协议onvif,这个协议对于市面上大部分的网络摄像机都支持的,我买过的最便宜的onvif摄像机才70块钱,onvif内容还是比较多的,常用的基本上就三点:搜索设备拿到设备的详细信息比如视频流地址;云台控制;预置位设置与控制;
摄像机搜索功能直接做到了摄像机管理中,顶部提供一个搜索按钮,平时搜索功能是看不到了,切换搜索按钮用来显示和隐藏搜素区域,提供了广播搜索和单播搜索两种,广播搜素需要指定网卡搜索,单播搜索适用于一些不在同一个网段内,但是设备是可以ping通的,这就需要指定摄像机的IP进行单播搜索,用户姓名和用户密码必须填写正确,是对应摄像机的onvif用户信息,而不是摄像机的web登录用户。搜索到的设备都会在左侧的列表中显示,此时设备地址、媒体地址、云台地址、主码流地址、子码流地址、5个字段是空白的,需要单击获取所有按钮来获取这几个信息,一般来说一个系统所用的摄像机是一个厂家的,默认的用户名和密码也是统一的,这样就只要设置一次用户信息单击获取所有即可,个别用户名和密码不同的需要单独设置好再单击获取当前按钮。单击添加选中按钮自动一键添加所有勾选了的摄像机,默认搜索到的设备是全部勾选的,单击添加当前只用来当前焦点的设备。
分页组件开源地址:https://gitee.com/feiyangqingyun/QWidgetDemo https://github.com/feiyangqingyun/QWidgetDemo
文件名称:dbpage
体验地址:https://gitee.com/feiyangqingyun/QWidgetExe https://github.com/feiyangqingyun/QWidgetExe
文件名称:bin_video_system.zip
#include "frmconfigsearch.h"
#include "ui_frmconfigsearch.h"
#include "quiwidget.h"
#include "onviffunction.h"
#include "onvifmedia.h"
frmConfigSearch::frmConfigSearch(QWidget *parent) : QWidget(parent), ui(new Ui::frmConfigSearch)
{
ui->setupUi(this);
this->initForm();
this->initTable();
}
frmConfigSearch::~frmConfigSearch()
{
delete ui;
}
void frmConfigSearch::initForm()
{
//设置默认的用户名和密码+本机IP,可以自行更改
ui->txtUserName->setText("admin");
ui->txtUserPwd->setText("admin");
ui->cboxLocalIP->addItems(OnvifFunction::getLocalIps());
//实例化onvif搜索,绑定信号槽
onvifSearch = new OnvifSearch(this);
connect(onvifSearch, SIGNAL(receiveDevice(OnvifSearch::DeviceInfo)), this, SLOT(receiveDevice(OnvifSearch::DeviceInfo)));
}
void frmConfigSearch::initTable()
{
QList columnNames;
columnNames << "" << "地址" << "用户名称" << "用户密码" << "厂家" << "设备地址" << "媒体地址" << "云台地址" << "主码流" << "子码流";
QList columnWidths;
columnWidths << 21 << 50 << 80 << 80 << 80 << 300 << 300 << 300 << 350 << 350;
ui->tableWidget->setStyleSheet("QCheckBox{padding:0px 0px 0px 3px;}");
//设置列数和列宽
int columnCount = columnWidths.count();
ui->tableWidget->setColumnCount(columnCount);
for (int i = 0; i < columnCount; i++) {
ui->tableWidget->setColumnWidth(i, columnWidths.at(i));
}
//设置行高
ui->tableWidget->setRowCount(99);
for (int i = 0; i < ui->tableWidget->rowCount(); i++) {
ui->tableWidget->setRowHeight(i, 25);
}
ui->tableWidget->setHorizontalHeaderLabels(columnNames);
ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
//ui->tableWidget->setAlternatingRowColors(true);
ui->tableWidget->verticalHeader()->setVisible(true);
ui->tableWidget->verticalHeader()->setHighlightSections(false);
ui->tableWidget->horizontalHeader()->setHighlightSections(false);
ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
ui->tableWidget->verticalHeader()->setDefaultSectionSize(22);
ui->tableWidget->setColumnHidden(1, true);
ui->tableWidget->setColumnHidden(2, true);
ui->tableWidget->setColumnHidden(3, true);
ui->tableWidget->horizontalHeader()->setSortIndicator(1, Qt::AscendingOrder);
//增加一个全选按钮
ckAll = new QCheckBox(ui->tableWidget);
connect(ckAll, SIGNAL(stateChanged(int)), this, SLOT(stateChanged(int)));
ckAll->move(23, 5);
ckAll->setChecked(true);
}
void frmConfigSearch::clear()
{
qDeleteAll(devices);
devices.clear();
ui->tableWidget->clearContents();
}
void frmConfigSearch::stateChanged(int arg1)
{
for(int row = 0; row < devices.count(); row++) {
QCheckBox *itemCk = (QCheckBox *)ui->tableWidget->cellWidget(row, 0);
if (itemCk > 0) {
itemCk->setChecked(arg1 != 0);
}
}
}
void frmConfigSearch::receiveDevice(const OnvifSearch::DeviceInfo &deviceInfo)
{
int deviceCount = devices.count();
QString addr = deviceInfo.addr;
QString ip = deviceInfo.ip;
QTableWidgetItem *itemName = new QTableWidgetItem(deviceInfo.name);
QTableWidgetItem *itemAddr = new QTableWidgetItem(deviceInfo.addr);
//复选框,如果勾选了全选则自动选中当前设备
QCheckBox *itemCk = new QCheckBox(this);
itemCk->setChecked(ckAll->isChecked());
//取IP地址末尾位数作为排序依据
QStringList list = ip.split(".");
QString lastAddr = list.last();
QTableWidgetItem *itemLastAddr = new QTableWidgetItem;
itemLastAddr->setData(Qt::DisplayRole, lastAddr.toInt());
ui->tableWidget->setCellWidget(deviceCount, 0, itemCk);
ui->tableWidget->setItem(deviceCount, 1, itemLastAddr);
ui->tableWidget->setItem(deviceCount, 4, itemName);
ui->tableWidget->setItem(deviceCount, 5, itemAddr);
OnvifDevice *device = new OnvifDevice(this);
device->setDeviceUrl(addr);
//将当前广播搜索返回的设备信息一起打包发给设备类
QMap info;
info.insert("addr", deviceInfo.addr);
info.insert("ip", deviceInfo.ip);
info.insert("name", deviceInfo.name);
info.insert("location", deviceInfo.location);
info.insert("hardware", deviceInfo.hardware);
device->setDeviceInfo(info);
devices << device;
ui->tableWidget->sortByColumn(1);
}
void frmConfigSearch::on_btnSearchAll_clicked()
{
clear();
QString localIP = ui->cboxLocalIP->currentText();
QString deviceIP = ui->txtDeviceIP->text();
onvifSearch->search(localIP, deviceIP, false);
}
void frmConfigSearch::on_btnSearchOne_clicked()
{
clear();
QString localIP = ui->cboxLocalIP->currentText();
QString deviceIP = ui->txtDeviceIP->text();
onvifSearch->search(localIP, deviceIP, true);
}
void frmConfigSearch::on_btnMediaAll_clicked()
{
QString userName = ui->txtUserName->text().trimmed();
QString userPwd = ui->txtUserPwd->text().trimmed();
QString userInfo = QString("rtsp://%1:%2@").arg(userName).arg(userPwd);
int count = devices.count();
foreach (OnvifDevice *device, devices) {
//找到当前设备所在行
QString url = device->getDeviceUrl();
int row = -1;
for (int i = 0; i < count; i++) {
QString addr = ui->tableWidget->item(i, 5)->text();
if (url == addr) {
row = i;
break;
}
}
if (row >= 0) {
//先设置用户名和密码
device->setUser(userName, userPwd);
//如果没有获取到地址则说明设备可能是 onvif1.0 重新发送1.0对应的请求数据
device->getServices();
if (device->getMediaUrl().isEmpty() || device->getPtzUrl().isEmpty()) {
device->getCapabilities();
}
//获取概要文件profile
QString rtspMain, rtspSub;
QStringList tokens = device->getMedia();
if (tokens.count() > 0) {
rtspMain = device->getRtspUri(tokens.at(0));
rtspMain = rtspMain.replace("rtsp://", userInfo);
//如果有第二个token则为子码流
if (tokens.count() > 1) {
rtspSub = device->getRtspUri(tokens.at(1));
rtspSub = rtspSub.replace("rtsp://", userInfo);
}
}
//返回的数据添加到表格中
QTableWidgetItem *itemUserName = new QTableWidgetItem(userName);
QTableWidgetItem *itemUserPwd = new QTableWidgetItem(userPwd);
QTableWidgetItem *itemMediaUri = new QTableWidgetItem(device->getMediaUrl());
QTableWidgetItem *itemPtzUri = new QTableWidgetItem(device->getPtzUrl());
QTableWidgetItem *itemRtspMain = new QTableWidgetItem(rtspMain);
QTableWidgetItem *itemRtspSub = new QTableWidgetItem(rtspSub);
ui->tableWidget->setItem(row, 2, itemUserName);
ui->tableWidget->setItem(row, 3, itemUserPwd);
ui->tableWidget->setItem(row, 6, itemMediaUri);
ui->tableWidget->setItem(row, 7, itemPtzUri);
ui->tableWidget->setItem(row, 8, itemRtspMain);
ui->tableWidget->setItem(row, 9, itemRtspSub);
}
}
}
void frmConfigSearch::on_btnMediaOne_clicked()
{
int row = ui->tableWidget->currentRow();
if (row < 0) {
QUIHelper::showMessageBoxError("请先选中设备!", 3);
return;
}
QCheckBox *itemCk = (QCheckBox *)ui->tableWidget->cellWidget(row, 0);
if (itemCk <= 0) {
QUIHelper::showMessageBoxError("设备地址不能为空!", 3);
return;
}
QString userName = ui->txtUserName->text().trimmed();
QString userPwd = ui->txtUserPwd->text().trimmed();
QString userInfo = QString("rtsp://%1:%2@").arg(userName).arg(userPwd);
QString addr = ui->tableWidget->item(row, 5)->text();
foreach (OnvifDevice *device, devices) {
if (device->getDeviceUrl() == addr) {
//先设置用户名和密码
device->setUser(userName, userPwd);
//如果没有获取到地址则说明设备可能是 onvif1.0 重新发送1.0对应的请求数据
device->getServices();
if (device->getMediaUrl().isEmpty() || device->getPtzUrl().isEmpty()) {
device->getCapabilities();
}
//获取概要文件profile
QString rtspMain, rtspSub;
QStringList tokens = device->getMedia();
if (tokens.count() > 0) {
rtspMain = device->getRtspUri(tokens.at(0));
rtspMain = rtspMain.replace("rtsp://", userInfo);
//如果有第二个token则为子码流
if (tokens.count() > 1) {
rtspSub = device->getRtspUri(tokens.at(1));
rtspSub = rtspSub.replace("rtsp://", userInfo);
}
}
//返回的数据添加到表格中
QTableWidgetItem *itemUserName = new QTableWidgetItem(userName);
QTableWidgetItem *itemUserPwd = new QTableWidgetItem(userPwd);
QTableWidgetItem *itemMediaUri = new QTableWidgetItem(device->getMediaUrl());
QTableWidgetItem *itemPtzUri = new QTableWidgetItem(device->getPtzUrl());
QTableWidgetItem *itemRtspMain = new QTableWidgetItem(rtspMain);
QTableWidgetItem *itemRtspSub = new QTableWidgetItem(rtspSub);
ui->tableWidget->setItem(row, 2, itemUserName);
ui->tableWidget->setItem(row, 3, itemUserPwd);
ui->tableWidget->setItem(row, 6, itemMediaUri);
ui->tableWidget->setItem(row, 7, itemPtzUri);
ui->tableWidget->setItem(row, 8, itemRtspMain);
ui->tableWidget->setItem(row, 9, itemRtspSub);
break;
}
}
}
void frmConfigSearch::on_btnAddAll_clicked()
{
//找出勾选的,过滤没有视频流地址的
QList deviceInfos;
int deviceCount = devices.count();
for(int row = 0; row < deviceCount; row++) {
QCheckBox *itemCk = (QCheckBox *)ui->tableWidget->cellWidget(row, 0);
if (itemCk > 0) {
QTableWidgetItem *item = ui->tableWidget->item(row, 8);
if (item > 0) {
QString rtspMain = item->text();
if (itemCk->isChecked() && !rtspMain.isEmpty()) {
QStringList deviceInfo;
QString userName = ui->tableWidget->item(row, 2)->text();
QString userPwd = ui->tableWidget->item(row, 3)->text();
QString ipcType = ui->tableWidget->item(row, 4)->text();
QString onvifAddr = ui->tableWidget->item(row, 5)->text();
QString mediaAddr = ui->tableWidget->item(row, 6)->text();
QString ptzAddr = ui->tableWidget->item(row, 7)->text();
QString rtspSub = ui->tableWidget->item(row, 9)->text();
deviceInfo << userName << userPwd << ipcType << onvifAddr << mediaAddr << ptzAddr << rtspMain << rtspSub;
deviceInfos << deviceInfo;
}
}
}
}
emit addDevices(deviceInfos);
}
void frmConfigSearch::on_btnAddOne_clicked()
{
int row = ui->tableWidget->currentRow();
if (row < 0) {
QUIHelper::showMessageBoxError("请先选中设备!", 3);
return;
}
QList deviceInfos;
QTableWidgetItem *item = ui->tableWidget->item(row, 8);
if (item > 0) {
QString rtspMain = item->text();
if (!rtspMain.isEmpty()) {
QStringList deviceInfo;
QString userName = ui->tableWidget->item(row, 2)->text();
QString userPwd = ui->tableWidget->item(row, 3)->text();
QString ipcType = ui->tableWidget->item(row, 4)->text();
QString onvifAddr = ui->tableWidget->item(row, 5)->text();
QString mediaAddr = ui->tableWidget->item(row, 6)->text();
QString ptzAddr = ui->tableWidget->item(row, 7)->text();
QString rtspSub = ui->tableWidget->item(row, 9)->text();
deviceInfo << userName << userPwd << ipcType << onvifAddr << mediaAddr << ptzAddr << rtspMain << rtspSub;
deviceInfos << deviceInfo;
}
}
emit addDevices(deviceInfos);
}