赞
踩
/** * 根据不同操作系统选择不同的库文件和库路径 * * @param windowsPath 库文件加载地址Windows环境 * @param linuxPath 库文件加载地址linux环境 * @return */ private static boolean createSdkInstance(String windowsPath, String linuxPath) { if (hCNetSDK == null) { synchronized (HCNetSDK.class) { String strDllPath = ""; try { if (OsSelect.isWindows()) { //win系统加载库路径System.getProperty("user.dir") strDllPath = windowsPath + "\\HCNetSDK.dll"; } else if (OsSelect.isLinux()) { //Linux系统加载库路径 strDllPath = linuxPath + "/libhcnetsdk.so"; } hCNetSDK = (HCNetSDK) Native.loadLibrary(strDllPath, HCNetSDK.class); } catch (Exception ex) { System.out.println("loadLibrary: " + strDllPath + " Error: " + ex.getMessage()); ex.printStackTrace(); return false; } } } return true; }
/** * 初始化NET_DVR_Init接口在程序开始时调用,一个程序只需要调用一次。 * * @param windowsPath 库文件加载地址Windows环境 * @param linuxPath 库文件加载地址Linu环境 * @return */ public static BackResult initInformation(String windowsPath, String linuxPath) { BackResult backResult = new BackResult(); if (hCNetSDK == null) { if (!createSdkInstance(windowsPath, linuxPath)) { backResult.setStatusCode(11); backResult.setStatusCodeMsg("Load SDK fail"); return backResult; } } //linux系统建议调用以下接口加载组件库 if (OsSelect.isLinux()) { HCNetSDK.BYTE_ARRAY ptrByteArray1 = new HCNetSDK.BYTE_ARRAY(256); HCNetSDK.BYTE_ARRAY ptrByteArray2 = new HCNetSDK.BYTE_ARRAY(256); //这里是库的绝对路径,请根据实际情况修改,注意改路径必须有访问权限 String strPath1 = linuxPath + "/libcrypto.so.1.1"; String strPath2 = linuxPath + "/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 = linuxPath; HCNetSDK.NET_DVR_LOCAL_SDK_PATH struComPath = new HCNetSDK.NET_DVR_LOCAL_SDK_PATH(); System.arraycopy(strPathCom.getBytes(), 0, struComPath.sPath, 0, strPathCom.length()); struComPath.write(); hCNetSDK.NET_DVR_SetSDKInitCfg(2, struComPath.getPointer()); } //进行初始化 boolean b = hCNetSDK.NET_DVR_Init(); if (b) { backResult.setStatusCode(0); backResult.setStatusCodeMsg("初始化成功"); } else { backResult.setStatusCode(hCNetSDK.NET_DVR_GetLastError()); backResult.setStatusCodeMsg("初始化失败" + CommonUtil.getLastErrorMsg(hCNetSDK.NET_DVR_GetLastError())); } return backResult; }
/** * 设备登录-登录时要注意设备是否已经登录 * * @param ipadress IP地址 * @param user 用户名 * @param psw 密码 * @param port 端口,默认8000 * @return 登录成功返回用户登录语句柄, 否则返回操作错误码 */ public static BackResult login(String ipadress, String user, String psw, short port) { //注册 //设备登录信息 HCNetSDK.NET_DVR_USER_LOGIN_INFO strLoginInfo = new HCNetSDK.NET_DVR_USER_LOGIN_INFO(); //设备ip地址 String sDeviceIp = ipadress; strLoginInfo.sDeviceAddress = new byte[HCNetSDK.NET_DVR_DEV_ADDRESS_MAX_LEN]; System.arraycopy(sDeviceIp.getBytes(), 0, strLoginInfo.sDeviceAddress, 0, sDeviceIp.length()); //设备用户名 String sUsername = user; strLoginInfo.sUserName = new byte[HCNetSDK.NET_DVR_LOGIN_USERNAME_MAX_LEN]; System.arraycopy(sUsername.getBytes(), 0, strLoginInfo.sUserName, 0, sUsername.length()); //设备密码 String sPassword = psw; strLoginInfo.sPassword = new byte[HCNetSDK.NET_DVR_LOGIN_PASSWD_MAX_LEN]; System.arraycopy(sPassword.getBytes(), 0, strLoginInfo.sPassword, 0, sPassword.length()); //sdk端口 strLoginInfo.wPort = port; //是否异步登录:0- 否,1- 是 strLoginInfo.bUseAsynLogin = false; strLoginInfo.write(); //设备信息 HCNetSDK.NET_DVR_DEVICEINFO_V40 strDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V40(); //lUserID登录该设备返回的语句柄--每台设备不一样 BackResult backResult = new BackResult(); int userId = hCNetSDK.NET_DVR_Login_V40(strLoginInfo, strDeviceInfo); if ((userId == -1) || (userId == 0xFFFFFFFF)) { backResult.setlUserId(userId); backResult.setStatusCode(hCNetSDK.NET_DVR_GetLastError()); backResult.setStatusCodeMsg("登录失败:" + CommonUtil.getLastErrorMsg(hCNetSDK.NET_DVR_GetLastError())); return backResult; } else { strDeviceInfo.read(); //编码类型 iCharEncodeType = strDeviceInfo.byCharEncodeType; backResult.setStatusCode(0); backResult.setlUserId(userId); //初始的布防状态为-1,进行告警布防后获取布防状态 backResult.setlAlarmHandle(-1); backResult.setStatusCodeMsg("登录成功"); return backResult; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。