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