ONVIF协议,连接海康大华摄像头实现如下功能
1.搜索摄像头设备
2.获取rtsp地址
3.获取抓图地址并抓图
4.云台控制
采用.net core 3.1 C#编写命令行工程DEMO源码
ONVIF协议,连接海康大华摄像头实现如下功能
1.搜索摄像头设备
2.获取rtsp地址
3.获取抓图地址并抓图
4.云台控制
采用.net core 3.1 C#编写命令行工程DEMO源码
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using ONVIF.Core;
using ONVIF.Core.Device;
using ONVIF.Core.Media;
using ONVIF.Core.PTZ;
using ONVIF.Core.Profile;
using ONVIF.Core.Utils;
namespace OnvifDemo
{
class Program
{
static async Task Main(string[] args)
{
string ipAddress = "192.168.1.64"; // 替换为摄像头的IP地址
int port = 8000; // 摄像头ONVIF服务的端口,通常是8000或80
string user = "admin"; // 替换为摄像头的用户名
string password = "password"; // 替换为摄像头的密码
try
{
// 搜索设备
var devices = await DiscoverDevicesAsync();
Console.WriteLine("Discovered Devices:");
foreach (var device in devices)
{
Console.WriteLine($"IP: {device.XAddr.Host}, Name: {device.Name}");
}
// 获取设备实例
var deviceManager = new DeviceManager(new DeviceClient(ipAddress, port, user, password));
// 获取RTSP地址
var profiles = await deviceManager.GetProfilesAsync();
var mediaService = new MediaService(new MediaClient(ipAddress, port, user, password));
foreach (var profile in profiles)
{
var streamingCapabilities = await mediaService.GetStreamingCapabilitiesAsync(profile.Token);
foreach (var rtspUri in streamingCapabilities.RtspUris)
{
Console.WriteLine($"RTSP URI for profile {profile.Name}: {rtspUri.Uri}");
}
}
// 获取抓图地址并抓图
var snapshotUri = await GetSnapshotUriAsync(deviceManager, profiles.First().Token);
await DownloadImageAsync(snapshotUri, "snapshot.jpg");
// 云台控制
var ptzService = new PTZService(new PTZClient(ipAddress, port, user, password));
var ptzConfigurations = await ptzService.GetConfigurationsAsync();
if (ptzConfigurations.Any())
{
var ptzConfiguration = ptzConfigurations.First();
var ptzProfile = profiles.First(p => p.Token == ptzConfiguration.ProfileToken);
var request = new AbsoluteMove
{
Profile = ptzProfile.Token,
Speed = new PTZSpeed
{
PanTilt = new Vector2D { Space = "http://www.onvif.org/ver20/ptz/speed/PanTilt", X = 1.0, Y = 1.0 },
Zoom = new Vector1D { Space = "http://www.onvif.org/ver20/ptz/speed/Zoom", X = 1.0 }
},
Position = new PTZVector
{
PanTilt = new Vector2D { Space = "http://www.onvif.org/ver10/absolute/position", X = 0.0, Y = 0.0 },
Zoom = new Vector1D { Space = "http://www.onvif.org/ver10/absolute/zoom", X = 0.0 }
}
};
await ptzService.ContinuousMoveAsync(request);
// 等待几秒钟以观察效果
await Task.Delay(5000);
await ptzService.StopAsync();
}
}
catch (SocketException ex)
{
Console.WriteLine($"Socket error: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
private static async Task<List<Device>> DiscoverDevicesAsync()
{
var discoverer = new DeviceDiscoverer();
var devices = await discoverer.DiscoverAsync(TimeSpan.FromSeconds(5));
return devices;
}
private static async Task<string> GetSnapshotUriAsync(DeviceManager deviceManager, string profileToken)
{
var mediaProfiles = await deviceManager.GetProfilesAsync(new[] { Profiles.Media });
var mediaProfile = mediaProfiles.FirstOrDefault(p => p.Token == profileToken);
if (mediaProfile == null)
throw new InvalidOperationException("Profile not found");
var mediaService = new MediaService(new MediaClient(deviceManager.Client.Endpoint.Address.Uri.Host, deviceManager.Client.Endpoint.Address.Uri.Port, deviceManager.Client.Credentials.UserName, deviceManager.Client.Credentials.Password));
var snapshotUri = await mediaService.GetSnapshotUriAsync(mediaProfile.Token);
return snapshotUri;
}
private static async Task DownloadImageAsync(string uri, string filePath)
{
using (var client = new System.Net.Http.HttpClient())
{
var response = await client.GetAsync(uri);
response.EnsureSuccessStatusCode();
using (var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
await response.Content.CopyToAsync(fs);
}
}
}
}
}