记录一下使用Java的SpringBoot+大华SDK在智慧公厕项目中使大华惠智双目半球网络摄像机DH-IPC-HD4140X-E2获取人流量统计数据
首先根据说明书登录摄像头,一般摄像头都有自己的账号和密码(可能是admin admin 也可能是admin 888888 还有可能是admin 12345),有IP的(可能是192.168.1.108),在同一个网段内浏览器访问摄像头的IP就可以访问web页面
使用windows系统自带的浏览器Microsoft Edge 谷歌浏览器也可以,主要是要安装插件
下载安装插件(如果电脑之前安装过就可以不用安装)
安装完成之后 重启浏览器登录(更保险)
可能有提示要加载,那就允许
设置人流量统计的相关参数
首先在设置 智能方案里把图标点亮
在设置-人数统计里选择人数统计
绘制区域和绘制方向线 绘制区域表示人流统计的区域,只有这个区域内的才统计的,绘制方向线,这个默认是入的方向线
参数设置进入和离开人数需要打钩,灵敏度这个的配置需要根据实际来确认 人数统计报警的进入人数和离开人数需要设置成1,滞留人数不需要设置(这样用程序去监听的时候就能被动获取报警数据)
布撤防配置
标定配置 安装高度和安装角度
自动维护配置 这个很关键,必须要配置,不然这个摄像头运行时间过长会被卡死的,可能是数据满了
网络设置 根据实际需求设置IP地址
首先去官网拿到开发需要的SDK
https://support.dahuatech.com/tools/sdkExploit
新建一个SpringBoot项目后调用sdk,在这里使用的是win10 64位的,sdk需要重新封装,去除gui方面的那部分代码
SpringBoot调用sdk的核心代码
DaHuaConstantConfig .java
public class DaHuaConstantConfig {
// 摄像头的IP地址
public static final String DA_HUA_IP= "192.168.1.108";
// 摄像头的获取人流数据的端口是37777 不是80端口 这里有坑
public static final int DA_HUA_PORT = 37777;
// 登录的账号
public static final String DA_HUA_ADMIN = "admin";
// 登录的密码
public static final String DA_HUA_PASSWORD = "admin";
// 通道 默认0
public static final int DA_HUA_SUB_CHANNEL = 0;
// 登录状态 默认false 当登录成功后true
public static boolean DA_HUA_LOGIN_STATE = false;
// 自定义状态
public static boolean DA_HUA_SUB_STATE = false;
}
DaHuaDisConnectCallBack.java
import com.netsdk.lib.NetSDKLib;
import com.sun.jna.Pointer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// 设备断线回调: 通过 CLIENT_Init 设置该回调函数,当设备出现断线时,SDK会调用该函数
public class DaHuaDisConnectCallBack implements NetSDKLib.fDisConnect {
private final Logger log = LoggerFactory.getLogger(this.getClass());
public void invoke(NetSDKLib.LLong m_hLoginHandle, String pchDVRIP, int nDVRPort, Pointer dwUser) {
log.error("Device:"+pchDVRIP+"Port:"+nDVRPort+"-DisConnectCallBack");
DaHuaConstantConfig.DA_HUA_LOGIN_STATE = false;
DaHuaConstantConfig.DA_HUA_SUB_STATE = false;
// 断线提示
}
}
DaHuaHaveReConnectCallBack.java
import com.netsdk.demo.module.VideoStateSummaryModule;
import com.netsdk.lib.NetSDKLib;
import com.sun.jna.Pointer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
// 网络连接恢复,设备重连成功回调
// 通过 CLIENT_SetAutoReconnect 设置该回调函数,当已断线的设备重连成功时,SDK会调用该函数
public class DaHuaHaveReConnectCallBack implements NetSDKLib.fHaveReConnect {
private final Logger log = LoggerFactory.getLogger(this.getClass());
public void invoke(NetSDKLib.LLong m_hLoginHandle, String pchDVRIP, int nDVRPort, Pointer dwUser) {
log.error("ReConnect Device[%s] Port[%d]\n", pchDVRIP, nDVRPort);
DaHuaConstantConfig.DA_HUA_LOGIN_STATE = true;
// 断线后需要重新订阅
ExecutorService service = Executors.newSingleThreadExecutor();
service.execute(new Runnable() {
@Override
public void run() {
log.warn("Reconnect Device ReAttache next");
if (VideoStateSummaryModule.getM_hAttachMap().size() > 0) { // 如果断前正在订阅
// 重订阅事件
log.warn("Reconnect Device ReAttache need");
VideoStateSummaryModule.reAttachAllVideoStatSummary(DaHuaNetSdkManCountThread.humanNumberStatisticCB);
DaHuaConstantConfig.DA_HUA_SUB_STATE = true;
}
}
});
service.shutdown();
}
}
DaHuaHumanNumberStatisticCallBack .java
import com.netsdk.lib.NetSDKLib;
import com.sun.jna.Pointer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
// 人流统计数据回调
public class DaHuaHumanNumberStatisticCallBack implements NetSDKLib.fVideoStatSumCallBack {
private final Logger log = LoggerFactory.getLogger(this.getClass());
public void invoke(NetSDKLib.LLong lAttachHandle, NetSDKLib.NET_VIDEOSTAT_SUMMARY stVideoState, int dwBufLen, Pointer dwUser) throws ParseException {
try {
// 自定义接收的类
CsRecordHumanNumber csRecordHumanNumber = new CsRecordHumanNumber();
csRecordHumanNumber.setEnteredToday(stVideoState.stuEnteredSubtotal.nToday); // 当日进入的人数
csRecordHumanNumber.setEnteredHour(stVideoState.stuEnteredSubtotal.nHour);// 单位小时内进入的人数
csRecordHumanNumber.setEnteredTotal(stVideoState.stuEnteredSubtotal.nTotal);// 设备运行后进入的总人数
csRecordHumanNumber.setExitedToday(stVideoState.stuExitedSubtotal.nToday);// 当日出去的人数
csRecordHumanNumber.setExitedHour(stVideoState.stuExitedSubtotal.nHour);// 单位小时内出去的人数
csRecordHumanNumber.setExitedTotal(stVideoState.stuExitedSubtotal.nTotal);// 设备运行后出去的总人数
log.info(csRecordHumanNumber.toString());
// to do 把数据传给Spring去插入数据库或者其他处理
} catch (IOException e) {
e.printStackTrace();
}
}
}
DaHuaNetSdkManCountThread.java 创建连接的线程类
import com.netsdk.demo.module.LoginModule;
import com.netsdk.demo.module.VideoStateSummaryModule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DaHuaNetSdkManCountThread extends Thread {
private static final Logger log = LoggerFactory.getLogger(DaHuaNetSdkManCountThread.class);
// 设备断线通知回调
private static final DaHuaDisConnectCallBack disConnectCB = new DaHuaDisConnectCallBack();
// 网络连接恢复
private static final DaHuaHaveReConnectCallBack haveReConnectCB = new DaHuaHaveReConnectCallBack();
// 人数统计回调事件
public static DaHuaHumanNumberStatisticCallBack humanNumberStatisticCB = new DaHuaHumanNumberStatisticCallBack();
public void run(){
System.out.println(Thread.currentThread().getName()+" NetSdk");
if(LoginModule.init(disConnectCB, haveReConnectCB)){
login();
}
}
// 登录
public static boolean login() {
if (LoginModule.login(DaHuaConstantConfig.DA_HUA_IP, DaHuaConstantConfig.DA_HUA_PORT, DaHuaConstantConfig.DA_HUA_ADMIN, DaHuaConstantConfig.DA_HUA_PASSWORD)) {
int chanNum = LoginModule.m_stDeviceInfo.byChanNum;
log.info("da-hua-sdk-login success:"+chanNum);
DaHuaConstantConfig.DA_HUA_LOGIN_STATE = true;
if (!VideoStateSummaryModule.channelAttached(DaHuaConstantConfig.DA_HUA_SUB_CHANNEL)) {
if (VideoStateSummaryModule.attachVideoStatSummary(DaHuaConstantConfig.DA_HUA_SUB_CHANNEL, humanNumberStatisticCB)) {
log.info("da-hua-sdk human number open:");
DaHuaConstantConfig.DA_HUA_SUB_STATE = true;
} else {
DaHuaConstantConfig.DA_HUA_SUB_STATE = false;
}
}
return true;
}
return false;
}
}
放在SpringBoot启动页面内启动
DaHuaNetSdkManCountThread daHuaNetSdkManCountThread = new DaHuaNetSdkManCountThread();
daHuaNetSdkManCountThread.start();
启动后控制台输出
08:41:39.142 spring-boot-logging [main] INFO o.s.j.e.a.AnnotationMBeanExporter - Registering beans for JMX exposure on startup
08:41:39.166 spring-boot-logging [main] INFO o.a.coyote.http11.Http11NioProtocol - Starting ProtocolHandler ["http-nio-8780"]
08:41:39.188 spring-boot-logging [main] INFO o.a.tomcat.util.net.NioSelectorPool - Using a shared selector for servlet write/read
08:41:39.217 spring-boot-logging [main] INFO o.s.b.w.e.tomcat.TomcatWebServer - Tomcat started on port(s): 8780 (http) with context path ''
08:41:39.223 spring-boot-logging [main] INFO com.lavatory.CloudRoomApplication - Started CloudRoomApplication in 6.143 seconds (JVM running for 7.822)
Thread-4 NetSdk
load library: C:\Users\18380\AppData\Local\Temp\dhnetsdk.dll
C:/DevelopSpace/ZexinSpaces/smart-lavatory-system/lavatory-client/lavatory-client-terminal/libs/win64/
load library: C:\Users\18380\AppData\Local\Temp\dhnetsdk.dll
dhnetsdk----C:\Users\18380\AppData\Local\Temp\dhnetsdk.dll
load library: C:\Users\18380\AppData\Local\Temp\dhconfigsdk.dll
dhconfigsdk----C:\Users\18380\AppData\Local\Temp\dhconfigsdk.dll
arch-amd64--name-Windows 10
arch-amd64--name-Windows 10
load library: C:\Users\18380\AppData\Local\Temp\dhconfigsdk.dll
Login Success [ 192.168.1.108 ]
08:41:44.239 spring-boot-logging [Thread-4] INFO c.l.d.n.DaHuaNetSdkManCountThread - da-hua-sdk-login success:1
Attach Succeed at Channel 0 ! AttachHandle: 681853824. Wait Device Notify Information
08:41:44.847 spring-boot-logging [Thread-4] INFO c.l.d.n.DaHuaNetSdkManCountThread - da-hua-sdk human number open:
08:43:30.676 spring-boot-logging [Thread-6] INFO c.l.d.n.DaHuaHumanNumberStatisticCallBack - CsRecordHumanNumber(enteredToday=68, enteredHour=18, enteredTotal=2278, exitedToday=63, exitedHour=17, exitedTotal=2268)
CsRecordHumanNumber(enteredToday=68, enteredHour=18, enteredTotal=2278, exitedToday=63, exitedHour=17, exitedTotal=2268)
08:43:37.655 spring-boot-logging [Thread-8] INFO c.l.d.n.DaHuaHumanNumberStatisticCallBack - CsRecordHumanNumber(enteredToday=68, enteredHour=18, enteredTotal=2278, exitedToday=64, exitedHour=18, exitedTotal=2269)
CsRecordHumanNumber(enteredToday=68, enteredHour=18, enteredTotal=2278, exitedToday=64, exitedHour=18, exitedTotal=2269)
这个就是从摄像头设备里取到的客流统计值
CsRecordHumanNumber(enteredToday=68, enteredHour=18, enteredTotal=2278, exitedToday=63, exitedHour=17, exitedTotal=2268)
CsRecordHumanNumber(enteredToday=68, enteredHour=18, enteredTotal=2278, exitedToday=64, exitedHour=18, exitedTotal=2269)