使用Java調用海康的SDK人臉監控,實現智能考勤。
項目使用Springboot構建,海康攝像頭爲雙目攝像頭。
部分代碼如下:
package com.swf.attence.hikConfig;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import org.dom4j.DocumentException;
import org.springframework.stereotype.Component;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
@Component
public class ClientDemo {
static HCNetSDK hCNetSDK = HCNetSDK.INSTANCE;
HCNetSDK.NET_DVR_DEVICEINFO_V30 m_strDeviceInfo;//設備信息
public NativeLong lUserID;//用戶句柄
public NativeLong lAlarmHandle;//報警佈防句柄
public NativeLong lListenHandle;//報警監聽句柄
public NativeLong RemoteConfig;
public FMSGCallBack fMSFCallBack;//報警回調函數實現
public FMSGCallBack_V31 fMSFCallBack_V31;//報警回調函數實現
public String m_sDeviceIP;//已登錄設備的IP地址
public String username; //設備用戶名
public String password;//設備登錄密碼
public int iPort;//設備端口號
public ClientDemo() {
lUserID = new NativeLong(-1);
lAlarmHandle = new NativeLong(-1);
lListenHandle = new NativeLong(-1);
fMSFCallBack = null;
fMSFCallBack_V31 = new FMSGCallBack_V31();
}
public boolean CameraInit(){
//初始化
boolean initSuc = hCNetSDK.NET_DVR_Init();
if (initSuc != true){
System.out.println("初始化失敗");
return false;
}else{
System.out.println("初始化成功");
return true;
}
}
//註冊
public boolean register(String username, String password, String m_sDeviceIP){
//註冊之前先註銷已註冊的用戶,預覽情況下不可註銷
if (lUserID.longValue() > -1) {
//先註銷
hCNetSDK.NET_DVR_Logout(lUserID);
lUserID = new NativeLong(-1);
}
//註冊
m_strDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V30();
int iPort = 8000;
System.out.println("註冊,設備IP:"+m_sDeviceIP);
lUserID = hCNetSDK.NET_DVR_Login_V30(m_sDeviceIP,(short) iPort, username, password, m_strDeviceInfo);
long userID = lUserID.longValue();
if (userID == -1) {
System.out.println("註冊失敗");
return false;
} else {
System.out.println("註冊成功,lUserID:"+userID);
return true;
}
}
//註銷
public void Logout(){
//報警撤防
if (lAlarmHandle.intValue() > -1)
{
if(!hCNetSDK.NET_DVR_CloseAlarmChan_V30(lAlarmHandle))
{
System.out.println("撤防失敗");
}else{
lAlarmHandle = new NativeLong(-1);
System.out.println("撤防成功");
}
}
//註銷
if (lUserID.longValue() > -1) {
if(hCNetSDK.NET_DVR_Logout(lUserID))
{
System.out.println("註銷成功");
lUserID = new NativeLong(-1);
}
hCNetSDK.NET_DVR_Cleanup();
}
}
//佈防
public String SetupAlarmChan() {
if (lUserID.intValue() == -1)
{
System.out.println("請先註冊");
return "請先註冊";
}
if (lAlarmHandle.intValue() < 0)//尚未佈防,需要佈防
{
if (fMSFCallBack_V31 != null)
{
Pointer pUser = null;
if (!hCNetSDK.NET_DVR_SetDVRMessageCallBack_V31(fMSFCallBack_V31, pUser))
{
System.out.println("設置回調函數失敗!");
return "設置回調函數失敗!";
}
}
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;
m_strAlarmInfo.write();
lAlarmHandle = hCNetSDK.NET_DVR_SetupAlarmChan_V41(lUserID, m_strAlarmInfo);
if (lAlarmHandle.intValue() == -1)
{
System.out.println("佈防失敗");
System.out.println("錯誤代碼:"+hCNetSDK.NET_DVR_GetLastError());
return "佈防失敗: "+"錯誤代碼:"+hCNetSDK.NET_DVR_GetLastError();
}else{
System.out.println("佈防成功");
return "佈防成功";
}
}else{
System.out.println("已經佈防,不要重複操作");
return "已經佈防,不要重複操作";
}
}
//撤防
public void CloseAlarmChan() {
//報警撤防
if (lAlarmHandle.intValue() > -1)
{
if(hCNetSDK.NET_DVR_CloseAlarmChan_V30(lAlarmHandle))
{
System.out.println("撤防成功");
lAlarmHandle = new NativeLong(-1);
}
}
}
//開始監聽
public void StartAlarmListen() {
int iListenPort = 8000;
Pointer pUser = null;
if (fMSFCallBack == null)
{
fMSFCallBack = new FMSGCallBack();
}
lListenHandle = hCNetSDK.NET_DVR_StartListen_V30(m_sDeviceIP, (short)iListenPort,fMSFCallBack, pUser);
if(lListenHandle.intValue() < 0)
{
System.out.println("啓動監聽失敗");
}else{
System.out.println("啓動監聽成功");
}
}
//停止監聽
public void StopAlarmListen() {
if(lListenHandle.intValue() < 0)
{
return;
}
if(!hCNetSDK.NET_DVR_StopListen_V30(lListenHandle))
{
System.out.println("停止監聽失敗");
System.out.println("停止監聽失敗,錯誤:"+hCNetSDK.NET_DVR_GetLastError());
}else{
System.out.println("停止監聽成功");
}
}
public static void main(String[] args) throws UnsupportedEncodingException, DocumentException, InterruptedException {
ClientDemo clientDemo = new ClientDemo();
clientDemo.CameraInit();
/*clientDemo.register("admin","ytkj123456","10.21.244.169");*/
clientDemo.register("admin","admin123456","10.21.244.166");
clientDemo.SetupAlarmChan();
/*FDLibBox fdLibBox = new FDLibBox(clientDemo);
clientDemo.SetupAlarmChan();*/
Thread.sleep(100000);
}
}
如有需要請聯繫[email protected]
Over