您好,登录后才能下订单哦!
海康威视是全球领先的安防产品及行业解决方案提供商,其产品广泛应用于视频监控、智能交通、智能建筑等领域。为了便于开发者集成海康威视的设备,海康威视提供了丰富的SDK(软件开发工具包)。本文将详细介绍如何在C#中连接海康威视设备,并实现常见的功能,如实时预览、视频回放、云台控制等。
海康威视SDK是海康威视为开发者提供的一套软件开发工具包,包含了丰富的API接口,支持多种编程语言,如C、C++、C#、Java等。通过SDK,开发者可以方便地集成海康威视的设备,实现视频监控、报警处理、设备管理等功能。
在开始之前,我们需要准备以下环境:
bin
目录添加到系统的PATH
环境变量中。HCNetSDK.dll
文件。x64
,以确保与64位的SDK兼容。在使用SDK之前,需要先进行初始化操作。初始化SDK的代码如下:
using System;
using System.Runtime.InteropServices;
class Program
{
[DllImport("HCNetSDK.dll")]
public static extern bool NET_DVR_Init();
static void Main(string[] args)
{
if (NET_DVR_Init())
{
Console.WriteLine("SDK初始化成功!");
}
else
{
Console.WriteLine("SDK初始化失败!");
}
}
}
在初始化SDK之后,我们需要登录到海康威视设备。设备登录的代码如下:
using System;
using System.Runtime.InteropServices;
class Program
{
[DllImport("HCNetSDK.dll")]
public static extern int NET_DVR_Login_V30(string sDVRIP, short wDVRPort, string sUserName, string sPassword, ref NET_DVR_DEVICEINFO_V30 lpDeviceInfo);
[StructLayout(LayoutKind.Sequential)]
public struct NET_DVR_DEVICEINFO_V30
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 48)]
public byte[] sSerialNumber;
public byte byAlarmInPortNum;
public byte byAlarmOutPortNum;
public byte byDiskNum;
public byte byDVRType;
public byte byChanNum;
public byte byStartChan;
public byte byAudioChanNum;
public byte byIPChanNum;
public byte byZeroChanNum;
public byte byMainProto;
public byte bySubProto;
public byte bySupport;
public byte bySupport1;
public byte bySupport2;
public byte bySupport3;
public byte byAnalogAlarmInPortNum;
public byte byStartAlarmInNo;
public byte byStartAlarmOutNo;
public byte byStartIpChanNo;
public byte byStartZeroChanNo;
public byte byStartDChanNo;
public byte byStartDTalkChanNo;
public byte byHighDChanNum;
public byte bySupport4;
public byte byLanguageType;
public byte byVoiceInChanNum;
public byte byStartVoiceInChanNo;
public byte byRes1;
public byte byRes2;
}
static void Main(string[] args)
{
string ip = "192.168.1.64";
short port = 8000;
string username = "admin";
string password = "12345";
NET_DVR_DEVICEINFO_V30 deviceInfo = new NET_DVR_DEVICEINFO_V30();
int userId = NET_DVR_Login_V30(ip, port, username, password, ref deviceInfo);
if (userId >= 0)
{
Console.WriteLine("设备登录成功!");
}
else
{
Console.WriteLine("设备登录失败!");
}
}
}
登录设备后,我们可以开始实时预览视频。实时预览的代码如下:
using System;
using System.Runtime.InteropServices;
class Program
{
[DllImport("HCNetSDK.dll")]
public static extern int NET_DVR_RealPlay_V40(int lUserID, ref NET_DVR_PREVIEWINFO lpPreviewInfo, RealDataCallBack fRealDataCallBack, IntPtr pUser);
[DllImport("HCNetSDK.dll")]
public static extern bool NET_DVR_StopRealPlay(int lRealHandle);
[StructLayout(LayoutKind.Sequential)]
public struct NET_DVR_PREVIEWINFO
{
public int lChannel;
public int dwStreamType;
public int dwLinkMode;
public IntPtr hPlayWnd;
public bool bBlocked;
public bool bPassbackRecord;
public byte byPreviewMode;
}
public delegate void RealDataCallBack(int lRealHandle, uint dwDataType, IntPtr pBuffer, uint dwBufSize, IntPtr pUser);
static void Main(string[] args)
{
int userId = 0; // 假设已经登录成功,获取到userId
NET_DVR_PREVIEWINFO previewInfo = new NET_DVR_PREVIEWINFO
{
lChannel = 1,
dwStreamType = 0,
dwLinkMode = 0,
hPlayWnd = IntPtr.Zero,
bBlocked = true,
bPassbackRecord = false,
byPreviewMode = 0
};
int realHandle = NET_DVR_RealPlay_V40(userId, ref previewInfo, null, IntPtr.Zero);
if (realHandle >= 0)
{
Console.WriteLine("实时预览启动成功!");
}
else
{
Console.WriteLine("实时预览启动失败!");
}
// 停止预览
NET_DVR_StopRealPlay(realHandle);
}
}
除了实时预览,我们还可以进行视频回放。视频回放的代码如下:
using System;
using System.Runtime.InteropServices;
class Program
{
[DllImport("HCNetSDK.dll")]
public static extern int NET_DVR_PlayBackByName(int lUserID, string sFileName, IntPtr hWnd);
[DllImport("HCNetSDK.dll")]
public static extern bool NET_DVR_StopPlayBack(int lPlayHandle);
static void Main(string[] args)
{
int userId = 0; // 假设已经登录成功,获取到userId
string fileName = "20230101080000_20230101090000.mp4"; // 回放文件名
int playHandle = NET_DVR_PlayBackByName(userId, fileName, IntPtr.Zero);
if (playHandle >= 0)
{
Console.WriteLine("视频回放启动成功!");
}
else
{
Console.WriteLine("视频回放启动失败!");
}
// 停止回放
NET_DVR_StopPlayBack(playHandle);
}
}
云台控制是视频监控中的重要功能之一。通过云台控制,我们可以调整摄像头的视角。云台控制的代码如下:
using System;
using System.Runtime.InteropServices;
class Program
{
[DllImport("HCNetSDK.dll")]
public static extern bool NET_DVR_PTZControl_Other(int lUserID, int lChannel, uint dwPTZCommand, uint dwStop, uint dwSpeed);
static void Main(string[] args)
{
int userId = 0; // 假设已经登录成功,获取到userId
int channel = 1; // 通道号
uint command = 21; // 云台控制命令,21表示向上
uint stop = 0; // 0表示开始,1表示停止
uint speed = 3; // 速度,范围1-7
bool result = NET_DVR_PTZControl_Other(userId, channel, command, stop, speed);
if (result)
{
Console.WriteLine("云台控制成功!");
}
else
{
Console.WriteLine("云台控制失败!");
}
}
}
报警处理是视频监控系统中的重要功能。通过报警处理,我们可以及时响应设备发出的报警信号。报警处理的代码如下:
using System;
using System.Runtime.InteropServices;
class Program
{
[DllImport("HCNetSDK.dll")]
public static extern bool NET_DVR_SetDVRMessageCallBack_V31(MessageCallBack fMessageCallBack, IntPtr pUser);
public delegate void MessageCallBack(int lCommand, IntPtr pAlarmer, IntPtr pAlarmInfo, uint dwBufLen, IntPtr pUser);
static void Main(string[] args)
{
int userId = 0; // 假设已经登录成功,获取到userId
bool result = NET_DVR_SetDVRMessageCallBack_V31(OnMessageCallBack, IntPtr.Zero);
if (result)
{
Console.WriteLine("报警回调设置成功!");
}
else
{
Console.WriteLine("报警回调设置失败!");
}
}
private static void OnMessageCallBack(int lCommand, IntPtr pAlarmer, IntPtr pAlarmInfo, uint dwBufLen, IntPtr pUser)
{
Console.WriteLine("收到报警信息!");
}
}
录像与截图是视频监控系统中的常见功能。通过录像与截图,我们可以保存重要的视频片段或图像。录像与截图的代码如下:
using System;
using System.Runtime.InteropServices;
class Program
{
[DllImport("HCNetSDK.dll")]
public static extern bool NET_DVR_SaveRealData(int lRealHandle, string sFileName);
[DllImport("HCNetSDK.dll")]
public static extern bool NET_DVR_CapturePicture(int lRealHandle, string sFileName);
static void Main(string[] args)
{
int realHandle = 0; // 假设已经启动实时预览,获取到realHandle
// 录像
bool result = NET_DVR_SaveRealData(realHandle, "record.mp4");
if (result)
{
Console.WriteLine("录像成功!");
}
else
{
Console.WriteLine("录像失败!");
}
// 截图
result = NET_DVR_CapturePicture(realHandle, "capture.jpg");
if (result)
{
Console.WriteLine("截图成功!");
}
else
{
Console.WriteLine("截图失败!");
}
}
}
获取设备信息是设备管理中的重要功能。通过获取设备信息,我们可以了解设备的型号、序列号、通道数等信息。设备信息获取的代码如下:
using System;
using System.Runtime.InteropServices;
class Program
{
[DllImport("HCNetSDK.dll")]
public static extern bool NET_DVR_GetDVRConfig(int lUserID, uint dwCommand, int lChannel, IntPtr lpOutBuffer, uint dwOutBufferSize, ref uint lpBytesReturned);
[StructLayout(LayoutKind.Sequential)]
public struct NET_DVR_DEVICECFG_V40
{
public uint dwSize;
public byte byDVRType;
public byte byChanNum;
public byte byStartChan;
public byte byAudioChanNum;
public byte byIPChanNum;
public byte byZeroChanNum;
public byte byMainProto;
public byte bySubProto;
public byte bySupport;
public byte bySupport1;
public byte bySupport2;
public byte bySupport3;
public byte byAnalogAlarmInPortNum;
public byte byStartAlarmInNo;
public byte byStartAlarmOutNo;
public byte byStartIpChanNo;
public byte byStartZeroChanNo;
public byte byStartDChanNo;
public byte byStartDTalkChanNo;
public byte byHighDChanNum;
public byte bySupport4;
public byte byLanguageType;
public byte byVoiceInChanNum;
public byte byStartVoiceInChanNo;
public byte byRes1;
public byte byRes2;
}
static void Main(string[] args)
{
int userId = 0; // 假设已经登录成功,获取到userId
uint command = 100; // 获取设备配置的命令
int channel = 1; // 通道号
NET_DVR_DEVICECFG_V40 deviceCfg = new NET_DVR_DEVICECFG_V40();
uint bytesReturned = 0;
IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(deviceCfg));
Marshal.StructureToPtr(deviceCfg, ptr, false);
bool result = NET_DVR_GetDVRConfig(userId, command, channel, ptr, (uint)Marshal.SizeOf(deviceCfg), ref bytesReturned);
if (result)
{
deviceCfg = (NET_DVR_DEVICECFG_V40)Marshal.PtrToStructure(ptr, typeof(NET_DVR_DEVICECFG_V40));
Console.WriteLine("设备信息获取成功!");
Console.WriteLine("设备类型:" + deviceCfg.byDVRType);
Console.WriteLine("通道数:" + deviceCfg.byChanNum);
}
else
{
Console.WriteLine("设备信息获取失败!");
}
Marshal.FreeHGlobal(ptr);
}
}
设备配置是设备管理中的重要功能。通过设备配置,我们可以修改设备的参数,如IP地址、端口号、用户名、密码等。设备配置的代码如下:
”`csharp using System; using System.Runtime.InteropServices;
class Program { [DllImport(“HCNetSDK.dll”)] public static extern bool NET_DVR_SetDVRConfig(int lUserID, uint dwCommand, int lChannel, IntPtr lpInBuffer, uint dwInBufferSize);
[StructLayout(LayoutKind.Sequential)]
public struct NET_DVR_NETCFG_V30
{
public uint dwSize;
public byte byDhcpEnable;
public byte byRes1;
public byte byRes2;
public byte byRes3;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public byte[] sIpAddress;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public byte[] sSubNetMask;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public byte[] sGateway;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public byte[] sDnsServer1;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public byte[] sDnsServer2;
public ushort wHttpPortNo;
public ushort wTcpPortNo;
public ushort wUdpPortNo;
public ushort wMaxConn;
public ushort wAlarmListenPortNo;
public ushort wVideoListenPortNo;
public ushort wPreviewListenPortNo;
public ushort wPreviewListenPortNo2;
public ushort wPreviewListenPortNo3;
public ushort wPreviewListenPortNo4;
public ushort wPreviewListenPortNo5;
public ushort wPreviewListenPortNo6;
public ushort wPreviewListenPortNo7;
public ushort wPreviewListenPortNo8;
public ushort wPreviewListenPortNo9;
public ushort wPreviewListenPortNo10;
public ushort wPreviewListenPortNo11;
public ushort wPreviewListenPortNo12;
public ushort wPreviewListenPortNo13;
public ushort wPreviewListenPortNo14;
public ushort wPreviewListenPortNo15;
public ushort wPreviewListenPortNo16;
public ushort wPreviewListenPortNo17;
public ushort wPreviewListenPortNo18;
public ushort wPreviewListenPortNo19;
public ushort wPreviewListenPortNo20;
public ushort wPreviewListenPortNo21;
public ushort wPreviewListenPortNo22;
public ushort wPreviewListenPortNo23;
public ushort wPreviewListenPortNo24;
public ushort wPreviewListenPortNo25;
public ushort wPreviewListenPortNo26;
public ushort wPreviewListenPortNo27;
public ushort
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。