对接海康明眸门禁设备,初始化SDK
public interface HCNetSDK extends Library {
public static HCNetSDK getInstance(String sdkPath) {
System.out.println("sdk_path " + sdkPath);
HCNetSDK INSTANCE = null;
if (osSelect.isWindows()) {
INSTANCE = (HCNetSDK) Native.loadLibrary(sdkPath, HCNetSDK.class);
}
if (osSelect.isLinux()) {
INSTANCE = (HCNetSDK) Native.loadLibrary(sdkPath, HCNetSDK.class);
}
return INSTANCE;
}
boolean NET_DVR_Init();
boolean NET_DVR_SetSDKInitCfg(int enumType, Pointer lpInBuff);
boolean NET_DVR_SetLogToFile(int bLogEnable, String strLogDir, boolean bAutoDel);
int NET_DVR_GetLastError();
int NET_DVR_Login_V40(NET_DVR_USER_LOGIN_INFO pLoginInfo, NET_DVR_DEVICEINFO_V40 lpDeviceInfo);
boolean NET_DVR_Logout(int lUserID);
int NET_DVR_StartRemoteConfig(int lUserID, int dwCommand, Pointer lpInBuffer, int dwInBufferLen, fRemoteConfigCallback cbStateCallback, Pointer pUserData);
boolean NET_DVR_Cleanup();
int NET_DVR_SendWithRecvRemoteConfig(int lHandle, Pointer lpInBuff, int dwInBuffSize, Pointer lpOutBuff, int dwOutBuffSize, IntByReference dwOutDataLen);
boolean NET_DVR_StopRemoteConfig(int lHandle);
boolean NET_DVR_STDXMLConfig(int lUserID, NET_DVR_XML_CONFIG_INPUT lpInputParam, NET_DVR_XML_CONFIG_OUTPUT lpOutputParam);
int NET_DVR_GetNextRemoteConfig(int lHandle, Pointer lpOutBuff, int dwOutBuffSize);
boolean NET_DVR_GetDeviceConfig(int lUserID, int dwCommand, int dwCount, Pointer lpInBuffer, int dwInBufferSize, Pointer lpStatusList, Pointer lpOutBuffer, int dwOutBufferSize);
boolean NET_DVR_SetDeviceConfig(int lUserID, int dwCommand, int dwCount, Pointer lpInBuffer, int dwInBufferSize, Pointer lpStatusList, Pointer lpInParamBuffer, int dwInParamBufferSize);
boolean NET_DVR_RemoteControl(int lUserID, int dwCommand, Pointer lpInBuffer, int dwInBufferSize);
int NET_DVR_SetupAlarmChan_V41(int lUserID, NET_DVR_SETUPALARM_PARAM lpSetupParam);
boolean NET_DVR_CloseAlarmChan(int lAlarmHandle);
boolean NET_DVR_CloseAlarmChan_V30(int lAlarmHandle);
boolean NET_DVR_StopListen_V30(int lListenHandle);
boolean NET_DVR_SetSDKLocalCfg(int enumType, Pointer lpInBuff);
int NET_DVR_StartListen_V30(String sLocalIP, short wLocalPort, FMSGCallBack_V31 DataCallBack, Pointer pUserData);
boolean NET_DVR_SetDVRMessageCallBack_V31(FMSGCallBack_V31 fMessageCallBack, Pointer pUser);
int NET_DVR_SetupAlarmChan_V50(int iUserID, NET_DVR_SETUPALARM_PARAM_V50 lpSetupParam, Pointer pSub, int dwSubSize);
public interface FMSGCallBack_V31 extends Callback {
public boolean invoke(int lCommand, NET_DVR_ALARMER pAlarmer, Pointer pAlarmInfo, int dwBufLen, Pointer pUser);
}
public interface MSGCallBack_V31 extends Callback {
void invoke(int lCommand, NET_DVR_ALARMER pAlarmer, Pointer pAlarmInfo, int dwBufLen, Pointer pUser);
}
public interface FLoginResultCallBack extends Callback {
public int invoke(int lUserID, int dwResult, NET_DVR_DEVICEINFO_V30 lpDeviceinfo, Pointer pUser);
}
public interface fRemoteConfigCallback extends Callback {
public int invoke(int lUserID, int dwResult, NET_DVR_DEVICEINFO_V30 lpDeviceinfo, Pointer pUser);
}
}
初始化
public class IotHikvisionDeviceApiService implements IotPanelApiService {
private static HCNetSDK hCNetSDK = null;
private static HikvisionAlarmCallBack fMSFCallBack_V31 = null;
private static String HK_SDK_PATH = "HK_SDK_PATH";
static {
try {
String sdkFilePath = null;
if (osSelect.isWindows()) {
sdkFilePath = HK_SDK_PATH + File.separator + "HCNetSDK.dll";
}
if (osSelect.isLinux()) {
sdkFilePath = HK_SDK_PATH + File.separator + "libhcnetsdk.so";
}
if (FileUtil.exist(sdkFilePath)) {
hCNetSDK = HCNetSDK.getInstance(sdkFilePath);
if(ObjectUtil.isNull(hCNetSDK)){
log.warn("海康SDK初始化失败");
}
if (osSelect.isLinux()) {
loadSdk(sdkPath);
}
log.info("start NET_DVR_Init");
boolean initSuc = hCNetSDK.NET_DVR_Init();
if (!initSuc) {
log.error("HikvisionVideoService NET_DVR_Init fail");
}
hCNetSDK.NET_DVR_SetLogToFile(3, FileUtil.getTmpDirPath() + File.separator + "sdklog", false);
} else {
log.warn("海康SDK文件目录不存在");
}
} catch (Exception ex) {
log.error("Error:{} ", ex.getMessage());
}
}
加载文件
public static void loadSdk(String sdkPath) {
BYTE_ARRAY ptrByteArray1 = new BYTE_ARRAY(256);
BYTE_ARRAY ptrByteArray2 = new BYTE_ARRAY(256);
String strPath1 = sdkPath + "libcrypto.so.1.1";
String strPath2 = sdkPath + "libssl.so.1.1";
System.arraycopy(strPath1.getBytes(), 0, ptrByteArray1.byValue, 0, strPath1.length());
ptrByteArray1.write();
hCNetSDK.NET_DVR_SetSDKInitCfg(3, ptrByteArray1.getPointer());
System.arraycopy(strPath2.getBytes(), 0, ptrByteArray2.byValue, 0, strPath2.length());
ptrByteArray2.write();
hCNetSDK.NET_DVR_SetSDKInitCfg(4, ptrByteArray2.getPointer());
String strPathCom = sdkPath;
NET_DVR_LOCAL_SDK_PATH struComPath = new NET_DVR_LOCAL_SDK_PATH();
System.arraycopy(strPathCom.getBytes(), 0, struComPath.sPath, 0, strPathCom.length());
struComPath.write();
hCNetSDK.NET_DVR_SetSDKInitCfg(2, struComPath.getPointer());
}
建立长连接
private int getLongConnt(String strInBuffer, int HCode, int lUserID) {
BYTE_ARRAY ptrByteArray = new BYTE_ARRAY(1024);
System.arraycopy(strInBuffer.getBytes(), 0, ptrByteArray.byValue, 0, strInBuffer.length());
ptrByteArray.write();
return hCNetSDK.NET_DVR_StartRemoteConfig(lUserID, HCode, ptrByteArray.getPointer(), strInBuffer.length(), null, null);
}
登录
public int login_V40(String m_sDeviceIP, short wPort, String m_sUsername, String m_sPassword) {
NET_DVR_USER_LOGIN_INFO pLoginInfo = new NET_DVR_USER_LOGIN_INFO();
pLoginInfo.sDeviceAddress = new byte[IotHikNetConstant.NET_DVR_DEV_ADDRESS_MAX_LEN];
System.arraycopy(m_sDeviceIP.getBytes(), 0, pLoginInfo.sDeviceAddress, 0, m_sDeviceIP.length());
pLoginInfo.wPort = wPort;
pLoginInfo.sUserName = new byte[IotHikNetConstant.NET_DVR_LOGIN_USERNAME_MAX_LEN];
System.arraycopy(m_sUsername.getBytes(), 0, pLoginInfo.sUserName, 0, m_sUsername.length());
pLoginInfo.sPassword = new byte[IotHikNetConstant.NET_DVR_LOGIN_PASSWD_MAX_LEN];
System.arraycopy(m_sPassword.getBytes(), 0, pLoginInfo.sPassword, 0, m_sPassword.length());
pLoginInfo.bUseAsynLogin = false;
pLoginInfo.write();
NET_DVR_DEVICEINFO_V40 lpDeviceInfo = new NET_DVR_DEVICEINFO_V40();
int lUserID = -1;
try {
lUserID = hCNetSDK.NET_DVR_Login_V40(pLoginInfo, lpDeviceInfo);
if (lUserID == -1) {
log.warn("登录失败,错误码为 : {}", hCNetSDK.NET_DVR_GetLastError());
} else {
log.info("登录成功!ip: {}", m_sDeviceIP);
lpDeviceInfo.read();
}
} catch (Exception e) {
log.error("海康SDK 登录异常,请检查网络和SDK路径是否正确", e);
} finally {
pLoginInfo.clear();
lpDeviceInfo.clear();
}
return lUserID;
}
设备注销 SDK释放
public void logout(int lUserID) {
if (lUserID >= 0) {
if (!hCNetSDK.NET_DVR_Logout(lUserID)) {
log.warn("注销失败,错误码为: {}", hCNetSDK.NET_DVR_GetLastError());
}
log.info("注销成功");
return;
} else {
log.warn("设备未登录");
hCNetSDK.NET_DVR_Cleanup();
}
}