java对接海康门禁人脸添加代码
时间: 2023-08-10 18:02:20 浏览: 206
JAVA海康威视人脸机isup方式对接demo包
对接海康门禁添加人脸需要使用海康门禁SDK,以下是Java调用海康门禁SDK实现对门禁设备添加人脸的示例代码:
```java
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.ByteByReference;
import com.sun.jna.ptr.IntByReference;
public class HikvisionFaceController {
public interface HikvisionLibrary extends com.sun.jna.Library {
HikvisionLibrary INSTANCE = (HikvisionLibrary) Native.loadLibrary("HCNetSDK", HikvisionLibrary.class);
int NET_DVR_Init();
int NET_DVR_Login_V40();
int NET_DVR_Logout_V30();
int NET_DVR_GetLastError();
int NET_DVR_SetDVRConfig();
int NET_DVR_GetDVRConfig();
int NET_DVR_SendRemoteConfig();
int NET_DVR_StopRemoteConfig();
}
public static void main(String[] args) {
// 初始化SDK
HikvisionLibrary.INSTANCE.NET_DVR_Init();
// 登录门禁
IntByReference lUserID = new IntByReference();
HikvisionLibrary.NET_DVR_DEVICEINFO_V40 struDeviceInfo = new HikvisionLibrary.NET_DVR_DEVICEINFO_V40();
lUserID.setValue(HikvisionLibrary.INSTANCE.NET_DVR_Login_V40("192.168.1.64", (short) 8000, "admin", "password", struDeviceInfo));
// 添加人脸
HikvisionLibrary.NET_DVR_XML_CONFIG_INPUT struInput = new HikvisionLibrary.NET_DVR_XML_CONFIG_INPUT();
struInput.dwSize = struInput.size();
String str = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<FaceAppendData>" +
"<name>test</name>" +
"<gender>1</gender>" +
"<bornTime>19900101</bornTime>" +
"<certificateType>111</certificateType>" +
"<certificateNumber>123456789012345678</certificateNumber>" +
"<certificatePicUrl>http://192.168.1.64/certificate.jpg</certificatePicUrl>" +
"<facePicUrl>http://192.168.1.64/face.jpg</facePicUrl>" +
"</FaceAppendData>";
byte[] byInput = str.getBytes();
struInput.lpRequestUrl = "POST /ISAPI/Intelligent/FDLib/FDSearch/AddFaceDataInFDLib?format=json".getBytes();
struInput.dwRequestUrlLen = struInput.lpRequestUrl.length;
struInput.lpInBuffer = byInput;
struInput.dwInBufferSize = byInput.length;
struInput.write();
Pointer lpInBuffer = struInput.getPointer();
HikvisionLibrary.NET_DVR_XML_CONFIG_OUTPUT struOutput = new HikvisionLibrary.NET_DVR_XML_CONFIG_OUTPUT();
struOutput.dwSize = struOutput.size();
struOutput.lpOutBuffer = new byte[1024];
struOutput.dwOutBufferSize = struOutput.lpOutBuffer.length;
struOutput.write();
Pointer lpOutBuffer = struOutput.getPointer();
boolean result = HikvisionLibrary.INSTANCE.NET_DVR_SendRemoteConfig(lUserID.getValue(), 255, HikvisionLibrary.NET_DVR_SET_FACEAPPEND_DATA, lpInBuffer, struInput.size(), lpOutBuffer, struOutput.size());
if (!result) {
System.out.println("添加人脸失败,错误码:" + HikvisionLibrary.INSTANCE.NET_DVR_GetLastError());
}
// 注销登录
HikvisionLibrary.INSTANCE.NET_DVR_Logout_V30(lUserID.getValue());
}
}
```
需要注意的是,代码中的IP地址、端口、用户名、密码等信息需要替换为实际的门禁设备信息。同时,需要将海康门禁SDK的库文件 HCNetSDK.dll 放置在Java工程的运行路径下。人脸信息需要按照门禁设备接口要求的XML格式进行组装,并通过海康门禁SDK的接口发送到门禁设备。
阅读全文