简介
Java 接入海康 SDK 设置 PTZ,主要实现云台基本控制和云台预置点两个功能:
云台基本控制:
NET_DVR_PTZControl
:云台控制操作(需先启动图象预览)NET_DVR_PTZControl_Other
:云台控制操作(不用启动图象预览)NET_DVR_PTZControlWithSpeed
:带速度的云台控制操作(需先启动图象预览)NET_DVR_PTZControlWithSpeed_Other
:带速度的云台控制操作(不用启动图象预览)
云台预置点功能:
NET_DVR_PTZPreset
:云台预置点操作(需先启动预览)NET_DVR_PTZPreset_Other
云台预置点操作(不用启动预览)
下载 SDK
首先,我们需要下载并安装海康SDK,并进行相关配置。具体步骤如下:
- 在海康威视官网下载 SDK 开发包,解压缩到本地目录
- 配置环境变量,将 SDK 的 bin 目录添加到系统的 PATH 环境变量中
- 在项目中引入SDK的jar包
下载SDK:https://open.hikvision.com/download/5cda567cf47ae80dd41a54b3?type=10&id=5cda5902f47ae80dd41a54b7
实现功能
添加依赖
在项目目录下新建 lib
文件夹,将下载的 SDK 开发包 库文件
里的 HCNetSDK.dll
、HCCore.dll
、HCNetSDKCom文件夹
、libssl-1_1.dll
、libcrypto-1_1.dll
、hlog.dll
、hpr.dll
、zlib1.dll
、PlayCtrl.dll
、SuperRender.dll
、AudioRender.dll
等文件均要加载到程序里面,HCNetSDKCom文件夹(包含里面的功能组件dll库文件)
需要和 HCNetSDK.dll
、HCCore.dll
一起加载,放在同一个目录下,且 HCNetSDKCom
文件夹名不能修改。
同时将 jna.jar
和 examples.jar
也放在 lib
目录下。
pom.xml
<dependency>
<groupId>com.sun</groupId>
<artifactId>jna</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/jna.jar</systemPath>
</dependency>
<dependency>
<groupId>com.sun</groupId>
<artifactId>examples</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/examples.jar</systemPath>
</dependency>
实现代码
SDK初始化
static HCNetSDK hCNetSDK = null;
static FExceptionCallBack_Imp fExceptionCallBack;
static class FExceptionCallBack_Imp implements HCNetSDK.FExceptionCallBack {
public void invoke(int dwType, int lUserID, int lHandle, Pointer pUser) {
log.error("异常事件类型:{}", dwType);
return;
}
}
/**
* @MonthName: init
* @Description: SDK初始化
* @Param: []
* @return: void
**/
public static void init() {
// 动态库加载
if (hCNetSDK == null) {
synchronized (HCNetSDK.class) {
String strDllPath = "";
try {
hCNetSDK = (HCNetSDK) Native.loadLibrary(System.getProperty("user.dir") + "\\lib\\HCNetSDK.dll", HCNetSDK.class);
} catch (Exception ex) {
log.error("加载SDK失败,loadLibrary: {},Error: {}", strDllPath, ex.getMessage());
return;
}
}
}
// SDK初始化,一个程序只需要调用一次
boolean initSuc = hCNetSDK.NET_DVR_Init();
log.info("SDK初始化: {}", initSuc);
// 异常消息回调
if (fExceptionCallBack == null) {
fExceptionCallBack = new FExceptionCallBack_Imp();
}
Pointer pUser = null;
if (!hCNetSDK.NET_DVR_SetExceptionCallBack_V30(0, 0, fExceptionCallBack, pUser)) {
return;
}
log.info("设置异常消息回调成功");
// 启动SDK写日志
hCNetSDK.NET_DVR_SetLogToFile(3, "./sdkLog", false);
}
登录
/**
* @MonthName: login
* @Description: 登录
* @Param: [ip, port, user, psw]
* @return: Integer
**/
public static Integer login(String ip, Integer port, String user, String psw) {
// 创建设备登录信息和设备信息对象
HCNetSDK.NET_DVR_USER_LOGIN_INFO loginInfo = new HCNetSDK.NET_DVR_USER_LOGIN_INFO();
HCNetSDK.NET_DVR_DEVICEINFO_V40 deviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V40();
// 设置设备IP地址
byte[] deviceAddress = new byte[HCNetSDK.NET_DVR_DEV_ADDRESS_MAX_LEN];
byte[] ipBytes = ip.getBytes();
System.arraycopy(ipBytes, 0, deviceAddress, 0, Math.min(ipBytes.length, deviceAddress.length));
loginInfo.sDeviceAddress = deviceAddress;
// 设置用户名和密码
byte[] userName = new byte[HCNetSDK.NET_DVR_LOGIN_USERNAME_MAX_LEN];
byte[] password = psw.getBytes();
System.arraycopy(user.getBytes(), 0, userName, 0, Math.min(user.length(), userName.length));
System.arraycopy(password, 0, loginInfo.sPassword, 0, Math.min(password.length, loginInfo.sPassword.length));
loginInfo.sUserName = userName;
// 设置端口和登录模式
loginInfo.wPort = port.shortValue();
loginInfo.bUseAsynLogin = false; // 同步登录
loginInfo.byLoginMode = 0; // 使用SDK私有协议
// 执行登录操作
int userID = hCNetSDK.NET_DVR_Login_V40(loginInfo, deviceInfo);
if (userID == -1) {
log.error("登录失败,错误码为: {}", hCNetSDK.NET_DVR_GetLastError());
} else {
log.info("{} 设备登录成功!", ip);
// read()后,结构体中才有对应的数据
deviceInfo.read();
// 处理通道号逻辑
int startDChan = deviceInfo.struDeviceV30.byStartDChan;
log.info("预览起始通道号: {}", startDChan);
}
// 返回登录结果
return userID;
}
登出
/**
* @MonthName: logout
* @Description: 退出登录
* @Param: [lUserID]
* @return: void
**/
public static void logout(Integer lUserID) {
if (Objects.nonNull(hCNetSDK) && Objects.nonNull(lUserID)) {
hCNetSDK.NET_DVR_Logout(lUserID);
hCNetSDK.NET_DVR_Cleanup();
}
}
设置球机预置点(不用启动图象预览)
/**
* @MonthName: setPTZPresetOther
* @Description: 设置球机预置点(不用启动图象预览)
* @Param:
* lChannel:通道号
* dwPTZPresetCmd:8、设置预置点,9、清除预置点,39、转到预置点
* dwPresetIndex:预置点的序号(从1开始),最多支持300个预置点
* @return: void
**/
public static void setPTZPresetOther(String ip, Integer port, String username, String password, Integer lChannel, Integer dwPTZPresetCmd, Integer dwPresetIndex) {
Integer lUserID = null;
try {
init();
lUserID = login(ip, port, username, password);
// 设置球机预置点
if (lUserID != -1) {
log.info("设置预置点开始:lUserID:{},lChannel:{},dwPTZPresetCmd:{},dwPresetIndex:{}", lUserID, lChannel, dwPTZPresetCmd, dwPresetIndex);
boolean status = hCNetSDK.NET_DVR_PTZPreset_Other(lUserID, lChannel, dwPTZPresetCmd, dwPresetIndex);
if (status) {
log.info("设置预置点成功");
} else {
log.error("设置预置点失败,错误码:{}", hCNetSDK.NET_DVR_GetLastError());
}
}
} catch (Exception e) {
log.error("设置预置点异常:{}", e);
} finally {
logout(lUserID);
}
}
云台控制操作(不用启动图象预览)
/**
* @MonthName: setPTZControlOther
* @Description: 云台控制操作(不用启动图象预览)
* @Param:
* lChannel:通道号
* dwPTZCommand:云台控制命令【21、上仰,22、下俯,23、左转,24、右转】
* dwStop:云台停止动作或开始动作:0-开始,1-停止
* dwSpeed:云台控制的速度,用户按不同解码器的速度控制值设置。取值范围[1,7]
* @return: void
**/
public static void setPTZControlOther(String ip, Integer port, String username, String password, Integer lChannel, Integer dwPTZCommand, Integer dwStop, Integer dwSpeed) {
Integer lUserID = null;
try {
init();
lUserID = login(ip, port, username, password);
if (lUserID != -1) {
log.info("云台控制操作:lUserID:{},lChannel:{},dwPTZPresetCmd:{},dwPresetIndex:{},dwSpeed:{}", lUserID, lChannel, dwPTZCommand, dwStop, dwSpeed);
boolean status = false;
if (Objects.isNull(dwSpeed)) {
status = hCNetSDK.NET_DVR_PTZControl_Other(lUserID, lChannel, dwPTZCommand, dwStop);
} else {
// 带速度
status = hCNetSDK.NET_DVR_PTZControlWithSpeed_Other(lUserID, lChannel, dwPTZCommand, dwStop, dwSpeed);
}
if (status) {
log.info("云台控制操作成功");
} else {
log.error("云台控制操作失败,错误码:{}", hCNetSDK.NET_DVR_GetLastError());
}
}
} catch (Exception e) {
log.error("云台控制操异常:{}", e);
} finally {
logout(lUserID);
}
}
完整代码
package com.demo.util;
import com.demo.common.HCNetSDK;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import lombok.extern.slf4j.Slf4j;
import java.util.Objects;
/**
* @ClassName: HCNetSDKUtil.java
* @ClassPath: com.demo.util.HCNetSDKUtil.java
* @Description: 海康 SDK
**/
@Slf4j
public class HCNetSDKUtil {
static HCNetSDK hCNetSDK = null;
static FExceptionCallBack_Imp fExceptionCallBack;
static class FExceptionCallBack_Imp implements HCNetSDK.FExceptionCallBack {
public void invoke(int dwType, int lUserID, int lHandle, Pointer pUser) {
log.error("异常事件类型:{}", dwType);
return;
}
}
/**
* @MonthName: init
* @Description: SDK初始化
* @Param: []
* @return: void
**/
public static void init() {
// 动态库加载
if (hCNetSDK == null) {
synchronized (HCNetSDK.class) {
String strDllPath = "";
try {
hCNetSDK = (HCNetSDK) Native.loadLibrary(System.getProperty("user.dir") + "\\lib\\HCNetSDK.dll", HCNetSDK.class);
} catch (Exception ex) {
log.error("加载SDK失败,loadLibrary: {},Error: {}", strDllPath, ex.getMessage());
return;
}
}
}
// SDK初始化,一个程序只需要调用一次
boolean initSuc = hCNetSDK.NET_DVR_Init();
log.info("SDK初始化: {}", initSuc);
// 异常消息回调
if (fExceptionCallBack == null) {
fExceptionCallBack = new FExceptionCallBack_Imp();
}
Pointer pUser = null;
if (!hCNetSDK.NET_DVR_SetExceptionCallBack_V30(0, 0, fExceptionCallBack, pUser)) {
return;
}
log.info("设置异常消息回调成功");
// 启动SDK写日志
hCNetSDK.NET_DVR_SetLogToFile(3, "./sdkLog", false);
}
/**
* @MonthName: login
* @Description: 登录
* @Param: [ip, port, user, psw]
* @return: Integer
**/
public static Integer login(String ip, Integer port, String user, String psw) {
// 创建设备登录信息和设备信息对象
HCNetSDK.NET_DVR_USER_LOGIN_INFO loginInfo = new HCNetSDK.NET_DVR_USER_LOGIN_INFO();
HCNetSDK.NET_DVR_DEVICEINFO_V40 deviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V40();
// 设置设备IP地址
byte[] deviceAddress = new byte[HCNetSDK.NET_DVR_DEV_ADDRESS_MAX_LEN];
byte[] ipBytes = ip.getBytes();
System.arraycopy(ipBytes, 0, deviceAddress, 0, Math.min(ipBytes.length, deviceAddress.length));
loginInfo.sDeviceAddress = deviceAddress;
// 设置用户名和密码
byte[] userName = new byte[HCNetSDK.NET_DVR_LOGIN_USERNAME_MAX_LEN];
byte[] password = psw.getBytes();
System.arraycopy(user.getBytes(), 0, userName, 0, Math.min(user.length(), userName.length));
System.arraycopy(password, 0, loginInfo.sPassword, 0, Math.min(password.length, loginInfo.sPassword.length));
loginInfo.sUserName = userName;
// 设置端口和登录模式
loginInfo.wPort = port.shortValue();
loginInfo.bUseAsynLogin = false; // 同步登录
loginInfo.byLoginMode = 0; // 使用SDK私有协议
// 执行登录操作
int userID = hCNetSDK.NET_DVR_Login_V40(loginInfo, deviceInfo);
if (userID == -1) {
log.error("登录失败,错误码为: {}", hCNetSDK.NET_DVR_GetLastError());
} else {
log.info("{} 设备登录成功!", ip);
// read()后,结构体中才有对应的数据
deviceInfo.read();
// 处理通道号逻辑
int startDChan = deviceInfo.struDeviceV30.byStartDChan;
log.info("预览起始通道号: {}", startDChan);
}
// 返回登录结果
return userID;
}
/**
* @MonthName: logout
* @Description: 退出登录
* @Param: [lUserID]
* @return: void
**/
public static void logout(Integer lUserID) {
if (Objects.nonNull(hCNetSDK) && Objects.nonNull(lUserID)) {
hCNetSDK.NET_DVR_Logout(lUserID);
hCNetSDK.NET_DVR_Cleanup();
}
}
/**
* @MonthName: setPTZPresetOther
* @Description: 设置球机预置点(不用启动图象预览)
* @Param:
* lChannel:通道号
* dwPTZPresetCmd:8、设置预置点,9、清除预置点,39、转到预置点
* dwPresetIndex:预置点的序号(从1开始),最多支持300个预置点
* @return: void
**/
public static void setPTZPresetOther(String ip, Integer port, String username, String password, Integer lChannel, Integer dwPTZPresetCmd, Integer dwPresetIndex) {
Integer lUserID = null;
try {
init();
lUserID = login(ip, port, username, password);
// 设置球机预置点
if (lUserID != -1) {
log.info("设置预置点开始:lUserID:{},lChannel:{},dwPTZPresetCmd:{},dwPresetIndex:{}", lUserID, lChannel, dwPTZPresetCmd, dwPresetIndex);
boolean status = hCNetSDK.NET_DVR_PTZPreset_Other(lUserID, lChannel, dwPTZPresetCmd, dwPresetIndex);
if (status) {
log.info("设置预置点成功");
} else {
log.error("设置预置点失败,错误码:{}", hCNetSDK.NET_DVR_GetLastError());
}
}
} catch (Exception e) {
log.error("设置预置点异常:{}", e);
} finally {
logout(lUserID);
}
}
/**
* @MonthName: setPTZControlOther
* @Description: 云台控制操作(不用启动图象预览)
* @Param:
* lChannel:通道号
* dwPTZCommand:云台控制命令【21、上仰,22、下俯,23、左转,24、右转】
* dwStop:云台停止动作或开始动作:0-开始,1-停止
* dwSpeed:云台控制的速度,用户按不同解码器的速度控制值设置。取值范围[1,7]
* @return: void
**/
public static void setPTZControlOther(String ip, Integer port, String username, String password, Integer lChannel, Integer dwPTZCommand, Integer dwStop, Integer dwSpeed) {
Integer lUserID = null;
try {
init();
lUserID = login(ip, port, username, password);
if (lUserID != -1) {
log.info("云台控制操作:lUserID:{},lChannel:{},dwPTZPresetCmd:{},dwPresetIndex:{},dwSpeed:{}", lUserID, lChannel, dwPTZCommand, dwStop, dwSpeed);
boolean status = false;
if (Objects.isNull(dwSpeed)) {
status = hCNetSDK.NET_DVR_PTZControl_Other(lUserID, lChannel, dwPTZCommand, dwStop);
} else {
// 带速度
status = hCNetSDK.NET_DVR_PTZControlWithSpeed_Other(lUserID, lChannel, dwPTZCommand, dwStop, dwSpeed);
}
if (status) {
log.info("云台控制操作成功");
} else {
log.error("云台控制操作失败,错误码:{}", hCNetSDK.NET_DVR_GetLastError());
}
}
} catch (Exception e) {
log.error("云台控制操异常:{}", e);
} finally {
logout(lUserID);
}
}
}
测试运行
测试
public static void main(String[] args) {
// setPTZPresetOther("192.168.1.64", 8000, "admin", "LZAP3508", 1, 39, 2);
setPTZControlOther("192.168.1.64", 8000, "admin", "123456", 1, 22, 0, 3);
}
日志
16:18:01.987 [main] INFO com.demo.util.HCNetSDKUtil - SDK初始化: true
16:18:02.003 [main] INFO com.demo.util.HCNetSDKUtil - 设置异常消息回调成功
16:18:02.391 [main] INFO com.demo.util.HCNetSDKUtil - 192.168.1.64 设备登录成功!
16:18:02.391 [main] INFO com.demo.util.HCNetSDKUtil - 预览起始通道号: 0
16:18:02.391 [main] INFO com.demo.util.HCNetSDKUtil - 云台控制操作:lUserID:0,lChannel:1,dwPTZPresetCmd:22,dwPresetIndex:0,dwSpeed:3
16:18:02.420 [main] INFO com.demo.util.HCNetSDKUtil - 云台控制操作成功