海康威视SDK实战
一、官网获取海康威视SDK
二、打开IDEA,新建项目导入SDK
在project Structure-> modules添加Jna.jar和examples.jar
在project Structure-> sdks添加Jna.jar和examples.jar
三、修改HCNetSDK.java
修改HCNetSDK.dll加载路径
HCNetSDK INSTANCE = (HCNetSDK) Native.loadLibrary(System.getProperty("user.dir") +"/HCNetSDK.dll",
HCNetSDK.class);
修改 PlayCtrl.dll加载路径
PlayCtrl INSTANCE = (PlayCtrl) Native.loadLibrary(System.getProperty("user.dir") +"/PlayCtrl.dll",
PlayCtrl.class);
四、运行程序
输入IP,用户名,密码,端口,点击注册
预览成功
回放功能,通道号为i+33
五、SDK程序函数调用顺序
六、SDK实战-手动录像功能
创建定时任务
task
@Scheduled(fixedRate = 60000)//每小时执行一次
@Async
public void startVideoTask() {
try {
HKUtil.startVideo(v.username,v.password,v.ip);
}catch (Exception e){
e.printStackTrace();
}
}
HKUtil.java
public static void startVideo(String username,String password,String ip) {
//匿名内部类实现方式
new Thread() {
//重写run方法
@Override
public void run() {
boolean initSuc = hCNetSDK.NET_DVR_Init();
HKUtil a = new HKUtil();
a.init(username,password,ip);
}
}.start();
}
/*************************************************
函数: "注册" 按钮单击响应函数
函数描述: 注册登录设备
*************************************************/
public void init(String username,String password,String ip) {//GEN-FIRST:event_jButtonLoginActionPerformed
//注册之前先注销已注册的用户,预览情况下不可注销
if (bRealPlay) {
System.out.println("注册新用户请先停止当前预览!");
return;
}
if (lUserID.longValue() > -1) {
//先注销
hCNetSDK.NET_DVR_Logout_V30(lUserID);
lUserID = new NativeLong(-1);
m_iTreeNodeNum = 0;
m_DeviceRoot.removeAllChildren();
}
//注册
m_sDeviceIP = ip;//设备ip地址
m_strDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V30();
int iPort = 8000;
lUserID = hCNetSDK.NET_DVR_Login_V30(m_sDeviceIP,
(short) iPort, username, password, m_strDeviceInfo);
long userID = lUserID.longValue();
if (userID == -1) {
m_sDeviceIP = "";//登录未成功,IP置为空
System.out.println("注册失败");
} else {
try {
hCNetSDK.NET_DVR_StartDVRRecord(lUserID, new NativeLong(0xffff), new NativeLong());
Thread.sleep(30000);
hCNetSDK.NET_DVR_StopDVRRecord(lUserID, new NativeLong(0xffff));
jButtonExitActionPerformed();
} catch (Exception e) {
e.printStackTrace();
}
}
}
流程大致如下:
注销登录-》构建登录参数-》登录注册-》开始录制-》睡眠-》停止录制
对应方法:
hCNetSDK.NET_DVR_Logout_V30(lUserID);//注销登录
m_strDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V30();//构建登录参数
hCNetSDK.NET_DVR_Login_V30(m_sDeviceIP,(short) iPort, username, password, m_strDeviceInfo);//登录注册
hCNetSDK.NET_DVR_StartDVRRecord(lUserID, new NativeLong(0xffff), new NativeLong());//开始录制
Thread.sleep(30000);//睡眠
hCNetSDK.NET_DVR_StopDVRRecord(lUserID, new NativeLong(0xffff));//停止录制
结果如图