本文中代码基于前面文章 对接海康明眸门禁设备-初始化SDK和登录设备来写的。
/**
* 按用户名称查询
* camera 设备信息
* name 用户名称
*/
public List<IotPersonFaceResult> getUserInfoByName(IotCameraParam camera, String name) {
//登录设备
int lUserID = -1;
int longConnt = -1;
try {
lUserID = login_V40(camera.getIp(), new Short(String.valueOf(camera.getPort())), camera.getUsername(), camera.getPassword());
if (lUserID == -1) {
log.warn("登录失败,错误码为 :{}", hCNetSDK.NET_DVR_GetLastError());
return Collections.emptyList();
}
String strInBuffer = "POST /ISAPI/AccessControl/UserInfo/Search?format=json";
longConnt = getLongConnt(strInBuffer, IotHikNetConstant.NET_DVR_JSON_CONFIG, lUserID);
if (longConnt < 0) {
log.warn("SearchUserInfo NET_DVR_StartRemoteConfig 失败,错误码为:{}", hCNetSDK.NET_DVR_GetLastError());
return Collections.emptyList();
}
String employeeData = getUserByName(name, longConnt);
if (StrUtil.isEmpty(employeeData)) {
return Collections.emptyList();
}
//解析数据
List<IotPersonFaceResult> faceResults = new ArrayList<>();
JSONObject objectData = JSONUtil.parseObj(employeeData);
JSONObject userInfoSearch = objectData.getJSONObject("UserInfoSearch");
if (ObjectUtil.isNull(userInfoSearch)) {
return faceResults;
}
JSONArray jsonArray = userInfoSearch.getJSONArray("UserInfo");
if (jsonArray == null || jsonArray.size() < 1) {
return faceResults;
}
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject jsonData = (JSONObject) jsonArray.get(i);
IotPersonFaceResult result = new IotPersonFaceResult();
result.setUserId(jsonData.getStr("employeeNo"));
result.setUserName(jsonData.getStr("name"));
faceResults.add(result);
}
return faceResults;
} catch (Exception e) {
log.error("海康查询用户", e);
return Collections.emptyList();
} finally {
//关闭长连接,释放资源
if (!hCNetSDK.NET_DVR_StopRemoteConfig(longConnt)) {
log.warn("NET_DVR_StopRemoteConfig接口调用失败,错误码:{}", hCNetSDK.NET_DVR_GetLastError());
} else {
log.info("NET_DVR_StopRemoteConfig接口成功");
}
//退出登录
logout(lUserID);
}
}
/**
* 按名称查询
* name 用户名称
* longConnt 长连接
*/
private static String getUserByName(String name, int longConnt) {
if (StrUtil.isEmpty(name)) {
log.warn("userIds 参数为空");
return null;
}
//组装查询的JSON报文,这边查询的是所有的人员
JSONObject jsonObject = new JSONObject();
JSONObject jsonSearchCond = new JSONObject();
jsonSearchCond.set("searchID", String.valueOf(DateUtil.currentSeconds()));
jsonSearchCond.set("searchResultPosition", 0);
jsonSearchCond.set("maxResults", 20);
/*opt, string, 模糊查询关键字, range:[,]*/
// "fuzzySearch": "test"
// jsonSearchCond.set("EmployeeNoList", null);
jsonSearchCond.set("fuzzySearch", name);
jsonObject.set("UserInfoSearchCond", jsonSearchCond);
String strInbuff = jsonObject.toString();
log.info("查询的json报文:" + strInbuff);
//把string传递到Byte数组中,后续用.getPointer()方法传入指针地址中。
BYTE_ARRAY ptrInbuff = new BYTE_ARRAY(strInbuff.length());
System.arraycopy(strInbuff.getBytes(), 0, ptrInbuff.byValue, 0, strInbuff.length());
ptrInbuff.write();
//定义接收结果的结构体
BYTE_ARRAY ptrOutuff = new BYTE_ARRAY(10 * 1024);
IntByReference pInt = new IntByReference(0);
/*
*dwOutBuffSize是输出缓冲区大小,需要自定义指定大小,如果接口报错错误码43.说明接收设备数据的缓冲区或存放图片缓冲区不足,应扩大缓冲区大小
*/
int dwState = hCNetSDK.NET_DVR_SendWithRecvRemoteConfig(longConnt, ptrInbuff.getPointer(), strInbuff.length(), ptrOutuff.getPointer(), 20 * 1024, pInt);
log.info("查询返回值 :{}", dwState);
if (dwState == -1) {
log.warn("NET_DVR_SendWithRecvRemoteConfig接口调用失败,错误码:{},{}", hCNetSDK.NET_DVR_GetLastError(), IotHkExceptionEnum.getMsg(hCNetSDK.NET_DVR_GetLastError()));
} else if (dwState == IotHikNetConstant.NET_SDK_CONFIG_STATUS_NEED_WAIT) {
log.info("配置等待");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
log.warn("配置等待", e);
}
} else if (dwState == IotHikNetConstant.NET_SDK_CONFIG_STATUS_FAILED) {
log.info("查询人员失败");
log.warn("错误码:{},{}", hCNetSDK.NET_DVR_GetLastError(), IotHkExceptionEnum.getMsg(hCNetSDK.NET_DVR_GetLastError()));
return null;
} else if (dwState == IotHikNetConstant.NET_SDK_CONFIG_STATUS_EXCEPTION) {
log.info("查询人员异常");
log.warn("错误码:{},{}", hCNetSDK.NET_DVR_GetLastError(), IotHkExceptionEnum.getMsg(hCNetSDK.NET_DVR_GetLastError()));
return null;
} else if (dwState == IotHikNetConstant.NET_SDK_CONFIG_STATUS_SUCCESS) {
ptrOutuff.read();
log.info("查询人员成功");
String jsonObj = new String(ptrOutuff.byValue).trim();
return new String(ptrOutuff.byValue).trim();
} else if (dwState == IotHikNetConstant.NET_SDK_CONFIG_STATUS_FINISH) {
log.info("获取人员完成");
}
return null;
}
/**
* 根据用户id查
* camera 设备信息
* userIds 用户id集合
*/
public List<IotPersonFaceResult> getFaceByUserId(IotCameraParam camera, Collection<Long> userIds) {
//登录设备
int lUserID = login_V40(camera.getIp(), new Short(String.valueOf(camera.getPort())), camera.getUsername(), camera.getPassword());
if (lUserID == -1) {
log.warn("登录失败,错误码为 :{}", hCNetSDK.NET_DVR_GetLastError());
return Collections.emptyList();
}
String strInBuffer = "POST /ISAPI/AccessControl/UserInfo/Search?format=json";
int longConnt = this.getLongConnt(strInBuffer, IotHikNetConstant.NET_DVR_JSON_CONFIG, lUserID);
if (longConnt < 0) {
log.warn("SearchUserInfo NET_DVR_StartRemoteConfig 失败,错误码为:{}", hCNetSDK.NET_DVR_GetLastError());
return Collections.emptyList();
}
String employeeData = this.getUserByEmployeeNo(userIds, longConnt);
if (StrUtil.isEmpty(employeeData)) {
return Collections.emptyList();
}
//解析数据
List<IotPersonFaceResult> faceResults = new ArrayList<>();
JSONObject objectData = JSONUtil.parseObj(employeeData);
JSONObject userInfoSearch = objectData.getJSONObject("UserInfoSearch");
JSONArray jsonArray = userInfoSearch.getJSONArray("UserInfo");
if (jsonArray == null || jsonArray.size() < 1) {
return faceResults;
}
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject jsonData = (JSONObject) jsonArray.get(i);
IotPersonFaceResult result = new IotPersonFaceResult();
result.setUserId(jsonData.getStr("employeeNo"));
result.setUserName(jsonData.getStr("name"));
faceResults.add(result);
}
//关闭长连接,释放资源
if (!hCNetSDK.NET_DVR_StopRemoteConfig(longConnt)) {
log.warn("NET_DVR_StopRemoteConfig接口调用失败,错误码:{}", hCNetSDK.NET_DVR_GetLastError());
} else {
log.info("NET_DVR_StopRemoteConfig接口成功");
}
//退出登录
logout(lUserID);
return faceResults;
}
/**
* 按员工编号查询
* userIds 用户id集合
* longConnt 长连接
*/
private String getUserByEmployeeNo(Collection<Long> userIds, int longConnt) {
if (CollUtil.isEmpty(userIds)) {
log.warn("userIds 参数为空");
return null;
}
//组装查询的JSON报文,这边查询的是所有的人员
JSONObject jsonObject = new JSONObject();
JSONObject jsonSearchCond = new JSONObject();
//如果需要查询指定的工号人员信息,把下面注释的内容去除掉即可
JSONArray employeeNoList = new JSONArray();
List<Long> userIdList = new ArrayList<>(userIds);
for (int i = 0; i < userIdList.size(); i++) {
JSONObject employeeNo = new JSONObject();
employeeNo.set("employeeNo", String.valueOf(userIdList.get(i)));
employeeNoList.put(employeeNo);
}
jsonSearchCond.set("EmployeeNoList", employeeNoList);
jsonSearchCond.set("searchID", IdWorker.getIdStr());
jsonSearchCond.set("searchResultPosition", 0);
jsonSearchCond.set("maxResults", 100);
jsonObject.set("UserInfoSearchCond", jsonSearchCond);
String strInbuff = jsonObject.toString();
log.info("查询的json报文:" + strInbuff);
//把string传递到Byte数组中,后续用.getPointer()方法传入指针地址中。
BYTE_ARRAY ptrInbuff = new BYTE_ARRAY(strInbuff.length());
System.arraycopy(strInbuff.getBytes(), 0, ptrInbuff.byValue, 0, strInbuff.length());
ptrInbuff.write();
//定义接收结果的结构体
BYTE_ARRAY ptrOutuff = new BYTE_ARRAY(10 * 1024);
IntByReference pInt = new IntByReference(0);
/*
*dwOutBuffSize是输出缓冲区大小,需要自定义指定大小,如果接口报错错误码43.说明接收设备数据的缓冲区或存放图片缓冲区不足,应扩大缓冲区大小
*/
int dwState = hCNetSDK.NET_DVR_SendWithRecvRemoteConfig(longConnt, ptrInbuff.getPointer(), strInbuff.length(), ptrOutuff.getPointer(), 20 * 1024, pInt);
log.info("查询返回值 :{}", dwState);
if (dwState == -1) {
log.warn("NET_DVR_SendWithRecvRemoteConfig接口调用失败,错误码:{},{}", hCNetSDK.NET_DVR_GetLastError(), IotHkExceptionEnum.getMsg(hCNetSDK.NET_DVR_GetLastError()));
} else if (dwState == IotHikNetConstant.NET_SDK_CONFIG_STATUS_NEED_WAIT) {
log.info("配置等待");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
log.warn("配置等待", e);
}
} else if (dwState == IotHikNetConstant.NET_SDK_CONFIG_STATUS_FAILED) {
log.info("查询人员失败");
return null;
} else if (dwState == IotHikNetConstant.NET_SDK_CONFIG_STATUS_EXCEPTION) {
log.info("查询人员异常");
return null;
} else if (dwState == IotHikNetConstant.NET_SDK_CONFIG_STATUS_SUCCESS) {
ptrOutuff.read();
log.info("查询人员成功");
return new String(ptrOutuff.byValue).trim();
} else if (dwState == IotHikNetConstant.NET_SDK_CONFIG_STATUS_FINISH) {
log.info("获取人员完成");
}
return null;
}
/**
* 查询所有用户信息
* camera 设备信息
*/
public List<IotPersonFaceResult> queryListFace(IotCameraParam camera) {
//登录设备
int lUserID = login_V40(camera.getIp(), new Short(String.valueOf(camera.getPort())), camera.getUsername(), camera.getPassword());
if (lUserID == -1) {
int error = hCNetSDK.NET_DVR_GetLastError();
log.warn("登录失败,错误码为 :{},{}", error, IotHkExceptionEnum.getMsg(error));
// IotHkExceptionEnum.getMsg(hCNetSDK.NET_DVR_GetLastError());
return Collections.emptyList();
}
//建立长连接
String strInBuffer = "POST /ISAPI/AccessControl/UserInfo/Search?format=json";
int longConnt = this.getLongConnt(strInBuffer, IotHikNetConstant.NET_DVR_JSON_CONFIG, lUserID);
if (longConnt < 0) {
int error = hCNetSDK.NET_DVR_GetLastError();
log.warn("getQueryAllUser NET_DVR_StartRemoteConfig 失败,错误码为:{},{}", error, IotHkExceptionEnum.getMsg(error));
return Collections.emptyList();
}
//获取用户总数
int rowCount = getCountUser(lUserID);
int pageSize = 100;
//计算页数
int pageNum = (rowCount + pageSize - 1) / pageSize;
List<IotPersonFaceResult> faceResults = new ArrayList<>();
for (int i = 0; i < pageNum; i++) {
//获取用户信息
this.getQueryAllList(pageSize, pageNum, faceResults, longConnt);
}
//关闭长连接,释放资源
if (!hCNetSDK.NET_DVR_StopRemoteConfig(longConnt)) {
log.warn("NET_DVR_StopRemoteConfig接口调用失败,错误码:{}", hCNetSDK.NET_DVR_GetLastError());
} else {
log.info("NET_DVR_StopRemoteConfig接口成功");
}
logout(lUserID);
return faceResults;
}
/**
* 获取总人数
*/
public int getCountUser(int lUserID) {
String countUrl = "GET /ISAPI/AccessControl/UserInfo/Count?format=json";
NET_DVR_XML_CONFIG_INPUT struXMLInput = new NET_DVR_XML_CONFIG_INPUT();
struXMLInput.read();
BYTE_ARRAY stringRequest = new BYTE_ARRAY(1024);
stringRequest.read();
//输入ISAPI协议命令
System.arraycopy(countUrl.getBytes(), 0, stringRequest.byValue, 0, countUrl.length());
stringRequest.write();
struXMLInput.dwSize = struXMLInput.size();
struXMLInput.lpRequestUrl = stringRequest.getPointer();
struXMLInput.dwRequestUrlLen = countUrl.length();
struXMLInput.lpInBuffer = null;
struXMLInput.dwInBufferSize = 0;
struXMLInput.write();
BYTE_ARRAY stringXMLOut = new BYTE_ARRAY(8 * 1024);
stringXMLOut.read();
BYTE_ARRAY struXMLStatus = new BYTE_ARRAY(1024);
struXMLStatus.read();
NET_DVR_XML_CONFIG_OUTPUT struXMLOutput = new NET_DVR_XML_CONFIG_OUTPUT();
struXMLOutput.read();
struXMLOutput.dwSize = struXMLOutput.size();
struXMLOutput.lpOutBuffer = stringXMLOut.getPointer();
struXMLOutput.dwOutBufferSize = stringXMLOut.size();
struXMLOutput.lpStatusBuffer = struXMLStatus.getPointer();
struXMLOutput.dwStatusSize = struXMLStatus.size();
struXMLOutput.write();
if (!hCNetSDK.NET_DVR_STDXMLConfig(lUserID, struXMLInput, struXMLOutput)) {
log.warn("获取人员数据量失败 :{},{}", hCNetSDK.NET_DVR_GetLastError(), IotHkExceptionEnum.getMsg(hCNetSDK.NET_DVR_GetLastError()));
return 0;
}
stringXMLOut.read();
log.info("输出文本大小:{}", struXMLOutput.dwReturnedXMLSize);
//打印输出XML文本
String strOutXML = new String(stringXMLOut.byValue).trim();
log.info("getCountUser strOutXML:{}", strOutXML);
JSONObject jsonObject = JSONUtil.parseObj(strOutXML);
JSONObject userInfoCount = jsonObject.getJSONObject("UserInfoCount");
return userInfoCount.getInt("userNumber");
}
/**
* 分页查询所有用户
* pageSize 条数
* pageNum 页数
* longConnt 长连接
* faceResults 返回信息收集集合
*/
private void getQueryAllList(int pageSize, int pageNum, List<IotPersonFaceResult> faceResults, int longConnt) {
//组装查询的JSON报文,这边查询的是所有的人员
JSONObject jsonObject = new JSONObject();
JSONObject jsonSearchCond = new JSONObject();
jsonSearchCond.set("searchID", IdWorker.getIdStr());
jsonSearchCond.set("searchResultPosition", pageNum);
jsonSearchCond.set("maxResults", pageSize);
jsonObject.set("UserInfoSearchCond", jsonSearchCond);
String strInbuff = jsonObject.toString();
log.info("查询的json报文:" + strInbuff);
//把string传递到Byte数组中,后续用.getPointer()方法传入指针地址中。
BYTE_ARRAY ptrInbuff = new BYTE_ARRAY(strInbuff.length());
System.arraycopy(strInbuff.getBytes(), 0, ptrInbuff.byValue, 0, strInbuff.length());
ptrInbuff.write();
//定义接收结果的结构体
BYTE_ARRAY ptrOutuff = new BYTE_ARRAY(10 * 1024);
IntByReference pInt = new IntByReference(0);
/*
*dwOutBuffSize是输出缓冲区大小,需要自定义指定大小,如果接口报错错误码43.说明接收设备数据的缓冲区或存放图片缓冲区不足,应扩大缓冲区大小
*/
int dwState = hCNetSDK.NET_DVR_SendWithRecvRemoteConfig(longConnt, ptrInbuff.getPointer(), strInbuff.length(), ptrOutuff.getPointer(), 20 * 1024, pInt);
log.info("查询返回值 :{}", dwState);
if (dwState == -1) {
log.warn("NET_DVR_SendWithRecvRemoteConfig接口调用失败,错误码:{},{}", hCNetSDK.NET_DVR_GetLastError(), IotHkExceptionEnum.getMsg(hCNetSDK.NET_DVR_GetLastError()));
} else if (dwState == IotHikNetConstant.NET_SDK_CONFIG_STATUS_NEED_WAIT) {
log.info("配置等待");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
log.warn("配置等待", e);
}
} else if (dwState == IotHikNetConstant.NET_SDK_CONFIG_STATUS_FAILED) {
log.info("查询人员失败");
return;
} else if (dwState == IotHikNetConstant.NET_SDK_CONFIG_STATUS_EXCEPTION) {
log.info("查询人员异常");
return;
} else if (dwState == IotHikNetConstant.NET_SDK_CONFIG_STATUS_SUCCESS) {
ptrOutuff.read();
log.info("查询人员成功");
} else if (dwState == IotHikNetConstant.NET_SDK_CONFIG_STATUS_FINISH) {
log.info("获取人员完成");
}
String jsonObj = new String(ptrOutuff.byValue).trim();
if (StrUtil.isEmpty(jsonObj)) {
log.warn("返回值为空 jsonObj :{}", jsonObj);
return;
}
JSONObject objectData = JSONUtil.parseObj(jsonObj);
JSONObject userInfoSearch = objectData.getJSONObject("UserInfoSearch");
JSONArray jsonArray = userInfoSearch.getJSONArray("UserInfo");
for (Object obj : jsonArray) {
JSONObject jsonData = (JSONObject) obj;
IotPersonFaceResult result = new IotPersonFaceResult();
result.setUserId(jsonData.getStr("employeeNo"));
result.setUserName(jsonData.getStr("name"));
faceResults.add(result);
}
}
/**
* 根据ID查询用户详情返回信息
*/
@Data
public class IotPersonFaceResult {
/**
* 用户ID
*/
private String userId;
/**
* 用户信息
*/
private String userName;
/**
* 人脸图片
*/
private String faceImage;
}