基于海康SDK的web系统开发「终于解决」

基于海康SDK的web系统开发「终于解决」在网上搜索了大量资料,都没有找到适合自己的。网上大部分都是对海康SDK的讲解,或者是图形化界面的运行。涉及到web开发时,也是通过WebComponents.exe进行开发,与我所需的功能均不一致,所以还是打算自己尝试并最终上线使用。这里进行一下整理,希望对其他人也能有所帮助。首先步骤都是一样的,到官网下载自己需要的SDK开发包,官网地址如下,自取。https://www.hikvision….

大家好,欢迎来到IT知识分享网。

在网上搜索了大量资料,都没有找到适合自己的。网上大部分都是对海康SDK的讲解,或者是图形化界面的运行。涉及到web开发时,也是通过WebComponents.exe进行开发,与我所需的功能均不一致,所以还是打算自己尝试并最终上线使用。这里进行一下整理,希望对其他人也能有所帮助。
首先步骤都是一样的,到官网下载自己需要的SDK开发包,官网地址如下,自取。
https://www.hikvision.com/cn/download_61.html
我这里用到的是linux64,将/lib和/lib/HCNetSDKCom下自己需要的.so库文件到拷贝到linux /usr/lib/目录下,如果不知道哪些是自己需要的,全丢进去就好。
SDK开发包结构
在官方的LinuxJavaDemo中,提供了两个jar包,自行导入并build path即可。由于我的项目通过maven进行管理,所以需要导入依赖。
SDK开发包结构
jna.jar在仓库中可以搜索到,选择sun公司的最新版本导入即可。examples.jar仓库中是搜不到的,所以要加到自己的仓库中并增加依赖,这一步需要自行完成。
maven仓库jna依赖
下面才开始进入开发阶段。官方的demo中有一个至关重要的文件,对于初次进行海康SDK二次开发的人来说,是非常有帮助的。HCNetSDK.java中涵盖了海康大部分的SDK及相应的Structure,后续的开发主要基于这些接口和Structure来完成。
SDK开发包javademo
同时需要注意的是,要弄清楚自己的开发平台,当基于linux进行开发时,该类需要继承Library来对.so库文件进行加载,而当基于windows进行开发时要继承StdCallLibrary对.dll进行加载,否则可能会出现部分SDK不可用或内存字段赋值错误的情况。Native.loadLibrary用来指定库文件路径及转换之后的类。windows下可以指定全路径。
linux下继承Library
windows下继承StdCallLibrary
下面直接放上我自己写的sdk文件,由于我的项目中只需要实现视频的查找、下载等相关功能,所以代码中的接口及Structure肯定是没有demo中全的。只是为了给大家提供个思路和方向,毕竟coder最擅长的就是根据API码砖了。这里有一点需要注意的是,在实际的项目开发中,demo中HCNetSDK.java是不能拿来直接用的,编译器会报错,根据报错提示,需要将为override的方法补全。主要涉及到的方法是父类Structure中的public List getFieldOrder();方法,该方法是为了以正确的顺序返回此Structure的字段名称,因此override此方法时,需要根据当前Structure的字段来完成,否则会报错。

import com.sun.jna.*;
import com.sun.jna.examples.win32.W32API.HWND;
import com.sun.jna.ptr.IntByReference;

import java.util.Arrays;
import java.util.List;

/**
 * SDK接口说明
 *
 * @author DiCaprito
 */
public interface HcNetSdk extends Library {

    HcNetSdk INSTANCE = (HcNetSdk) Native.loadLibrary(
            "hcnetsdk",
            HcNetSdk.class);

    /**
     * 正在查找文件
     */
    public static final int NET_DVR_IS_FINDING = 1002;
    /**
     * 用户名长度
     */
    public static final int NAME_LEN = 32;
    /**
     * 密码长度
     */
    public static final int PWD_LEN = 16;
    /**
     * 序列号长度
     */
    public static final int SERIAL_NUM_LEN = 48;
    /**
     * 最大32个模拟通道
     */
    public static final int MAX_ANALOG_CHAN_NUM = 32;
    /**
     * 允许接入的最大IP设备数
     */
    public static final int MAX_IP_DEVICE = 32;
    /**
     * 允许加入的最多IP通道数
     */
    public static final int MAX_IP_CHANNEL = 32;
    /**
     * 最大支持的通道数 最大模拟加上最大IP支持64
     */
    public static final int MAX_CHAN_NUM_V30 = (MAX_ANALOG_CHAN_NUM + MAX_IP_CHANNEL);
    /**
     * 开始播放
     */
    public static final int NET_DVR_PLAY_START = 1;
    /**
     * 获取文件回放的进度
     */
    public static final int NET_DVR_PLAY_GET_POS = 13;
    /**
     * 获取IP接入配置信息
     */
    public static final int NET_DVR_GET_IPPARACFG = 1048;

    /**
     * 用户交互时异常
     */
    public static final int EXCEPTION_EXCHANGE = 0x8000;

    /**
     * 校时结构参数
     */
    public static class NET_DVR_TIME extends Structure {
        /**
         * 年
         */
        public int dwYear;
        /**
         * 月
         */
        public int dwMonth;
        /**
         * 日
         */
        public int dwDay;
        /**
         * 时
         */
        public int dwHour;
        /**
         * 分
         */
        public int dwMinute;
        /**
         * 秒
         */
        public int dwSecond;

        @Override
        public String toString() {
            return "NET_DVR_TIME.dwYear: " + dwYear + "\n" + "NET_DVR_TIME.dwMonth: \n" + dwMonth + "\n" + "NET_DVR_TIME.dwDay: \n" + dwDay + "\n" + "NET_DVR_TIME.dwHour: \n" + dwHour + "\n" + "NET_DVR_TIME.dwMinute: \n" + dwMinute + "\n" + "NET_DVR_TIME.dwSecond: \n" + dwSecond;
        }

        public String toStringTime() {
            return String.format("%02d/%02d/%02d%02d:%02d:%02d", dwYear, dwMonth, dwDay, dwHour, dwMinute, dwSecond);
        }

        public String toStringTitle() {
            return String.format("Time%02d%02d%02d%02d%02d%02d", dwYear, dwMonth, dwDay, dwHour, dwMinute, dwSecond);
        }

        @Override
        public List<String> getFieldOrder() {
            return Arrays.asList("dwYear", "dwMonth", "dwDay", "dwHour", "dwMinute", "dwSecond");
        }
    }

    /**
     * ip结构体
     */
    public static class NET_DVR_IPADDR extends Structure {
        public byte[] sIpV4 = new byte[16];
        public byte[] byRes = new byte[128];

        @Override
        public String toString() {
            return "NET_DVR_IPADDR.sIpV4: " + new String(sIpV4) + "\n" + "NET_DVR_IPADDR.byRes: " + new String(byRes) + "\n";
        }

        @Override
        public List<String> getFieldOrder() {
            return Arrays.asList("sIpV4", "byRes");
        }
    }

    /**
     * IP设备结构
     */
    public static class NET_DVR_IPDEVINFO extends Structure {
        /**
         * 该IP设备是否启用
         */
        public int dwEnable;
        /**
         * 用户名
         */
        public byte[] sUserName = new byte[NAME_LEN];
        /**
         * 密码
         */
        public byte[] sPassword = new byte[PWD_LEN];
        /**
         * IP地址
         */
        public NET_DVR_IPADDR struIP = new NET_DVR_IPADDR();
        /**
         * 端口号
         */
        public short wDVRPort;
        /**
         * 保留
         */
        public byte[] byres = new byte[34];

        @Override
        public String toString() {
            return "NET_DVR_IPDEVINFO{" +
                    "dwEnable=" + dwEnable +
                    ", sUserName=" + Arrays.toString(sUserName) +
                    ", sPassword=" + Arrays.toString(sPassword) +
                    ", struIP=" + struIP +
                    ", wDVRPort=" + wDVRPort +
                    ", byres=" + Arrays.toString(byres) +
                    '}';
        }

        @Override
        public List<String> getFieldOrder() {
            return Arrays.asList("dwEnable", "sUserName", "sPassword", "struIP", "wDVRPort", "byres");
        }
    }

    /**
     * IP通道匹配参数
     */
    public static class NET_DVR_IPCHANINFO extends Structure {
        /**
         * 该通道是否启用
         */
        public byte byEnable;
        /**
         * IP设备ID 取值1- MAX_IP_DEVICE
         */
        public byte byIPID;
        /**
         * 通道号
         */
        public byte byChannel;
        /**
         * 保留
         */
        public byte[] byres = new byte[33];

        @Override
        public String toString() {
            return "NET_DVR_IPCHANINFO{" +
                    "byEnable=" + byEnable +
                    ", byIPID=" + byIPID +
                    ", byChannel=" + byChannel +
                    ", byres=" + Arrays.toString(byres) +
                    '}';
        }

        @Override
        public List<String> getFieldOrder() {
            return Arrays.asList("byEnable", "byIPID", "byChannel", "byres");
        }
    }

    /**
     * IP接入配置结构
     */
    public static class NET_DVR_IPPARACFG extends Structure {
        /**
         * 结构大小
         */
        public int dwSize;
        /**
         * IP设备
         */
        public NET_DVR_IPDEVINFO[] struIPDevInfo = new NET_DVR_IPDEVINFO[MAX_IP_DEVICE];
        /**
         * 模拟通道是否启用,从低到高表示1-32通道,0表示无效 1有效
         */
        public byte[] byAnalogChanEnable = new byte[MAX_ANALOG_CHAN_NUM];
        /**
         * IP通道
         */
        public NET_DVR_IPCHANINFO[] struIPChanInfo = new NET_DVR_IPCHANINFO[MAX_IP_CHANNEL];

        @Override
        public String toString() {
            return "NET_DVR_IPPARACFG{" +
                    "dwSize=" + dwSize +
                    ", struIPDevInfo=" + Arrays.toString(struIPDevInfo) +
                    ", byAnalogChanEnable=" + Arrays.toString(byAnalogChanEnable) +
                    ", struIPChanInfo=" + Arrays.toString(struIPChanInfo) +
                    '}';
        }

        @Override
        public List<String> getFieldOrder() {
            return Arrays.asList("dwSize", "struIPDevInfo", "byAnalogChanEnable", "struIPChanInfo");
        }
    }

    /**
     * NET_DVR_Login_V30()参数结构
     */
    public static class NET_DVR_DEVICEINFO_V30 extends Structure {
        /**
         * 序列号
         */
        public byte[] sSerialNumber = new byte[SERIAL_NUM_LEN];
        /**
         * 报警输入个数
         */
        public byte byAlarmInPortNum;
        /**
         * 报警输出个数
         */
        public byte byAlarmOutPortNum;
        /**
         * 硬盘个数
         */
        public byte byDiskNum;
        /**
         * 设备类型, 1:DVR 2:ATM DVR 3:DVS ......
         */
        public byte byDVRType;
        /**
         * 模拟通道个数
         */
        public byte byChanNum;
        /**
         * 起始通道号,例如DVS-1,DVR - 1
         */
        public byte byStartChan;
        /**
         * 语音通道数
         */
        public byte byAudioChanNum;
        /**
         * 最大数字通道个数
         */
        public byte byIPChanNum;
        public byte byZeroChanNum;
        public byte byMainProto;
        public byte bySubProto;
        public byte bySupport;
        public byte bySupport1;
        public byte bySupport2;
        public int wDevType;
        public byte bySupport3;
        public byte byMultiStreamProto;
        public byte byStartDChan;
        public byte byStartDTalkChan;
        public byte byHighDChanNum;
        public byte bySupport4;
        public byte byLanguageType;
        public byte byVoiceInChanNum;
        public byte byStartVoiceInChanNo;
        public byte[] byRes3 = new byte[2];
        public byte byMirrorChanNum;
        public int wStartMirrorChanNo;
        public byte[] byRes2 = new byte[2];

        @Override
        public String toString() {
            return "NET_DVR_DEVICEINFO_V30{" +
                    "sSerialNumber=" + Arrays.toString(sSerialNumber) +
                    ", byAlarmInPortNum=" + byAlarmInPortNum +
                    ", byAlarmOutPortNum=" + byAlarmOutPortNum +
                    ", byDiskNum=" + byDiskNum +
                    ", byDVRType=" + byDVRType +
                    ", byChanNum=" + byChanNum +
                    ", byStartChan=" + byStartChan +
                    ", byAudioChanNum=" + byAudioChanNum +
                    ", byIPChanNum=" + byIPChanNum +
                    ", byZeroChanNum=" + byZeroChanNum +
                    ", byMainProto=" + byMainProto +
                    ", bySubProto=" + bySubProto +
                    ", bySupport=" + bySupport +
                    ", bySupport1=" + bySupport1 +
                    ", bySupport2=" + bySupport2 +
                    ", wDevType=" + wDevType +
                    ", bySupport3=" + bySupport3 +
                    ", byMultiStreamProto=" + byMultiStreamProto +
                    ", byStartDChan=" + byStartDChan +
                    ", byStartDTalkChan=" + byStartDTalkChan +
                    ", byHighDChanNum=" + byHighDChanNum +
                    ", bySupport4=" + bySupport4 +
                    ", byLanguageType=" + byLanguageType +
                    ", byVoiceInChanNum=" + byVoiceInChanNum +
                    ", byStartVoiceInChanNo=" + byStartVoiceInChanNo +
                    ", byRes3=" + Arrays.toString(byRes3) +
                    ", byMirrorChanNum=" + byMirrorChanNum +
                    ", wStartMirrorChanNo=" + wStartMirrorChanNo +
                    ", byRes2=" + Arrays.toString(byRes2) +
                    '}';
        }

        @Override
        public List<String> getFieldOrder() {
            return Arrays.asList("sSerialNumber", "byAlarmInPortNum", "byAlarmOutPortNum", "byDiskNum", "byDVRType",
                    "byChanNum", "byStartChan", "byAudioChanNum", "byIPChanNum", "byZeroChanNum", "byMainProto",
                    "bySubProto", "bySupport", "bySupport1", "bySupport2", "wDevType", "bySupport3", "byMultiStreamProto",
                    "byStartDChan", "byStartDTalkChan", "byHighDChanNum", "bySupport4", "byLanguageType",
                    "byVoiceInChanNum", "byStartVoiceInChanNo", "byRes3", "byMirrorChanNum", "wStartMirrorChanNo",
                    "byRes2");
        }
    }

    /**
     * NET_DVR_Login_V40()参数
     */
    public static class NET_DVR_DEVICEINFO_V40 extends Structure {
        public NET_DVR_DEVICEINFO_V30 struDeviceV30 = new NET_DVR_DEVICEINFO_V30();
        public byte bySupportLock;
        public byte byRetryLoginTime;
        public byte byPasswordLevel;
        public byte byProxyType;
        public byte dwSurplusLockTime;
        public byte byCharEncodeType;
        public byte bySupportDev5;
        public byte byLoginMode;
        public byte[] byRes2 = new byte[253];

        @Override
        public String toString() {
            return "NET_DVR_DEVICEINFO_V40{" +
                    "struDeviceV30=" + struDeviceV30 +
                    ", bySupportLock=" + bySupportLock +
                    ", byRetryLoginTime=" + byRetryLoginTime +
                    ", byPasswordLevel=" + byPasswordLevel +
                    ", byProxyType=" + byProxyType +
                    ", dwSurplusLockTime=" + dwSurplusLockTime +
                    ", byCharEncodeType=" + byCharEncodeType +
                    ", bySupportDev5=" + bySupportDev5 +
                    ", byLoginMode=" + byLoginMode +
                    ", byRes2=" + Arrays.toString(byRes2) +
                    '}';
        }

        @Override
        public List<String> getFieldOrder() {
            return Arrays.asList("struDeviceV30", "bySupportLock", "byRetryLoginTime", "byPasswordLevel",
                    "byProxyType", "dwSurplusLockTime", "byCharEncodeType", "bySupportDev5", "byLoginMode", "byRes2");
        }
    }

    /**
     * 录象文件参数
     */
    public static class NET_DVR_FINDDATA_V30 extends Structure {
        /**
         * 文件名
         */
        public byte[] sFileName = new byte[100];
        /**
         * 文件的开始时间
         */
        public NET_DVR_TIME struStartTime = new NET_DVR_TIME();
        /**
         * 文件的结束时间
         */
        public NET_DVR_TIME struStopTime = new NET_DVR_TIME();
        /**
         * 文件的大小
         */
        public int dwFileSize;
        public byte[] sCardNum = new byte[32];
        /**
         * 9000设备支持,1表示此文件已经被锁定,0表示正常的文件
         */
        public byte byLocked;
        public byte[] byRes = new byte[3];

        @Override
        public String toString() {
            return "NET_DVR_FINDDATA_V30{" +
                    "sFileName=" + Arrays.toString(sFileName) +
                    ", struStartTime=" + struStartTime +
                    ", struStopTime=" + struStopTime +
                    ", dwFileSize=" + dwFileSize +
                    ", sCardNum=" + Arrays.toString(sCardNum) +
                    ", byLocked=" + byLocked +
                    ", byRes=" + Arrays.toString(byRes) +
                    '}';
        }

        @Override
        public List<String> getFieldOrder() {
            return Arrays.asList("sFileName", "struStartTime", "struStopTime", "dwFileSize", "sCardNum", "byLocked", "byRes");
        }
    }

    public static class NET_DVR_FINDDATA_V40 extends Structure {
        public byte[] sFileName = new byte[100];
        public NET_DVR_TIME struStartTime = new NET_DVR_TIME();
        public NET_DVR_TIME struStopTime = new NET_DVR_TIME();
        public int dwFileSize;
        public byte[] sCardNum = new byte[32];
        public byte byLocked;
        public byte[] byRes1 = new byte[127];

        public byte byFileType;
        public byte byQuickSearch;
        public byte byRes;
        public int dwFileIndex;
        public byte byStreamType;

        @Override
        public String toString() {
            return "NET_DVR_FINDDATA_V40{" +
                    "sFileName=" + Arrays.toString(sFileName) +
                    ", struStartTime=" + struStartTime +
                    ", struStopTime=" + struStopTime +
                    ", dwFileSize=" + dwFileSize +
                    ", sCardNum=" + Arrays.toString(sCardNum) +
                    ", byLocked=" + byLocked +
                    ", byRes1=" + Arrays.toString(byRes1) +
                    ", byFileType=" + byFileType +
                    ", byQuickSearch=" + byQuickSearch +
                    ", byRes=" + byRes +
                    ", dwFileIndex=" + dwFileIndex +
                    ", byStreamType=" + byStreamType +
                    '}';
        }

        @Override
        public List<String> getFieldOrder() {
            return Arrays.asList("sFileName", "struStartTime", "struStopTime", "dwFileSize", "sCardNum", "byLocked",
                    "byRes1", "byFileType", "byQuickSearch", "byRes", "dwFileIndex", "byStreamType");
        }

    }

    /**
     * 录象文件查找条件结构
     */
    public static class NET_DVR_FILECOND extends Structure {
        /**
         * 通道号
         */
        public int lChannel;
        /**
         * 录象文件类型0xff-全部,0-定时录像,1-移动侦测 ,2-报警触发,3-报警|移动侦测 4-报警&移动侦测 5-命令触发 6-手动录像
         */
        public int dwFileType;
        /**
         * 是否锁定 0-正常文件,1-锁定文件, 0xff表示所有文件
         */
        public int dwIsLocked;
        /**
         * 是否使用卡号
         */
        public int dwUseCardNo;
        /**
         * 卡号
         */
        public byte[] sCardNumber = new byte[32];
        /**
         * 开始时间
         */
        public NET_DVR_TIME struStartTime = new NET_DVR_TIME();
        /**
         * 结束时间
         */
        public NET_DVR_TIME struStopTime = new NET_DVR_TIME();

        @Override
        public String toString() {
            return "NET_DVR_FILECOND{" +
                    "lChannel=" + lChannel +
                    ", dwFileType=" + dwFileType +
                    ", dwIsLocked=" + dwIsLocked +
                    ", dwUseCardNo=" + dwUseCardNo +
                    ", sCardNumber=" + Arrays.toString(sCardNumber) +
                    ", struStartTime=" + struStartTime +
                    ", struStopTime=" + struStopTime +
                    '}';
        }

        @Override
        public List<String> getFieldOrder() {
            return Arrays.asList("lChannel", "dwFileType", "dwIsLocked", "dwUseCardNo", "sCardNumber", "struStartTime", "struStopTime");
        }
    }

    public static class NET_DVR_FILECOND_V40 extends Structure {
        public int lChannel;
        public int dwFileType;
        public int dwIsLocked;
        public int dwUseCardNo;
        public byte[] sCardNumber = new byte[32];
        public NET_DVR_TIME struStartTime = new NET_DVR_TIME();
        public NET_DVR_TIME struStopTime = new NET_DVR_TIME();
        public byte byDrawFrame;
        public byte byFindType;
        public byte byQuickSearch;
        public byte bySpecialFindInfoType;
        public int dwVolumeNum;
        public byte[] byWorkingDeviceGUID = new byte[16];
        public NET_DVR_SPECIAL_FINDINFO_UNION uSpecialFindInfo = new NET_DVR_SPECIAL_FINDINFO_UNION();
        public byte byStreamType;
        public byte byAudioFile;
        public byte[] byRes2 = new byte[30];

        @Override
        public String toString() {
            return "NET_DVR_FILECOND_V40{" +
                    "lChannel=" + lChannel +
                    ", dwFileType=" + dwFileType +
                    ", dwIsLocked=" + dwIsLocked +
                    ", dwUseCardNo=" + dwUseCardNo +
                    ", sCardNumber=" + Arrays.toString(sCardNumber) +
                    ", struStartTime=" + struStartTime +
                    ", struStopTime=" + struStopTime +
                    ", byDrawFrame=" + byDrawFrame +
                    ", byFindType=" + byFindType +
                    ", byQuickSearch=" + byQuickSearch +
                    ", bySpecialFindInfoType=" + bySpecialFindInfoType +
                    ", dwVolumeNum=" + dwVolumeNum +
                    ", byWorkingDeviceGUID=" + Arrays.toString(byWorkingDeviceGUID) +
                    ", uSpecialFindInfo=" + uSpecialFindInfo +
                    ", byStreamType=" + byStreamType +
                    ", byAudioFile=" + byAudioFile +
                    ", byRes2=" + Arrays.toString(byRes2) +
                    '}';
        }

        @Override
        public List<String> getFieldOrder() {
            return Arrays.asList("lChannel", "dwFileType", "dwIsLocked", "dwUseCardNo", "sCardNumber",
                    "struStartTime", "struStopTime", "byDrawFrame", "byFindType", "byQuickSearch",
                    "bySpecialFindInfoType", "dwVolumeNum", "byWorkingDeviceGUID", "uSpecialFindInfo",
                    "byStreamType", "byAudioFile", "byRes2");
        }
    }

    public static class NET_DVR_SPECIAL_FINDINFO_UNION extends Structure {
        public byte[] byLength = new byte[8];
        public NET_DVR_ATMEINDINFO struATMFindInfo = new NET_DVR_ATMEINDINFO();

        @Override
        public String toString() {
            return "NET_DVR_SPECIAL_FINDINFO_UNION{" +
                    "byLength=" + Arrays.toString(byLength) +
                    ", struATMFindInfo=" + struATMFindInfo +
                    '}';
        }

        @Override
        public List<String> getFieldOrder() {
            return Arrays.asList("byLength", "struATMFindInfo");
        }
    }

    public static class NET_DVR_ATMEINDINFO extends Structure {
        public byte byTransactionType;
        public byte[] byRes = new byte[3];
        public int dwTransationAmount;

        @Override
        public String toString() {
            return "NET_DVR_ATMEINDINFO{" +
                    "byTransactionType=" + byTransactionType +
                    ", byRes=" + Arrays.toString(byRes) +
                    ", dwTransationAmount=" + dwTransationAmount +
                    '}';
        }

        @Override
        public List<String> getFieldOrder() {
            return Arrays.asList("byTransactionType", "byRes", "dwTransationAmount");
        }

    }

    /**
     * API函数声明,详细说明见API手册
     */
    public static interface FExceptionCallBack extends Callback {
        public void invoke(int dwType, NativeLong lUserID, NativeLong lHandle, Pointer pUser);
    }

    boolean NET_DVR_Init();

    boolean NET_DVR_Cleanup();

    boolean NET_DVR_SetExceptionCallBack_V30(int nMessage, int hWnd, FExceptionCallBack fExceptionCallBack, Pointer pUser);

    boolean NET_DVR_SetConnectTime(int dwWaitTime, int dwTryTimes);

    boolean NET_DVR_SetReconnect(int dwInterval, boolean bEnableRecon);

    NativeLong NET_DVR_Login_V30(String sDVRIP, short wDVRPort, String sUserName, String sPassword, NET_DVR_DEVICEINFO_V30 lpDeviceInfo);

    boolean NET_DVR_Logout(NativeLong lUserID);

    int NET_DVR_GetLastError();

    NativeLong NET_DVR_FindNextFile_V30(NativeLong lFindHandle, NET_DVR_FINDDATA_V30 lpFindData);

    NativeLong NET_DVR_FindNextFile_V40(NativeLong lFindHandle, NET_DVR_FINDDATA_V40 lpFindData);

    NativeLong NET_DVR_FindFile_V30(NativeLong lUserID, NET_DVR_FILECOND pFindCond);

    NativeLong NET_DVR_FindFile_V40(NativeLong lUserID, NET_DVR_FILECOND_V40 pFindCond);

    boolean NET_DVR_FindClose_V30(NativeLong lFindHandle);

    NativeLong NET_DVR_PlayBackByTime(NativeLong lUserID, NativeLong lChannel, NET_DVR_TIME lpStartTime, NET_DVR_TIME lpStopTime, HWND hWnd);

    boolean NET_DVR_PlayBackControl(NativeLong lPlayHandle, int dwControlCode, int dwInValue, IntByReference LPOutValue);

    boolean NET_DVR_PlayBackControl_V40(NativeLong lPlayHandle, int dwControlCode, Pointer lpInBuffer, int dwInLen, Pointer lpOutBuffer, IntByReference LPOutValue);

    NativeLong NET_DVR_GetFileByTime(NativeLong lUserID, NativeLong lChannel, NET_DVR_TIME lpStartTime, NET_DVR_TIME lpStopTime, String sSavedFileName);

    boolean NET_DVR_StopGetFile(NativeLong lFileHandle);

    boolean NET_DVR_GetDVRConfig(NativeLong lUserID, int dwCommand, NativeLong lChannel, Pointer lpOutBuffer, int dwOutBufferSize, IntByReference lpBytesReturned);
}

上述代码比较多,可以只看思路,没必要全看,因为每个人的需求不同,用到的SDK也不尽相同。下面以下载视频的代码为例演示如何在项目中使用SDK。

import com.sun.jna.NativeLong;
import com.sun.jna.ptr.IntByReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDateTime;

/**
 * @author DiCaprito
 */
public class Test {
    private static final Logger LOGGER = LoggerFactory.getLogger(Test.class);

    private static HcNetSdk hCNetSDK = HcNetSdk.INSTANCE;

    public Boolean download(NativeLong userId, LocalDateTime startTime, LocalDateTime stopTime) throws Exception {
        LOGGER.info("[HikService]#Start to download video.");
        HcNetSdk.NET_DVR_TIME struStartTime = new HcNetSdk.NET_DVR_TIME();
        HcNetSdk.NET_DVR_TIME struStopTime = new HcNetSdk.NET_DVR_TIME();
        // 开始时间
        struStartTime.dwYear = startTime.getYear();
        struStartTime.dwMonth = startTime.getMonth().getValue();
        struStartTime.dwDay = startTime.getDayOfMonth();
        struStartTime.dwHour = startTime.getHour();
        struStartTime.dwMinute = startTime.getMinute();
        struStartTime.dwSecond = startTime.getSecond();
        // 结束时间
        struStopTime.dwYear = stopTime.getYear();
        struStopTime.dwMonth = stopTime.getMonth().getValue();
        struStopTime.dwDay = stopTime.getDayOfMonth();
        struStopTime.dwHour = stopTime.getHour();
        struStopTime.dwMinute = stopTime.getMinute();
        struStopTime.dwSecond = stopTime.getSecond();

        String fileName = "";
        int channel = 1;
        LOGGER.info("sFileName: " + fileName);
        NativeLong loadHandle = hCNetSDK.NET_DVR_GetFileByTime(userId, new NativeLong(channel), struStartTime,
                struStopTime, fileName);

        if (loadHandle.intValue() >= 0) {
            LOGGER.info("[HikService]#Start to download.");
            hCNetSDK.NET_DVR_PlayBackControl(loadHandle, HcNetSdk.NET_DVR_PLAY_START, 0, null);
            while (true) {
                IntByReference nPos = new IntByReference(0);
                boolean success = hCNetSDK.NET_DVR_PlayBackControl(loadHandle, HcNetSdk.NET_DVR_PLAY_GET_POS, 0, nPos);
                if (!success) {
                    return null;
                }
                LOGGER.info("[HikService]#Download process: {}", nPos.getValue());
                if (nPos.getValue() > 100) {
                    hCNetSDK.NET_DVR_StopGetFile(loadHandle);
                    loadHandle.setValue(-1);
                    LOGGER.error("[HikService]#Download exception.");
                    return false;
                } else if (nPos.getValue() == 100) {
                    hCNetSDK.NET_DVR_StopGetFile(loadHandle);
                    loadHandle.setValue(-1);
                    break;
                } else {
                    //下载进度
                }
            }
        } else {
            LOGGER.error("[HikService]#Download failed.");
            return false;
        }
        LOGGER.info("[HikService]#Download success.");
        return true;
    }
}

以上就是我在进行海康SDK开发时的一些体会,欢迎骚扰。

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/25385.html

(0)

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信