as everyone knows , Haikang's camera sdk More perfect , But it's still a little troublesome for beginners .
Today, I will write an essay to show you how to control the PTZ of Haikang camera ( The premise is that there is ptz PTZ equipment )
1、sdk Get ready


This article is based on C# Of frame To develop a ptz The control of the demo, The picture above shows Haikang sdk Dynamic library provided cs file , adopt dllimport To extract encapsulated functions . The main use of NET_DVR_PTZControlWithSpeed_Other To control the pan tilt .

The flow of the dotted box is an optional part , It will not affect the functional use of other processes and modules . According to the different functions, it can be divided into ten A module , Initialize when realizing the function of each module SDK、 User registration device 、 Log off the device and release SDK Resources 4 This process is unnecessary But less , The decoder function module and behavior analysis function module are aimed at the decoder and intelligent devices , We do not describe in this document .
User registration device (NET_DVR_Login_V30): Realize the user's registration function , After successful registration , Returned users ID As other merits Unique identification that can be operated ,SDK The maximum number of registered users allowed is 512 individual . Network cameras and network ball machines are allowed to have 16 Registered users name , And at most at the same time 128 User registration .
1.1 User registration and link camera devices
private bool m_bInitSDK = false;
private bool m_bRecord = false;
private uint iLastErr = 0;
private Int32 m_lUserID = -1;
private Int32 m_lRealHandle = -1;
private string str1;
private string str2;
private Int32 i = 0;
private Int32 m_lTree = 0;
private string str;
private long iSelIndex = 0;
private uint dwAChanTotalNum = 0;
private uint dwDChanTotalNum = 0;
private Int32 m_lPort = -1;
private IntPtr m_ptrRealHandle;
private int[] iIPDevID = new int[96];
private int[] iChannelNum = new int[96];
private CHCNetSDK.REALDATACALLBACK RealData = null;
public CHCNetSDK.NET_DVR_DEVICEINFO_V30 DeviceInfo;
public CHCNetSDK.NET_DVR_IPPARACFG_V40 m_struIpParaCfgV40;
public CHCNetSDK.NET_DVR_STREAM_MODE m_struStreamMode;
public CHCNetSDK.NET_DVR_IPCHANINFO m_struChanInfo;
public CHCNetSDK.NET_DVR_PU_STREAM_URL m_struStreamURL;
public CHCNetSDK.NET_DVR_IPCHANINFO_V40 m_struChanInfoV40;
public delegate void MyDebugInfo(string str);
CHCNetSDK.NET_DVR_PREVIEWINFO lpPreviewInfo = new CHCNetSDK.NET_DVR_PREVIEWINFO();
public MainForm()
{
InitializeComponent();
m_bInitSDK = CHCNetSDK.NET_DVR_Init();
if (m_bInitSDK == false)
{
MessageBox.Show("NET_DVR_Init error!");
return;
}
else
{
// preservation SDK journal To save the SDK log
CHCNetSDK.NET_DVR_SetLogToFile(3, @"./SdkLog", true);
//comboBoxView.SelectedIndex = 0;
//for (int i = 0; i < 64; i++)
//{
// iIPDevID[i] = -1;
// iChannelNum[i] = -1;
//}
}
}
public void ListIPChannel(Int32 iChanNo, byte byOnline, int byIPID)
{
str1 = String.Format("IPCamera {0}", iChanNo);
m_lTree++;
if (byIPID == 0)
{
str2 = "X"; // The channel is idle , No front-end devices are added the channel is idle
}
else
{
if (byOnline == 0)
{
str2 = "offline"; // The channel is not online the channel is off-line
}
else
str2 = "online"; // Channel Online The channel is on-line
}
//listViewIPChannel.Items.Add(new ListViewItem(new string[] { str1, str2 }));// Add channels to the list add the channel to the list
}
public void ListAnalogChannel(Int32 iChanNo, byte byEnable)
{
str1 = String.Format("Camera {0}", iChanNo);
m_lTree++;
if (byEnable == 0)
{
str2 = "Disabled"; // The channel has been disabled This channel has been disabled
}
else
{
str2 = "Enabled"; // The channel is enabled This channel has been enabled
}
//listViewIPChannel.Items.Add(new ListViewItem(new string[] { str1, str2 }));// Add channels to the list add the channel to the list
}
public void InfoIPChannel()
{
uint dwSize = (uint)Marshal.SizeOf(m_struIpParaCfgV40);
IntPtr ptrIpParaCfgV40 = Marshal.AllocHGlobal((Int32)dwSize);
Marshal.StructureToPtr(m_struIpParaCfgV40, ptrIpParaCfgV40, false);
uint dwReturn = 0;
int iGroupNo = 0; // The Demo Get only the first group 64 Channels , If the device IP The channel is greater than 64 road , Need to press group number 0~i Multiple calls NET_DVR_GET_IPPARACFG_V40 obtain
if (!CHCNetSDK.NET_DVR_GetDVRConfig(m_lUserID, CHCNetSDK.NET_DVR_GET_IPPARACFG_V40, iGroupNo, ptrIpParaCfgV40, dwSize, ref dwReturn))
{
iLastErr = CHCNetSDK.NET_DVR_GetLastError();
str = "NET_DVR_GET_IPPARACFG_V40 failed, error code= " + iLastErr;
// obtain IP Resource configuration information failed , Output error number Failed to get configuration of IP channels and output the error code
}
else
{
m_struIpParaCfgV40 = (CHCNetSDK.NET_DVR_IPPARACFG_V40)Marshal.PtrToStructure(ptrIpParaCfgV40, typeof(CHCNetSDK.NET_DVR_IPPARACFG_V40));
for (i = 0; i < dwAChanTotalNum; i++)
{
ListAnalogChannel(i + 1, m_struIpParaCfgV40.byAnalogChanEnable[i]);
iChannelNum[i] = i + (int)DeviceInfo.byStartChan;
}
byte byStreamType = 0;
uint iDChanNum = 64;
if (dwDChanTotalNum < 64)
{
iDChanNum = dwDChanTotalNum; // If the device IP Channel less than 64 road , Get by the actual number of routes
}
for (i = 0; i < iDChanNum; i++)
{
iChannelNum[i + dwAChanTotalNum] = i + (int)m_struIpParaCfgV40.dwStartDChan;
byStreamType = m_struIpParaCfgV40.struStreamMode[i].byGetStreamType;
dwSize = (uint)Marshal.SizeOf(m_struIpParaCfgV40.struStreamMode[i].uGetStream);
switch (byStreamType)
{
// at present NVR Only direct streaming from the device is supported NVR supports only the mode: get stream from device directly
case 0:
IntPtr ptrChanInfo = Marshal.AllocHGlobal((Int32)dwSize);
Marshal.StructureToPtr(m_struIpParaCfgV40.struStreamMode[i].uGetStream, ptrChanInfo, false);
m_struChanInfo = (CHCNetSDK.NET_DVR_IPCHANINFO)Marshal.PtrToStructure(ptrChanInfo, typeof(CHCNetSDK.NET_DVR_IPCHANINFO));
// List IP passageway List the IP channel
ListIPChannel(i + 1, m_struChanInfo.byEnable, m_struChanInfo.byIPID);
iIPDevID[i] = m_struChanInfo.byIPID + m_struChanInfo.byIPIDHigh * 256 - iGroupNo * 64 - 1;
Marshal.FreeHGlobal(ptrChanInfo);
break;
case 4:
IntPtr ptrStreamURL = Marshal.AllocHGlobal((Int32)dwSize);
Marshal.StructureToPtr(m_struIpParaCfgV40.struStreamMode[i].uGetStream, ptrStreamURL, false);
m_struStreamURL = (CHCNetSDK.NET_DVR_PU_STREAM_URL)Marshal.PtrToStructure(ptrStreamURL, typeof(CHCNetSDK.NET_DVR_PU_STREAM_URL));
// List IP passageway List the IP channel
ListIPChannel(i + 1, m_struStreamURL.byEnable, m_struStreamURL.wIPID);
iIPDevID[i] = m_struStreamURL.wIPID - iGroupNo * 64 - 1;
Marshal.FreeHGlobal(ptrStreamURL);
break;
case 6:
IntPtr ptrChanInfoV40 = Marshal.AllocHGlobal((Int32)dwSize);
Marshal.StructureToPtr(m_struIpParaCfgV40.struStreamMode[i].uGetStream, ptrChanInfoV40, false);
m_struChanInfoV40 = (CHCNetSDK.NET_DVR_IPCHANINFO_V40)Marshal.PtrToStructure(ptrChanInfoV40, typeof(CHCNetSDK.NET_DVR_IPCHANINFO_V40));
// List IP passageway List the IP channel
ListIPChannel(i + 1, m_struChanInfoV40.byEnable, m_struChanInfoV40.wIPID);
iIPDevID[i] = m_struChanInfoV40.wIPID - iGroupNo * 64 - 1;
Marshal.FreeHGlobal(ptrChanInfoV40);
break;
default:
break;
}
}
}
Marshal.FreeHGlobal(ptrIpParaCfgV40);
}
The above shows the variables to be applied for when the device registers the link ; Next, start linking , I'm going to use a button To realize the function of user connection :
private void button1_Click(object sender, EventArgs e)
{
if (m_lUserID < 0)
{
string DVRIPAddress = tb_IP.Text; // equipment IP Address or domain name Device IP
Int16 DVRPortNumber = Int16.Parse(tb_Port.Text);// Device service port number Device Port
string DVRUserName = tb_ID.Text;// Device login user name User name to login
string DVRPassword = tb_Pwd.Text;// Device login password Password to login
// Log in to the device Login the device
m_lUserID = CHCNetSDK.NET_DVR_Login_V30(DVRIPAddress, DVRPortNumber, DVRUserName, DVRPassword, ref DeviceInfo);
if (m_lUserID < 0)
{
iLastErr = CHCNetSDK.NET_DVR_GetLastError();
str = "NET_DVR_Login_V30 failed, error code= " + iLastErr; // Login failed , Output error number Failed to login and output the error code
return;
}
else
{
// Login successful
button1.Text = " To break off ";
dwAChanTotalNum = (uint)DeviceInfo.byChanNum;
dwDChanTotalNum = (uint)DeviceInfo.byIPChanNum + 256 * (uint)DeviceInfo.byHighDChanNum;
if (dwDChanTotalNum > 0)
{
InfoIPChannel();
}
else
{
for (i = 0; i < dwAChanTotalNum; i++)
{
ListAnalogChannel(i + 1, 1);
iChannelNum[i] = i + (int)DeviceInfo.byStartChan;
}
//comboBoxView.SelectedItem = 1;
// MessageBox.Show("This device has no IP channel!");
}
}
}
else
{
// Sign out Logout the device
if (m_lRealHandle >= 0)
{
m_lUserID = -1;
button1.Text = " Connecting camera ";
return;
}
if (!CHCNetSDK.NET_DVR_Logout(m_lUserID))
{
iLastErr = CHCNetSDK.NET_DVR_GetLastError();
str = "NET_DVR_Logout failed, error code= " + iLastErr;
return;
}
//listViewIPChannel.Items.Clear();// Clear the channel list Clean up the channel list
m_lUserID = -1;
button1.Text = " Connecting camera ";
}
if (m_lRealHandle < 0)
{
//lpPreviewInfo.hPlayWnd = RealPlayWnd.Handle;// Preview window live view window
lpPreviewInfo.lChannel = iChannelNum[(int)iSelIndex];// Preview device channel the device channel number
lpPreviewInfo.dwStreamType = 0;// Stream Type :0- Main stream ,1- Sub-stream ,2- stream 3,3- stream 4, And so on
lpPreviewInfo.dwLinkMode = 0;// How to connect :0- TCP The way ,1- UDP The way ,2- Multicast way ,3- RTP The way ,4-RTP/RTSP,5-RSTP/HTTP
lpPreviewInfo.bBlocked = true; //0- Non blocking fetching ,1- Blocking flow
lpPreviewInfo.dwDisplayBufNum = 15; // The maximum number of frames in the playback library display buffer
IntPtr pUser = IntPtr.Zero;// User data user data
//if (comboBoxView.SelectedIndex == 0)
//{
// Open preview Start live view
m_lRealHandle = CHCNetSDK.NET_DVR_RealPlay_V40(m_lUserID, ref lpPreviewInfo, null/*RealData*/, pUser);
//}
//else
//{
// lpPreviewInfo.hPlayWnd = IntPtr.Zero;// Preview window live view window
// m_ptrRealHandle = RealPlayWnd.Handle;
// RealData = new CHCNetSDK.REALDATACALLBACK(RealDataCallBack);// Preview live stream callback function real-time stream callback function
// m_lRealHandle = CHCNetSDK.NET_DVR_RealPlay_V40(m_lUserID, ref lpPreviewInfo, RealData, pUser);
//}
if (m_lRealHandle < 0)
{
iLastErr = CHCNetSDK.NET_DVR_GetLastError();
str = "NET_DVR_RealPlay_V40 failed, error code= " + iLastErr; // Preview failed , Output error number failed to start live view, and output the error code.
return;
}
else
{
//// Preview successful
//DebugInfo("NET_DVR_RealPlay_V40 succ!");
//btnPreview.Text = "Stop View";
MessageBox.Show(" Successful connection !");
}
}
return;
}
If there is no error reported here, it means that the connection is connected
1.2 Control the platform
Connect the device in the previous step , Be careful : We don't preview the screen to control the PTZ alone when we do this time !
Need to use :CHCNetSDK.NET_DVR_PTZControlWithSpeed_Other(m_lRealHandle, lpPreviewInfo.lChannel, 21, 0, 15);
explain :

CHCNetSDK.NET_DVR_PTZControlWithSpeed_Other(m_lRealHandle, lpPreviewInfo.lChannel, 21, 0, 15);// Example
PTZ control instructions are shown in the figure :

You can try it if you need it , Practice hands !









