海康威视 sdk 报警订阅 java代码编写
时间: 2023-07-03 08:29:27 浏览: 174
海康威视提供了基于Java的SDK,可以通过该SDK实现报警订阅。以下是基本的Java代码示例:
```java
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.NativeLongByReference;
import com.sun.jna.ptr.PointerByReference;
import com.sun.jna.win32.StdCallLibrary;
// 定义回调函数的接口
public interface FMSGCallBack_V31 extends StdCallLibrary.StdCallCallback {
public void invoke(NativeLong lCommand, NET_DVR_ALARMER pAlarmer, Pointer pAlarmInfo, NativeLong dwBufLen, Pointer pUser);
}
public class AlarmSubscribe {
// 加载SDK库文件
static {
System.loadLibrary("HCNetSDK");
}
// 登录设备
public static NativeLong login(String ip, short port, String username, String password) {
HCNetSDK.NET_DVR_USER_LOGIN_INFO loginInfo = new HCNetSDK.NET_DVR_USER_LOGIN_INFO();
loginInfo.sDeviceAddress = new byte[HCNetSDK.NET_DVR_DEV_ADDRESS_MAX_LEN];
System.arraycopy(ip.getBytes(), 0, loginInfo.sDeviceAddress, 0, ip.length());
loginInfo.wPort = port;
loginInfo.sUserName = new byte[HCNetSDK.NET_DVR_LOGIN_USERNAME_MAX_LEN];
System.arraycopy(username.getBytes(), 0, loginInfo.sUserName, 0, username.length());
loginInfo.sPassword = new byte[HCNetSDK.NET_DVR_LOGIN_PASSWD_MAX_LEN];
System.arraycopy(password.getBytes(), 0, loginInfo.sPassword, 0, password.length());
HCNetSDK.NET_DVR_DEVICEINFO_V40 deviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V40();
return HCNetSDK.INSTANCE.NET_DVR_Login_V40(loginInfo, deviceInfo);
}
// 注册回调函数
public static NativeLong registerAlarmCallback(NativeLong lUserID) {
FMSGCallBack_V31 callBack = new FMSGCallBack_V31() {
@Override
public void invoke(NativeLong lCommand, NET_DVR_ALARMER pAlarmer, Pointer pAlarmInfo, NativeLong dwBufLen, Pointer pUser) {
System.out.println("Alarm Info Received");
}
};
PointerByReference pUser = new PointerByReference();
return HCNetSDK.INSTANCE.NET_DVR_SetDVRMessageCallBack_V31(callBack, pUser);
}
// 设置报警布防
public static boolean setupAlarmChan(NativeLong lUserID) {
HCNetSDK.NET_DVR_SETUPALARM_PARAM alarmParam = new HCNetSDK.NET_DVR_SETUPALARM_PARAM();
alarmParam.dwSize = alarmParam.size();
return HCNetSDK.INSTANCE.NET_DVR_SetupAlarmChan_V41(lUserID, alarmParam) != null;
}
// 关闭报警布防
public static boolean closeAlarmChan(NativeLong lAlarmHandle) {
return HCNetSDK.INSTANCE.NET_DVR_CloseAlarmChan_V30(lAlarmHandle);
}
// 注销登录
public static boolean logout(NativeLong lUserID) {
return HCNetSDK.INSTANCE.NET_DVR_Logout(lUserID);
}
public static void main(String[] args) {
// 登录设备
NativeLong lUserID = login("192.168.0.1", (short) 8000, "admin", "12345");
if (lUserID.intValue() < 0) {
System.out.println("Login failed");
return;
}
// 注册回调函数
NativeLong lAlarmHandle = registerAlarmCallback(lUserID);
if (lAlarmHandle.intValue() < 0) {
System.out.println("Register callback failed");
return;
}
// 设置报警布防
if (!setupAlarmChan(lUserID)) {
System.out.println("Setup alarm failed");
closeAlarmChan(lAlarmHandle);
return;
}
// 等待报警信息
System.out.println("Waiting for alarm info...");
try {
Thread.sleep(30000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 关闭报警布防
if (!closeAlarmChan(lAlarmHandle)) {
System.out.println("Close alarm failed");
}
// 注销登录
if (!logout(lUserID)) {
System.out.println("Logout failed");
}
}
}
```
该示例代码中定义了登录设备、注册回调函数、设置报警布防、关闭报警布防、注销登录等操作的具体实现。需要注意的是,回调函数需要在`FMSGCallBack_V31`接口的`invoke`方法中实现具体的回调逻辑。在本示例中,回调函数只简单地输出一行日志信息。实际应用中,需要根据具体的业务需求实现回调函数的逻辑。
阅读全文