文章目录
- 1、下载大华SDK
- 2、引入NetSDKCS工程
- 3、编写调用SDK的逻辑处理类
- 4、程序启动时初始化SDK调用
- 5、定时任务中调用截图方法
- 6、遇到的问题
- 7、DEMO实例下载
1、下载大华SDK
可以到大华官方网站的“下载中心”下载最新的SDK,地址是:https://www.dahuatech.com/service/downloadlists/836.html
2、引入NetSDKCS工程
(1)将SDK中的NetSDKCS项目拷贝到VideoMonitorPlatform解决方案下
(2)在VS中选中解决方案,点击右键,选择“添加”-“现有项目”,选择刚拷贝过来的NetSDKCS项目
(3)在VideoMonitorPlatform项目的“引用”上点击右键,选择“添加引用”
在弹出的画面中在“解决方案”中选择新加入的NetSDKCS
至此,在VideoMonitorPlatform项目中使用“use NetSDKCS”即可引入SDK
(4)将SDK中的“库文件”文件夹下的文件拷贝到项目的“bin”目录下,在运行程序的时候调用
3、编写调用SDK的逻辑处理类
SDK中提供哪些接口可以参考说明文档,具体代码编写可参考官方DEMO,这里只介绍登录和截图,具体的逻辑类如下:
using System;
using NetSDKCS;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
namespace VideoMonitorPlatform.Services
{
public class NvrService
{
static log4net.ILog logger = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
#region Field 字段
public static IntPtr m_LoginID;
private static fDisConnectCallBack m_DisConnectCallBack;
private static fHaveReConnectCallBack m_ReConnectCallBack;
private static fRealDataCallBackEx2 m_RealDataCallBackEx2;
private static fSnapRevCallBack m_SnapRevCallBack;
private bool m_IsInSave = false;
#endregion
public static void Init()
{
m_DisConnectCallBack = new fDisConnectCallBack(DisConnectCallBack);
m_ReConnectCallBack = new fHaveReConnectCallBack(ReConnectCallBack);
m_RealDataCallBackEx2 = new fRealDataCallBackEx2(RealDataCallBackEx);
m_SnapRevCallBack = new fSnapRevCallBack(SnapRevCallBack);
try
{
NETClient.Init(m_DisConnectCallBack, IntPtr.Zero, null);
NETClient.SetAutoReconnect(m_ReConnectCallBack, IntPtr.Zero);
NETClient.SetSnapRevCallBack(m_SnapRevCallBack, IntPtr.Zero);
logger.Info("初始化摄像头连接成功");
}
catch (Exception ex)
{
logger.Error("初始化摄像头连接失败", ex);
}
var customerConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~")
.GetSection("CustomerConfig") as VideoMonitorPlatform.Models.Config.CustomerConfig;
string nvrIp = customerConfig.NvrIp; // NVR的IP
string nvrPort = customerConfig.NvrPort; // NVR的端口号
string nvrUserName = customerConfig.NvrUserName; // NVR的登录用户名
string nvrPassword = customerConfig.NvrPassword; // NVR的登录密码
ushort port = 0;
try
{
port = Convert.ToUInt16(nvrPort);
}
catch (Exception ex)
{
logger.Error("输入端口错误", ex);
}
NET_DEVICEINFO_Ex m_DeviceInfo = new NET_DEVICEINFO_Ex();
NvrService.m_LoginID = NETClient.Login(nvrIp, port, nvrUserName, nvrPassword, EM_LOGIN_SPAC_CAP_TYPE.TCP, IntPtr.Zero, ref m_DeviceInfo);
}
public static IntPtr RealPlayCapture(uint channel, uint quality, uint imageSize)
{
NET_SNAP_PARAMS asyncSnap = new NET_SNAP_PARAMS();
asyncSnap.Channel = channel;
asyncSnap.Quality = quality;
asyncSnap.ImageSize = 2;
asyncSnap.mode = 0;
asyncSnap.InterSnap = 0;
asyncSnap.CmdSerial = channel;
bool ret = NETClient.SnapPictureEx(NvrService.m_LoginID, asyncSnap, IntPtr.Zero);
if (!ret)
{
return new IntPtr(1);
}
return IntPtr.Zero;
}
protected void OnClosed()
{
NETClient.Cleanup();
}
#region CallBack 回调
private static void DisConnectCallBack(IntPtr lLoginID, IntPtr pchDVRIP, int nDVRPort, IntPtr dwUser)
{
}
private static void ReConnectCallBack(IntPtr lLoginID, IntPtr pchDVRIP, int nDVRPort, IntPtr dwUser)
{
}
private static void RealDataCallBackEx(IntPtr lRealHandle, uint dwDataType, IntPtr pBuffer, uint dwBufSize, IntPtr param, IntPtr dwUser)
{
}
private static void SnapRevCallBack(IntPtr lLoginID, IntPtr pBuf, uint RevLen, uint EncodeType, uint CmdSerial, IntPtr dwUser)
{
var customerConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~")
.GetSection("CustomerConfig") as VideoMonitorPlatform.Models.Config.CustomerConfig;
string captureSavePath = customerConfig.CaptureSavePath;
string path = captureSavePath + "\\" + CmdSerial.ToString();
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
if (EncodeType == 10) //.jpg
{
DateTime now = DateTime.Now;
string fileName = now.ToString("yyyyMMddHHmmss") + ".jpg";
string filePath = path + "\\" + fileName;
byte[] data = new byte[RevLen];
Marshal.Copy(pBuf, data, 0, (int)RevLen);
using (FileStream stream = new FileStream(filePath, FileMode.OpenOrCreate))
{
stream.Write(data, 0, (int)RevLen);
stream.Flush();
stream.Dispose();
}
}
}
#endregion
}
}
4、程序启动时初始化SDK调用
在“Global.asax”的“Application_Start()”方法中,初始化SDK调用,代码如下:
// 初始化NVR的SDK
NvrService.Init();
5、定时任务中调用截图方法
在定时任务处理类“CaptureJob”中调用SDK的方法进行截图,具体如下:
using System;
using Quartz;
using System.Threading.Tasks;
using System.Reflection;
using VideoMonitorPlatform.Services;
namespace VideoMonitorPlatform.Utils
{
public class CaptureJob : IJob
{
#region IJob 成员
private static log4net.ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public async Task Execute(IJobExecutionContext context)
{
//log.Info("任务运行");
DateTime now = DateTime.Now;
System.Diagnostics.Debug.WriteLine("*********************JOB【" + context.JobDetail.Key + "】执行开始*********************");
System.Diagnostics.Debug.WriteLine("执行时间:" + now.ToString("yyyy-MM-dd HH:mm:ss"));
object channelObject = context.JobDetail.JobDataMap.Get("channel");
object quailityObject = context.JobDetail.JobDataMap.Get("quaility");
object imageSizeObject = context.JobDetail.JobDataMap.Get("imageSize");
uint channel = uint.Parse(channelObject.ToString());
uint quaility = uint.Parse(quailityObject.ToString());
uint imageSize = uint.Parse(imageSizeObject.ToString());
NvrService.RealPlayCapture(channel, quaility, imageSize);
System.Diagnostics.Debug.WriteLine("*********************JOB【" + context.JobDetail.Key + "】执行结束*********************");
}
#endregion
}
}
6、遇到的问题
我在编码时只遇到一个问题,困扰了我很久,我连的设备是硬盘录像机,只在第一个channel连接了一个摄像头,这个channel起始是0,在调用截图的API时,传递了个“1”过去,结果执行和返回结果都成功,但是回调函数就是不调用,怎么调试都不好使,在此感谢大华SDK支持dh_sdk@dahuatech.com帮我解决了问题。
7、DEMO实例下载