最近对接大华接口要控制摄像头的旋转,SDK源码中注释比较少也没有详细的技术文档,自己研究下写了这个帮助类可以实现摄像头8个方向的控制,首先需要引入netsdk的jar包,直接上源码:
public class PTZControlUtil {
// 初始化sdk
public static NetSDKLib netsdk = NetSDKLib.NETSDK_INSTANCE;
// 设备信息
private static NetSDKLib.NET_DEVICEINFO_Ex m_stDeviceInfo = new NetSDKLib.NET_DEVICEINFO_Ex();
// 登陆句柄
private static NetSDKLib.LLong m_hLoginHandle = new NetSDKLib.LLong(0);
// 网络断线处理
private static DisConnect disConnect = new DisConnect();
// 设备连接恢复,实现设备连接恢复接口
private static HaveReConnect haveReConnect = new HaveReConnect();
/**
* 云台控制
* @param m_strIp ip
* @param m_nPort 端口
* @param m_strUser 登录名
* @param m_strPassword 密码
* @param nChannelID 通道id 默认为0
* @param command 命令
* @param lParam1 默认 0,当有左上或左下等操作时才会传值 (1-8)
* @param lParam2 垂直/水平 移动速度 (1-8)
*/
public static void upControlPtz(String m_strIp, int m_nPort, String m_strUser, String m_strPassword, int nChannelID, String command, int lParam1, int lParam2) {
// 初始化
init(disConnect, haveReConnect);
// 若未登录,先登录
if (m_hLoginHandle.longValue() == 0) {
login(m_strIp, m_nPort, m_strUser, m_strPassword);
}
// 开始移动,若超过角度则会变为左右移动
if (m_hLoginHandle.longValue() != 0) {
control(nChannelID, command, lParam1, lParam2);
}
// 退出
logout();
// 释放资源
cleanup();
}
/**
* 云台控制
* @param nChannelID
* @param command
* @param lParam1
* @param lParam2
*/
private static void control(int nChannelID, String command, int lParam1, int lParam2) {
if ("UP".equals(command)) {
//向上
netsdk.CLIENT_DHPTZControlEx(m_hLoginHandle, nChannelID,
NetSDKLib.NET_PTZ_ControlType.NET_PTZ_UP_CONTROL,
0, lParam2, 0, 0);
netsdk.CLIENT_DHPTZControlEx(m_hLoginHandle, nChannelID,
NetSDKLib.NET_PTZ_ControlType.NET_PTZ_UP_CONTROL,
0, 0, 0, 1);
} else if ("DOWN".equals(command)) {
//向下
netsdk.CLIENT_DHPTZControlEx(m_hLoginHandle, nChannelID,
NetSDKLib.NET_PTZ_ControlType.NET_PTZ_DOWN_CONTROL,
0, lParam2, 0, 0);
netsdk.CLIENT_DHPTZControlEx(m_hLoginHandle, nChannelID,
NetSDKLib.NET_PTZ_ControlType.NET_PTZ_DOWN_CONTROL,
0, 0, 0, 1);
} else if ("LEFT".equals(command)) {
//向左
netsdk.CLIENT_DHPTZControlEx(m_hLoginHandle, nChannelID,
NetSDKLib.NET_PTZ_ControlType.NET_PTZ_LEFT_CONTROL,
0, lParam2, 0, 0);
netsdk.CLIENT_DHPTZControlEx(m_hLoginHandle, nChannelID,
NetSDKLib.NET_PTZ_ControlType.NET_PTZ_LEFT_CONTROL,
0, 0, 0, 1);
} else if ("RIGHT".equals(command)) {
//向右
netsdk.CLIENT_DHPTZControlEx(m_hLoginHandle, nChannelID,
NetSDKLib.NET_PTZ_ControlType.NET_PTZ_RIGHT_CONTROL,
0, lParam2, 0, 0);
netsdk.CLIENT_DHPTZControlEx(m_hLoginHandle, nChannelID,
NetSDKLib.NET_PTZ_ControlType.NET_PTZ_RIGHT_CONTROL,
0, 0, 0, 1);
} else if ("LEFT_UP".equals(command)) {
//向左上
netsdk.CLIENT_DHPTZControlEx(m_hLoginHandle, nChannelID,
NetSDKLib.NET_EXTPTZ_ControlType.NET_EXTPTZ_LEFTTOP,
lParam1, lParam2, 0, 0);
netsdk.CLIENT_DHPTZControlEx(m_hLoginHandle, nChannelID,
NetSDKLib.NET_EXTPTZ_ControlType.NET_EXTPTZ_LEFTTOP,
0, 0, 0, 1);
} else if ("LEFT_DOWN".equals(command)) {
//向左下
netsdk.CLIENT_DHPTZControlEx(m_hLoginHandle, nChannelID,
NetSDKLib.NET_EXTPTZ_ControlType.NET_EXTPTZ_LEFTDOWN,
lParam1, lParam2, 0, 0);
netsdk.CLIENT_DHPTZControlEx(m_hLoginHandle, nChannelID,
NetSDKLib.NET_EXTPTZ_ControlType.NET_EXTPTZ_LEFTDOWN,
0, 0, 0, 1);
} else if ("RIGHT_UP".equals(command)) {
//向右上
netsdk.CLIENT_DHPTZControlEx(m_hLoginHandle, nChannelID,
NetSDKLib.NET_EXTPTZ_ControlType.NET_EXTPTZ_RIGHTTOP,
lParam1, lParam2, 0, 0);
netsdk.CLIENT_DHPTZControlEx(m_hLoginHandle, nChannelID,
NetSDKLib.NET_EXTPTZ_ControlType.NET_EXTPTZ_RIGHTTOP,
0, 0, 0, 1);
} else if ("RIGHT_DOWN".equals(command)) {
//向右下
netsdk.CLIENT_DHPTZControlEx(m_hLoginHandle, nChannelID,
NetSDKLib.NET_EXTPTZ_ControlType.NET_EXTPTZ_RIGHTDOWN,
lParam1, lParam2, 0, 0);
netsdk.CLIENT_DHPTZControlEx(m_hLoginHandle, nChannelID,
NetSDKLib.NET_EXTPTZ_ControlType.NET_EXTPTZ_RIGHTDOWN,
0, 0, 0, 1);
} else if ("FOCUS_NEAR".equals(command)) {
//变焦+
netsdk.CLIENT_DHPTZControlEx(m_hLoginHandle, nChannelID,
NetSDKLib.NET_PTZ_ControlType.NET_PTZ_FOCUS_ADD_CONTROL,
0, lParam2, 0, 0);
netsdk.CLIENT_DHPTZControlEx(m_hLoginHandle, nChannelID,
NetSDKLib.NET_PTZ_ControlType.NET_PTZ_FOCUS_ADD_CONTROL,
0, 0, 0, 1);
} else if ("FOCUS_FAR".equals(command)) {
//变焦-
netsdk.CLIENT_DHPTZControlEx(m_hLoginHandle, nChannelID,
NetSDKLib.NET_PTZ_ControlType.NET_PTZ_FOCUS_DEC_CONTROL,
0, lParam2, 0, 0);
netsdk.CLIENT_DHPTZControlEx(m_hLoginHandle, nChannelID,
NetSDKLib.NET_PTZ_ControlType.NET_PTZ_FOCUS_DEC_CONTROL,
0, 0, 0, 1);
} else if ("ZOOM_IN".equals(command)) {
//变倍+
netsdk.CLIENT_DHPTZControlEx(m_hLoginHandle, nChannelID,
NetSDKLib.NET_PTZ_ControlType.NET_PTZ_ZOOM_ADD_CONTROL,
0, lParam2, 0, 0);
netsdk.CLIENT_DHPTZControlEx(m_hLoginHandle, nChannelID,
NetSDKLib.NET_PTZ_ControlType.NET_PTZ_ZOOM_ADD_CONTROL,
0, 0, 0, 1);
} else if ("ZOOM_OUT".equals(command)) {
//变倍-
netsdk.CLIENT_DHPTZControlEx(m_hLoginHandle, nChannelID,
NetSDKLib.NET_PTZ_ControlType.NET_PTZ_ZOOM_DEC_CONTROL,
0, lParam2, 0, 0);
netsdk.CLIENT_DHPTZControlEx(m_hLoginHandle, nChannelID,
NetSDKLib.NET_PTZ_ControlType.NET_PTZ_ZOOM_DEC_CONTROL,
0, 0, 0, 1);
} else if ("IRIS_ENLARGE".equals(command)) {
//光圈+
netsdk.CLIENT_DHPTZControlEx(m_hLoginHandle, nChannelID,
NetSDKLib.NET_PTZ_ControlType.NET_PTZ_APERTURE_ADD_CONTROL,
0, lParam2, 0, 0);
netsdk.CLIENT_DHPTZControlEx(m_hLoginHandle, nChannelID,
NetSDKLib.NET_PTZ_ControlType.NET_PTZ_APERTURE_ADD_CONTROL,
0, 0, 0, 1);
} else if ("IRIS_REDUCE".equals(command)) {
//光圈-
netsdk.CLIENT_DHPTZControlEx(m_hLoginHandle, nChannelID,
NetSDKLib.NET_PTZ_ControlType.NET_PTZ_APERTURE_DEC_CONTROL,
0, lParam2, 0, 0);
netsdk.CLIENT_DHPTZControlEx(m_hLoginHandle, nChannelID,
NetSDKLib.NET_PTZ_ControlType.NET_PTZ_APERTURE_DEC_CONTROL,
0, 0, 0, 1);
}
}
/**
* 初始化
*/
private static boolean bInit = false;
private static boolean bLogopen = false;
public static boolean init(NetSDKLib.fDisConnect disConnect, NetSDKLib.fHaveReConnect haveReConnect) {
bInit = netsdk.CLIENT_Init(disConnect, null);
if(!bInit) {
System.out.println("Initialize SDK failed");
return false;
}
//打开日志,可选
NetSDKLib.LOG_SET_PRINT_INFO setLog = new NetSDKLib.LOG_SET_PRINT_INFO();
File path = new File("./sdklog/");
if (!path.exists()) {
path.mkdir();
}
String logPath = path.getAbsoluteFile().getParent() + "\\sdklog\\" + ToolKits.getDate() + ".log";
setLog.nPrintStrategy = 0;
setLog.bSetFilePath = 1;
System.arraycopy(logPath.getBytes(), 0, setLog.szLogFilePath, 0, logPath.getBytes().length);
System.out.println(logPath);
setLog.bSetPrintStrategy = 1;
bLogopen = netsdk.CLIENT_LogOpen(setLog);
if(!bLogopen ) {
System.err.println("Failed to open NetSDK log");
}
// 设置断线重连回调接口,设置过断线重连成功回调函数后,当设备出现断线情况,SDK内部会自动进行重连操作
// 此操作为可选操作,但建议用户进行设置
netsdk.CLIENT_SetAutoReconnect(haveReConnect, null);
//设置登录超时时间和尝试次数,可选
int waitTime = 5000; //登录请求响应超时时间设置为5S
int tryTimes = 1; //登录时尝试建立链接1次
netsdk.CLIENT_SetConnectTime(waitTime, tryTimes);
// 设置更多网络参数,NET_PARAM的nWaittime,nConnectTryNum成员与CLIENT_SetConnectTime
// 接口设置的登录设备超时时间和尝试次数意义相同,可选
NetSDKLib.NET_PARAM netParam = new NetSDKLib.NET_PARAM();
netParam.nConnectTime = 10000; // 登录时尝试建立链接的超时时间
netParam.nGetConnInfoTime = 3000; // 设置子连接的超时时间
netParam.nGetDevInfoTime = 3000;//获取设备信息超时时间,为0默认1000ms
netsdk.CLIENT_SetNetworkParam(netParam);
return true;
}
/**
* 登录
*
* @param m_strIp ip
* @param m_nPort 端口号
* @param m_strUser 账号
* @param m_strPassword 密码
* @return 成功则 true
*/
public static boolean login(String m_strIp, int m_nPort, String m_strUser, String m_strPassword) {
//入参
NetSDKLib.NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY pstInParam = new NetSDKLib.NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY();
pstInParam.nPort = m_nPort;
pstInParam.szIP = m_strIp.getBytes();
pstInParam.szPassword = m_strPassword.getBytes();
pstInParam.szUserName = m_strUser.getBytes();
//出参
NetSDKLib.NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY pstOutParam = new NetSDKLib.NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY();
pstOutParam.stuDeviceInfo = m_stDeviceInfo;
m_hLoginHandle = netsdk.CLIENT_LoginWithHighLevelSecurity(pstInParam, pstOutParam);
System.out.println(netsdk.getClass());
if (m_hLoginHandle.longValue() == 0) {
System.err.printf("登录失败!\n", m_strIp, m_nPort, ToolKits.getErrorCodePrint());
} else {
System.out.println("登录成功: [ " + m_strIp + " ]");
}
return m_hLoginHandle.longValue() == 0 ? false : true;
}
/**
* 退出登录
*/
private static boolean logout() {
if (m_hLoginHandle.longValue() == 0) {
return false;
}
boolean bRet = netsdk.CLIENT_Logout(m_hLoginHandle);
if (bRet) {
m_hLoginHandle.setValue(0);
}
return bRet;
}
/**
* 释放资源
*/
public static void cleanup() {
if (bLogopen) {
netsdk.CLIENT_LogClose();
}
if (bInit) {
netsdk.CLIENT_Cleanup();
}
}
// 设备断线回调: 通过 CLIENT_Init 设置该回调函数,当设备出现断线时,SDK会调用该函数
private static class DisConnect implements NetSDKLib.fDisConnect {
public void invoke(NetSDKLib.LLong m_hLoginHandle, String pchDVRIP, int nDVRPort, Pointer dwUser) {
System.out.printf("Device[%s] Port[%d] DisConnect!\n", pchDVRIP, nDVRPort);
}
}
// 网络连接恢复,设备重连成功回调
// 通过 CLIENT_SetAutoReconnect 设置该回调函数,当已断线的设备重连成功时,SDK会调用该函数
private static class HaveReConnect implements NetSDKLib.fHaveReConnect {
public void invoke(NetSDKLib.LLong m_hLoginHandle, String pchDVRIP, int nDVRPort, Pointer dwUser) {
System.out.printf("ReConnect Device[%s] Port[%d]\n", pchDVRIP, nDVRPort);
}
}
}