最近项目中需要控制大话摄像头的旋转,网上资料很少,那就写个博客记录一下吧
准备工作
下载SDK
首先是前往官网下载SDK(https://support.dahuatech.com)
一般是选择windows平台的类似“设备网络SDK_CSharp_Win**”,因为Unity3d内使用,所以选择C#语言的。
Unity3d工程
首先需要将\libs下的文件复制一份放到工程的Plugins文件夹下,如果没有自建一个。
然后打开一个demo工程,将“NetSDKStruct.cs”、“NetSDK.cs”和“OriginalSDK.cs”脚本放到unity工程下,这些脚本是SDK接口入口脚本。
云台实现
开始 ==> sdk初始化 ==> 登录设备 ==> 控制操作 ==> 注销用户 ==> 释放sdk ==> 结束
初始化
public void Init()
{
m_ReConnectCallBack = new fHaveReConnectCallBack(ReConnectCallBack);
try
{
isInited = NETClient.Init(m_DisConnectCallBack, IntPtr.Zero, null);
NETClient.SetAutoReconnect(m_ReConnectCallBack, IntPtr.Zero);
Login();
}
catch (Exception ex)
{
Process.GetCurrentProcess().Kill();
}
}
登录
m_DeviceInfo = new NET_DEVICEINFO_Ex();
m_LoginID = NETClient.LoginWithHighLevelSecurity(Ip, Port, Name, Pwd, EM_LOGIN_SPAC_CAP_TYPE.TCP, IntPtr.Zero, ref m_DeviceInfo);
if (IntPtr.Zero == m_LoginID)
{
UnityEngine.Debug.Log("登录相机失败!:" + NETClient.GetLastError());
return;
}
else
UnityEngine.Debug.Log("登录相机成功!:" + m_LoginID);
}
云台控制
private void PTZControl(int channel, EM_EXTPTZ_ControlType type, int param1, int param2, bool isStop)
{
bool ret = NETClient.PTZControl(m_LoginID, channel, type, param1, param2, 0, isStop, IntPtr.Zero);
if (!ret)
{
UnityEngine.Debug.Log("云台控制失败:" + NETClient.GetLastError());
}
}
具体方法
public void PTZControl_Up(string type)
{
switch (type)
{
case "0":
PTZControl(channel, EM_EXTPTZ_ControlType.UP_CONTROL, 0, SpeedValue, true);
break;//上
case "1":
PTZControl(channel, EM_EXTPTZ_ControlType.RIGHTTOP, SpeedValue, SpeedValue, true);
break;//右上
case "2":
PTZControl(channel, EM_EXTPTZ_ControlType.RIGHT_CONTROL, 0, SpeedValue, true);
break;//右
case "3":
PTZControl(channel, EM_EXTPTZ_ControlType.RIGHTDOWN, SpeedValue, SpeedValue, true);
break;//右下
case "4":
// PTZControl(channel, EM_EXTPTZ_ControlType.DOWN_CONTROL, 0, SpeedValue, true);
PTZControl(0, EM_EXTPTZ_ControlType.DOWN_CONTROL, 0, SpeedValue, true);
break;//下
case "5":
PTZControl(channel, EM_EXTPTZ_ControlType.LEFTDOWN, SpeedValue, SpeedValue, true);
break;//左下
case "6":
PTZControl(channel, EM_EXTPTZ_ControlType.LEFT_CONTROL, 0, SpeedValue, true);
break;//左
case "7":
PTZControl(channel, EM_EXTPTZ_ControlType.LEFTTOP, SpeedValue, SpeedValue, true);
break;//左上
case "8":
PTZControl(channel, EM_EXTPTZ_ControlType.ZOOM_ADD_CONTROL, 0, SpeedValue, true);
break;//zoom+
case "9":
PTZControl(channel, EM_EXTPTZ_ControlType.ZOOM_DEC_CONTROL, 0, SpeedValue, true);
break;//zoom-
case "10":
PTZControl(channel, EM_EXTPTZ_ControlType.FOCUS_ADD_CONTROL, 0, SpeedValue, true);
break;//变焦+
case "11":
PTZControl(channel, EM_EXTPTZ_ControlType.FOCUS_DEC_CONTROL, 0, SpeedValue, true);
break;//变焦-
case "12":
PTZControl(channel, EM_EXTPTZ_ControlType.APERTURE_ADD_CONTROL, 0, SpeedValue, true);
UnityEngine.Debug.Log("光圈+:");
break;//光圈+
case "13":
PTZControl(channel, EM_EXTPTZ_ControlType.APERTURE_DEC_CONTROL, 0, SpeedValue, true);
UnityEngine.Debug.Log("光圈-:");
break;//光圈-
}
}
public void PTZControl_Down(string type)
{
switch (type)
{
case "0":
PTZControl(channel, EM_EXTPTZ_ControlType.UP_CONTROL, 0, SpeedValue, false);
break;//上
case "1":
PTZControl(channel, EM_EXTPTZ_ControlType.RIGHTTOP, SpeedValue, SpeedValue, false);
break;//右上
case "2":
PTZControl(channel, EM_EXTPTZ_ControlType.RIGHT_CONTROL, 0, SpeedValue, false);
break;//右
case "3":
PTZControl(channel, EM_EXTPTZ_ControlType.RIGHTDOWN, SpeedValue, SpeedValue, false);
break;//右下
case "4":
PTZControl(channel, EM_EXTPTZ_ControlType.DOWN_CONTROL, 0, SpeedValue, false);
break;//下
case "5":
PTZControl(channel, EM_EXTPTZ_ControlType.LEFTDOWN, SpeedValue, SpeedValue, false);
break;//左下
case "6":
PTZControl(channel, EM_EXTPTZ_ControlType.LEFT_CONTROL, 0, SpeedValue, false);
break;//左
case "7":
PTZControl(channel, EM_EXTPTZ_ControlType.LEFTTOP, SpeedValue, SpeedValue, false);
break;//左上
case "8":
PTZControl(channel, EM_EXTPTZ_ControlType.ZOOM_ADD_CONTROL, 0, SpeedValue, false);
break;//zoom+
case "9":
PTZControl(channel, EM_EXTPTZ_ControlType.ZOOM_DEC_CONTROL, 0, SpeedValue, false);
break;//zoom-
case "10":
PTZControl(channel, EM_EXTPTZ_ControlType.FOCUS_ADD_CONTROL, 0, SpeedValue, false);
break;//变焦+
case "11":
PTZControl(channel, EM_EXTPTZ_ControlType.FOCUS_DEC_CONTROL, 0, SpeedValue, false);
break;//变焦-
case "12":
PTZControl(channel, EM_EXTPTZ_ControlType.APERTURE_ADD_CONTROL, 0, SpeedValue, false);
break;//光圈+
case "13":
PTZControl(channel, EM_EXTPTZ_ControlType.APERTURE_DEC_CONTROL, 0, SpeedValue, false);
break;//光圈-
}
}
Unity中的操作:
添加Event Trigger 事件 调用对应的方法
注意一定要先调用down,然后再调用Up,不然会报用户参数不合法的错误