海康SDK停车侦测报警 java代码
时间: 2023-07-03 15:10:59 浏览: 374
以下是使用海康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_V41(NativeLong lUserID, NET_DVR_ALARMER pAlarmer, NET_DVR_SETUPALARM_PARAM lpSetupParam);
public static native boolean NET_DVR_CloseAlarmChan_V30(NativeLong lAlarmHandle);
// 获取错误码和错误信息
public static native int NET_DVR_GetLastError();
public static native Pointer NET_DVR_GetErrorMsg(NativeLongByReference pErrorNo);
}
// 报警回调函数
public class AlarmCallBack implements HCNetSDK.FMSGCallBack_V41 {
public void invoke(NativeLong lCommand, HCNetSDK.NET_DVR_ALARMER pAlarmer, HCNetSDK.NET_DVR_ALARMINFO_V41 pAlarmInfo, Pointer pUser) {
switch (lCommand.intValue()) {
case HCNetSDK.COMM_ALARM_PDC: // 人数统计报警信息
System.out.println("Receive alarm message, type: COMM_ALARM_PDC");
int i;
for (i = 0; i < pAlarmInfo.struDevInfo.byFaceAlarmPicNum; i++) {
HCNetSDK.BYTE_ARRAY ptrPicByte = pAlarmInfo.pPicPackData.getPointer().getByteArray(i * pAlarmInfo.dwPicLen, pAlarmInfo.dwPicLen);
System.out.println("Receive alarm picture, size: " + ptrPicByte.size());
}
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.NET_DVR_SETUPALARM_PARAM alarmParam = new HCNetSDK.NET_DVR_SETUPALARM_PARAM();
alarmParam.dwSize = alarmParam.size();
alarmParam.byFaceAlarmDetection = 1; // 人脸侦测
alarmParam.byAlarmInfoType = 1; // 报警信息类型
alarmParam.bySupport = 1; // 支持的功能
alarmParam.write();
// 设置报警回调函数
HCNetSDK.FMSGCallBack_V41 alarmCallback = new AlarmCallBack();
Pointer alarmCallbackPointer = alarmCallback.getPointer();
NativeLong alarmHandle = HCNetSDK.NET_DVR_SetupAlarmChan_V41(userId, null, alarmParam);
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_V41`函数设置报警回调函数和报警参数,并等待报警信息。当接收到报警信息时,报警回调函数会被调用,并进行相应的处理。最后,我们使用`NET_DVR_CloseAlarmChan_V30`函数关闭报警通道,并使用`NET_DVR_Logout`函数注销设备,最后使用`NET_DVR_Cleanup`函数反初始化SDK。
阅读全文