使用前先阅读SDK下的文档
加入项目工程 按照SDK里面的文档要求把下图文件夹和文件引入(在SDK库文件夹里)
HCNetSDK的绝对路径!!!!
核心代码
@Slf4j
@Service
public class HikService {
static FMSGCallBack_V30 fmsgCallBack_v30;//消息回调
static FExceptionCallBack fExceptionCallBack;//异常回调
String username = "admin";//设备用户名
String password = "12345";//设备密码
short m_sPort = 8000;//海康设备端口号
int lUserID = -1;//用户
private static int iCharEncodeType = 0;//设备字符集
boolean b1 = hCNetSDK.NET_DVR_Init();//调用设备网络 SDK初始化 其他函数的前提
private static HCNetSDK hCNetSDK = HCNetSDK.INSTANCE;//SDK实例
private String ip;
public void faceMonitor(String ip, String port, String username , String assword) {
if(b1==false)
hCNetSDK.NET_DVR_Init();//调用设备网络 SDK初始化 其他函数的前提
//设置连接时间
hCNetSDK.NET_DVR_SetConnectTime(2000, 1);
//设置重连时间
hCNetSDK.NET_DVR_SetReconnect(120000, true);
HCNetSDK.NET_DVR_LOCAL_CHECK_DEV struLoaclCheckDev = new HCNetSDK.NET_DVR_LOCAL_CHECK_DEV();
struLoaclCheckDev.dwCheckOnlineTimeout = 0;
struLoaclCheckDev.dwCheckOnlineNetFailMax = 0;
// //DWORD dwCheckOnlineTimeout; //巡检时间间隔,单位ms 最小值为30s,最大值120s。为0时,表示用默认值(120s)
// //DWORD dwCheckOnlineNetFailMax; //由于网络原因失败的最大累加次数;超过该值SDK才回调用户异常,为0时,表示使用默认值1
hCNetSDK.NET_DVR_SetSDKLocalCfg(10, struLoaclCheckDev);
// hCNetSDK.NET_DVR_SetLogToFile(3, "C:\\SdkLog\\", true);//日志
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 = this.m_sUsername;//设备用户名
m_strLoginInfo.sUserName = new byte[HCNetSDK.NET_DVR_LOGIN_USERNAME_MAX_LEN];
System.arraycopy(m_sUsername.getBytes(), 0, m_strLoginInfo.sUserName, 0, m_sUsername.length());
m_strLoginInfo.sPassword = new byte[HCNetSDK.NET_DVR_LOGIN_PASSWD_MAX_LEN];
System.arraycopy(m_sPassword.getBytes(), 0, m_strLoginInfo.sPassword, 0, m_sPassword.length());
m_strLoginInfo.wPort = this.m_sPort;
m_strLoginInfo.bUseAsynLogin = false; //是否异步登录:0- 否,1- 是
m_strLoginInfo.write();
HCNetSDK.NET_DVR_DEVICEINFO_V40 m_strDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V40();//设备信息
lUserID = hCNetSDK.NET_DVR_Login_V40(m_strLoginInfo, m_strDeviceInfo);
if (lUserID == -1) {
log.info("登录失败,错误码为" + hCNetSDK.NET_DVR_GetLastError());
} else {
log.info("登录成功!");
iCharEncodeType = m_strDeviceInfo.byCharEncodeType;
byte[] sSerialNumber = m_strDeviceInfo.struDeviceV30.sSerialNumber;
String sn = new String(sSerialNumber).trim();
String strsn = sn.substring(sn.length() - 10);
log.info("ip: " + ip + " ++deviId++ " +deviId +", ++strsn++ "+strsn );
callBack();//回调
setAlarm();//布防
}
}
private void callBack(){//回调函数
if (fmsgCallBack_v30 == null) {
fmsgCallBack_v30 = new FMSGCallBack_V30();//报警回调
if (!hCNetSDK.NET_DVR_SetDVRMessageCallBack_V30(fmsgCallBack_v30, null)) {//报警回调
log.info("Set fmsgCallBack function fail");
return;
} else {
log.info("Set fmsgCallBack function successfully!");
}
}
if (fExceptionCallBack == null) {
fExceptionCallBack = new FExceptionCallBack();//异常回调
//设置异常回调函数(可在回调函数中获取设备上下线状态等)
// Pointer pUser = m_strDeviceInfo.getPointer();
if (!hCNetSDK.NET_DVR_SetExceptionCallBack_V30(0, 0, fExceptionCallBack, null)) {
log.info("Set fExceptionCallBack function fail");
return;
} else {
log.info("Set fExceptionCallBack function successfully!");
}
}
}
public void setAlarm() {//布防
//建立报警上传通道(布防)
HCNetSDK.NET_DVR_SETUPALARM_PARAM m_strAlarmInfo = new HCNetSDK.NET_DVR_SETUPALARM_PARAM();
m_strAlarmInfo.dwSize = m_strAlarmInfo.size();
m_strAlarmInfo.byLevel = 1;
m_strAlarmInfo.byAlarmInfoType = 1;
m_strAlarmInfo.byDeployType = 1;
m_strAlarmInfo.write();
int nativeLong = hCNetSDK.NET_DVR_SetupAlarmChan_V41(lUserID, m_strAlarmInfo);
log.info("nativeLong=====" + nativeLong);
// //如果布防失败返回-1
if (nativeLong == -1) {
log.info("deployment fail");
//注销
hCNetSDK.NET_DVR_Logout(lUserID);
//释放SDK资源
hCNetSDK.NET_DVR_Cleanup();
} else {
log.info("deployment Successful");
}
}
异常消息回调
/**
* 异常消息回调
*/
@Slf4j
public class FExceptionCallBack implements HCNetSDK.FExceptionCallBack {
private static Map ipUserIdMap = new HashMap<>();
public static void addUserId(Map map) {
ipUserIdMap.putAll(map);
}
@Override
public void invoke(int dwType, int lUserID, int lHandle, Pointer pUser) {
switch (dwType) {
case 0x8000: //用户交互时异常
log.info("用户交互时异常(注册心跳超时,心跳间隔为2分钟)!!!"+"lUserID:"+lUserID );
break;
case 0x8001: //语音对讲异常
log.info("语音对讲异常!!!"+"lUserID:"+lUserID );
break;
case 0x8002: //报警异常
log.info("报警异常!!!" +"lUserID:"+lUserID );
break;
case 0x8003://网络预览异常
log.info("网络预览异常!!!"+"lUserID:"+lUserID );
break;
case 0x8004: //透明通道异常
log.info("透明通道异常!!!"+"lUserID:"+lUserID );
break;
case 0x8005: //预览时重连
log.info("预览时重连!!!"+"lUserID:"+lUserID );
break;
case 0x8006: //报警时重连
log.info("报警时重连!!!"+"lUserID:"+lUserID );
break;
case 0x8007://透明通道重连
log.info("透明通道重连!!!"+"lUserID:"+lUserID );
break;
case 0x8008: //透明通道重连成功
log.info("透明通道重连成功!!!"+"lUserID:"+lUserID );
break;
case 0x8010: //回放异常
log.info("回放异常!!!" +"lUserID:"+lUserID );
break;
case 0x8011://硬盘格式化
log.info("硬盘格式化!!!" + lHandle);
break;
case 0x8012: //被动解码异常
log.info("被动解码异常!!!" +"lUserID:"+lUserID );
break;
case 0x8013: //邮件测试异常
log.info("邮件测试异常!!!" +"lUserID:"+lUserID );
break;
case 0x8014: //备份异常
log.info("备份异常!!!" +"lUserID:"+lUserID );
break;
case 0x8015://预览时重连成功
log.info("预览时重连成功!!!" +"lUserID:"+lUserID );
break;
case 0x8016: //报警时重连成功
log.info("报警时重连成功!!!"+"lUserID:"+lUserID );
break;
case 0x8017: //网络预览时网络异常
log.info("用户交互恢复!!!"+"lUserID:"+lUserID );
break;
case 0x8018: //网络流量检测异常
log.info("网络流量检测异常!!!"+"lUserID:"+lUserID );
break;
case 0x8019://图片预览重连
log.info("图片预览重连!!!"+"lUserID:"+lUserID );
break;
case 0x8020: //图片预览重连成功
log.info("图片预览重连成功!!!"+"lUserID:"+lUserID );
break;
case 0x8021://图片预览异常
log.info("图片预览异常!!!"+"lUserID:"+lUserID );
break;
case 0x8022: //报警信息缓存已达上限
log.info("报警信息缓存已达上限!!!" +"lUserID:"+lUserID );
break;
case 0x8023: //报警丢失
log.info("报警丢失!!!" +"lUserID:"+lUserID );
break;
case 0x8024: //被动转码重连
log.info("被动转码重连!!!" +"lUserID:"+lUserID );
break;
case 0x8025://被动转码重连成功
log.info("被动转码重连成功!!!" +"lUserID:"+lUserID );
break;
case 0x8026: //被动转码异常
log.info("被动转码异常!!!" +"lUserID:"+lUserID );
break;
case 0x8040: //用户重登陆
log.info("用户重登陆!!!" +"lUserID:"+lUserID );
break;
case 0x8041://用户重登陆成功
log.info("用户重登陆成功!!!" +"lUserID:"+lUserID );
break;
case 0x8042: //被动解码重连
log.info("被动解码重连!!!"+"lUserID:"+lUserID );
break;
case 0x8043: //集群报警异常
log.info("集群报警异常!!!" +"lUserID:"+lUserID );
break;
case 0x8044: //重登陆失败,停止重登陆
log.info("重登陆失败,停止重登陆!!!"+"lUserID:"+lUserID );
break;
case 0x8045://关闭预览重连功能
log.info("关闭预览重连功能!!!"+"lUserID:"+lUserID );
break;
case 0x8046: //关闭报警重连功能
log.info("关闭报警重连功能!!!" +"lUserID:"+lUserID );
break;
case 0x8047: //关闭透明通道重连功能
log.info("关闭透明通道重连功能!!!" +"lUserID:"+lUserID );
break;
case 0x8048: //关闭回显重连功能
log.info("关闭回显重连功能!!!" +"lUserID:"+lUserID );
break;
case 0x8049://关闭被动解码重连功能
log.info("关闭被动解码重连功能!!!" +"lUserID:"+lUserID );
break;
default:
log.info("异常"+"lUserID:"+lUserID );
break;
}
}
}
欢迎留言,您的的建议是我前进的动力,车辆抓拍和人脸抓拍关键代码,请点击https://blog.csdn.net/qq_24981615/article/details/123060744https://blog.csdn.net/qq_24981615/article/details/123060744