1.登录海康官网下载sdk包
https://www.hikvision.com/cn/download_61.html
2.登录海康sdk包 库文件中的ClientDemo.exe 然后添加设备 只用填写ip,用户名,密码即可 端口默认为8000
我这里的设备是连接的公司的wifi 连接后就会显示ip,用户名为admin ,密码为设备登录管理界面的密码
添加后点击预览能显示表示参数正确
3.java开发
我这里的开发流程是 扫脸——回调函数逻辑判断——远程控门
以下是我需要做的流程逻辑
3.1准备工作
将海康的库文件一股脑的全部都放入你的项目中,然后将demo里面的jna.jar和examples.jar 加载到你的项目中(注意 大华和海康都有这个jna的jar包但是两个不一样 不能共用),随后将demo中的HCNetSDK文件放入你的项目中
3.2实例化登录
public class InitModule {
HCNetSDK hcNetSDK = HCNetSDK.INSTANCE;
HCNetSDK.NET_DVR_USER_LOGIN_INFO m_strLoginInfo = new HCNetSDK.NET_DVR_USER_LOGIN_INFO();
HCNetSDK.NET_DVR_DEVICEINFO_V40 m_strDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V40();
static FMSGCallBack_V31 fMSFCallBack_V31;
public static String ip = "xxxxxxx";
public static String port = "8000";
public static String userName = "admin";
public static String password = "xxxxx";
public int loginDevice() {
//海康初始化接口
boolean status = this.hcNetSDK.NET_DVR_Init();
if (!status) {
System.out.println("初始化失败:"+ this.hcNetSDK.NET_DVR_GetCardLastError_Card());
} else {
System.out.println("初始化成功!");
}
//设置回调函数
if (fMSFCallBack_V31 == null) {
fMSFCallBack_V31 = new FMSGCallBack_V31();
Pointer pUser = null;
if (!this.hcNetSDK.NET_DVR_SetDVRMessageCallBack_V31((HCNetSDK.FMSGCallBack_V31)fMSFCallBack_V31, pUser)){
System.out.println("设置回调函数失败!");}
}
String m_sDeviceIP = ip;
this.m_strLoginInfo.sDeviceAddress = new byte[129];
System.arraycopy(m_sDeviceIP.getBytes(), 0, this.m_strLoginInfo.sDeviceAddress, 0, m_sDeviceIP.length());
String m_sUsername = userName;
this.m_strLoginInfo.sUserName = new byte[64];
System.arraycopy(m_sUsername.getBytes(), 0, this.m_strLoginInfo.sUserName, 0, m_sUsername.length());
String m_sPassword = password;
this.m_strLoginInfo.sPassword = new byte[64];
System.arraycopy(m_sPassword.getBytes(), 0, this.m_strLoginInfo.sPassword, 0, m_sPassword.length());
this.m_strLoginInfo.wPort = (short)Integer.parseInt(port);
this.m_strLoginInfo.bUseAsynLogin = false;
this.m_strLoginInfo.write();
//登录
int lUserID = this.hcNetSDK.NET_DVR_Login_V40(this.m_strLoginInfo, this.m_strDeviceInfo);
return lUserID;
}
}
3.3布防
public class RealLoadModule {
HCNetSDK hcNetSDK = HCNetSDK.INSTANCE;
public static final int ISAPI_DATA_LEN = 1024*1024;
public static final int ISAPI_STATUS_LEN = 4*4096;
public static final int BYTE_ARRAY_LEN = 1024;
public void readload(int lUserID) throws Exception {
System.out.println("Run");
CaseMenu menu = new CaseMenu();
boolean status = hcNetSDK.NET_DVR_Init();
//配置远程核验
//获取参数
String isapi = getISAPI(lUserID, "GET /ISAPI/AccessControl/AcsCfg?format=json");
System.out.println("修改前参数:"+isapi);
//修改参数
JSONObject jsonObject = JSONObject.parseObject(isapi);
JSONObject acsCfg = (JSONObject) jsonObject.get("AcsCfg");
acsCfg.put("remoteCheckDoorEnabled",true);
acsCfg.put("checkChannelType","PrivateSDK");
InetAddress addr = InetAddress.getLocalHost();
String ip = addr.getHostAddress();
//我这里是设置了我的本机地址
acsCfg.put("channelIp", "xxx.xxx.xxx.xxx");
//重新put回去
putISAPI(lUserID,"PUT /ISAPI/AccessControl/AcsCfg?format=json",jsonObject.toJSONString());
//测试参数是否修改成功
String isapi2 = getISAPI(lUserID, "GET /ISAPI/AccessControl/AcsCfg?format=json");
System.out.println("修改后参数:"+isapi2);
if (!status) {
System.out.println("初始化失败!" + this.hcNetSDK.NET_DVR_GetCardLastError_Card());
} else {
System.out.println("初始化成功");
}
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;
//这里参数改成1 可支持一台设备多个布防
m_strAlarmInfo.byDeployType = 1;
m_strAlarmInfo.write();
int lAlarmHandle = this.hcNetSDK.NET_DVR_SetupAlarmChan_V41(lUserID, m_strAlarmInfo);
if (lAlarmHandle == -1) {
System.out.println("布防失败,错误号:" + this.hcNetSDK.NET_DVR_GetLastError());
} else {
System.out.println("布防成功!");
}
menu.run();
}
public void putISAPI(int lUserID,String strUrl,String json){
String strURL = strUrl;
HCNetSDK.BYTE_ARRAY ptrUrl = new HCNetSDK.BYTE_ARRAY(BYTE_ARRAY_LEN);
System.arraycopy(strURL.getBytes(), 0, ptrUrl.byValue, 0, strURL.length());
ptrUrl.write();
HCNetSDK.BYTE_ARRAY ptrInBuffer = new HCNetSDK.BYTE_ARRAY(ISAPI_DATA_LEN);
ptrInBuffer.read();
String strInbuffer = json;
ptrInBuffer.byValue = strInbuffer.getBytes();
ptrInBuffer.write();
HCNetSDK.NET_DVR_XML_CONFIG_INPUT struXMLInput = new HCNetSDK.NET_DVR_XML_CONFIG_INPUT();
struXMLInput.read();
struXMLInput.dwSize = struXMLInput.size();
struXMLInput.lpRequestUrl = ptrUrl.getPointer();
struXMLInput.dwRequestUrlLen = ptrUrl.byValue.length;
struXMLInput.lpInBuffer = ptrInBuffer.getPointer();
struXMLInput.dwInBufferSize = ptrInBuffer.byValue.length;
struXMLInput.write();
HCNetSDK.BYTE_ARRAY ptrStatusByte = new HCNetSDK.BYTE_ARRAY(ISAPI_STATUS_LEN);
ptrStatusByte.read();
HCNetSDK.BYTE_ARRAY ptrOutByte = new HCNetSDK.BYTE_ARRAY(ISAPI_DATA_LEN);
ptrOutByte.read();
HCNetSDK.NET_DVR_XML_CONFIG_OUTPUT struXMLOutput = new HCNetSDK.NET_DVR_XML_CONFIG_OUTPUT();
struXMLOutput.read();
struXMLOutput.dwSize = struXMLOutput.size();
struXMLOutput.lpOutBuffer = ptrOutByte.getPointer();
struXMLOutput.dwOutBufferSize = ptrOutByte.size();
struXMLOutput.lpStatusBuffer = ptrStatusByte.getPointer();
struXMLOutput.dwStatusSize = ptrStatusByte.size();
struXMLOutput.write();
if(!hcNetSDK.NET_DVR_STDXMLConfig(lUserID, struXMLInput, struXMLOutput)) {
int iErr = hcNetSDK.NET_DVR_GetLastError();
return;
} else {
struXMLOutput.read();
ptrOutByte.read();
ptrStatusByte.read();
}
}
public String getISAPI(int lUserID,String strUrl){
String strURL = strUrl;
HCNetSDK.BYTE_ARRAY ptrUrl = new HCNetSDK.BYTE_ARRAY(BYTE_ARRAY_LEN);
System.arraycopy(strURL.getBytes(), 0, ptrUrl.byValue, 0, strURL.length());
ptrUrl.write();
HCNetSDK.NET_DVR_XML_CONFIG_INPUT struXMLInput = new HCNetSDK.NET_DVR_XML_CONFIG_INPUT();
struXMLInput.read();
struXMLInput.dwSize = struXMLInput.size();
struXMLInput.lpRequestUrl = ptrUrl.getPointer();
struXMLInput.dwRequestUrlLen = ptrUrl.byValue.length;
struXMLInput.lpInBuffer = null;
struXMLInput.dwInBufferSize = 0;
struXMLInput.write();
HCNetSDK.BYTE_ARRAY ptrStatusByte = new HCNetSDK.BYTE_ARRAY(ISAPI_STATUS_LEN);
ptrStatusByte.read();
HCNetSDK.BYTE_ARRAY ptrOutByte = new HCNetSDK.BYTE_ARRAY(ISAPI_DATA_LEN);
ptrOutByte.read();
HCNetSDK.NET_DVR_XML_CONFIG_OUTPUT struXMLOutput = new HCNetSDK.NET_DVR_XML_CONFIG_OUTPUT();
struXMLOutput.read();
struXMLOutput.dwSize = struXMLOutput.size();
struXMLOutput.lpOutBuffer = ptrOutByte.getPointer();
struXMLOutput.dwOutBufferSize = ptrOutByte.size();
struXMLOutput.lpStatusBuffer = ptrStatusByte.getPointer();
struXMLOutput.dwStatusSize = ptrStatusByte.size();
struXMLOutput.write();
if(!hcNetSDK.NET_DVR_STDXMLConfig(lUserID, struXMLInput, struXMLOutput)) {
int iErr = hcNetSDK.NET_DVR_GetLastError();
return "";
} else {
struXMLOutput.read();
ptrOutByte.read();
ptrStatusByte.read();
String strOutXML = new String(ptrOutByte.byValue).trim();
return strOutXML;
}
}
public class CaseMenu {
public static class Item {
private Object object;
private String itemName;
private String methodName;
public Item(Object object, String itemName, String methodName) {
super();
this.object = object;
this.itemName = itemName;
this.methodName = methodName;
}
public Object getObject() {
return object;
}
public String getItemName() {
return itemName;
}
public String getMethodName() {
return methodName;
}
}
private Vector<Item> items;
public CaseMenu() {
super();
items = new Vector<Item>();
}
public void addItem(Item item) {
items.add(item);
}
private void showItem() {
final String format = "%2d\t%-20s\n";
int index = 0;
System.out.printf(format, index++, "exit App");
for (Item item : items) {
System.out.printf(format, index++, item.getItemName());
}
System.out.println("Please input a item index to invoke the method:");
}
public void run() {
Scanner scanner = new Scanner(System.in);
while(true) {
showItem();
try {
int input = Integer.parseInt(scanner.nextLine());
if (input <= 0 ) {
System.err.println("input <= 0 || scanner.nextLine() == null");
break;
}
if (input < 0 || input > items.size()) {
System.err.println("Input Error Item Index.");
continue;
}
Item item = items.get(input - 1);
Class<?> itemClass = item.getObject().getClass();
Method method = itemClass.getMethod(item.getMethodName());
method.invoke(item.getObject());
} catch (NoSuchElementException e) {
break;
} catch (NumberFormatException e) {
System.err.println("Input Error NumberFormat.");
continue;
} catch (Exception e) {
e.printStackTrace();
}
}
scanner.close();
}
3.4回调函数
public class FMSGCallBack_V31 implements HCNetSDK.FMSGCallBack_V31 {
@Override
public boolean invoke(int lCommand, HCNetSDK.NET_DVR_ALARMER pAlarmer, Pointer pAlarmInfo, int dwBufLen, Pointer pUser) {
System.out.println("进入回调");
//门禁相关参数
HCNetSDK.NET_DVR_ACS_ALARM_INFO strACSInfo = new HCNetSDK.NET_DVR_ACS_ALARM_INFO();
//远控相关参数
HCNetSDK.NET_DVR_ACS_EVENT_INFO_EXTEND_V20 extendV20=new HCNetSDK.NET_DVR_ACS_EVENT_INFO_EXTEND_V20();
strACSInfo.write();
Pointer pACSInfo = strACSInfo.getPointer();
pACSInfo.write(0, pAlarmInfo.getByteArray(0, strACSInfo.size()), 0, strACSInfo.size());
strACSInfo.read();
switch (lCommand){
//扫脸
case HCNetSDK.COMM_ALARM_ACS:
//获取流水号
String dwSerialNo = Integer.toString(strACSInfo.struAcsEventInfo.dwSerialNo);
System.out.println("流水号:"+dwSerialNo);
//主事件
String dwMajor = Integer.toString(strACSInfo.dwMajor);
System.out.println("主事件:"+dwMajor);
//次事件
String dwMinor = Integer.toString(strACSInfo.dwMinor);
System.out.println("次事件:"+dwMinor);
Pointer pAcsEventInfoExtendV20 = strACSInfo.pAcsEventInfoExtendV20;
extendV20.write();
Pointer pointer = extendV20.getPointer();
pointer.write(0, pAcsEventInfoExtendV20.getByteArray(0, extendV20.size()), 0, extendV20.size());
String byRemoteCheck = Integer.toString(extendV20.byRemoteCheck);
System.out.println("byRemoteCheck参数:"+byRemoteCheck);
//工号
String empCode = Integer.toString(strACSInfo.struAcsEventInfo.dwEmployeeNo);
//卡号
String cardNo = new String(strACSInfo.struAcsEventInfo.byCardNo).trim();
/**
*此处编写你的开门逻辑,随后远控开门即可
*PUT /ISAPI/AccessControl/remoteCheck?format=json 远控开门接口
*/
//远控开门
break;
//身份证
case HCNetSDK.COMM_ID_INFO_ALARM:
//海康的门禁刷卡需要配合搭配USB身份证阅读器 ,实现人证比对 我没有就不写了
System.out.println("刷卡");
break;
}
return true;
}
}
撤防以及退出登录我这里就不写了