海康SDK报警订阅java代码
时间: 2023-07-03 10:10:58 浏览: 245
海康SDK警报回调demo
4星 · 用户满意度95%
以下是使用海康SDK进行报警订阅的Java代码示例,仅供参考:
```java
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.ByteByReference;
import com.sun.jna.ptr.NativeLongByReference;
import com.sun.jna.ptr.PointerByReference;
import com.sun.jna.win32.StdCallLibrary;
public interface HCNetSDK extends StdCallLibrary {
// SDK初始化和反初始化
public static native boolean NET_DVR_Init();
public static native boolean NET_DVR_Cleanup();
// 用户注册和注销
public static native NativeLong NET_DVR_Login_V30(String sDVRIP, short wDVRPort, String sUserName, String sPassword, NET_DVR_DEVICEINFO_V30 lpDeviceInfo);
public static native boolean NET_DVR_Logout(NativeLong lUserID);
// 报警布防和撤防
public static native NativeLong NET_DVR_SetupAlarmChan_V30(NativeLong lUserID, NET_DVR_ALARMER pAlarmer, Pointer fAlarmDataCallBack, Pointer pUser);
public static native boolean NET_DVR_CloseAlarmChan_V30(NativeLong lAlarmHandle);
}
// 报警回调函数
public class AlarmCallBack implements HCNetSDK.FMSGCallBack_V31 {
public void invoke(NativeLong lCommand, HCNetSDK.NET_DVR_ALARMER pAlarmer, HCNetSDK.RECV_ALARM pAlarmInfo, int dwBufLen, Pointer pUser) {
switch (lCommand.intValue()) {
case HCNetSDK.COMM_ALARM_V30: // 报警信息
System.out.println("Receive alarm message, type: COMM_ALARM_V30");
break;
case HCNetSDK.COMM_ALARM_RULE: // 行为分析报警信息
System.out.println("Receive alarm message, type: COMM_ALARM_RULE");
break;
case HCNetSDK.COMM_UPLOAD_PLATE_RESULT: // 车牌识别结果上传
System.out.println("Receive alarm message, type: COMM_UPLOAD_PLATE_RESULT");
break;
default:
System.out.println("Receive alarm message, type: other");
break;
}
}
}
public class Main {
public static void main(String[] args) {
// 初始化SDK
boolean initResult = HCNetSDK.NET_DVR_Init();
if (!initResult) {
System.out.println("Failed to initialize SDK");
return;
}
// 注册设备
String deviceIP = "192.168.1.100"; // 设备IP地址
short devicePort = 8000; // 设备端口号
String username = "admin"; // 设备用户名
String password = "12345"; // 设备密码
HCNetSDK.NET_DVR_DEVICEINFO_V30 deviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V30();
NativeLong userId = HCNetSDK.NET_DVR_Login_V30(deviceIP, devicePort, username, password, deviceInfo);
if (userId.intValue() < 0) {
System.out.println("Failed to login device, error code: " + HCNetSDK.NET_DVR_GetLastError());
HCNetSDK.NET_DVR_Cleanup(); // 反初始化SDK
return;
}
// 设置报警回调函数
HCNetSDK.FMSGCallBack_V31 alarmCallback = new AlarmCallBack();
Pointer alarmCallbackPointer = alarmCallback.getPointer();
NativeLong alarmHandle = HCNetSDK.NET_DVR_SetupAlarmChan_V30(userId, null, alarmCallbackPointer, null);
if (alarmHandle.intValue() < 0) {
System.out.println("Failed to setup alarm channel, error code: " + HCNetSDK.NET_DVR_GetLastError());
HCNetSDK.NET_DVR_Logout(userId); // 注销设备
HCNetSDK.NET_DVR_Cleanup(); // 反初始化SDK
return;
}
// 等待报警信息
System.out.println("Waiting for alarm message...");
Scanner scanner = new Scanner(System.in);
scanner.nextLine();
// 关闭报警通道
boolean closeResult = HCNetSDK.NET_DVR_CloseAlarmChan_V30(alarmHandle);
if (!closeResult) {
System.out.println("Failed to close alarm channel, error code: " + HCNetSDK.NET_DVR_GetLastError());
}
// 注销设备
boolean logoutResult = HCNetSDK.NET_DVR_Logout(userId);
if (!logoutResult) {
System.out.println("Failed to logout device, error code: " + HCNetSDK.NET_DVR_GetLastError());
}
// 反初始化SDK
boolean cleanupResult = HCNetSDK.NET_DVR_Cleanup();
if (!cleanupResult) {
System.out.println("Failed to cleanup SDK");
}
}
}
```
在代码中,我们首先使用`NET_DVR_Init`函数初始化海康SDK,然后使用`NET_DVR_Login_V30`函数注册设备,并获取设备的用户ID。接着,我们使用`NET_DVR_SetupAlarmChan_V30`函数设置报警回调函数,并等待报警信息。当接收到报警信息时,报警回调函数会被调用,并进行相应的处理。最后,我们使用`NET_DVR_CloseAlarmChan_V30`函数关闭报警通道,并使用`NET_DVR_Logout`函数注销设备,最后使用`NET_DVR_Cleanup`函数反初始化SDK。
阅读全文