JAVA使用海康SDK
首先到海康威视官网下载SDK
1.导入项目时候需要注意,linux环境跟windows不同的
区别如下:
public interface HCNetSDK extends Library //linux
public interface HCNetSDK extends StdCallLibrary //windows
2.初始化SDK
public static HCNetSDK hCNetSDK;
{
hCNetSDK = HCNetSDK.INSTANCE;
boolean b = hCNetSDK.NET_DVR_Init();
if (b) {
log.info("海康SDk初始化成功");
} else {
log.info("海康SDk初始化失败");
}
}
3.查看SDK调用的流程以及参考Demo(黄色部分可以在摄像头直接设置)
4.参考示例是C语言的,我们需要对应转换成JAVA代码,首先设置一个回调函数
public class MsgCallBack_V31 implements HCNetSDK.FMSGCallBack_V31 {
@Override
public boolean invoke(int lCommand, HCNetSDK.NET_DVR_ALARMER pAlarmer, Pointer pAlarmInfo, int dwBufLen, Pointer pUser) {
switch (lCommand) {
case HCNetSDK.COMM_VCA_ALARM: // 智能检测通用报警
{
break;
}
case HCNetSDK.COMM_ISAPI_ALARM: {//图片分离模式
//触发事件终端ip
String terminalIp = new String(pAlarmer.sDeviceIP).trim();
//写入接收结构体
HCNetSDK.NET_DVR_ALARM_ISAPI_INFO strDvrAlarmIspi = new HCNetSDK.NET_DVR_ALARM_ISAPI_INFO();
strDvrAlarmIspi.write();
Pointer pVcaIspiInfo = strDvrAlarmIspi.getPointer();
pVcaIspiInfo.write(0, pAlarmInfo.getByteArray(0, strDvrAlarmIspi.size()), 0, strDvrAlarmIspi.size());
strDvrAlarmIspi.read();
break;
}
default:
System.out.println("其他报警,报警信息类型: " + lCommand);
break;
}
return true;
}
5.开始对摄像头进行布防,废话不多说,直接上代码
@Service
public class TerminalSdkServiceImpl{
public static HCNetSDK hCNetSDK = HCNetSDK.INSTANCE;
public static HCNetSDK.FMSGCallBack_V31 fMSFCallBack_V31;//报警回调函数实现
public void InitTerminal(String ip, String userName, String password, short port) {
fMSFCallBack_V31 = new MsgCallBack_V31();
// 初始化
if(hCNetSDK == null) {
hCNetSDK.NET_DVR_Init();
}
//建议使用图片分离模式,否则解析会内存溢出或者引发其他问题
HCNetSDK.NET_DVR_LOCAL_GENERAL_CFG struGeneralCfg = new HCNetSDK.NET_DVR_LOCAL_GENERAL_CFG();
struGeneralCfg.byAlarmJsonPictureSeparate = 1;// 分离模式0不分离,1分离
struGeneralCfg.write();
if (!hCNetSDK.NET_DVR_SetSDKLocalCfg(17, struGeneralCfg.getPointer())) {
JOptionPane.showMessageDialog(null, "NET_DVR_SetSDKLocalCfg失败");
}
// 设置连接时间与重连时间
hCNetSDK.NET_DVR_SetConnectTime(2000, 1);
hCNetSDK.NET_DVR_SetReconnect(10000, true);
// 登录参数,包括设备地址、登录用户、密码等
HCNetSDK.NET_DVR_USER_LOGIN_INFO struLoginInfo = new HCNetSDK.NET_DVR_USER_LOGIN_INFO();
// 设备IP地址
struLoginInfo.sDeviceAddress = new byte[HCNetSDK.NET_DVR_DEV_ADDRESS_MAX_LEN];
System.arraycopy(ip.getBytes(), 0, struLoginInfo.sDeviceAddress, 0, ip.length());
//用户名
struLoginInfo.sUserName = new byte[HCNetSDK.NET_DVR_LOGIN_USERNAME_MAX_LEN];
System.arraycopy(userName.getBytes(), 0, struLoginInfo.sUserName, 0, userName.length());
//设备登录密码
struLoginInfo.sPassword = new byte[HCNetSDK.NET_DVR_LOGIN_PASSWD_MAX_LEN];
System.arraycopy(password.getBytes(), 0, struLoginInfo.sPassword, 0, password.length());
struLoginInfo.bUseAsynLogin = false; // 同步登录方式
struLoginInfo.wPort = port; // 设备服务端口
struLoginInfo.write();
// 设备信息, 输出参数
HCNetSDK.NET_DVR_DEVICEINFO_V40 struDeviceInfoV40 = new HCNetSDK.NET_DVR_DEVICEINFO_V40();
lUserID = hCNetSDK.NET_DVR_Login_V40(struLoginInfo, struDeviceInfoV40);
if (lUserID < 0) {
System.out.println("Login failed, error code: " + hCNetSDK.NET_DVR_GetLastError());
hCNetSDK.NET_DVR_Cleanup();
return;
}else{
System.out.println("设备ip:" + ip + "登录成功!");
}
// 设置报警回调函数
hCNetSDK.NET_DVR_SetDVRMessageCallBack_V31(fMSFCallBack_V31, null);
int lHandle;
HCNetSDK.NET_DVR_SETUPALARM_PARAM struAlarmParam = new HCNetSDK.NET_DVR_SETUPALARM_PARAM();
struAlarmParam.dwSize = struAlarmParam.size();
// 报警图片数据类型,bit1-报警类型为COMM_VCA_ALARM对应的json报警数据中图片数据长传类型,0- 二进制传输,1- URL传输
struAlarmParam.byAlarmTypeURL = 1 << 1;
// 其他报警布防参数不需要设置,不支持
lHandle = hCNetSDK.NET_DVR_SetupAlarmChan_V41(lUserID, struAlarmParam);
if (lHandle < 0) {
System.out.println("NET_DVR_SetupAlarmChan_V41 error," + hCNetSDK.NET_DVR_GetLastError());
hCNetSDK.NET_DVR_Logout(lUserID);
hCNetSDK.NET_DVR_Cleanup();
return;
}
System.out.println("设备ip:" + ip + "布防成功!");
}
}
6.收工,一般情况下,不成功的原因有以下几个:
1.摄像头未打开检测功能
2.摄像头未设置规则上传功能