一.开发环境:Linux64(ubuntu环境)
二.SDK版本:CH-HCNetSDKV6.1.4.17_build20200331_Linux64(官网下载sdk)
三.开发环境准备:安装 jdk , eclipse, Linux下需要将库文件放置到/usr/lib64目录下
三.demo样例
1. 设备登录
HCNetSDK sdk = HCNetSDK.INSTANCE;//加载设备库文件
sdk.NET_DVR_Init();//初始化SDK
logger.info("初始化状态码为:" + sdk.NET_DVR_GetLastError());
HCNetSDK.NET_DVR_DEVICEINFO_V30 m_strDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V30();
int lUserId = sdk.NET_DVR_Login_V30(ip, port, userName, passwd, m_strDeviceInfo);
if(lUserId > 0 ){
logger.info("登录成功");
} else {
logger.info("登录失败, 错误码:" + sdk.NET_DVR_GetLastError());
}
2. 获取设备用户信息
HCNetSDK.NET_DVR_CARD_COND struCardCond = new HCNetSDK.NET_DVR_CARD_COND();
struCardCond.read();
struCardCond.dwSize = struCardCond.size();
struCardCond.dwCardNum = 0xffffffff; // 查询所有
struCardCond.write();
Pointer ptrStruCond = struCardCond.getPointer();
m_lSetCardCfgHandle = sdk.NET_DVR_StartRemoteConfig(userId, HCNetSDK.NET_DVR_GET_CARD, ptrStruCond,
struCardCond.size(), null, null);
if (m_lSetCardCfgHandle == -1) {
String desc = "建立下发卡长连接失败,错误码为" + sdk.NET_DVR_GetLastError();
logger.info(desc);
res.setResult(UsersResult.RES_DEVICE_ERROR);
res.setDescription(desc);
return res;
} else {
logger.info("建立下发卡长连接成功!");
}
HCNetSDK.NET_DVR_CARD_RECORD struCardRecord = new HCNetSDK.NET_DVR_CARD_RECORD();
struCardRecord.read();
struCardRecord.dwSize = struCardRecord.size();
struCardRecord.write();
List list = new ArrayList();
// TODO 根据卡号获取单个卡
while (true) {
dwState = sdk.NET_DVR_GetNextRemoteConfig(m_lSetCardCfgHandle, struCardRecord.getPointer(),
struCardRecord.size());
struCardRecord.read();
if (dwState == -1) {
String desc = "NET_DVR_SendWithRecvRemoteConfig接口调用失败,错误码:" + sdk.NET_DVR_GetLastError();
logger.info(desc);
break;
} else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_NEEDWAIT) {
logger.info("配置等待");
Thread.sleep(10);
continue;
} else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_FAILED) {
String desc = "获取卡参数失败,错误码:" + sdk.NET_DVR_GetLastError();
logger.info(desc);
break;
} else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_EXCEPTION) {
String desc = "获取卡参数异常,错误码:" + sdk.NET_DVR_GetLastError();
logger.info(desc);
break;
} else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_SUCCESS) {
UserBean userInfo = new UserBean();
userInfo.setName(new String(struCardRecord.byName, "UTF-8").trim());
userInfo.setPwd(new String(struCardRecord.byCardPassword, "UTF-8").trim());
userInfo.setCardNo(new String(struCardRecord.byCardNo).trim());
userInfo.setCardType((int)struCardRecord.byCardType);
logger.info("获取卡信息为:" + JSONObject.toJSONString(userInfo));
list.add(userInfo);
continue;
} else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_FINISH) {
String desc = ("获取卡参数完成");
break;
}
}
3. 对设备进行插入用户数据
HCNetSDK.NET_DVR_CARD_COND struCardCond = new HCNetSDK.NET_DVR_CARD_COND();
struCardCond.read();
struCardCond.dwSize = struCardCond.size();
struCardCond.dwCardNum = 1; // 下发一张
struCardCond.write();
Pointer ptrStruCond = struCardCond.getPointer();
m_lSetCardCfgHandle = sdk.NET_DVR_StartRemoteConfig(userId, HCNetSDK.NET_DVR_SET_CARD, ptrStruCond,
struCardCond.size(), null, null);
if (m_lSetCardCfgHandle == -1) {
String desc = "下发卡建立长连接失败,错误码为" + sdk.NET_DVR_GetLastError();
logger.info(desc);
return res;
} else {
logger.info("下发卡建立长连接成功!");
}
HCNetSDK.NET_DVR_CARD_RECORD struCardRecord = new HCNetSDK.NET_DVR_CARD_RECORD();
struCardRecord.read();
struCardRecord.dwSize = struCardRecord.size();
for (int i = 0; i < HCNetSDK.ACS_CARD_NO_LEN; i++) {
struCardRecord.byCardNo[i] = 0;
}
byte[] cardBytes = user.getCardNo().getBytes();
for (int i = 0; i < user.getCardNo().length(); i++) {
struCardRecord.byCardNo[i] = cardBytes[i];
}
struCardRecord.byCardType = 1; // 普通卡
struCardRecord.byLeaderCard = 0; // 是否为首卡,0-否,1-是
struCardRecord.byUserType = 0;
struCardRecord.byDoorRight[0] = 1; // 门1有权限
struCardRecord.struValid.byEnable = 1; // 卡有效期使能,下面是卡有效期从2000-1-1 11:11:11到2030-1-1 11:11:11
struCardRecord.struValid.struBeginTime.wYear = 2000;
struCardRecord.struValid.struBeginTime.byMonth = 1;
struCardRecord.struValid.struBeginTime.byDay = 1;
struCardRecord.struValid.struBeginTime.byHour = 11;
struCardRecord.struValid.struBeginTime.byMinute = 11;
struCardRecord.struValid.struBeginTime.bySecond = 11;
struCardRecord.struValid.struEndTime.wYear = 2030;
struCardRecord.struValid.struEndTime.byMonth = 1;
struCardRecord.struValid.struEndTime.byDay = 1;
struCardRecord.struValid.struEndTime.byHour = 11;
struCardRecord.struValid.struEndTime.byMinute = 11;
struCardRecord.struValid.struEndTime.bySecond = 11;
struCardRecord.wCardRightPlan[0] = 1;// 卡计划模板1有效
struCardRecord.dwEmployeeNo = Integer.valueOf(user.getWkno()); // 工号
byte[] strCardName = user.getName().getBytes("UTF-8"); // 姓名
for (int i = 0; i < HCNetSDK.NAME_LEN; i++) {
struCardRecord.byName[i] = 0;
}
for (int i = 0; i < strCardName.length; i++) {
struCardRecord.byName[i] = strCardName[i];
}
byte[] strCardPwd = user.getPwd().getBytes();
for (int i = 0; i < HCNetSDK.CARD_PASSWORD_LEN; i++) {
struCardRecord.byCardPassword[i] = 0;
}
for (int i = 0; i < strCardPwd.length; i++) {
struCardRecord.byCardPassword[i] = strCardPwd[i];
}
struCardRecord.write();
HCNetSDK.NET_DVR_CARD_STATUS struCardStatus = new HCNetSDK.NET_DVR_CARD_STATUS();
struCardStatus.read();
struCardStatus.dwSize = struCardStatus.size();
struCardStatus.write();
IntByReference pInt = new IntByReference(0);
while (true) {
dwState = sdk.NET_DVR_SendWithRecvRemoteConfig(m_lSetCardCfgHandle, struCardRecord.getPointer(),
struCardRecord.size(), struCardStatus.getPointer(), struCardStatus.size(), pInt);
struCardStatus.read();
if (dwState == -1) {
String desc = "下发卡NET_DVR_SendWithRecvRemoteConfig接口调用失败,错误码:" + sdk.NET_DVR_GetLastError();
logger.info(desc);
break;
} else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_NEEDWAIT) {
logger.info("下发卡配置等待");
Thread.sleep(10);
continue;
} else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_FAILED) {
String desc = "下发卡失败, 卡号: " + new String(struCardStatus.byCardNo).trim() + ", 错误码:"
+ struCardStatus.dwErrorCode;
logger.info(desc);
break;
} else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_EXCEPTION) {
String desc = "下发卡异常, 卡号: " + new String(struCardStatus.byCardNo).trim() + ", 错误码:"
+ struCardStatus.dwErrorCode;
logger.info(desc);
break;
} else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_SUCCESS) {
if (struCardStatus.dwErrorCode != 0) {
String desc = "下发卡成功,但是错误码" + struCardStatus.dwErrorCode + ", 卡号:"
+ new String(struCardStatus.byCardNo).trim();
logger.info(desc);
} else {
String desc = "下发卡成功, 卡号: " + new String(struCardStatus.byCardNo).trim() + ", 状态:"
+ struCardStatus.byStatus;
logger.info(desc);
}
continue;
} else if (dwState == HCNetSDK.NET_SDK_CONFIG_STATUS_FINISH) {
String desc = "下发卡完成";
logger.info(desc);
break;
}
}
4.下发人脸
int m_lSetFaceCfgHandle = -1; // 下发人脸长连接句柄
int dwFaceState = -1; // 下发人脸数据状态
HCNetSDK.NET_DVR_FACE_COND struFaceCond = new HCNetSDK.NET_DVR_FACE_COND();
struFaceCond.read();
struFaceCond.dwSize = struFaceCond.size();
struFaceCond.byCardNo = (user.getCardNo() + "1").getBytes();
struFaceCond.dwFaceNum = 1; // 下发一张
struFaceCond.dwEnableReaderNo = 1;// 人脸读卡器编号
struFaceCond.write();
Pointer ptrStruFaceCond = struFaceCond.getPointer();
m_lSetFaceCfgHandle = sdk.NET_DVR_StartRemoteConfig(userId, HCNetSDK.NET_DVR_SET_FACE, ptrStruFaceCond,
struFaceCond.size(), null, null);
if (m_lSetFaceCfgHandle == -1) {
String desc = "下发人脸建立长连接失败,错误码为" + sdk.NET_DVR_GetLastError();
logger.info(desc);
return res;
} else {
logger.info("下发人脸建立长连接成功!");
}
HCNetSDK.NET_DVR_FACE_RECORD struFaceRecord = new HCNetSDK.NET_DVR_FACE_RECORD();
struFaceRecord.read();
struFaceRecord.dwSize = struFaceRecord.size();
for (int i = 0; i < HCNetSDK.ACS_CARD_NO_LEN; i++) {
struFaceRecord.byCardNo[i] = 0;
}
byte[] cardNoByte = user.getCardNo().getBytes();
for (int i = 0; i < user.getCardNo().length(); i++) {
struFaceRecord.byCardNo[i] = cardNoByte[i];
}
/*****************************************
* 文件里二进制数据
*****************************************/
InputStream picfile = new ByteArrayInputStream(imageBytes);
int picdataLength = 0;
try {
picdataLength = picfile.available();
} catch (IOException e1) {
e1.printStackTrace();
}
HCNetSDK.BYTE_ARRAY ptrpicByte = new HCNetSDK.BYTE_ARRAY(picdataLength);
try {
picfile.read(ptrpicByte.byValue);
} catch (IOException e2) {
e2.printStackTrace();
}
ptrpicByte.write();
struFaceRecord.dwFaceLen = picdataLength;
struFaceRecord.pFaceBuffer = ptrpicByte.getPointer();
struFaceRecord.write();
HCNetSDK.NET_DVR_FACE_STATUS struFaceStatus = new HCNetSDK.NET_DVR_FACE_STATUS();
struFaceStatus.read();
struFaceStatus.dwSize = struFaceStatus.size();
struFaceStatus.write();
IntByReference pInt = new IntByReference(0);
while (true) {
dwFaceState = sdk.NET_DVR_SendWithRecvRemoteConfig(m_lSetFaceCfgHandle, struFaceRecord.getPointer(),
struFaceRecord.size(), struFaceStatus.getPointer(), struFaceStatus.size(), pInt);
struFaceStatus.read();
if (dwFaceState == -1) {
String desc = ("下发人脸 NET_DVR_SendWithRecvRemoteConfig接口调用失败,错误码:" + sdk.NET_DVR_GetLastError());
logger.info(desc);
break;
} else if (dwFaceState == HCNetSDK.NET_SDK_CONFIG_STATUS_NEEDWAIT) {
logger.info("下发人脸 配置等待");
Thread.sleep(10);
continue;
} else if (dwFaceState == HCNetSDK.NET_SDK_CONFIG_STATUS_FAILED) {
String desc = ("下发人脸失败, 卡号: " + new String(struFaceStatus.byCardNo).trim() + ", 错误码:"
+ sdk.NET_DVR_GetLastError());
logger.info(desc);
break;
} else if (dwFaceState == HCNetSDK.NET_SDK_CONFIG_STATUS_EXCEPTION) {
String desc = ("下发人脸异常, 卡号: " + new String(struFaceStatus.byCardNo).trim() + ", 错误码:"
+ sdk.NET_DVR_GetLastError());
logger.info(desc);
break;
} else if (dwFaceState == HCNetSDK.NET_SDK_CONFIG_STATUS_SUCCESS) {
if (struFaceStatus.byRecvStatus != 1) {
String desc = ("下发人脸失败,人脸读卡器状态:" + struFaceStatus.byRecvStatus + ", 卡号:"
+ new String(struFaceStatus.byCardNo).trim());
logger.info(desc);
break;
} else {
String desc = ("下发人脸成功, 卡号: " + new String(struFaceStatus.byCardNo).trim() + ", 状态:"
+ struFaceStatus.byRecvStatus);
logger.info(desc);
}
continue;
} else if (dwFaceState == HCNetSDK.NET_SDK_CONFIG_STATUS_FINISH) {
String desc = ("下发人脸完成,卡号: " + new String(struFaceStatus.byCardNo).trim());
logger.info(desc);
res.setResult(ActionResult.RES_SUCC);
res.setDescription(desc);
break;
}
}
5. 设备布防监听
5.1 创建监听回调类
public class AlarmHikFMSGCallBack implements HCNetSDK.FMSGCallBack {
Logger logger = LoggerFactory.getLogger(getClass());
public AlarmHikFMSGCallBack() {
super();
}
@Override
public void invoke(int lCommand, NET_DVR_ALARMER pAlarmer, Pointer pAlarmInfo, int dwBufLen, Pointer pUser) {
String sAlarmType = new String();
// DefaultTableModel alarmTableModel = ((DefaultTableModel)
// jTableAlarm.getModel());//获取表格模型
String[] newRow = new String[3];
// 报警时间
Date today = new Date();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String[] sIP = new String[2];
sAlarmType = new String("lCommand=") + lCommand;
// lCommand是传的报警类型
int cmdValue = lCommand;
logger.info("模式类型:" + cmdValue);
switch (cmdValue) {
case HCNetSDK.COMM_ALARM_V30:
HCNetSDK.NET_DVR_ALARMINFO_V30 strAlarmInfoV30 = new HCNetSDK.NET_DVR_ALARMINFO_V30();
strAlarmInfoV30.write();
Pointer pInfoV30 = strAlarmInfoV30.getPointer();
pInfoV30.write(0, pAlarmInfo.getByteArray(0, strAlarmInfoV30.size()), 0, strAlarmInfoV30.size());
strAlarmInfoV30.read();
switch (strAlarmInfoV30.dwAlarmType) {
case 0:
sAlarmType = sAlarmType + new String(":信号量报警") + "," + "报警输入口:"
+ (strAlarmInfoV30.dwAlarmInputNumber + 1);
break;
case 1:
sAlarmType = sAlarmType + new String(":硬盘满");
break;
case 2:
sAlarmType = sAlarmType + new String(":信号丢失");
break;
case 3:
sAlarmType = sAlarmType + new String(":移动侦测") + "," + "报警通道:";
for (int i = 0; i < 64; i++) {
if (strAlarmInfoV30.byChannel[i] == 1) {
sAlarmType = sAlarmType + "ch" + (i + 1) + " ";
}
}
break;
case 4:
sAlarmType = sAlarmType + new String(":硬盘未格式化");
break;
case 5:
sAlarmType = sAlarmType + new String(":读写硬盘出错");
break;
case 6:
sAlarmType = sAlarmType + new String(":遮挡报警");
break;
case 7:
sAlarmType = sAlarmType + new String(":制式不匹配");
break;
case 8:
sAlarmType = sAlarmType + new String(":非法访问");
break;
}
newRow[0] = dateFormat.format(today);
// 报警类型
newRow[1] = sAlarmType;
// 报警设备IP地址
sIP = new String(pAlarmer.sDeviceIP).split("\0", 2);
newRow[2] = sIP[0];
// alarmTableModel.insertRow(0, newRow);
break;
case HCNetSDK.COMM_UPLOAD_PLATE_RESULT:
HCNetSDK.NET_DVR_PLATE_RESULT strPlateResult = new HCNetSDK.NET_DVR_PLATE_RESULT();
strPlateResult.write();
Pointer pPlateInfo = strPlateResult.getPointer();
pPlateInfo.write(0, pAlarmInfo.getByteArray(0, strPlateResult.size()), 0, strPlateResult.size());
strPlateResult.read();
try {
String srt3 = new String(strPlateResult.struPlateInfo.sLicense, "UTF-8");
sAlarmType = sAlarmType + ":交通抓拍上传,车牌:" + srt3;
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
newRow[0] = dateFormat.format(today);
// 报警类型
newRow[1] = sAlarmType;
// 报警设备IP地址
sIP = new String(pAlarmer.sDeviceIP).split("\0", 2);
newRow[2] = sIP[0];
break;
case HCNetSDK.COMM_ITS_PLATE_RESULT:
HCNetSDK.NET_ITS_PLATE_RESULT strItsPlateResult = new HCNetSDK.NET_ITS_PLATE_RESULT();
strItsPlateResult.write();
Pointer pItsPlateInfo = strItsPlateResult.getPointer();
pItsPlateInfo.write(0, pAlarmInfo.getByteArray(0, strItsPlateResult.size()), 0,
strItsPlateResult.size());
strItsPlateResult.read();
String srt3;
try {
srt3 = new String(strItsPlateResult.struPlateInfo.sLicense, "UTF-8").trim().substring(1);
logger.info("车牌号:" + srt3);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
break;
case HCNetSDK.COMM_ALARM_ACS: // 门禁主机报警信息
try {
HCNetSDK.NET_DVR_ACS_ALARM_INFO strACSInfo = new HCNetSDK.NET_DVR_ACS_ALARM_INFO();
strACSInfo.write();
Pointer pACSInfo = strACSInfo.getPointer();
pACSInfo.write(0, pAlarmInfo.getByteArray(0, strACSInfo.size()), 0, strACSInfo.size());
strACSInfo.read();
// 报警设备IP地址
sIP = new String(pAlarmer.sDeviceIP).split("\0", 2);
String cardNo = new String(strACSInfo.struAcsEventInfo.byCardNo).trim();
String serialNumber = new String(pAlarmer.sSerialNumber).trim();
newRow[2] = sIP[0];
sAlarmType = sAlarmType + ":门禁主机报警信息,卡号:" + cardNo + ",卡类型:"
+ strACSInfo.struAcsEventInfo.byCardType + ",报警主类型:" + strACSInfo.dwMajor + ",报警次类型:"
+ strACSInfo.dwMinor + ",设备编号:" + strACSInfo.struAcsEventInfo.byDeviceNo + "远程主机::"
+ new String(pAlarmer.byMacAddr) + ",设备ip地址:" + sIP[0] + ",序列号 :" + serialNumber + ",设备名称 :"
+ new String(pAlarmer.sDeviceName);
logger.info(sAlarmType);
if (StringUtils.isNotEmpty(cardNo)) {
JSONObject content = new JSONObject();
String image = "";
if (strACSInfo.dwPicDataLen > 0) {
try {
long offset = 0;
ByteBuffer buffers = strACSInfo.pPicData.getByteBuffer(offset, strACSInfo.dwPicDataLen);
byte[] bytes = new byte[strACSInfo.dwPicDataLen];
// image = new String(bytes, "ISO-8859-1");
buffers.rewind();
buffers.get(bytes);
image = Base64.encodeBase64String(bytes);
} catch (Exception e) {
logger.error("图片转字节异常", e);
}
}
content.put("deviceIp", deviceIp);
content.put("devicePort", devicePort);
content.put("cardNo", cardNo);
// content.put("serialNumber", serialNumber);
content.put("image", image);
logger.info("收集信息为:" + content.toJSONString());
} else {
logger.info("卡号为空, 非管理人员");
}
} catch (Exception e) {
logger.error("门禁主机报警处理异常", e);
}
break;
default:
newRow[0] = dateFormat.format(today);
// 报警类型
newRow[1] = sAlarmType;
// 报警设备IP地址
sIP = new String(pAlarmer.sDeviceIP).split("\0", 2);
newRow[2] = sIP[0];
// alarmTableModel.insertRow(0, newRow);
logger.info("未找到匹配模式");
break;
}
}
}
5.2 创建布防监听
public void setupAlarmChan() {
try {
int lAlarmHandle;// 布防句柄
HCNetSDK sdk = getSdk();
int userId =
getlUserID(Ip, Port, DeviceName, DevicePwd);
if (userId == -1) {
String desc = "注册失败,错误代码:" + sdk.NET_DVR_GetLastError();
res.setResult(AlarmResult.RES_LOGIN_FAIL);
res.setDescription(desc);
logger.info(desc);
return res;
}
AlarmHikFMSGCallBack fMSFCallBack =
new AlarmHikFMSGCallBack();
Pointer pUser = null;
if (!sdk.NET_DVR_SetDVRMessageCallBack_V30(fMSFCallBack, pUser)) {
logger.info("设置回调函数失败!");
}
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 = 0;// 客户端布防
m_strAlarmInfo.write();
lAlarmHandle = sdk.NET_DVR_SetupAlarmChan_V41(userId, m_strAlarmInfo);
if (lAlarmHandle == -1) {
String desc = ("布防失败, 错误代码:" + sdk.NET_DVR_GetLastError());
logger.info(desc);
} else {
String desc = "布防成功";
logger.info(desc);
}
} catch (Exception e) {
String desc = "布防处理异常";
logger.error(desc, e);
}
}
未完待续......