[Java]海康人脸识别门禁(明眸)以卡为中心SDK用法举例

2021-07-16 4227点热度 0人点赞 0条评论

最近使用海康的人脸识别门禁(明眸)做二次开发,整理下使用SDK开发的工具类,实现的功能有:

  • 登录设备
  • 注销
  • 获取事件列表
  • 获取卡列表
  • 获取卡信息
  • 下发卡号
  • 获取人脸信息
  • 下发人脸
  • 根据卡号删除人脸
  • 删除卡号
  • 远程控门

供有需要的参考。以人为中心SDK用法举例详见:https://blog.terrynow.com/2021/07/18/java-hikvision-face-recognition-entrance-guard-sdk-based-on-user-example/

我用到的HCNetSDK.java,这个只要在官方下载到的SDK的DEMO里下载就可以了。

添加依赖

  • 海康SKD使用到了jna,不过是挺老的版本3.0.9,这个可以在maven库里面找到,也可以手动引入官方的jar,这里我用maven引入
  • 还需要org.json
  • 还用到了log4j,使用其他log框架的,自行修改
<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20200518</version>
</dependency>

<dependency>
    <groupId>net.java.dev.jna</groupId>
    <artifactId>jna</artifactId>
    <version>3.0.9</version>
</dependency>

工具类

HCNetDeviceUtil.java

import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.ByteArrayInputStream;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author Terry E-mail: yaoxinghuo at 126 dot com
 * @date 2020-11-7 15:14
 * @description 设备网络SDK编程指南(明眸-以卡号为中心).pdf
 */
public class HCNetDeviceUtil {

    private static final Log log = LogFactory.getLog(HCNetDeviceUtil.class);

    HCNetSDK hcNetSDK = HCNetSDK.INSTANCE;
    private int userId;//记录登录后的userId

    /**
     * 登录设备
     */
    public void loginDevice(String ip, int port, String userName, String password) throws Exception {
//        String strPathCom = "/opt/HCNetSDK/HCNetSDKCom";
//        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());

        //设置报警回调函数,这个函数将会上次人脸识别比对结果
//        if (fMSFCallBack_V31 == null){
//            fMSFCallBack_V31 = new FMSGCallBack_V31();
//            Pointer pUser = null;
//            if (!hcNetSDK.NET_DVR_SetDVRMessageCallBack_V31(fMSFCallBack_V31, pUser)){
//                System.out.println("设置回调函数失败!");
//            }
//        }

//        boolean status = hcNetSDK.NET_DVR_Init();
//        if (!status) {
//            throw new Exception("初始化失败:" + hcNetSDK.NET_DVR_GetCardLastError_Card());
//        }

        HCNetSDK.NET_DVR_USER_LOGIN_INFO m_strLoginInfo = new HCNetSDK.NET_DVR_USER_LOGIN_INFO();//设备登录信息
        //注册
        String m_sDeviceIP = ip;//设备ip地址
        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());

        String m_sUsername = userName;//设备用户名
        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());

        String m_sPassword = password;//设备密码
        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());

        m_strLoginInfo.wPort = (short) port;

        m_strLoginInfo.bUseAsynLogin = false; //是否异步登录:0- 否,1- 是

        m_strLoginInfo.write();

        HCNetSDK.NET_DVR_DEVICEINFO_V40 m_strDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V40();//设备信息
//        NativeLong lUserID = hcNetSDK.NET_DVR_Login_V40(m_strLoginInfo.getPointer(), m_strDeviceInfo.getPointer());
        int lUserID = hcNetSDK.NET_DVR_Login_V40(m_strLoginInfo, m_strDeviceInfo);

        if (lUserID == -1) {
            throw new Exception("注册失败,错误代码:" + hcNetSDK.NET_DVR_GetLastError());
        }
//        else {
//            HCNetSDK.NET_DVR_SETUPALARM_PARAM m_strAlarmInfo = new HCNetSDK.NET_DVR_SETUPALARM_PARAM();
//
//            hcNetSDK.NET_DVR_SetupAlarmChan_V41(lUserID.intValue(), m_strAlarmInfo);
//        }
//        userId = lUserID.intValue();
        userId = lUserID;
    }

//    public String capture(String path) throws Exception {
//        File file = new File(path);
//        if (!file.exists()) {
//            file.mkdirs();
//        }
//        String fileName = path + System.currentTimeMillis() + ".jpg";
//        HCNetSDK.NET_DVR_JPEGPARA picPara = new HCNetSDK.NET_DVR_JPEGPARA();
//        System.out.println("******" + hcNetSDK.NET_DVR_GetLastError() + "*********");
//        picPara.wPicSize = 0;
//        picPara.wPicQuality = 0;
//        NativeLong chan = new NativeLong();
//        hcNetSDK.NET_DVR_GetDVRConfig(new NativeLong(this.userId), 0, chan, null, 0, null);
//        System.out.println("the chan is :" + chan + "");
//        NativeLong channel = new NativeLong(1);
//        System.out.println("the channel is :" + channel + "");
//        String picPath = fileName;
//        boolean ff = hcNetSDK.NET_DVR_CaptureJPEGPicture(new NativeLong(this.userId), channel, picPara, picPath);
//        System.out.println("******" + ff + hcNetSDK.NET_DVR_GetLastError() + "*********");
//        if (!ff) {
//            throw new Exception();
//        }
//        return fileName;
//    }

    /**
     * 注销,用完后记得注销,要不然登录的太多,后续登录会报错
     */
    public void logout() {
        hcNetSDK.NET_DVR_Logout_V30(this.userId);
    }

    /**
     * 获取设备能力集
     */
    public JSONObject getAbility() throws Exception {
        JSONObject jsonObject = new JSONObject();
        String strURL = "GET /ISAPI/AccessControl/UserInfo/capabilities?format=json";
        HCNetSDK.BYTE_ARRAY ptrUrl = new HCNetSDK.BYTE_ARRAY(1024);
        System.arraycopy(strURL.getBytes(), 0, ptrUrl.byValue, 0, strURL.length());
        ptrUrl.write();

        //获取能力集时输入参数为空即可
        /*HCNetSDK.BYTE_ARRAY ptrInBuffer = new HCNetSDK.BYTE_ARRAY(ISAPI_DATA_LEN);
        ptrInBuffer.read();
        String strInbuffer = "";
        ptrInBuffer.byValue = strInbuffer.getBytes();
        ptrInBuffer.write();
        */

        HCNetSDK.NET_DVR_XML_CONFIG_INPUT struXMLInput = new HCNetSDK.NET_DVR_XML_CONFIG_INPUT();
        struXMLInput.read();
        struXMLInput.dwSize = struXMLInput.size();
        struXMLInput.lpRequestUrl = ptrUrl.getPointer();
        struXMLInput.dwRequestUrlLen = ptrUrl.byValue.length;
        struXMLInput.lpInBuffer = null;//ptrInBuffer.getPointer();
        struXMLInput.dwInBufferSize = 0;//ptrInBuffer.byValue.length;
        struXMLInput.write();

        HCNetSDK.BYTE_ARRAY ptrStatusByte = new HCNetSDK.BYTE_ARRAY(4 * 4096);
        ptrStatusByte.read();

        HCNetSDK.BYTE_ARRAY ptrOutByte = new HCNetSDK.BYTE_ARRAY(1024 * 1024);
        ptrOutByte.read();

        HCNetSDK.NET_DVR_XML_CONFIG_OUTPUT struXMLOutput = new HCNetSDK.NET_DVR_XML_CONFIG_OUTPUT();
        struXMLOutput.read();
        struXMLOutput.dwSize = struXMLOutput.size();
        struXMLOutput.lpOutBuffer = ptrOutByte.getPointer();
        struXMLOutput.dwOutBufferSize = ptrOutByte.size();
        struXMLOutput.lpStatusBuffer = ptrStatusByte.getPointer();
        struXMLOutput.dwStatusSize = ptrStatusByte.size();
        struXMLOutput.write();

        if (!hcNetSDK.NET_DVR_STDXMLConfig(this.userId, struXMLInput, struXMLOutput)) {
            int iErr = hcNetSDK.NET_DVR_GetLastError();
            throw new Exception("NET_DVR_STDXMLConfig失败,错误号:" + iErr);
        } else {
            struXMLOutput.read();
            ptrOutByte.read();
            ptrStatusByte.read();
            String strOut = new String(ptrOutByte.byValue).trim();
//            System.out.println("获取设备能力集输出结果:" + strOutXML);
            String strStatus = new String(ptrStatusByte.byValue).trim();
            log.info("获取设备能力集返回状态:" + strStatus);
            jsonObject = new JSONObject(strOut);
            return jsonObject;
        }
    }

    /**
     * 去掉空白这种特殊字符
     */
    private String hcBytesToString(byte[] bytes) {
        return new String(bytes).replace("\u0000", "");
    }

    /**
     * 获取事件列表
     */
    public JSONArray getDeviceEvents(Date start, Date end) throws Exception {
        JSONArray jsonArray = new JSONArray();

        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(start);
        HCNetSDK.NET_DVR_TIME startTime = new HCNetSDK.NET_DVR_TIME();
        startTime.dwYear = startCalendar.get(Calendar.YEAR); // 年
        startTime.dwMonth = startCalendar.get(Calendar.MONTH) + 1; // 月
        startTime.dwDay = startCalendar.get(Calendar.DAY_OF_MONTH);
        startTime.dwHour = startCalendar.get(Calendar.HOUR_OF_DAY); // 时
        startTime.dwMinute = startCalendar.get(Calendar.MINUTE); // 分
        startTime.dwSecond = startCalendar.get(Calendar.SECOND); // 秒

        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(end);
        HCNetSDK.NET_DVR_TIME endTime = new HCNetSDK.NET_DVR_TIME();
        endTime.dwYear = endCalendar.get(Calendar.YEAR); // 年
        endTime.dwMonth = endCalendar.get(Calendar.MONTH) + 1; // 月
        endTime.dwDay = endCalendar.get(Calendar.DAY_OF_MONTH);
        endTime.dwHour = endCalendar.get(Calendar.HOUR_OF_DAY); // 时
        endTime.dwMinute = endCalendar.get(Calendar.MINUTE); // 分
        endTime.dwSecond = endCalendar.get(Calendar.SECOND); // 秒

        // 调用获取设备事件长连接
        HCNetSDK.NET_DVR_ACS_EVENT_COND_V50 struAcsEventCond = new HCNetSDK.NET_DVR_ACS_EVENT_COND_V50();
        struAcsEventCond.read();
        struAcsEventCond.dwSize = struAcsEventCond.size();
        struAcsEventCond.dwMajor = 5;// 5代表event事件
//        struAcsEventCond.dwMinor = 0x4b;// 代表刷脸成功
        struAcsEventCond.dwMinor = 0;// 代表全部
        struAcsEventCond.struStartTime = startTime;// ****必须要
        struAcsEventCond.struEndTime = endTime;// ***必须要
        struAcsEventCond.write();
        Pointer ptrStruEventCond = struAcsEventCond.getPointer();
        int lHandle = hcNetSDK.NET_DVR_StartRemoteConfig(this.userId, HCNetSDK.NET_DVR_GET_ACS_EVENT, ptrStruEventCond,
                struAcsEventCond.size(), null, null);
        if (lHandle == -1) {
            throw new Exception("建立获取设备事件长连接失败,错误码:" + hcNetSDK.NET_DVR_GetLastError());
        }
//        log.info("建立获取设备事件连接成功!" + lHandle);
        // 获取设备详细事件开始
        HCNetSDK.NET_DVR_ACS_EVENT_CFG_V50 struEventCFG = new HCNetSDK.NET_DVR_ACS_EVENT_CFG_V50();
        struEventCFG.read();
        struEventCFG.dwSize = struEventCFG.size();
        struEventCFG.write();
        int dwState = -1;
        while (true) {
            dwState = hcNetSDK.NET_DVR_GetNextRemoteConfig(lHandle, struEventCFG.getPointer(), struEventCFG.size());
            struEventCFG.read();
            if (dwState == -1) {
                log.warn("NET_DVR_GetNextRemoteConfig接口调用失败,错误码:" + hcNetSDK.NET_DVR_GetLastError());
            } else if (dwState == HCNetSDK.NET_SDK_GET_NEXT_STATUS_NEED_WAIT) {
                // 配置等待
                log.info("==========配置等待==============");
                try {
                    Thread.sleep(10);
                } catch (InterruptedException ignored) {
                }
            } else if (dwState == HCNetSDK.NET_SDK_GET_NEXT_STATUS_FAILED) {
                // 获取事件失败
                log.warn("===============获取设备事件失败=======================");
                break;

            } else if (dwState == HCNetSDK.NET_SDK_GET_NEXT_STATUS_EXCEPTION) {
                // 获取事件异常
                log.warn("===============获取设备事件异常=======================");
                break;
            } else if (dwState == HCNetSDK.NET_SDK_GET_NEXT_STATUS_SUCCESS) {
                // 获取设备事件成功
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("employeeNo", hcBytesToString(struEventCFG.struAcsEventInfo.byEmployeeNo));
                jsonObject.put("cardNo", hcBytesToString(struEventCFG.struAcsEventInfo.byCardNo));
                //MINOR_LEGAL_CARD_PASS	0x01	合法卡认证通过
                //MINOR_CARD_AND_PSW_PASS	0x02	刷卡加密码认证通过
                //MINOR_CARD_AND_PSW_FAIL	0x03	刷卡加密码认证失败
                //MINOR_CARD_AND_PSW_TIMEOUT	0x04	数卡加密码认证超时
                //MINOR_CARD_AND_PSW_OVER_TIME	0x05	刷卡加密码超次
                //MINOR_CARD_NO_RIGHT	0x06	未分配权限
                //MINOR_CARD_INVALID_PERIOD	0x07	无效时段
                //MINOR_CARD_OUT_OF_DATE	0x08	卡号过期
                //MINOR_INVALID_CARD	0x09	无此卡号
                //MINOR_ANTI_SNEAK_FAIL	0x0a	反潜回认证失败
                //MINOR_INTERLOCK_DOOR_NOT_CLOSE	0x0b	互锁门未关闭
                //MINOR_NOT_BELONG_MULTI_GROUP	0x0c	卡不属于多重认证群组
                //MINOR_INVALID_MULTI_VERIFY_PERIOD	0x0d	卡不在多重认证时间段内
                //MINOR_MULTI_VERIFY_SUPER_RIGHT_FAIL	0x0e	多重认证模式超级权限认证失败
                //MINOR_MULTI_VERIFY_REMOTE_RIGHT_FAIL	0x0f	多重认证模式远程认证失败
                //MINOR_MULTI_VERIFY_SUCCESS	0x10	多重认证成功
                //MINOR_LEADER_CARD_OPEN_BEGIN	0x11	首卡开门开始
                //MINOR_LEADER_CARD_OPEN_END	0x12	首卡开门结束
                //MINOR_ALWAYS_OPEN_BEGIN	0x13	常开状态开始
                //MINOR_ALWAYS_OPEN_END	0x14	常开状态结束
                //MINOR_LOCK_OPEN	0x15	门锁打开
                //MINOR_LOCK_CLOSE	0x16	门锁关闭
                //MINOR_DOOR_BUTTON_PRESS	0x17	开门按钮打开
                //MINOR_DOOR_BUTTON_RELEASE	0x18	开门按钮放开
                //MINOR_DOOR_OPEN_NORMAL	0x19	正常开门(门磁)
                //MINOR_DOOR_CLOSE_NORMAL	0x1a	正常关门(门磁)
                //MINOR_DOOR_OPEN_ABNORMAL	0x1b	门异常打开(门磁)
                //MINOR_DOOR_OPEN_TIMEOUT	0x1c	门打开超时(门磁)
                //MINOR_ALARMOUT_ON	0x1d	报警输出打开
                //MINOR_ALARMOUT_OFF	0x1e	报警输出关闭
                //MINOR_ALWAYS_CLOSE_BEGIN	0x1f	常关状态开始
                //MINOR_ALWAYS_CLOSE_END	0x20	常关状态结束
                //MINOR_MULTI_VERIFY_NEED_REMOTE_OPEN	0x21	多重多重认证需要远程开门
                //MINOR_MULTI_VERIFY_SUPERPASSWD_VERIFY_SUCCESS	0x22	多重认证超级密码认证成功事件
                //MINOR_MULTI_VERIFY_REPEAT_VERIFY	0x23	多重认证重复认证事件
                //MINOR_MULTI_VERIFY_TIMEOUT	0x24	多重认证重复认证事件
                //MINOR_DOORBELL_RINGING	0x25	门铃响
                //MINOR_FINGERPRINT_COMPARE_PASS	0x26	指纹比对通过
                //MINOR_FINGERPRINT_COMPARE_FAIL	0x27	指纹比对失败
                //MINOR_CARD_FINGERPRINT_VERIFY_PASS	0x28	刷卡加指纹认证通过
                //MINOR_CARD_FINGERPRINT_VERIFY_FAIL	0x29	刷卡加指纹认证失败
                //MINOR_CARD_FINGERPRINT_VERIFY_TIMEOUT	0x2a	刷卡加指纹认证超时
                //MINOR_CARD_FINGERPRINT_PASSWD_VERIFY_PASS	0x2b	刷卡加指纹加密码认证通过
                //MINOR_CARD_FINGERPRINT_PASSWD_VERIFY_FAIL	0x2c	刷卡加指纹加密码认证失败
                //MINOR_CARD_FINGERPRINT_PASSWD_VERIFY_TIMEOUT	0x2d	刷卡加指纹加密码认证超时
                //MINOR_FINGERPRINT_PASSWD_VERIFY_PASS	0x2e	指纹加密码认证通过
                //MINOR_FINGERPRINT_PASSWD_VERIFY_FAIL	0x2f	指纹加密码认证失败
                //MINOR_FINGERPRINT_PASSWD_VERIFY_TIMEOUT	0x30	指纹加密码认证超时
                //MINOR_FINGERPRINT_INEXISTENCE	0x31	指纹不存在
                //MINOR_CARD_PLATFORM_VERIFY	0x32	刷卡平台认证
                //MINOR_CALL_CENTER	0x33	呼叫中心事件
                //MINOR_FIRE_RELAY_TURN_ON_DOOR_ALWAYS_OPEN	0x34	消防继电器导通触发门常开
                //MINOR_FIRE_RELAY_RECOVER_DOOR_RECOVER_NORMAL	0x35	消防继电器恢复门恢复正常
                //MINOR_FACE_AND_FP_VERIFY_PASS	0x36	人脸加指纹认证通过
                //MINOR_FACE_AND_FP_VERIFY_FAIL	0x37	人脸加指纹认证失败
                //MINOR_FACE_AND_FP_VERIFY_TIMEOUT	0x38	人脸加指纹认证超时
                //MINOR_FACE_AND_PW_VERIFY_PASS	0x39	人脸加密码认证通过
                //MINOR_FACE_AND_PW_VERIFY_FAIL	0x3a	人脸加密码认证失败
                //MINOR_FACE_AND_PW_VERIFY_TIMEOUT	0x3b	人脸加密码认证超时
                //MINOR_FACE_AND_CARD_VERIFY_PASS	0x3c	人脸加刷卡认证通过
                //MINOR_FACE_AND_CARD_VERIFY_FAIL	0x3d	人脸加刷卡认证失败
                //MINOR_FACE_AND_CARD_VERIFY_TIMEOUT	0x3e	人脸加刷卡认证超时
                //MINOR_FACE_AND_PW_AND_FP_VERIFY_PASS	0x3f	人脸加密码加指纹认证通过
                //MINOR_FACE_AND_PW_AND_FP_VERIFY_FAIL	0x40	人脸加密码加指纹认证失败
                //MINOR_FACE_AND_PW_AND_FP_VERIFY_TIMEOUT	0x41	人脸加密码加指纹认证超时
                //MINOR_FACE_CARD_AND_FP_VERIFY_PASS	0x42	人脸加刷卡加指纹认证通过
                //MINOR_FACE_CARD_AND_FP_VERIFY_FAIL	0x43	人脸加刷卡加指纹认证失败
                //MINOR_FACE_CARD_AND_FP_VERIFY_TIMEOUT	0x44	人脸加刷卡加指纹认证超时
                //MINOR_EMPLOYEENO_AND_FP_VERIFY_PASS	0x45	工号加指纹认证通过
                //MINOR_EMPLOYEENO_AND_FP_VERIFY_FAIL	0x46	工号加指纹认证失败
                //MINOR_EMPLOYEENO_AND_FP_VERIFY_TIMEOUT	0x47	工号加指纹认证超时
                //MINOR_EMPLOYEENO_AND_FP_AND_PW_VERIFY_PASS	0x48	工号加指纹加密码认证通过
                //MINOR_EMPLOYEENO_AND_FP_AND_PW_VERIFY_FAIL	0x49	工号加指纹加密码认证失败
                //MINOR_EMPLOYEENO_AND_FP_AND_PW_VERIFY_TIMEOUT	0x4a	工号加指纹加密码认证超时
                //MINOR_FACE_VERIFY_PASS	0x4b	人脸认证通过
                //MINOR_FACE_VERIFY_FAIL	0x4c	人脸认证失败
                //MINOR_EMPLOYEENO_AND_FACE_VERIFY_PASS	0x4d	工号加人脸认证通过
                //MINOR_EMPLOYEENO_AND_FACE_VERIFY_FAIL	0x4e	工号加人脸认证失败
                //MINOR_EMPLOYEENO_AND_FACE_VERIFY_TIMEOUT	0x4f	工号加人脸认证超时
                //MINOR_FACE_RECOGNIZE_FAIL	0x50	人脸识别失败
                //MINOR_FIRSTCARD_AUTHORIZE_BEGIN	0x51	首卡授权开始
                //MINOR_FIRSTCARD_AUTHORIZE_END	0x52	首卡授权结束
                //MINOR_DOORLOCK_INPUT_SHORT_CIRCUIT	0x53	门锁输入短路报警
                //MINOR_DOORLOCK_INPUT_BROKEN_CIRCUIT	0x54	门锁输入断路报警
                //MINOR_DOORLOCK_INPUT_EXCEPTION	0x55	门锁输入异常报警
                //MINOR_DOORCONTACT_INPUT_SHORT_CIRCUIT	0x56	门磁输入短路报警
                //MINOR_DOORCONTACT_INPUT_BROKEN_CIRCUIT	0x57	门磁输入断路报警
                //MINOR_DOORCONTACT_INPUT_EXCEPTION	0x58	门磁输入异常报警
                //MINOR_OPENBUTTON_INPUT_SHORT_CIRCUIT	0x59	开门按钮输入短路报警
                //MINOR_OPENBUTTON_INPUT_BROKEN_CIRCUIT	0x5a	开门按钮输入断路报警
                //MINOR_OPENBUTTON_INPUT_EXCEPTION	0x5b	开门按钮输入异常报警
                //MINOR_DOORLOCK_OPEN_EXCEPTION	0x5c	门锁异常打开
                //MINOR_DOORLOCK_OPEN_TIMEOUT	0x5d	门锁打开超时
                //MINOR_FIRSTCARD_OPEN_WITHOUT_AUTHORIZE	0x5e	首卡未授权开门失败
                //MINOR_CALL_LADDER_RELAY_BREAK	0x5f	呼梯继电器断开
                //MINOR_CALL_LADDER_RELAY_CLOSE	0x60	呼梯继电器闭合
                //MINOR_AUTO_KEY_RELAY_BREAK	0x61	自动按键继电器断开
                //MINOR_AUTO_KEY_RELAY_CLOSE	0x62	自动按键继电器闭合
                //MINOR_KEY_CONTROL_RELAY_BREAK	0x63	按键梯控继电器断开
                //MINOR_KEY_CONTROL_RELAY_CLOSE	0x64	按键梯控继电器闭合
                //MINOR_EMPLOYEENO_AND_PW_PASS	0x65	工号加密码认证通过
                //MINOR_EMPLOYEENO_AND_PW_FAIL	0x66	工号加密码认证失败
                //MINOR_EMPLOYEENO_AND_PW_TIMEOUT	0x67	工号加密码认证超时
                //MINOR_HUMAN_DETECT_FAIL	0x68	真人检测失败
                //MINOR_PEOPLE_AND_ID_CARD_COMPARE_PASS	0x69	人证比对通过
                //MINOR_PEOPLE_AND_ID_CARD_COMPARE_FAIL	0x70	人证比对失败
                //MINOR_CERTIFICATE_BLACK_LIST	0x71	黑名单事件
                //MINOR_LEGAL_MESSAGE	0x72	合法短信
                //MINOR_ILLEGAL_MESSAGE	0x73	非法短信
                //MINOR_MAC_DETECT	0x74	MAC侦测
                //MINOR_DOOR_OPEN_OR_DORMANT_FAIL	0x75	门状态常闭或休眠状态认证失败
                //MINOR_AUTH_PLAN_DORMANT_FAIL	0x76	认证计划休眠模式认证失败
                //MINOR_AUTH_PLAN_DORMANT_FAIL	0x76	认证计划休眠模式认证失败
                //MINOR_CARD_ENCRYPT_VERIFY_FAIL	0x77	卡加密校验失败
                //MINOR_SUBMARINEBACK_REPLY_FAIL	0x78	反潜回服务器应答失败
                //MINOR_TRAILING	0x85	尾随通行
                //MINOR_REVERSE_ACCESS	0x86	反向闯入
                //MINOR_FORCE_ACCESS	0x87	外力冲撞
                //MINOR_CLIMBING_OVER_GATE	0x88	翻越
                //MINOR_PASSING_TIMEOUT	0x89	通行超时
                //MINOR_INTRUSION_ALARM	0x8a	误闯报警
                //MINOR_FREE_GATE_PASS_NOT_AUTH	0x8b	闸机自由通行时未认证通过
                //MINOR_DROP_ARM_BLOCK	0x8c	摆臂被阻挡
                //MINOR_DROP_ARM_BLOCK_RESUME	0x8d	摆臂阻挡消除
                //MINOR_LOCAL_FACE_MODELING_FAIL	0x8e	设备升级本地人脸建模失败
                //MINOR_STAY_EVENT	0x8f	逗留事件
                //MINOR_PASSWORD_MISMATCH	0x97	密码不匹配
                //MINOR_EMPLOYEE_NO_NOT_EXIST	0x98	工号不存在
                //MINOR_COMBINED_VERIFY_PASS	0x99	组合认证通过
                //MINOR_COMBINED_VERIFY_TIMEOUT	0x9a	组合认证超时
                //MINOR_VERIFY_MODE_MISMATCH	0x9b	认证方式不匹配
                jsonObject.put("minor", struEventCFG.dwMinor);
                jsonObject.put("time", formatTime(struEventCFG.struTime));
                jsonArray.put(jsonObject);
            } else if (dwState == HCNetSDK.NET_SDK_GET_NEXT_STATUS_FINISH) {
                // 获取设备事件信息完成
                log.info("=============获取设备事件完成================");
                break;
            }
        }
        return jsonArray;
    }

    /**
     * 获取卡列表
     */
    public JSONArray listCards() throws Exception {
        JSONArray jsonArray = new JSONArray();
        HCNetSDK.NET_DVR_CARD_COND struCardCond = new HCNetSDK.NET_DVR_CARD_COND();
        struCardCond.read();
        struCardCond.dwSize = struCardCond.size();
        struCardCond.dwCardNum = 0xffffffff; // 查询所有
        struCardCond.write();
        Pointer ptrStruCond = struCardCond.getPointer();

        int m_lSetCardCfgHandle = hcNetSDK.NET_DVR_StartRemoteConfig(userId, HCNetSDK.NET_DVR_GET_CARD, ptrStruCond,
                struCardCond.size(), null, null);
        if (m_lSetCardCfgHandle == -1) {
            throw new Exception("建立下发卡长连接失败,错误码为" + hcNetSDK.NET_DVR_GetLastError());
        }

        HCNetSDK.NET_DVR_CARD_RECORD struCardRecord = new HCNetSDK.NET_DVR_CARD_RECORD();
        struCardRecord.read();
        struCardRecord.dwSize = struCardRecord.size();
        struCardRecord.write();

        while (true) {
            int dwState = hcNetSDK.NET_DVR_GetNextRemoteConfig(m_lSetCardCfgHandle, struCardRecord.getPointer(),
                    struCardRecord.size());
            struCardRecord.read();
            if (dwState == -1) {
                log.error("NET_DVR_SendWithRecvRemoteConfig接口调用失败,错误码:" + hcNetSDK.NET_DVR_GetLastError());
                break;
            } else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_NEEDWAIT) {
                log.info("配置等待");
                try {
                    Thread.sleep(10);
                } catch (InterruptedException ignored) {
                }
            } else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_FAILED) {
                log.warn("获取卡参数失败");
                break;
            } else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_EXCEPTION) {
                log.warn("获取卡参数异常");
                break;
            } else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_SUCCESS) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("employeeNo", struCardRecord.dwEmployeeNo);
                jsonObject.put("cardNo", hcBytesToString(struCardRecord.byCardNo));
                jsonObject.put("name", hcBytesToString(struCardRecord.byName));
                jsonObject.put("start", formatTime(struCardRecord.struValid.struBeginTime));
                jsonObject.put("end", formatTime(struCardRecord.struValid.struEndTime));
//                jsonObject.put("type", struCardRecord.byCardType);
//                jsonObject.put("userType", struCardRecord.byUserType);
                jsonArray.put(jsonObject);
//                    log.info("获取卡参数成功, 卡号: " + Utils.hcBytesToString(struCardRecord.byCardNo) + ", 卡类型:"
//                            + struCardRecord.byCardType + ", 姓名:" + new String(struCardRecord.byName, "GBK").trim());
            } else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_FINISH) {
                log.info("获取卡参数完成");
                break;
            }
        }

        if (!hcNetSDK.NET_DVR_StopRemoteConfig(m_lSetCardCfgHandle)) {
            log.error("NET_DVR_StopRemoteConfig接口调用失败,错误码:" + hcNetSDK.NET_DVR_GetLastError());
        } else {
            log.info("NET_DVR_StopRemoteConfig接口成功");
        }
        return jsonArray;

    }

    private String formatTime(HCNetSDK.NET_DVR_TIME_EX ex) {
        if (ex == null) {
            return "";
        }
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, ex.wYear);
        calendar.set(Calendar.MONTH, ex.byMonth - 1);
        calendar.set(Calendar.DAY_OF_MONTH, ex.byDay);
        calendar.set(Calendar.HOUR_OF_DAY, ex.byHour);
        calendar.set(Calendar.MINUTE, ex.byMinute);
        calendar.set(Calendar.SECOND, ex.bySecond);
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
    }

    private String formatTime(HCNetSDK.NET_DVR_TIME ex) {
        if (ex == null) {
            return "";
        }
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, ex.dwYear);
        calendar.set(Calendar.MONTH, ex.dwMonth - 1);
        calendar.set(Calendar.DAY_OF_MONTH, ex.dwDay);
        calendar.set(Calendar.HOUR_OF_DAY, ex.dwHour);
        calendar.set(Calendar.MINUTE, ex.dwMinute);
        calendar.set(Calendar.SECOND, ex.dwSecond);
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
    }

    /**
     * 获取卡信息
     */
    public JSONObject getCardInfo(String cardNo) throws Exception {
        JSONObject jsonObject = null;
        HCNetSDK.NET_DVR_CARD_COND struCardCond = new HCNetSDK.NET_DVR_CARD_COND();
        struCardCond.read();
        struCardCond.dwSize = struCardCond.size();
        struCardCond.dwCardNum = 1; //查询一个卡参数
        struCardCond.write();
        Pointer ptrStruCond = struCardCond.getPointer();

        int lHandler = hcNetSDK.NET_DVR_StartRemoteConfig(this.userId, HCNetSDK.NET_DVR_GET_CARD, ptrStruCond, struCardCond.size(), null, null);
        if (lHandler == -1) {
            throw new Exception("建立查询卡参数长连接失败,错误码为" + hcNetSDK.NET_DVR_GetLastError());
        }

        //查找指定卡号的参数,需要下发查找的卡号条件
        HCNetSDK.NET_DVR_CARD_SEND_DATA struCardNo = new HCNetSDK.NET_DVR_CARD_SEND_DATA();
        struCardNo.read();
        struCardNo.dwSize = struCardNo.size();

        for (int i = 0; i < HCNetSDK.ACS_CARD_NO_LEN; i++) {
            struCardNo.byCardNo[i] = 0;
        }
        System.arraycopy(cardNo.getBytes(), 0, struCardNo.byCardNo, 0, cardNo.length());
        struCardNo.write();
        //用户记录
        HCNetSDK.NET_DVR_CARD_RECORD struCardRecord = new HCNetSDK.NET_DVR_CARD_RECORD();
        struCardRecord.read();

        IntByReference pInt = new IntByReference(0);

        while (true) {
            int dwState = hcNetSDK.NET_DVR_SendWithRecvRemoteConfig(lHandler, struCardNo.getPointer(), struCardNo.size(),
                    struCardRecord.getPointer(), struCardRecord.size(), pInt);
            struCardRecord.read();
            if (dwState == -1) {
                System.out.println("NET_DVR_SendWithRecvRemoteConfig查询卡参数调用失败,错误码:" + hcNetSDK.NET_DVR_GetLastError());
                break;
            } else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_NEEDWAIT) {
                System.out.println("配置等待");
                try {
                    Thread.sleep(10);
                } catch (InterruptedException ignored) {
                }
            } else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_FAILED) {
                log.error("获取卡参数失败, 卡号: " + cardNo);
                break;
            } else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_EXCEPTION) {
                log.error("获取卡参数异常, 卡号: " + cardNo);
                break;
            } else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_SUCCESS) {
                jsonObject = new JSONObject();
                jsonObject.put("name", new String(struCardRecord.byName).trim());
                jsonObject.put("type", struCardRecord.byCardType);
                jsonObject.put("cardNo", new String(struCardRecord.byCardNo).trim());
                jsonObject.put("no", struCardRecord.dwEmployeeNo);
                jsonObject.put("start", formatTime(struCardRecord.struValid.struBeginTime));
                jsonObject.put("end", formatTime(struCardRecord.struValid.struEndTime));
            } else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_FINISH) {
                log.info("获取卡参数完成");
                break;
            }
        }

        if (!hcNetSDK.NET_DVR_StopRemoteConfig(lHandler)) {
            log.warn("NET_DVR_StopRemoteConfig接口调用失败,错误码:" + hcNetSDK.NET_DVR_GetLastError());
        }
        return jsonObject;
    }

    /**
     * 下发卡号
     * @param empCode 人员工号
     * @param empName 人员姓名
     * @param cardNo 卡号
     * @param start 卡生效开始时间
     * @param end 卡有效期结束时间
     */
    public void setCardInfo(int empCode, String empName, String cardNo, Date start, Date end) throws Exception {
        HCNetSDK.NET_DVR_CARD_COND struCardCond = new HCNetSDK.NET_DVR_CARD_COND();
        struCardCond.read();
        struCardCond.dwSize = struCardCond.size();
        struCardCond.dwCardNum = 1; // 下发一张
        struCardCond.write();
        Pointer ptrStruCond = struCardCond.getPointer();

        int lHandle = hcNetSDK.NET_DVR_StartRemoteConfig(userId, HCNetSDK.NET_DVR_SET_CARD, ptrStruCond,
                struCardCond.size(), null, null);
        if (lHandle == -1) {
            throw new Exception("下发卡建立长连接失败,错误码为" + hcNetSDK.NET_DVR_GetLastError());
        }

        HCNetSDK.NET_DVR_CARD_RECORD struCardRecord = new HCNetSDK.NET_DVR_CARD_RECORD();
        struCardRecord.read();
        struCardRecord.dwSize = struCardRecord.size();

        for (int i = 0; i < HCNetSDK.ACS_CARD_NO_LEN; i++) {
            struCardRecord.byCardNo[i] = 0;
        }
        byte[] cardBytes = cardNo.getBytes();
        System.arraycopy(cardBytes, 0, struCardRecord.byCardNo, 0, cardNo.length());

        struCardRecord.byCardType = 1; // 普通卡
        struCardRecord.byLeaderCard = 0; // 是否为首卡,0-否,1-是
        struCardRecord.byUserType = 0;
        struCardRecord.byDoorRight[0] = 1; // 门1有权限

        struCardRecord.struValid.byEnable = 1; // 卡有效期使能,下面是卡有效期从2000-1-1 11:11:11到2030-1-1 11:11:11

        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(start);
        struCardRecord.struValid.struBeginTime.wYear = (short) startCalendar.get(Calendar.YEAR); // 年
        struCardRecord.struValid.struBeginTime.byMonth = (byte) (startCalendar.get(Calendar.MONTH) + 1); // 月
        struCardRecord.struValid.struBeginTime.byDay = (byte) startCalendar.get(Calendar.DAY_OF_MONTH);
        struCardRecord.struValid.struBeginTime.byHour = (byte) startCalendar.get(Calendar.HOUR_OF_DAY); // 时
        struCardRecord.struValid.struBeginTime.byMinute = (byte) startCalendar.get(Calendar.MINUTE); // 分
        struCardRecord.struValid.struBeginTime.bySecond = (byte) startCalendar.get(Calendar.SECOND); // 秒

        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(end);
        struCardRecord.struValid.struEndTime.wYear = (short) endCalendar.get(Calendar.YEAR); // 年
        struCardRecord.struValid.struEndTime.byMonth = (byte) (endCalendar.get(Calendar.MONTH) + 1); // 月
        struCardRecord.struValid.struEndTime.byDay = (byte) endCalendar.get(Calendar.DAY_OF_MONTH);
        struCardRecord.struValid.struEndTime.byHour = (byte) endCalendar.get(Calendar.HOUR_OF_DAY); // 时
        struCardRecord.struValid.struEndTime.byMinute = (byte) endCalendar.get(Calendar.MINUTE); // 分
        struCardRecord.struValid.struEndTime.bySecond = (byte) endCalendar.get(Calendar.SECOND); // 秒


        struCardRecord.wCardRightPlan[0] = 1;// 卡计划模板1有效
        struCardRecord.dwEmployeeNo = empCode; // 工号

        byte[] strCardName = empName.getBytes(); // 姓名
        for (int i = 0; i < HCNetSDK.NAME_LEN; i++) {
            struCardRecord.byName[i] = 0;
        }
        System.arraycopy(strCardName, 0, struCardRecord.byName, 0, strCardName.length);

        struCardRecord.write();

        HCNetSDK.NET_DVR_CARD_STATUS struCardStatus = new HCNetSDK.NET_DVR_CARD_STATUS();
        struCardStatus.read();
        struCardStatus.dwSize = struCardStatus.size();
        struCardStatus.write();

        IntByReference pInt = new IntByReference(0);

        while (true) {
            int dwState = hcNetSDK.NET_DVR_SendWithRecvRemoteConfig(lHandle, struCardRecord.getPointer(),
                    struCardRecord.size(), struCardStatus.getPointer(), struCardStatus.size(), pInt);
            struCardStatus.read();
            if (dwState == -1) {
                log.error("下发卡NET_DVR_SendWithRecvRemoteConfig接口调用失败,错误码:" + hcNetSDK.NET_DVR_GetLastError());
                break;
            } else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_NEEDWAIT) {
                log.info("下发卡配置等待");
                try {
                    Thread.sleep(10);
                } catch (InterruptedException ignored) {
                }
            } else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_FAILED) {
                log.error("下发卡失败, 卡号: " + new String(struCardStatus.byCardNo).trim() + ", 错误码:"
                        + struCardStatus.dwErrorCode);
                break;
            } else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_EXCEPTION) {
                log.error("下发卡异常, 卡号: " + new String(struCardStatus.byCardNo).trim() + ", 错误码:"
                        + struCardStatus.dwErrorCode);
                break;
            } else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_SUCCESS) {
                if (struCardStatus.dwErrorCode != 0) {
                    log.error("下发卡成功,但是错误码" + struCardStatus.dwErrorCode + ", 卡号:"
                            + new String(struCardStatus.byCardNo).trim());
                } else {
                    log.info("下发卡成功, 卡号: " + new String(struCardStatus.byCardNo).trim() + ", 状态:"
                            + struCardStatus.byStatus);
                }
            } else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_FINISH) {
                log.info("下发卡完成");
                break;
            }
        }
        if (!hcNetSDK.NET_DVR_StopRemoteConfig(lHandle)) {
            int iErr = hcNetSDK.NET_DVR_GetLastError();
            log.warn("断开长连接失败,错误号:" + iErr);
        }

    }

    /**
     * 获取人脸信息
     * @param cardNo 卡号
     */
    public byte[] getFaceInfo(String cardNo) throws Exception {
        byte[] bytes = null;
        int iErr = 0;
        HCNetSDK.NET_DVR_FACE_COND faceCond = new HCNetSDK.NET_DVR_FACE_COND();
        faceCond.dwSize = faceCond.size();
        faceCond.byCardNo = cardNo.getBytes(); //人脸关联的卡号
        faceCond.dwFaceNum = 1;
        faceCond.write();

        Pointer lpInBuffer = faceCond.getPointer();
        int lHandle = hcNetSDK.NET_DVR_StartRemoteConfig(this.userId, HCNetSDK.NET_DVR_GET_FACE, lpInBuffer, faceCond.size(), null, null);
        if (lHandle < 0) {
            iErr = hcNetSDK.NET_DVR_GetLastError();
            throw new Exception("建立长连接失败,错误号:" + iErr);
        }
//        System.out.println("建立获取卡参数长连接成功!");

        HCNetSDK.NET_DVR_FACE_RECORD struFaceRecord = new HCNetSDK.NET_DVR_FACE_RECORD();
        struFaceRecord.read();
        struFaceRecord.dwSize = struFaceRecord.size();
        struFaceRecord.write();

        while (true) {
            int dwState = hcNetSDK.NET_DVR_GetNextRemoteConfig(lHandle, struFaceRecord.getPointer(),
                    struFaceRecord.size());
            struFaceRecord.read();
            if (dwState == -1) {
                log.error("NET_DVR_SendWithRecvRemoteConfig接口调用失败,错误码:" + hcNetSDK.NET_DVR_GetLastError());
                break;
            } else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_NEEDWAIT) {
                log.info("配置等待");
                try {
                    Thread.sleep(10);
                } catch (InterruptedException ignored) {
                }
            } else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_FAILED) {
                log.warn("获取人脸失败");
                break;
            } else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_EXCEPTION) {
                log.warn("获取人脸异常");
                break;
            } else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_SUCCESS) {
                // 将字节写入文件
                long offset = 0;
                ByteBuffer buffers = struFaceRecord.pFaceBuffer.getByteBuffer(offset, struFaceRecord.dwFaceLen);
                bytes = new byte[struFaceRecord.dwFaceLen];
                buffers.rewind();
                buffers.get(bytes);

            } else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_FINISH) {
                log.info("获取人脸完成");
                break;
            }
        }

        if (!hcNetSDK.NET_DVR_StopRemoteConfig(lHandle)) {
            iErr = hcNetSDK.NET_DVR_GetLastError();
            log.warn("断开长连接失败,错误号:" + iErr);
        }
        return bytes;
    }

    /**
     * 下发人脸
     * @param cardNo 卡号
     * @param faceBytes 人脸图片文件或流 转成的字节,不超过200KB
     */
    public void setFaceInfo(String cardNo, byte[] faceBytes) throws Exception {
        int iErr = 0;

        HCNetSDK.NET_DVR_FACE_COND struFaceCond = new HCNetSDK.NET_DVR_FACE_COND();
        struFaceCond.read();
        struFaceCond.dwSize = struFaceCond.size();
        struFaceCond.byCardNo = cardNo.getBytes();
        struFaceCond.dwFaceNum = 1; // 下发一张
        struFaceCond.dwEnableReaderNo = 1;// 人脸读卡器编号
        struFaceCond.write();

        int lHandle = hcNetSDK.NET_DVR_StartRemoteConfig(this.userId, HCNetSDK.NET_DVR_SET_FACE, struFaceCond.getPointer(), struFaceCond.size(), null, null);
        if (lHandle < 0) {
            iErr = hcNetSDK.NET_DVR_GetLastError();
            throw new Exception("建立长连接失败,错误号:" + iErr);
        }
//        System.out.println("建立设置卡参数长连接成功!");

        HCNetSDK.NET_DVR_FACE_RECORD struFaceInfo = new HCNetSDK.NET_DVR_FACE_RECORD(); //卡参数
        struFaceInfo.read();
        struFaceInfo.dwSize = struFaceInfo.size();
        struFaceInfo.byCardNo = cardNo.getBytes();

        ByteArrayInputStream picfile = new ByteArrayInputStream(faceBytes);
        int picdataLength = picfile.available();
        if (picdataLength > 200 * 1024) {
            throw new Exception("input file dataSize > 200kb");
        }
        HCNetSDK.BYTE_ARRAY ptrpicByte = new HCNetSDK.BYTE_ARRAY(picdataLength);
        picfile.read(ptrpicByte.byValue, 0, ptrpicByte.byValue.length);
        ptrpicByte.write();

        struFaceInfo.dwFaceLen = picdataLength;
        struFaceInfo.pFaceBuffer = ptrpicByte.getPointer();
        struFaceInfo.write();

        HCNetSDK.NET_DVR_FACE_STATUS struFaceStatus = new HCNetSDK.NET_DVR_FACE_STATUS();
        struFaceStatus.read();
        struFaceStatus.dwSize = struFaceStatus.size();
        struFaceStatus.write();

        while (true) {
            int dwState = hcNetSDK.NET_DVR_SendWithRecvRemoteConfig(lHandle, struFaceInfo.getPointer(),
                    struFaceInfo.size(), struFaceStatus.getPointer(), struFaceStatus.size(), new IntByReference(0));
            struFaceStatus.read();
            if (dwState == -1) {
                log.error("下发人脸 NET_DVR_SendWithRecvRemoteConfig接口调用失败,错误码:" + hcNetSDK.NET_DVR_GetLastError());
                break;
            } else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_NEEDWAIT) {
                log.info("下发人脸 配置等待");
                try {
                    Thread.sleep(10);
                } catch (InterruptedException ignored) {
                }
            } else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_FAILED) {
                log.error("下发人脸失败, 卡号: " + new String(struFaceStatus.byCardNo).trim() + ", 错误码:"
                        + hcNetSDK.NET_DVR_GetLastError());
                break;
            } else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_EXCEPTION) {

                log.error("下发人脸异常, 卡号: " + new String(struFaceStatus.byCardNo).trim() + ", 错误码:"
                        + hcNetSDK.NET_DVR_GetLastError());
                break;
            } else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_SUCCESS) {
                if (struFaceStatus.byRecvStatus != 1) {
                    log.error("下发人脸失败,人脸读卡器状态:" + struFaceStatus.byRecvStatus + ", 卡号:"
                            + new String(struFaceStatus.byCardNo).trim());
                    break;
                } else {
                    log.info("下发人脸成功, 卡号: " + new String(struFaceStatus.byCardNo).trim() + ", 状态:"
                            + struFaceStatus.byRecvStatus);
                }
            } else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_FINISH) {
                log.info("下发人脸完成,卡号: " + new String(struFaceStatus.byCardNo).trim());
                break;
            }
        }

        if (!hcNetSDK.NET_DVR_StopRemoteConfig(lHandle)) {
            iErr = hcNetSDK.NET_DVR_GetLastError();
            log.warn("断开长连接失败,错误号:" + iErr);
        }
    }

    //报警布防
//    public void setupAlarmChan(int lUserID) {
//        //初始化SDK
//        boolean status = hcNetSDK.NET_DVR_Init();
//        if(status!=true) {
//            System.out.println("初始化失败:"+hcNetSDK.NET_DVR_GetCardLastError_Card());
//        }else {
//            System.out.println("初始化成功!");
//        }
//        HCNetSDK.NET_DVR_SETUPALARM_PARAM m_strAlarmInfo = new HCNetSDK.NET_DVR_SETUPALARM_PARAM();
//        m_strAlarmInfo.dwSize=m_strAlarmInfo.size();
//        m_strAlarmInfo.byLevel=1;//智能交通布防优先级:0- 一等级(高),1- 二等级(中),2- 三等级(低)
//        m_strAlarmInfo.byAlarmInfoType=1;//智能交通报警信息上传类型:0- 老报警信息(NET_DVR_PLATE_RESULT),1- 新报警信息(NET_ITS_PLATE_RESULT)
//        m_strAlarmInfo.byDeployType =0; //布防类型(仅针对门禁主机、人证设备):0-客户端布防(会断网续传),1-实时布防(只上传实时数据)
//        m_strAlarmInfo.write();
//        int lAlarmHandle = hcNetSDK.NET_DVR_SetupAlarmChan_V41(lUserID, m_strAlarmInfo);
//        if (lAlarmHandle == -1){
//            System.out.println("布防失败,错误号:" +  hcNetSDK.NET_DVR_GetLastError());
//        }
//        else{
//            System.out.println("布防成功");
//        }
//    }

    /**
     * 根据卡号删除人脸
     */
    public void delFace(String cardNo) throws Exception {
        int iErr = 0;
        //删除人脸数据
        HCNetSDK.NET_DVR_FACE_PARAM_CTRL m_struFaceDel = new HCNetSDK.NET_DVR_FACE_PARAM_CTRL();
        m_struFaceDel.dwSize = m_struFaceDel.size();
        m_struFaceDel.byMode = 0; //删除方式:0- 按卡号方式删除,1- 按读卡器删除
        m_struFaceDel.struProcessMode.setType(HCNetSDK.NET_DVR_FACE_PARAM_BYCARD.class);
        m_struFaceDel.struProcessMode.struByCard.byCardNo = cardNo.getBytes();//需要删除人脸关联的卡号
        m_struFaceDel.struProcessMode.struByCard.byEnableCardReader[0] = 1; //读卡器
        m_struFaceDel.struProcessMode.struByCard.byFaceID[0] = 1; //人脸ID
        m_struFaceDel.write();

        boolean lRemoteCtrl = hcNetSDK.NET_DVR_RemoteControl(this.userId, HCNetSDK.NET_DVR_DEL_FACE_PARAM_CFG, m_struFaceDel.getPointer(), m_struFaceDel.size());
        if (!lRemoteCtrl) {
            iErr = hcNetSDK.NET_DVR_GetLastError();
            throw new Exception("删除人脸图片失败,错误号:" + iErr);
        }
    }

    /**
     * 删除卡号 删除卡号前需要先删除人脸
     * @param cardNo 卡号
     */
    public void delCardInfo(String cardNo) throws Exception {
        HCNetSDK.NET_DVR_CARD_COND struCardCond = new HCNetSDK.NET_DVR_CARD_COND();
        struCardCond.read();
        struCardCond.dwSize = struCardCond.size();
        struCardCond.dwCardNum = 1;  //下发一张
        struCardCond.write();
        Pointer ptrStruCond = struCardCond.getPointer();

        int lHandler = hcNetSDK.NET_DVR_StartRemoteConfig(this.userId, HCNetSDK.NET_DVR_DEL_CARD, ptrStruCond, struCardCond.size(), null, null);
        if (lHandler == -1) {
            throw new Exception("建立删除卡长连接失败,错误码为" + hcNetSDK.NET_DVR_GetLastError());
        }
//        else {
//            System.out.println("建立删除卡长连接成功!");
//        }

        HCNetSDK.NET_DVR_CARD_SEND_DATA struCardData = new HCNetSDK.NET_DVR_CARD_SEND_DATA();
        struCardData.read();
        struCardData.dwSize = struCardData.size();

        for (int i = 0; i < HCNetSDK.ACS_CARD_NO_LEN; i++) {
            struCardData.byCardNo[i] = 0;
        }
        System.arraycopy(cardNo.getBytes(), 0, struCardData.byCardNo, 0, cardNo.length());
        struCardData.write();

        HCNetSDK.NET_DVR_CARD_STATUS struCardStatus = new HCNetSDK.NET_DVR_CARD_STATUS();
        struCardStatus.read();
        struCardStatus.dwSize = struCardStatus.size();
        struCardStatus.write();

        IntByReference pInt = new IntByReference(0);

        while (true) {
            int dwState = hcNetSDK.NET_DVR_SendWithRecvRemoteConfig(lHandler, struCardData.getPointer(), struCardData.size(), struCardStatus.getPointer(), struCardStatus.size(), pInt);
            struCardStatus.read();
            if (dwState == -1) {
                log.error("NET_DVR_SendWithRecvRemoteConfig接口调用失败,错误码:" + hcNetSDK.NET_DVR_GetLastError());
                break;
            } else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_NEEDWAIT) {
                log.info("配置等待");
                try {
                    Thread.sleep(10);
                } catch (InterruptedException ignored) {
                }
            } else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_FAILED) {
                log.error("删除卡失败, 卡号: " + new String(struCardStatus.byCardNo).trim() + ", 错误码:" + struCardStatus.dwErrorCode);
                break;
            } else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_EXCEPTION) {
                log.error("删除卡异常, 卡号: " + new String(struCardStatus.byCardNo).trim() + ", 错误码:" + struCardStatus.dwErrorCode);
                break;
            } else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_SUCCESS) {
                if (struCardStatus.dwErrorCode != 0) {
                    log.error("删除卡成功,但是错误码" + struCardStatus.dwErrorCode + ", 卡号:" + new String(struCardStatus.byCardNo).trim());
                } else {
                    log.info("删除卡成功, 卡号: " + new String(struCardStatus.byCardNo).trim() + ", 状态:" + struCardStatus.byStatus);
                }
            } else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_FINISH) {
                log.info("删除卡完成");
                break;
            }

        }

        if (!hcNetSDK.NET_DVR_StopRemoteConfig(lHandler)) {
            log.warn("NET_DVR_StopRemoteConfig接口调用失败,错误码:" + hcNetSDK.NET_DVR_GetLastError());
        }
    }

    /**
     * 远程控门
     * @param control 0-关门,1-开门,2-常开,3-常关,4-恢复(恢复为普通状态)
     */
    public void controlGate(int control) throws Exception {
        int iErr = 0;
        //lGatewayIndex门编号(-1 表示对所有门进行操作;明眸仅支持一个门,故门编号填 1 即可)
//       dwStatic 0-关门,1-开门,2-常开,3-常关,4-恢复(恢复为普通状态)
        boolean lRemoteCtrl = hcNetSDK.NET_DVR_ControlGateway(this.userId, 1, control);
        if (!lRemoteCtrl) {
            iErr = hcNetSDK.NET_DVR_GetLastError();
            throw new Exception("远程控门失败,错误号:" + iErr);
        }
//        else {
//            System.out.println("NET_DVR_ControlGateway成功!");
//        }
    }
}

使用举例

HCNetDeviceUtil hcNetDeviceUtil = new HCNetDeviceUtil();
String ip = "192.168.1.10";
int port = 8000;
String username = "admin";
Sting password = "111";
Date start = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-06-01 00:00:00");
Date end = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-07-01 00:00:00");
try {
    hcNetDeviceUtil.loginDevice(ip, port, username, password);
    JSONArray events = hcNetDeviceUtil.getDeviceEvents(start, end);
    hcNetDeviceUtil.logout();
} catch (Exception e) {
    e.printStackTrace();
}

admin

这个人很懒,什么都没留下

文章评论

您需要 登录 之后才可以评论