提交 | 用户 | 时间
|
149dd0
|
1 |
package com.iailab.module.data.video.hikvision.utils; |
H |
2 |
|
|
3 |
import com.sun.jna.Native; |
|
4 |
import com.sun.jna.NativeLong; |
|
5 |
import com.sun.jna.Platform; |
|
6 |
import lombok.extern.slf4j.Slf4j; |
|
7 |
import org.springframework.stereotype.Component; |
|
8 |
|
|
9 |
import javax.annotation.PreDestroy; |
|
10 |
import java.io.File; |
|
11 |
import java.util.ArrayList; |
|
12 |
import java.util.List; |
|
13 |
|
|
14 |
|
|
15 |
@Slf4j |
|
16 |
@Component |
|
17 |
public class HIKDevice { |
|
18 |
|
|
19 |
// 接口的实例,通过接口实例调用外部dll/so的函数 |
|
20 |
public static HCNetSDK hCNetSDK = null; |
|
21 |
|
|
22 |
// 用户登录返回句柄 |
|
23 |
public static int lUserID; |
|
24 |
//构造函数 |
|
25 |
public HIKDevice(){ |
|
26 |
if (hCNetSDK == null) { |
|
27 |
synchronized (HCNetSDK.class) { |
|
28 |
try { |
|
29 |
if (Platform.isWindows()) { |
|
30 |
hCNetSDK = (HCNetSDK) Native.loadLibrary(MessageCode.HIK_WIN_PATH, HCNetSDK.class); |
|
31 |
} |
|
32 |
if (Platform.isLinux()) { |
|
33 |
hCNetSDK = (HCNetSDK) Native.loadLibrary(MessageCode.HIK_LINUX_PATH, HCNetSDK.class); |
|
34 |
} |
|
35 |
} catch (Exception ex) { |
|
36 |
log.error("SdkInitService-init-hCNetSDK-error"); |
|
37 |
} |
|
38 |
} |
|
39 |
} |
|
40 |
} |
|
41 |
/** |
|
42 |
* 初始化资源配置 |
|
43 |
*/ |
|
44 |
public int initDevices(){ |
|
45 |
System.out.println("进入构造函数"); |
|
46 |
// 初始化SDK |
|
47 |
boolean initSuc = hCNetSDK.NET_DVR_Init(); |
|
48 |
if (initSuc != true) { |
|
49 |
System.out.println("初始化失败"); |
|
50 |
return 1; |
|
51 |
} |
|
52 |
// 设置连接时间与重连时间 |
|
53 |
hCNetSDK.NET_DVR_SetConnectTime(2000, 1); |
|
54 |
System.out.println("初始化SDK资源完成"); |
|
55 |
return 0; |
|
56 |
} |
|
57 |
|
|
58 |
//销毁函数 |
|
59 |
@PreDestroy |
|
60 |
public void destroy() throws Exception { |
|
61 |
hCNetSDK.NET_DVR_Cleanup(); |
|
62 |
} |
|
63 |
|
|
64 |
/* |
|
65 |
* 功能:登录设备 |
|
66 |
* 参数: |
|
67 |
* deviceIP-设备局域网IP地址 |
|
68 |
* devicePort-设备SDK服务端口 |
|
69 |
* userName-用户名 |
|
70 |
* passWord-密码 |
|
71 |
* */ |
|
72 |
// public String loginDevice() throws JSONException { |
|
73 |
// if (!getDeviceStatus()) { |
|
74 |
// int init = initDevices(); |
|
75 |
// if (init == 1) { |
|
76 |
// return "初始化失败"; |
|
77 |
// } |
|
78 |
// ArrayList<String> list = mZnfxCodeService.getDriverInfo("driverinfo"); |
|
79 |
// if (list != null && list.size() > 0) { |
|
80 |
// String driverip = list.get(0); |
|
81 |
// String driverport = list.get(1); |
|
82 |
// String username = list.get(2); |
|
83 |
// String password = list.get(3); |
|
84 |
// String returnVal = Login_V40(driverip, Short.parseShort(driverport), username, password); |
|
85 |
// System.out.println(returnVal); |
|
86 |
// return returnVal; |
|
87 |
// } else { |
|
88 |
// return "获取设备信息失败!"; |
|
89 |
// } |
|
90 |
// } |
|
91 |
// else |
|
92 |
// { |
|
93 |
// String result = captureJPEGPicture(); |
|
94 |
// System.out.println(result); |
|
95 |
// return result; |
|
96 |
// } |
|
97 |
// |
|
98 |
// } |
|
99 |
|
|
100 |
/** |
|
101 |
* |
|
102 |
* @param m_sDeviceIP 设备ip地址 |
|
103 |
* @param wPort 端口号,设备网络SDK登录默认端口8000 |
|
104 |
* @param m_sUsername 用户名 |
|
105 |
* @param m_sPassword 密码 |
|
106 |
*/ |
|
107 |
public String Login_V40(String m_sDeviceIP,short wPort,String m_sUsername,String m_sPassword) { |
|
108 |
/* 注册 */ |
|
109 |
// 设备登录信息 |
|
110 |
HCNetSDK.NET_DVR_USER_LOGIN_INFO m_strLoginInfo = new HCNetSDK.NET_DVR_USER_LOGIN_INFO(); |
|
111 |
|
|
112 |
// 设备信息 |
|
113 |
HCNetSDK.NET_DVR_DEVICEINFO_V40 m_strDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V40(); |
|
114 |
m_strLoginInfo.sDeviceAddress = new byte[HCNetSDK.NET_DVR_DEV_ADDRESS_MAX_LEN]; |
|
115 |
System.arraycopy(m_sDeviceIP.getBytes(), 0, m_strLoginInfo.sDeviceAddress, 0, m_sDeviceIP.length()); |
|
116 |
m_strLoginInfo.wPort =wPort ; |
|
117 |
m_strLoginInfo.sUserName = new byte[HCNetSDK.NET_DVR_LOGIN_USERNAME_MAX_LEN]; |
|
118 |
System.arraycopy(m_sUsername.getBytes(), 0, m_strLoginInfo.sUserName, 0, m_sUsername.length()); |
|
119 |
m_strLoginInfo.sPassword = new byte[HCNetSDK.NET_DVR_LOGIN_PASSWD_MAX_LEN]; |
|
120 |
System.arraycopy(m_sPassword.getBytes(), 0, m_strLoginInfo.sPassword, 0, m_sPassword.length()); |
|
121 |
// 是否异步登录:false- 否,true- 是 |
|
122 |
m_strLoginInfo.bUseAsynLogin = false; |
|
123 |
// write()调用后数据才写入到内存中 |
|
124 |
m_strLoginInfo.write(); |
|
125 |
|
|
126 |
lUserID = hCNetSDK.NET_DVR_Login_V40(m_strLoginInfo, m_strDeviceInfo); |
|
127 |
if (lUserID == -1) { |
|
128 |
// updateZnfxPostalStatus(PostalStatus.ABNORMAL.value()); |
|
129 |
// mZnfxPostalStatusService.changeStatus(MessageCode.POSTAL_STATUS_TYPE_CAMERA, PostalStatus.ABNORMAL.value()); |
|
130 |
|
|
131 |
System.out.println("登录失败,错误码为" + hCNetSDK.NET_DVR_GetLastError()); |
|
132 |
return "登录失败,错误码为" + hCNetSDK.NET_DVR_GetLastError(); |
|
133 |
} else { |
|
134 |
System.out.println("登录成功!"); |
|
135 |
// read()后,结构体中才有对应的数据 |
|
136 |
m_strDeviceInfo.read(); |
|
137 |
// updateZnfxPostalStatus(PostalStatus.NORMAL.value()); |
|
138 |
// mZnfxPostalStatusService.changeStatus(MessageCode.POSTAL_STATUS_TYPE_CAMERA, PostalStatus.NORMAL.value()); |
|
139 |
|
|
140 |
String result = captureJPEGPicture(); |
|
141 |
|
|
142 |
return result; |
|
143 |
} |
|
144 |
} |
|
145 |
|
|
146 |
//注销设备 |
|
147 |
public String logoutDevice() |
|
148 |
{ |
|
149 |
NativeLong nlUserID = new NativeLong(lUserID); |
|
150 |
if (!hCNetSDK.NET_DVR_Logout(nlUserID)) |
|
151 |
{ |
|
152 |
System.out.println("注销失败,设备ID:"+ nlUserID.intValue() + "错误码:"+hCNetSDK.NET_DVR_GetLastError()); |
|
153 |
return "{\"result\":\"loginDevice Fail\",\"userID\":"+nlUserID.intValue()+",\"errorCode\":"+hCNetSDK.NET_DVR_GetLastError()+"}"; |
|
154 |
}else{ |
|
155 |
System.out.println("注销成功,设备ID:"+ nlUserID.intValue()); |
|
156 |
return "{\"result\":\"logoutDevice Success\",\"userID\":"+nlUserID.intValue()+",\"errorCode\":0}"; |
|
157 |
} |
|
158 |
} |
|
159 |
/** |
|
160 |
* 设备抓图(无预览) |
|
161 |
* 单帧设备抓取保存的图片为JPG,具体注意问题需要详细查看【设备网络sdk使用手册】 |
|
162 |
* @return |
|
163 |
*/ |
|
164 |
public String captureJPEGPicture() |
|
165 |
{ |
|
166 |
// ArrayList<String> list = mZnfxCodeService.getDriverInfo("driverpic"); |
|
167 |
List<String> list = new ArrayList<>(); |
|
168 |
list.add("tlChannel"); |
|
169 |
list.add("tlPicSize"); |
|
170 |
list.add("tlPicQuality"); |
|
171 |
if (list != null && list.size() > 0) { |
|
172 |
int tlChannel = Integer.parseInt(list.get(0)); |
|
173 |
int tlPicSize = Integer.parseInt(list.get(1)); |
|
174 |
int tlPicQuality = Integer.parseInt(list.get(2)); |
|
175 |
|
|
176 |
NativeLong nlUserID = new NativeLong(lUserID); |
|
177 |
NativeLong nChannel = new NativeLong(tlChannel); |
|
178 |
HCNetSDK.NET_DVR_JPEGPARA lpJpegPara = new HCNetSDK.NET_DVR_JPEGPARA(); |
|
179 |
lpJpegPara.wPicSize = (short)tlPicSize; |
|
180 |
lpJpegPara.wPicQuality = (short)tlPicQuality; |
|
181 |
// 参数必须写入到结构体,否则在保存图片的时候异常图片 |
|
182 |
lpJpegPara.write(); |
|
183 |
String tFiledirName = CommonUtils.getCurrDate();//yyyyMMdd |
|
184 |
// 创建文件名称 |
|
185 |
String tDatePath = MessageCode.DvrPicSavePath+"/"+tFiledirName; |
|
186 |
// 判断文件夹是否创建 |
|
187 |
File tFile = new File(tDatePath); |
|
188 |
if (!tFile.exists()){ |
|
189 |
tFile.mkdirs(); |
|
190 |
} |
|
191 |
String tDate = CommonUtils.getCurrDate2();//yyyyMMddHHmmss |
|
192 |
// 文件名称 |
|
193 |
String tImageName = tDate +".png"; |
|
194 |
// 全路径 |
|
195 |
String tPath = tDatePath+"/"+tImageName; |
|
196 |
// 开始抓图 |
|
197 |
if (!hCNetSDK.NET_DVR_CaptureJPEGPicture(nlUserID, nChannel, lpJpegPara, tPath)) |
|
198 |
{ |
|
199 |
return "失败编码:"+hCNetSDK.NET_DVR_GetLastError() ; |
|
200 |
}else { |
|
201 |
Long tImageId = CommonUtils.Getnum();//生成流水号 |
|
202 |
// saveZnfxImageInfo(tFiledirName,tImageName,tPath,tImageId); |
|
203 |
|
|
204 |
return "success>>>>>>>>>>>" + tImageName; |
|
205 |
} |
|
206 |
} |
|
207 |
else |
|
208 |
{ |
|
209 |
return "login fail>>>>>>>>>>>"; |
|
210 |
} |
|
211 |
} |
|
212 |
/** |
|
213 |
* 检查设备是否在线 |
|
214 |
* @return |
|
215 |
*/ |
|
216 |
private boolean getDeviceStatus() { |
|
217 |
boolean isOnLine = hCNetSDK.NET_DVR_RemoteControl(lUserID, 20005, null, 0); |
|
218 |
// if (!isOnLine) { |
|
219 |
//// updateZnfxPostalStatus(PostalStatus.ABNORMAL.value()); //离线 |
|
220 |
//// insertZnfxPostalStatusLog(PostalStatus.ABNORMAL.value()); |
|
221 |
// mZnfxPostalStatusService.changeStatus(MessageCode.POSTAL_STATUS_TYPE_CAMERA, PostalStatus.ABNORMAL.value()); |
|
222 |
// return false; |
|
223 |
// } else { |
|
224 |
// mZnfxPostalStatusService.changeStatus(MessageCode.POSTAL_STATUS_TYPE_CAMERA, PostalStatus.NORMAL.value()); |
|
225 |
//// updateZnfxPostalStatus(PostalStatus.NORMAL.value());//在线 |
|
226 |
// } |
|
227 |
return true; |
|
228 |
} |
|
229 |
// /** |
|
230 |
// * 更新摄像头通讯状态 |
|
231 |
// * @param status |
|
232 |
// */ |
|
233 |
// private void updateZnfxPostalStatus(String status) { |
|
234 |
// ZnfxPostalStatusDTO tEntity = new ZnfxPostalStatusDTO(); |
|
235 |
// tEntity.setId(1); |
|
236 |
// tEntity.setStatus(status); |
|
237 |
// tEntity.setTime(CommonUtils.getCurrDate3()); |
|
238 |
// mZnfxPostalStatusService.update(tEntity); |
|
239 |
// |
|
240 |
// } |
|
241 |
// private void insertZnfxPostalStatusLog(String status) { |
|
242 |
// Calendar calendar = Calendar.getInstance(); |
|
243 |
// calendar.set(Calendar.MILLISECOND, 0); |
|
244 |
// calendar.set(Calendar.SECOND, 0); |
|
245 |
// Date runTime = calendar.getTime(); |
|
246 |
// ZnfxPostalStatusLogEntity tEntity = new ZnfxPostalStatusLogEntity(); |
|
247 |
// tEntity.setType(MessageCode.POSTAL_STATUS_TYPE_CAMERA); |
|
248 |
// tEntity.setStatus(status); |
|
249 |
// tEntity.setTime(runTime); |
|
250 |
// mZnfxPostalStatusLogService.insert(tEntity); |
|
251 |
// } |
|
252 |
// |
|
253 |
// private void saveZnfxImageInfo(String tFiledirName, String tImageName, String tPath, Long tImageId) { |
|
254 |
// ZnfxImageInfoDTO tZnfxImageInfoDTO = new ZnfxImageInfoDTO(); |
|
255 |
// String tImagepath = "/" + tFiledirName + "/" + tImageName; |
|
256 |
// tZnfxImageInfoDTO.setImageid(tImageId); |
|
257 |
// tZnfxImageInfoDTO.setCameraCode(MessageCode.POSTAL_STATUS_TYPE_CAMERA); |
|
258 |
// tZnfxImageInfoDTO.setImagePath(tImagepath);//图片相对路径 |
|
259 |
// tZnfxImageInfoDTO.setAbsImagePath(tPath);//图片绝对路径 |
|
260 |
// tZnfxImageInfoDTO.setCreateTime(new Date()); |
|
261 |
// mZnfxImageInfoService.save(tZnfxImageInfoDTO); |
|
262 |
// } |
|
263 |
} |