天天看點

Onvif協定學習:14、球機雲台控制PTZ

Onvif協定學習:14、球機雲台控制PTZ

文章目錄

  • ​​Onvif協定學習:14、球機雲台控制PTZ​​
  • ​​一、介紹​​
  • ​​二、代碼實作​​
  • ​​八個方向、放下及縮小控制​​
  • ​​聚焦控制​​

一、介紹

在安防攝像頭中,不僅僅涉及到固定攝像頭的槍擊,同樣還包含可以360°轉動的球機。是以對球機的雲台方向控制是Onvif協定開發過程中必不可少的過程

二、代碼實作

八個方向、放下及縮小控制
struct soap *stSoapNew = soap_new();
if (stSoapNew == nullptr)
{
  return ;
}
soap_set_namespaces(stSoapNew, namespaces);                                 // 設定soap的namespaces
stSoapNew->recv_timeout = 5;                                                     // 設定逾時5秒(超過指定時間沒有資料就退出)
stSoapNew->send_timeout = 5;
stSoapNew->connect_timeout = 5;
soap_set_mode(stSoapNew, SOAP_C_UTFSTRING);                          // 設定為UTF-8編碼,否則疊加中文    OSD會亂碼
if (stSoapNew == nullptr)
{
  printf( "Onvif New Soap error!");      
  return;
}

// 如果服務要求鑒權,則以下接口就需要加上使用者名密碼進行鑒權
soap_wsse_add_UsernameTokenDigest(stSoapNew, NULL, pUserName, pPassWord);//對使用者名密碼進行加密

struct _tptz__ContinuousMove stPtzMoveReq;
struct _tptz__ContinuousMoveResponse stPtzMoveRes;
memset(&stPtzMoveReq, 0x00, sizeof(stPtzMoveReq));
memset(&stPtzMoveRes, 0x00, sizeof(stPtzMoveRes));

struct tt__PTZSpeed* stVelocity = soap_new_tt__PTZSpeed(stSoapInfo, -1);
switch (nControlType)
{
case 0:    // 八個方向的控制
{
  struct tt__Vector2D* stPanTilt = soap_new_tt__Vector2D(stSoapInfo, -1);
  stPanTilt->x = 0.2;
  stPanTilt->y = 0.2;
  stPanTilt->space = "http://www.onvif.org/ver10/tptz/PanTiltSpaces/VelocityGenericSpace";
  stVelocity->PanTilt = stPanTilt;
  break;
}
case 1:      // 放大(stZoom->x > 0)、縮小(stZoom->x < 0)
{
  struct tt__Vector1D* stZoom = soap_new_tt__Vector1D(stSoapInfo, -1);
  stZoom->x = 0.2;    
  stZoom->space = "http://www.onvif.org/ver10/tptz/ZoomSpaces/VelocityGenericSpace";
  stVelocity->Zoom = 0.2;
  break;
}
default:
  break;
}

stPtzMoveReq.Velocity = stVelocity;
stPtzMoveReq.ProfileToken = pMainStreamToken;    // 前面擷取到的媒體流的token
// pProfilesAddr 是soap_call___tds__GetCapabilities接口擷取到的PTZ位址,具體實作參見:[擷取裝置能力]
nRet = soap_call___tptz__ContinuousMove(stSoapNew, pProfilesAddr, nullptr, &stPtzMoveReq, &stPtzMoveRes);
if (nRet != SOAP_OK || stSoapInfo->error != SOAP_OK)
{
  printf("Login handle is %d Gsoap return is %d labbuf is %s", (int)this, nRet, stSoapInfo->labbuf);
  return;
}      
聚焦控制
struct soap *stSoapNew = soap_new();
if (stSoapNew == nullptr)
{
  return ;
}
soap_set_namespaces(stSoapNew, namespaces);                                 // 設定soap的namespaces
stSoapNew->recv_timeout = 5;                                                     // 設定逾時5秒(超過指定時間沒有資料就退出)
stSoapNew->send_timeout = 5;
stSoapNew->connect_timeout = 5;
soap_set_mode(stSoapNew, SOAP_C_UTFSTRING);                          // 設定為UTF-8編碼,否則疊加中文    OSD會亂碼
if (stSoapNew == nullptr)
{
  printf( "Onvif New Soap error!");      
  return;
}

// 如果服務要求鑒權,則以下接口就需要加上使用者名密碼進行鑒權
soap_wsse_add_UsernameTokenDigest(stSoapNew, NULL, pUserName, pPassWord);//對使用者名密碼進行加密

struct _timg__Move stMoveReq;
struct _timg__MoveResponse stMoveRes;
memset(&stMoveReq, 0x00, sizeof(stMoveReq));
memset(&stMoveRes, 0x00, sizeof(stMoveRes));
tt__FocusMove *stFocusMove = soap_new_tt__FocusMove(stSoapNew, 1);
tt__ContinuousFocus *stContinuFocus = soap_new_tt__ContinuousFocus(stSoapNew, 1);
stContinuFocus->Speed = fSpeed;          // 聚焦的速度
stFocusMove->Continuous = stContinuFocus;
stFocusMove->Absolute = nullptr;
stFocusMove->Relative = nullptr;
stMoveReq.Focus = stFocusMove;
stMoveReq.VideoSourceToken = VideoSourceConfigurationToken;    // 配置token
// pProfilesAddr 是soap_call___tds__GetCapabilities接口擷取到的Ptz位址,具體實作參見:[擷取裝置能力]
nRet = soap_call___timg__Move(stSoapNew, pProfilesAddr, nullptr, &stMoveReq, &stMoveRes);
if (nRet != ONVIFSDK_NOERROR || stSoapNew->error != SOAP_OK)
{
  printf("Login handle is %d Gsoap return is %d labbuf is %s", (int)this, nRet, stSoapInfo->labbuf);
  return;
}