package com.iailab.module.data.video.hikvision.utils; import com.alibaba.fastjson.JSONException; import com.iailab.module.data.video.config.VideoConfiguration; import com.iailab.module.data.video.dto.CameraDTO; import com.iailab.module.data.video.enums.CameraStatusEnum; import com.sun.jna.Native; import com.sun.jna.NativeLong; import com.sun.jna.Platform; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import javax.annotation.PreDestroy; import java.io.File; import java.util.HashMap; import java.util.Map; @Slf4j @Component public class HIKDevice { // 接口的实例,通过接口实例调用外部dll/so的函数 public static HCNetSDK hCNetSDK = null; // 用户登录返回句柄 public static int lUserID; //构造函数 public HIKDevice() { if (hCNetSDK == null) { synchronized (HCNetSDK.class) { try { if (Platform.isWindows()) { hCNetSDK = (HCNetSDK) Native.loadLibrary(MessageCode.HIK_WIN_PATH, HCNetSDK.class); } if (Platform.isLinux()) { hCNetSDK = (HCNetSDK) Native.loadLibrary(MessageCode.HIK_LINUX_PATH, HCNetSDK.class); } } catch (Exception ex) { log.error("SdkInitService-init-hCNetSDK-error"); } } } } /** * 初始化资源配置 */ public int initDevices() { System.out.println("进入构造函数"); // 初始化SDK boolean initSuc = hCNetSDK.NET_DVR_Init(); if (initSuc != true) { System.out.println("初始化失败"); return 1; } // 设置连接时间与重连时间 hCNetSDK.NET_DVR_SetConnectTime(2000, 1); System.out.println("初始化SDK资源完成"); return 0; } //销毁函数 @PreDestroy public void destroy() throws Exception { hCNetSDK.NET_DVR_Cleanup(); } /* * 功能:登录设备 * 参数: * deviceIP-设备局域网IP地址 * devicePort-设备SDK服务端口 * userName-用户名 * passWord-密码 * */ public String loginDevice(CameraDTO camera) throws JSONException { if (!getDeviceStatus(camera.getId())) { int init = initDevices(); if (init == 1) { return "初始化失败"; } String driverip = camera.getIp(); String driverport = camera.getPort(); String username = camera.getUsername(); String password = camera.getPassword(); String returnVal = Login_V40(driverip, Short.parseShort(driverport), username, password, camera); System.out.println(returnVal); return returnVal; } else { String result = captureJPEGPicture(camera); System.out.println(result); return result; } } /** * @param m_sDeviceIP 设备ip地址 * @param wPort 端口号,设备网络SDK登录默认端口8000 * @param m_sUsername 用户名 * @param m_sPassword 密码 */ public String Login_V40(String m_sDeviceIP, short wPort, String m_sUsername, String m_sPassword, CameraDTO camera) { /* 注册 */ // 设备登录信息 HCNetSDK.NET_DVR_USER_LOGIN_INFO m_strLoginInfo = new HCNetSDK.NET_DVR_USER_LOGIN_INFO(); // 设备信息 HCNetSDK.NET_DVR_DEVICEINFO_V40 m_strDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V40(); m_strLoginInfo.sDeviceAddress = new byte[HCNetSDK.NET_DVR_DEV_ADDRESS_MAX_LEN]; System.arraycopy(m_sDeviceIP.getBytes(), 0, m_strLoginInfo.sDeviceAddress, 0, m_sDeviceIP.length()); m_strLoginInfo.wPort = wPort; m_strLoginInfo.sUserName = new byte[HCNetSDK.NET_DVR_LOGIN_USERNAME_MAX_LEN]; System.arraycopy(m_sUsername.getBytes(), 0, m_strLoginInfo.sUserName, 0, m_sUsername.length()); m_strLoginInfo.sPassword = new byte[HCNetSDK.NET_DVR_LOGIN_PASSWD_MAX_LEN]; System.arraycopy(m_sPassword.getBytes(), 0, m_strLoginInfo.sPassword, 0, m_sPassword.length()); // 是否异步登录:false- 否,true- 是 m_strLoginInfo.bUseAsynLogin = false; // write()调用后数据才写入到内存中 m_strLoginInfo.write(); lUserID = hCNetSDK.NET_DVR_Login_V40(m_strLoginInfo, m_strDeviceInfo); if (lUserID == -1) { VideoConfiguration.staticCameraService.setStatus(camera.getId(), CameraStatusEnum.OFFLINE.getCode()); System.out.println("登录失败,错误码为" + hCNetSDK.NET_DVR_GetLastError()); return "登录失败,错误码为" + hCNetSDK.NET_DVR_GetLastError(); } else { System.out.println("登录成功!"); // read()后,结构体中才有对应的数据 m_strDeviceInfo.read(); VideoConfiguration.staticCameraService.setStatus(camera.getId(), CameraStatusEnum.ONLIEN.getCode()); String result = captureJPEGPicture(camera); return result; } } //注销设备 public String logoutDevice() { NativeLong nlUserID = new NativeLong(lUserID); if (!hCNetSDK.NET_DVR_Logout(nlUserID)) { System.out.println("注销失败,设备ID:" + nlUserID.intValue() + "错误码:" + hCNetSDK.NET_DVR_GetLastError()); return "{\"result\":\"loginDevice Fail\",\"userID\":" + nlUserID.intValue() + ",\"errorCode\":" + hCNetSDK.NET_DVR_GetLastError() + "}"; } else { System.out.println("注销成功,设备ID:" + nlUserID.intValue()); return "{\"result\":\"logoutDevice Success\",\"userID\":" + nlUserID.intValue() + ",\"errorCode\":0}"; } } /** * 设备抓图(无预览) * 单帧设备抓取保存的图片为JPG,具体注意问题需要详细查看【设备网络sdk使用手册】 * * @return */ public String captureJPEGPicture(CameraDTO camera) { int tlChannel = camera.getChannel(); String tlPicSize = VideoConfiguration.staticPicSize; String tlPicQuality = VideoConfiguration.staticPicQuality; NativeLong nlUserID = new NativeLong(lUserID); NativeLong nChannel = new NativeLong(tlChannel); HCNetSDK.NET_DVR_JPEGPARA lpJpegPara = new HCNetSDK.NET_DVR_JPEGPARA(); lpJpegPara.wPicSize = Short.parseShort(tlPicSize); lpJpegPara.wPicQuality = Short.parseShort(tlPicQuality); // 参数必须写入到结构体,否则在保存图片的时候异常图片 lpJpegPara.write(); String tFilename = CommonUtils.getCurrDate();//yyyyMMdd // 创建文件名称 String tDatePath = VideoConfiguration.staticHkCapturePath + tFilename; // 判断文件夹是否创建 File tFile = new File(tDatePath); if (!tFile.exists()) { tFile.mkdirs(); } String tDate = CommonUtils.getCurrDate2();//yyyyMMddHHmmss // 文件名称 String tImageName = tDate + ".png"; // 全路径 String tPath = tDatePath + "/" + tImageName; // 开始抓图 if (!hCNetSDK.NET_DVR_CaptureJPEGPicture(nlUserID, nChannel, lpJpegPara, tPath)) { return "失败编码:" + hCNetSDK.NET_DVR_GetLastError(); } else { Map params = new HashMap<>(); params.put("strFileName", tPath); params.put("cmdSerial", camera.getCode()); params.put("brand", "HK"); VideoConfiguration.staticCameraService.dealCapture(params); return "success>>>>>>>>>>>" + tImageName; } } /** * 检查设备是否在线 * * @return */ private boolean getDeviceStatus(String id) { boolean isOnLine = hCNetSDK.NET_DVR_RemoteControl(lUserID, 20005, null, 0); if (!isOnLine) { VideoConfiguration.staticCameraService.setStatus(id, CameraStatusEnum.OFFLINE.getCode()); return false; } else { VideoConfiguration.staticCameraService.setStatus(id, CameraStatusEnum.ONLIEN.getCode()); } return true; } // /** // * 更新摄像头通讯状态 // * @param status // */ // private void updateZnfxPostalStatus(String status) { // ZnfxPostalStatusDTO tEntity = new ZnfxPostalStatusDTO(); // tEntity.setId(1); // tEntity.setStatus(status); // tEntity.setTime(CommonUtils.getCurrDate3()); // mZnfxPostalStatusService.update(tEntity); // // } // private void insertZnfxPostalStatusLog(String status) { // Calendar calendar = Calendar.getInstance(); // calendar.set(Calendar.MILLISECOND, 0); // calendar.set(Calendar.SECOND, 0); // Date runTime = calendar.getTime(); // ZnfxPostalStatusLogEntity tEntity = new ZnfxPostalStatusLogEntity(); // tEntity.setType(MessageCode.POSTAL_STATUS_TYPE_CAMERA); // tEntity.setStatus(status); // tEntity.setTime(runTime); // mZnfxPostalStatusLogService.insert(tEntity); // } // // private void saveZnfxImageInfo(String tFiledirName, String tImageName, String tPath, Long tImageId) { // ZnfxImageInfoDTO tZnfxImageInfoDTO = new ZnfxImageInfoDTO(); // String tImagepath = "/" + tFiledirName + "/" + tImageName; // tZnfxImageInfoDTO.setImageid(tImageId); // tZnfxImageInfoDTO.setCameraCode(MessageCode.POSTAL_STATUS_TYPE_CAMERA); // tZnfxImageInfoDTO.setImagePath(tImagepath);//图片相对路径 // tZnfxImageInfoDTO.setAbsImagePath(tPath);//图片绝对路径 // tZnfxImageInfoDTO.setCreateTime(new Date()); // mZnfxImageInfoService.save(tZnfxImageInfoDTO); // } }