ASjaSQTi   发表于 2020-1-21 23:05:41 |栏目:





准备海康摄像头SDK开发下载路径:开发过程中遇到的问题记录:添加maven依赖下面代码中会出现的实体类CameraManage实体类:ControlDto实体类:加载库文件:初始化:登录控制:查询历史视频保存时间:退出登陆释放资源:完整代码:视频下载、回放、视频直播、抓图等功能


https://www.hikvision.com/cn/download_61.html
1

下载解压之后的目录结构:
1 中为需要加载的库文件;2中有所需的jar包及HCNetSDK.java文件


一定要记得开启日志打印与及时获取最后一次的错误状态码
一个ip下面会有多个摄像头
如果返回的错误状态码为10:有可能是端口不对,改用8000试试
如果可以登陆却无法控制,有可能是通道错误,有的摄像机通道从1开始,有的从33开始【可通过NET_DVR_GetDVRConfig/NET_DVR_SetDVRConfig来查询和修改相关配置】
查询保存的历史视频的起始时间时:NET_DVR_RECORD_TIME_SPAN_INQUIRY结构体占用内存的大小是68个字节


<!--海康录像机二次开发依赖jar包-->
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>3.0.9</version>
</dependency>
<!--海康录像机sdk官方提供jar包-->
<dependency>
<groupId>com.qy</groupId>
<artifactId>examples</artifactId><!--需要在maven自行安装这个jar包-->
<version>1.0.0</version>
</dependency>
123456789101112


private String deviceIp;
private Integer devicePort;
private String username;
private String password;
1234

private Long controlUserId;//用户id
private Long channel;//通道【有的类型通道从1开始,有的则从33开始,具体看摄像头配置】
private Integer command;//命令
private Integer motionSpeed;//运动速度
private Integer runOrStop;//0启动/1停止
12345

//修改HCNetSDK.java文件中的加载路径
//如果是jar包可能找不到jar包中的文件,所以需要单独存放
        HCNetSDK INSTANCE = (HCNetSDK) Native.loadLibrary("此处为linux存放库文件libhcnetsdk.so的路径",HCNetSDK.class);
        hcNetSDK = HCNetSDK.INSTANCE;
//如果某些文件加载不到需要手动加载,加载过程如下
//设置HCNetSDKCom组件库所在路径
        String strPathCom = "/opt/project/yc/yc-video/HCNetLib";
        HCNetSDK.NET_DVR_LOCAL_SDK_PATH struComPath = new HCNetSDK.NET_DVR_LOCAL_SDK_PATH();
        System.out.println(strPathCom.getBytes());
        System.out.println(struComPath.sPath);
        System.arraycopy(strPathCom.getBytes(), 0, struComPath.sPath, 0, strPathCom.length());
        struComPath.write();
        hcNetSDK.NET_DVR_SetSDKInitCfg(2, struComPath.getPointer());
//设置libcrypto.so所在路径
        HCNetSDK.BYTE_ARRAY ptrByteArrayCrypto = new HCNetSDK.BYTE_ARRAY(256);
        String strPathCrypto = "/opt/project/yc/yc-video/HCNetLib/libcrypto.so";
        System.arraycopy(strPathCrypto.getBytes(), 0, ptrByteArrayCrypto.byValue, 0, strPathCrypto.length());
        ptrByteArrayCrypto.write();
        hcNetSDK.NET_DVR_SetSDKInitCfg(3, ptrByteArrayCrypto.getPointer());
//设置libssl.so所在路径
        HCNetSDK.BYTE_ARRAY ptrByteArraySsl = new HCNetSDK.BYTE_ARRAY(256);
        String strPathSsl = "/opt/project/yc/yc-video/HCNetLib/libssl.so";
        System.arraycopy(strPathSsl.getBytes(), 0, ptrByteArraySsl.byValue, 0, strPathSsl.length());
        ptrByteArraySsl.write();
        hcNetSDK.NET_DVR_SetSDKInitCfg(4, ptrByteArraySsl.getPointer());
12345678910111213141516171819202122232425262728

//设置连接超时时间及尝试次数
if (!StringUtils.isEmpty(dwWaitTime) && !StringUtils.isEmpty(dwTryTimes)){
boolean b1 = hcNetSDK.NET_DVR_SetConnectTime(Integer.parseInt(dwWaitTime), Integer.parseInt(dwTryTimes));
}
//开启日志
        hcNetSDK.NET_DVR_SetLogToFile(true,"/logs-video/",true);
//初始化
boolean initFlag = hcNetSDK.NET_DVR_Init();
if(!initFlag) {
return new ResultMoudel(ResultMoudel.ERROR_FLAG,"初始化失败NET_DVR_Init,状态码为"+hcNetSDK.NET_DVR_GetLastError());
}
1234567891011121314

//登陆
        HCNetSDK.NET_DVR_DEVICEINFO_V30 deviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V30();
        NativeLong userId = hcNetSDK.NET_DVR_Login_V30(cameraManage.getDeviceIp(), (short) cameraManage.getDevicePort().shortValue(), cameraManage.getUsername(), cameraManage.getPassword(), deviceInfo);
long lUserId = userId.intValue();
if(lUserId !=-1 || 4294967295L != lUserId) {
            Map<String, Long> hashMap = new HashMap<>();
            hashMap.put("controlUserId",lUserId);
            hashMap.put("channel",Long.parseLong(cameraManage.getDeviceNumber()) + 32);
            MAP_USERID_AND_CHANNEL.put(cameraManage.getDeviceIp(), hashMap);
return new ResultMoudel(ResultMoudel.SUCCESS_FLAG,hashMap);
}
return new ResultMoudel(ResultMoudel.ERROR_FLAG,"登陆失败,状态码为"+hcNetSDK.NET_DVR_GetLastError());
12345678910111213

        NativeLong channel = new NativeLong(controlDto.getChannel());
        NativeLong controlUserId = new NativeLong(controlDto.getControlUserId());
boolean b = hcNetSDK.NET_DVR_PTZControlWithSpeed_Other(controlUserId, channel, controlDto.getCommand(), controlDto.getRunOrStop(), controlDto.getMotionSpeed());
if (b){
return new ResultMoudel(ResultMoudel.SUCCESS_FLAG,"正在控制,状态码为"+hcNetSDK.NET_DVR_GetLastError());
}
return new ResultMoudel(ResultMoudel.ERROR_FLAG,"控制失败,状态码为"+hcNetSDK.NET_DVR_GetLastError());
1234567

        NativeLong userIdn = new NativeLong(controlUserId);
        HCNetSDK.NET_DVR_RECORD_TIME_SPAN_INQUIRY ipInquiry = new HCNetSDK.NET_DVR_RECORD_TIME_SPAN_INQUIRY();
        ipInquiry.dwSize = 68;//68为结构体大小
        ipInquiry.byType = 0;
        HCNetSDK.NET_DVR_RECORD_TIME_SPAN ipResult = new HCNetSDK.NET_DVR_RECORD_TIME_SPAN();
boolean b = hcNetSDK.NET_DVR_InquiryRecordTimeSpan(userIdn, channel, ipInquiry, ipResult);
//获取开始时间
        HCNetSDK.NET_DVR_TIME strBeginTime = ipResult.strBeginTime;
//获取结束时间
        HCNetSDK.NET_DVR_TIME strEndTime = ipResult.strEndTime;
12345678910

//退出登陆
boolean b = hcNetSDK.NET_DVR_Logout(new NativeLong(manageUserId));
//释放资源
boolean b1 = hcNetSDK.NET_DVR_Cleanup();
12345

public class VideoUtils {
private static HCNetSDK hcNetSDK;
// 用于缓存登陆信息,以防止短时间内重复登陆
private static final Map<String,Map<String,Long>> MAP_USERID_AND_CHANNEL = new HashMap<>();
//登陆
public static ResultMoudel login(CameraManage cameraManage){
//加载所需文件
        ResultMoudel resultMoudel = initHCNetSDK();
if (ResultMoudel.ERROR_FLAG.equals(resultMoudel.getResultFlag())){
return resultMoudel;
}
if (!StringUtils.isEmpty(cameraManage.getDeviceIp()) && MAP_USERID_AND_CHANNEL.containsKey(cameraManage.getDeviceIp())){
            Map<String, Long> stringLongMap = MAP_USERID_AND_CHANNEL.get(cameraManage.getDeviceIp());
            stringLongMap.put("channel",Long.parseLong(cameraManage.getDeviceNumber()) + 32);
return new ResultMoudel(ResultMoudel.SUCCESS_FLAG, stringLongMap);
}
//登陆
        HCNetSDK.NET_DVR_DEVICEINFO_V30 deviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V30();
        NativeLong userId = hcNetSDK.NET_DVR_Login_V30(cameraManage.getDeviceIp(), (short) cameraManage.getDevicePort().shortValue(), cameraManage.getUsername(), cameraManage.getPassword(), deviceInfo);
long lUserId = userId.intValue();
if(lUserId !=-1 || 4294967295L != lUserId) {
            Map<String, Long> hashMap = new HashMap<>();
            hashMap.put("controlUserId",lUserId);
            hashMap.put("channel",Long.parseLong(cameraManage.getDeviceNumber()) + 32);
            MAP_USERID_AND_CHANNEL.put(cameraManage.getDeviceIp(), hashMap);
return new ResultMoudel(ResultMoudel.SUCCESS_FLAG,hashMap);
}
return new ResultMoudel(ResultMoudel.ERROR_FLAG,"登陆失败,状态码为"+hcNetSDK.NET_DVR_GetLastError());
}
//控制
public static ResultMoudel commandControl(ControlDto controlDto) {

        NativeLong channel = new NativeLong(controlDto.getChannel());
        NativeLong controlUserId = new NativeLong(controlDto.getControlUserId());
boolean b = hcNetSDK.NET_DVR_PTZControlWithSpeed_Other(controlUserId, channel, controlDto.getCommand(), controlDto.getRunOrStop(), controlDto.getMotionSpeed());
if (b){
return new ResultMoudel(ResultMoudel.SUCCESS_FLAG,"正在控制,状态码为"+hcNetSDK.NET_DVR_GetLastError());
}
return new ResultMoudel(ResultMoudel.ERROR_FLAG,"控制失败,状态码为"+hcNetSDK.NET_DVR_GetLastError());
}
//退出登陆
public static ResultMoudel logout(Long manageUserId) {
//退出登陆
boolean b = hcNetSDK.NET_DVR_Logout(new NativeLong(manageUserId));
return null;
}
//释放资源
public static ResultMoudel release() {
boolean b1 = hcNetSDK.NET_DVR_Cleanup();
        hcNetSDK = null;
return null;
}
//初始化
private static ResultMoudel initHCNetSDK() {
if (hcNetSDK == null){
            System.out.println(HCNetSDK.MAX_NAMELEN);
            hcNetSDK = HCNetSDK.INSTANCE;
//设置HCNetSDKCom组件库所在路径
            String strPathCom = File.separator +"opt"+ File.separator +"project"+ File.separator +"yc-other" + File.separator + "HCNetLib";
            HCNetSDK.NET_DVR_LOCAL_SDK_PATH struComPath = new HCNetSDK.NET_DVR_LOCAL_SDK_PATH();
            System.out.println(strPathCom.getBytes());
            System.out.println(struComPath.sPath);
            System.arraycopy(strPathCom.getBytes(), 0, struComPath.sPath, 0, strPathCom.length());
            struComPath.write();
            hcNetSDK.NET_DVR_SetSDKInitCfg(2, struComPath.getPointer());
//设置libcrypto.so所在路径
            HCNetSDK.BYTE_ARRAY ptrByteArrayCrypto = new HCNetSDK.BYTE_ARRAY(256);
            String strPathCrypto = File.separator +"opt"+ File.separator +"project"+ File.separator +"yc-other" + File.separator + "HCNetLib" + File.separator+"libcrypto.so";
            System.arraycopy(strPathCrypto.getBytes(), 0, ptrByteArrayCrypto.byValue, 0, strPathCrypto.length());
            ptrByteArrayCrypto.write();
            hcNetSDK.NET_DVR_SetSDKInitCfg(3, ptrByteArrayCrypto.getPointer());
//设置libssl.so所在路径
            HCNetSDK.BYTE_ARRAY ptrByteArraySsl = new HCNetSDK.BYTE_ARRAY(256);
            String strPathSsl = File.separator +"opt"+ File.separator +"project"+ File.separator +"yc-other" + File.separator

回复 显示全部楼层 使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作者相关信息

更多资源

精品推荐

极品资源

原创模板

下载排行

热门标签

快速回复 返回顶部 返回列表