1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package com.iailab.module.data.dev.hikvision.utils;
 
import com.alibaba.fastjson.JSONException;
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.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
 
 
@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() throws JSONException {
//        if (!getDeviceStatus()) {
//            int init = initDevices();
//            if (init == 1) {
//                return "初始化失败";
//            }
//            ArrayList<String> list = mZnfxCodeService.getDriverInfo("driverinfo");
//            if (list != null && list.size() > 0) {
//                String driverip = list.get(0);
//                String driverport = list.get(1);
//                String username = list.get(2);
//                String password = list.get(3);
//                String returnVal = Login_V40(driverip, Short.parseShort(driverport), username, password);
//                System.out.println(returnVal);
//                return returnVal;
//            } else {
//                return "获取设备信息失败!";
//            }
//        }
//        else
//        {
//            String result = captureJPEGPicture();
//            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) {
       /* 注册 */
       // 设备登录信息
       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) {
//           updateZnfxPostalStatus(PostalStatus.ABNORMAL.value());
//           mZnfxPostalStatusService.changeStatus(MessageCode.POSTAL_STATUS_TYPE_CAMERA, PostalStatus.ABNORMAL.value());
           
           System.out.println("登录失败,错误码为" + hCNetSDK.NET_DVR_GetLastError());
           return "登录失败,错误码为" + hCNetSDK.NET_DVR_GetLastError();
       } else {
           System.out.println("登录成功!");
           // read()后,结构体中才有对应的数据 
           m_strDeviceInfo.read();  
//           updateZnfxPostalStatus(PostalStatus.NORMAL.value());
//           mZnfxPostalStatusService.changeStatus(MessageCode.POSTAL_STATUS_TYPE_CAMERA, PostalStatus.NORMAL.value());
           
               String result = captureJPEGPicture();
           
           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()
    {
//            ArrayList<String> list = mZnfxCodeService.getDriverInfo("driverpic");
            List<String> list = new ArrayList<>();
            list.add("tlChannel");
            list.add("tlPicSize");
            list.add("tlPicQuality");
               if (list != null && list.size() > 0) {
                   int tlChannel = Integer.parseInt(list.get(0));
                   int tlPicSize = Integer.parseInt(list.get(1));
                   int tlPicQuality = Integer.parseInt(list.get(2));
 
                   NativeLong nlUserID = new NativeLong(lUserID);
                   NativeLong nChannel = new NativeLong(tlChannel);
                   HCNetSDK.NET_DVR_JPEGPARA lpJpegPara = new HCNetSDK.NET_DVR_JPEGPARA();
                   lpJpegPara.wPicSize = (short)tlPicSize;
                   lpJpegPara.wPicQuality = (short)tlPicQuality;
                   // 参数必须写入到结构体,否则在保存图片的时候异常图片
                   lpJpegPara.write();
                   String tFiledirName = CommonUtils.getCurrDate();//yyyyMMdd
                   // 创建文件名称
                   String tDatePath = MessageCode.DvrPicSavePath+"/"+tFiledirName;
                   // 判断文件夹是否创建
                   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 {
                       Long tImageId = CommonUtils.Getnum();//生成流水号
//                       saveZnfxImageInfo(tFiledirName,tImageName,tPath,tImageId);
 
                       return "success>>>>>>>>>>>" + tImageName;
                   }
               }
               else
               {
                   return "login fail>>>>>>>>>>>";
               }
    }
    /**
     * 检查设备是否在线
     * @return
     */
    private boolean getDeviceStatus() {
        boolean isOnLine = hCNetSDK.NET_DVR_RemoteControl(lUserID, 20005, null, 0);
//        if (!isOnLine) {
////            updateZnfxPostalStatus(PostalStatus.ABNORMAL.value()); //离线
////            insertZnfxPostalStatusLog(PostalStatus.ABNORMAL.value());
//            mZnfxPostalStatusService.changeStatus(MessageCode.POSTAL_STATUS_TYPE_CAMERA, PostalStatus.ABNORMAL.value());
//            return false;
//        } else {
//            mZnfxPostalStatusService.changeStatus(MessageCode.POSTAL_STATUS_TYPE_CAMERA, PostalStatus.NORMAL.value());
////            updateZnfxPostalStatus(PostalStatus.NORMAL.value());//在线
//        }
        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);
//    }
}