海康SDK对接 明眸门禁设备-初始化SDK和登录设备

对接海康明眸门禁设备,初始化SDK


/**
 * @author czm
 * @date 2023/2/15
 */
public interface HCNetSDK extends Library {

    public static HCNetSDK getInstance(String sdkPath) {
//      sdkPath sdk 目录路径
        System.out.println("sdk_path " + sdkPath);
        HCNetSDK INSTANCE = null;
        if (osSelect.isWindows()) {
            INSTANCE = (HCNetSDK) Native.loadLibrary(sdkPath, HCNetSDK.class);
        }
        if (osSelect.isLinux()) {
            INSTANCE = (HCNetSDK) Native.loadLibrary(sdkPath, HCNetSDK.class);
        }
        return INSTANCE;
    }

    // 初始化SDK,调用其他SDK函数的前提
    boolean NET_DVR_Init();

    boolean NET_DVR_SetSDKInitCfg(int enumType, Pointer lpInBuff);

    //启用日志文件写入接口
    boolean NET_DVR_SetLogToFile(int bLogEnable, String strLogDir, boolean bAutoDel);

    int NET_DVR_GetLastError();

    int NET_DVR_Login_V40(NET_DVR_USER_LOGIN_INFO pLoginInfo, NET_DVR_DEVICEINFO_V40 lpDeviceInfo);

    boolean NET_DVR_Logout(int lUserID);

    int NET_DVR_StartRemoteConfig(int lUserID, int dwCommand, Pointer lpInBuffer, int dwInBufferLen, fRemoteConfigCallback cbStateCallback, Pointer pUserData);

    //释放资源
    boolean NET_DVR_Cleanup();

    //逐个下发
    int NET_DVR_SendWithRecvRemoteConfig(int lHandle, Pointer lpInBuff, int dwInBuffSize, Pointer lpOutBuff, int dwOutBuffSize, IntByReference dwOutDataLen);

    //关闭长连接 释放资源
    boolean NET_DVR_StopRemoteConfig(int lHandle);

    boolean NET_DVR_STDXMLConfig(int lUserID, NET_DVR_XML_CONFIG_INPUT lpInputParam, NET_DVR_XML_CONFIG_OUTPUT lpOutputParam);

    int NET_DVR_GetNextRemoteConfig(int lHandle, Pointer lpOutBuff, int dwOutBuffSize);

    boolean NET_DVR_GetDeviceConfig(int lUserID, int dwCommand, int dwCount, Pointer lpInBuffer, int dwInBufferSize, Pointer lpStatusList, Pointer lpOutBuffer, int dwOutBufferSize);

    boolean NET_DVR_SetDeviceConfig(int lUserID, int dwCommand, int dwCount, Pointer lpInBuffer, int dwInBufferSize, Pointer lpStatusList, Pointer lpInParamBuffer, int dwInParamBufferSize);

    //判断是否在线
    boolean NET_DVR_RemoteControl(int lUserID, int dwCommand, Pointer lpInBuffer, int dwInBufferSize);

    int NET_DVR_SetupAlarmChan_V41(int lUserID, NET_DVR_SETUPALARM_PARAM lpSetupParam);

    boolean NET_DVR_CloseAlarmChan(int lAlarmHandle);

    //撤销布防
    boolean NET_DVR_CloseAlarmChan_V30(int lAlarmHandle);

    boolean NET_DVR_StopListen_V30(int lListenHandle);

    boolean NET_DVR_SetSDKLocalCfg(int enumType, Pointer lpInBuff);

    int NET_DVR_StartListen_V30(String sLocalIP, short wLocalPort, FMSGCallBack_V31 DataCallBack, Pointer pUserData);

    //注册回调函数,接收设备报警消息等。
    boolean NET_DVR_SetDVRMessageCallBack_V31(FMSGCallBack_V31 fMessageCallBack, Pointer pUser);

    int NET_DVR_SetupAlarmChan_V50(int iUserID, NET_DVR_SETUPALARM_PARAM_V50 lpSetupParam, Pointer pSub, int dwSubSize);

    public interface FMSGCallBack_V31 extends Callback {
        public boolean invoke(int lCommand, NET_DVR_ALARMER pAlarmer, Pointer pAlarmInfo, int dwBufLen, Pointer pUser);
    }

    public interface MSGCallBack_V31 extends Callback {
        void invoke(int lCommand, NET_DVR_ALARMER pAlarmer, Pointer pAlarmInfo, int dwBufLen, Pointer pUser);

    }

    public interface FLoginResultCallBack extends Callback {
        public int invoke(int lUserID, int dwResult, NET_DVR_DEVICEINFO_V30 lpDeviceinfo, Pointer pUser);
    }

    public interface fRemoteConfigCallback extends Callback {
        public int invoke(int lUserID, int dwResult, NET_DVR_DEVICEINFO_V30 lpDeviceinfo, Pointer pUser);
    }

}

初始化

public class IotHikvisionDeviceApiService implements IotPanelApiService {

    // 接口的实例,通过接口实例调用外部dll/so的函数
    private static HCNetSDK hCNetSDK = null;

    private static HikvisionAlarmCallBack fMSFCallBack_V31 = null;

    //系统配置变量 获取SDK 文件目录
    private static String HK_SDK_PATH = "HK_SDK_PATH";
	
    static {
        try {
            // String sdkPath = "D:\\sdk\\winHikvision\\";
            String sdkFilePath = null;
            if (osSelect.isWindows()) {
                sdkFilePath = HK_SDK_PATH + File.separator + "HCNetSDK.dll";
            }
            if (osSelect.isLinux()) {
                sdkFilePath = HK_SDK_PATH + File.separator + "libhcnetsdk.so";
            }

            //判断文件是否存在
            if (FileUtil.exist(sdkFilePath)) {
                hCNetSDK = HCNetSDK.getInstance(sdkFilePath);
                if(ObjectUtil.isNull(hCNetSDK)){
                    log.warn("海康SDK初始化失败");
                }
                if (osSelect.isLinux()) {
                    loadSdk(sdkPath);
                }
                /******************************** 初始化 ********************************/
                log.info("start NET_DVR_Init");
                boolean initSuc = hCNetSDK.NET_DVR_Init();
                if (!initSuc) {
                    log.error("HikvisionVideoService NET_DVR_Init fail");
                }
                /**加载日志*/
                hCNetSDK.NET_DVR_SetLogToFile(3, FileUtil.getTmpDirPath() + File.separator + "sdklog", false);
            } else {
                log.warn("海康SDK文件目录不存在");
            }
        } catch (Exception ex) {
            log.error("Error:{} ", ex.getMessage());
        }
    }

加载文件

 public static void loadSdk(String sdkPath) {
        BYTE_ARRAY ptrByteArray1 = new BYTE_ARRAY(256);
        BYTE_ARRAY ptrByteArray2 = new BYTE_ARRAY(256);
        //这里是库的绝对路径,请根据实际情况修改,注意改路径必须有访问权限
        String strPath1 = sdkPath + "libcrypto.so.1.1";
        String strPath2 = sdkPath + "libssl.so.1.1";

        System.arraycopy(strPath1.getBytes(), 0, ptrByteArray1.byValue, 0, strPath1.length());
        ptrByteArray1.write();
        hCNetSDK.NET_DVR_SetSDKInitCfg(3, ptrByteArray1.getPointer());

        System.arraycopy(strPath2.getBytes(), 0, ptrByteArray2.byValue, 0, strPath2.length());
        ptrByteArray2.write();
        hCNetSDK.NET_DVR_SetSDKInitCfg(4, ptrByteArray2.getPointer());

        String strPathCom = sdkPath;
        NET_DVR_LOCAL_SDK_PATH struComPath = new NET_DVR_LOCAL_SDK_PATH();
        System.arraycopy(strPathCom.getBytes(), 0, struComPath.sPath, 0, strPathCom.length());
        struComPath.write();
        hCNetSDK.NET_DVR_SetSDKInitCfg(2, struComPath.getPointer());
    }

建立长连接

    /**
     * 建立长连接
     */
    private int getLongConnt(String strInBuffer, int HCode, int lUserID) {
        //创建长连接
        BYTE_ARRAY ptrByteArray = new BYTE_ARRAY(1024);    //数组
//        strInBuffer = "PUT /ISAPI/Intelligent/FDLib/FDSetUp?format=json";
        System.arraycopy(strInBuffer.getBytes(), 0, ptrByteArray.byValue, 0, strInBuffer.length());//字符串拷贝到数组中
        ptrByteArray.write();
        return hCNetSDK.NET_DVR_StartRemoteConfig(lUserID, HCode, ptrByteArray.getPointer(), strInBuffer.length(), null, null);
    }

登录

    /**
     * 登录
     *
     * @param m_sDeviceIP 设备ip地址
     * @param wPort       端口号,设备网络SDK登录默认端口8000
     * @param m_sUsername 用户名
     * @param m_sPassword 密码
     */
    public int login_V40(String m_sDeviceIP, short wPort, String m_sUsername, String m_sPassword) {

        NET_DVR_USER_LOGIN_INFO pLoginInfo = new NET_DVR_USER_LOGIN_INFO();
        pLoginInfo.sDeviceAddress = new byte[IotHikNetConstant.NET_DVR_DEV_ADDRESS_MAX_LEN];
        System.arraycopy(m_sDeviceIP.getBytes(), 0, pLoginInfo.sDeviceAddress, 0, m_sDeviceIP.length());
        pLoginInfo.wPort = wPort;
        pLoginInfo.sUserName = new byte[IotHikNetConstant.NET_DVR_LOGIN_USERNAME_MAX_LEN];
        System.arraycopy(m_sUsername.getBytes(), 0, pLoginInfo.sUserName, 0, m_sUsername.length());
        pLoginInfo.sPassword = new byte[IotHikNetConstant.NET_DVR_LOGIN_PASSWD_MAX_LEN];
        System.arraycopy(m_sPassword.getBytes(), 0, pLoginInfo.sPassword, 0, m_sPassword.length());
        // 是否异步登录:false- 否,true- 是
        pLoginInfo.bUseAsynLogin = false;
        // write()调用后数据才写入到内存中
        pLoginInfo.write();
        NET_DVR_DEVICEINFO_V40 lpDeviceInfo = new NET_DVR_DEVICEINFO_V40();
//        lpDeviceInfo.byLoginMode = 0;
        int lUserID = -1;//用户句柄 实现对设备登录
        try {
            lUserID = hCNetSDK.NET_DVR_Login_V40(pLoginInfo, lpDeviceInfo);
            if (lUserID == -1) {
                log.warn("登录失败,错误码为 : {}", hCNetSDK.NET_DVR_GetLastError());
            } else {
                log.info("登录成功!ip: {}", m_sDeviceIP);
                // read()后,结构体中才有对应的数据
                lpDeviceInfo.read();
            }
        } catch (Exception e) {
            log.error("海康SDK 登录异常,请检查网络和SDK路径是否正确", e);
        } finally {
            pLoginInfo.clear();
            lpDeviceInfo.clear();
        }
        return lUserID;
    }

设备注销 SDK释放

    public void logout(int lUserID) {

        if (lUserID >= 0) {
            if (!hCNetSDK.NET_DVR_Logout(lUserID)) {
                log.warn("注销失败,错误码为: {}", hCNetSDK.NET_DVR_GetLastError());
            }
            log.info("注销成功");
            //hCNetSDK.NET_DVR_Cleanup(); //释放SDK
            return;
        } else {
            log.warn("设备未登录");
            hCNetSDK.NET_DVR_Cleanup();
        }
//        if (hCNetSDK.NET_DVR_CloseAlarmChan(lAlarmHandle)) {
//            System.out.println("撤防成功");
//        }
    }
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值