java对接海康门禁代码
时间: 2023-07-03 19:24:13 浏览: 215
java对接大华门禁二次开发
5星 · 资源好评率100%
对接海康门禁需要使用海康门禁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 HikvisionDoorController {
public interface HikvisionLibrary extends com.sun.jna.Library {
HikvisionLibrary INSTANCE = (HikvisionLibrary) Native.loadLibrary("CHCNetSDK", HikvisionLibrary.class);
int NET_DVR_Init();
int NET_DVR_Login_V40();
int NET_DVR_Logout_V30();
int NET_DVR_ControlGateway_V50();
}
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_GATEWAY_CONTROL_CFG struControlCfg = new HikvisionLibrary.NET_DVR_GATEWAY_CONTROL_CFG();
struControlCfg.dwSize = struControlCfg.size();
struControlCfg.byControlType = 0x02; // 开门
struControlCfg.byOpenDoorMode = 0x00; // 常开模式
struControlCfg.byLockType = 0x00; // 无锁
struControlCfg.byDoorTerminalMode = 0x00; // 门禁终端模式
struControlCfg.byRes = new byte[31]; // 保留字段
Pointer lpInBuffer = struControlCfg.getPointer();
byte[] byOutBuffer = new byte[1024];
IntByReference lpBytesReturned = new IntByReference();
lpBytesReturned.setValue(0);
ByteByReference lpStatus = new ByteByReference();
lpStatus.setValue((byte) 0);
boolean result = HikvisionLibrary.INSTANCE.NET_DVR_ControlGateway_V50(lUserID.getValue(), 0, lpInBuffer, struControlCfg.size(), byOutBuffer, 1024, lpBytesReturned, lpStatus, null);
if (!result) {
System.out.println("开门失败");
}
// 注销登录
HikvisionLibrary.INSTANCE.NET_DVR_Logout_V30(lUserID.getValue());
}
}
```
需要注意的是,代码中的IP地址、端口、用户名、密码等信息需要替换为实际的门禁设备信息。同时,需要将海康门禁SDK的库文件 CHCNetSDK.dll 放置在Java工程的运行路径下。
阅读全文