下面我将详细讲解如何在C#中连接海康威视。
1.准备工作
在开始之前,我们需要做一些准备工作。
-
安装海康威视设备的SDK,可以从海康威视官网下载对应版本的SDK。
-
将SDK的bin目录下的HCNetSDK.dll文件拷贝到C#项目的bin目录下。
2.连接海康威视
接下来,我们将通过以下步骤连接海康威视。
- 引入HCNetSDK.dll
在代码中添加以下代码:
using System.Runtime.InteropServices;
[DllImport("HCNetSDK.dll")]
public static extern bool NET_DVR_Init();
[DllImport("HCNetSDK.dll")]
public static extern bool NET_DVR_Cleanup();
[DllImport("HCNetSDK.dll")]
public static extern int NET_DVR_Login_V30(string sDVRIP, int wDVRPort, string sUserName, string sPassword, ref NET_DVR_DEVICEINFO_V30 lpDeviceInfo);
[DllImport("HCNetSDK.dll")]
public static extern bool NET_DVR_Logout(int iUserID);
- 初始化SDK
在代码中添加以下代码:
if (NET_DVR_Init())
{
Console.WriteLine("初始化SDK成功!");
}
else
{
Console.WriteLine("初始化SDK失败!错误码:" + NET_DVR_GetLastError());
return;
}
其中,NET_DVR_GetLastError()函数可以获取当前SDK的错误码。
- 登录设备
在代码中添加以下代码:
NET_DVR_DEVICEINFO_V30 deviceInfo = new NET_DVR_DEVICEINFO_V30();
int userID = NET_DVR_Login_V30("设备IP地址", 端口号, "用户名", "密码", ref deviceInfo);
if (userID < 0)
{
Console.WriteLine("登录设备失败!错误码:" + NET_DVR_GetLastError());
NET_DVR_Cleanup();
return;
}
else
{
Console.WriteLine("登录设备成功!");
}
其中,NET_DVR_Login_V30()函数返回的是userID,如果返回值小于0,则表示登录失败。
- 登出设备
在代码中添加以下代码:
bool logout = NET_DVR_Logout(userID);
if (!logout)
{
Console.WriteLine("登出设备失败!错误码:" + NET_DVR_GetLastError());
}
else
{
Console.WriteLine("登出设备成功!");
}
3.示例
下面是两个示例。第一个示例连接设备并获取设备参数。第二个示例连接设备并打开实时预览。
示例1:获取设备参数
using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
namespace Hikvision
{
[StructLayout(LayoutKind.Sequential)]
public struct NET_DVR_DEVICEINFO_V30
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 48)]
public string 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 bySupport;
public byte byRes1;
public ushort wDevType;
public byte byRes2;
}
public class HikvisionTest
{
[DllImport("HCNetSDK.dll")]
public static extern bool NET_DVR_Init();
[DllImport("HCNetSDK.dll")]
public static extern bool NET_DVR_Cleanup();
[DllImport("HCNetSDK.dll")]
public static extern int NET_DVR_Login_V30(string sDVRIP, int wDVRPort, string sUserName, string sPassword, ref NET_DVR_DEVICEINFO_V30 lpDeviceInfo);
[DllImport("HCNetSDK.dll")]
public static extern bool NET_DVR_Logout(int iUserID);
[DllImport("HCNetSDK.dll")]
public static extern int NET_DVR_GetLastError();
static void Main(string[] args)
{
if (NET_DVR_Init())
{
Console.WriteLine("初始化SDK成功!");
}
else
{
Console.WriteLine("初始化SDK失败!错误码:" + NET_DVR_GetLastError());
return;
}
NET_DVR_DEVICEINFO_V30 deviceInfo = new NET_DVR_DEVICEINFO_V30();
int userID = NET_DVR_Login_V30("设备IP地址", 端口号, "用户名", "密码", ref deviceInfo);
if (userID < 0)
{
Console.WriteLine("登录设备失败!错误码:" + NET_DVR_GetLastError());
NET_DVR_Cleanup();
return;
}
else
{
Console.WriteLine("登录设备成功!");
}
Console.WriteLine("设备序列号:" + deviceInfo.sSerialNumber);
Console.WriteLine("通道数:" + deviceInfo.byChanNum);
bool logout = NET_DVR_Logout(userID);
if (!logout)
{
Console.WriteLine("登出设备失败!错误码:" + NET_DVR_GetLastError());
}
else
{
Console.WriteLine("登出设备成功!");
}
NET_DVR_Cleanup();
}
}
}
示例2:打开实时预览
using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
namespace Hikvision
{
[StructLayout(LayoutKind.Sequential)]
public struct NET_DVR_DEVICEINFO_V30
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 48)]
public string 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 bySupport;
public byte byRes1;
public ushort wDevType;
public byte byRes2;
}
[StructLayout(LayoutKind.Sequential)]
public struct NET_DVR_PREVIEWINFO
{
public int lChannel;
public int dwStreamType;
public int dwLinkMode;
public IntPtr hPlayWnd;
public int bBlocked;
public int bPassbackRecord;
public int bPreviewMode;
public int bRetrieveIPChannel;
public int dwDisplayBufNum;
public IntPtr byRes;
}
public class HikvisionTest
{
[DllImport("HCNetSDK.dll")]
public static extern bool NET_DVR_Init();
[DllImport("HCNetSDK.dll")]
public static extern bool NET_DVR_Cleanup();
[DllImport("HCNetSDK.dll")]
public static extern int NET_DVR_Login_V30(string sDVRIP, int wDVRPort, string sUserName, string sPassword, ref NET_DVR_DEVICEINFO_V30 lpDeviceInfo);
[DllImport("HCNetSDK.dll")]
public static extern bool NET_DVR_Logout(int iUserID);
[DllImport("HCNetSDK.dll")]
public static extern int NET_DVR_GetLastError();
[DllImport("HCNetSDK.dll")]
public static extern int NET_DVR_RealPlay_V40(int iUserID, ref NET_DVR_PREVIEWINFO lpPreviewInfo, RealDataCallBack_V30 fRealDataCallBack_V30, IntPtr pUser);
public delegate void RealDataCallBack_V30(int lRealHandle, ref NET_DVR_PACKET_INFO_EX pstruPacketInfo, IntPtr pUser);
static void Main(string[] args)
{
if (NET_DVR_Init())
{
Console.WriteLine("初始化SDK成功!");
}
else
{
Console.WriteLine("初始化SDK失败!错误码:" + NET_DVR_GetLastError());
return;
}
NET_DVR_DEVICEINFO_V30 deviceInfo = new NET_DVR_DEVICEINFO_V30();
int userID = NET_DVR_Login_V30("设备IP地址", 端口号, "用户名", "密码", ref deviceInfo);
if (userID < 0)
{
Console.WriteLine("登录设备失败!错误码:" + NET_DVR_GetLastError());
NET_DVR_Cleanup();
return;
}
else
{
Console.WriteLine("登录设备成功!");
}
NET_DVR_PREVIEWINFO lpPreviewInfo = new NET_DVR_PREVIEWINFO();
lpPreviewInfo.hPlayWnd = IntPtr.Zero;
lpPreviewInfo.lChannel = 1;
lpPreviewInfo.dwStreamType = 0;
lpPreviewInfo.dwLinkMode = 0;
lpPreviewInfo.bBlocked = true;
lpPreviewInfo.dwDisplayBufNum = 15;
int playHandle = NET_DVR_RealPlay_V40(userID, ref lpPreviewInfo, null, IntPtr.Zero);
if (playHandle < 0)
{
Console.WriteLine("打开实时预览失败!错误码:" + NET_DVR_GetLastError());
}
else
{
Console.WriteLine("打开实时预览成功!");
}
Thread.Sleep(5000);//5秒钟后关闭实时预览
bool stopPreview = NET_DVR_StopRealPlay(playHandle);
if (!stopPreview)
{
Console.WriteLine("关闭实时预览失败!错误码:" + NET_DVR_GetLastError());
}
else
{
Console.WriteLine("关闭实时预览成功!");
}
bool logout = NET_DVR_Logout(userID);
if (!logout)
{
Console.WriteLine("登出设备失败!错误码:" + NET_DVR_GetLastError());
}
else
{
Console.WriteLine("登出设备成功!");
}
NET_DVR_Cleanup();
}
}
}
以上就是连接海康威视的完整攻略,希望能对你有所帮助。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C#中如何连接海康威视 - Python技术站