使用libONVIF C++库。
先发现相机。
配置
lib目录
包含
编译提示缺的文件,到onvif>libonvif里面拷贝过来。
改UDP端口
代码
使用msvc 2022的向导生成空项目,从项目的main示例拷贝过来。
CameraOnvif.h
#pragma once#include <QObject>
#include <QUrl>#include "Response.h"
#include "SoapHelper.h"
#include "OnvifDevice.h"
#include "OnvifDiscovery.h"class CameraOnvif : public QObject
{QOBJECT_Hpublic:CameraOnvif(QObject* parent = nullptr);~CameraOnvif();QObject* _parent;};
CameraOnvif.cpp
#include "CameraOnvif.h"CameraOnvif::CameraOnvif(QObject* parent)
{_parent = parent;auto cb = SoapCtx::Builder();cb.SetSendTimeout(1000);cb.SetReceiveTimeout(2000);auto discovery = new OnvifDiscoveryClient(QUrl("soap.udp://239.255.255.250:3702"), cb.Build(), _parent);ProbeTypeRequest request;request.Types = (char*)"tds:Device";auto uuidOne = QString("uuid:%1").arg(SoapHelper::GenerateUuid());auto probeResponseTwo = discovery->Probe(request, uuidOne);request.Types = (char*)"tdn:NetworkVideoTransmitter";auto uuidTwo = QString("uuid:%1").arg(SoapHelper::GenerateUuid());auto probeResponseOne = discovery->Probe(request, uuidTwo);if (probeResponseOne && probeResponseTwo) {auto foundMatches = 0;auto matchResp = discovery->ReceiveProbeMatches();if (matchResp && matchResp.GetResultObject()) {auto relatesTo = matchResp.GetSoapHeaderRelatesTo();if (!relatesTo.isNull() && (uuidOne.compare(relatesTo) == 0 || uuidTwo.compare(relatesTo) == 0)) {if (auto matchs = matchResp.GetResultObject()) {if (matchs->wsdd__ProbeMatches) {for (auto i = 0; i < matchs->wsdd__ProbeMatches->__sizeProbeMatch; ++i) {wsdd__ProbeMatchesType match = matchs->wsdd__ProbeMatches[i];for (auto ii = 0; ii < match.__sizeProbeMatch; ++ii) {foundMatches++;auto probe = match.ProbeMatch[ii];qDebug() << "Found match:" << " Type:" << probe.Types << " Endpoint:" << probe.XAddrs;if (probe.wsa5__EndpointReference.Address) {qDebug() << " Reference:" << probe.wsa5__EndpointReference.Address;}if (probe.Scopes) {auto scopeList = QString::fromLocal8Bit(probe.Scopes->__item).split(' ');auto matchBy = QString::fromLocal8Bit(probe.Scopes->MatchBy);if (!matchBy.isEmpty()) {qDebug() << " Match:" << matchBy;}qDebug() << " Scope:";for (auto scope : scopeList) {if (!scope.isEmpty()) qDebug() << " " << scope;}}}}}}}else {qDebug() << "Skipping non related message with id:" << relatesTo;}}}else {qCritical() << "nothing";}
}CameraOnvif::~CameraOnvif()
{
}
调用类。
#include "camera_onvif.h"camera_onvif::camera_onvif(QWidget *parent): QMainWindow(parent)
{ui.setupUi(this);auto camera = new CameraOnvif(this);
}camera_onvif::~camera_onvif()
{}
结果
Found match: Type: “http://www.onvif.org/ver10/network/wsdl”:NetworkVideoTransmitter tds:Device Endpoint: http://192.168.1.109/onvif/device_service
Reference: uuid:42164801-9662-9563-32ba-33e24ea19662
Scope:
“onvif://www.onvif.org/location/country/china”
“onvif://www.onvif.org/name/Dahua”
“onvif://www.onvif.org/hardware/DH-IPC-HFW3237M-I2”
“onvif://www.onvif.org/Profile/Streaming”
“onvif://www.onvif.org/type/Network_Video_Transmitter”
“onvif://www.onvif.org/extension/unique_identifier/0”
“onvif://www.onvif.org/Profile/T”