C#实现实现IP摄像头的方法摄像头的方法
主要介绍了C#实现IP摄像头的方法,涉及C#IP连接与摄像头视频录像的相关技巧,需要的朋友可以参考下
本文实例讲述了C#实现IP摄像头的方法。分享给大家供大家参考。具体实现方法如下:
#region IP摄像头代码
/// <summary>
/// ip摄像头代码
/// </summary>
//视频
private IntPtr m_hChannel;
private IntPtr m_hServer, phPlay;
public delegate void MyInvoke(string str);
public delegate void OutDelegate(IntPtr hSearch, DVSNET_FILE_TIME pFileTime, DVSNET_FILE_INFO pFileInfo, IntPtr pcontext);
//远程视频处理
public void BeforeRecordFrame(IntPtr hChannel, ref RECORD_INFO pRecordInfo, ref FRAME_HEADER pFrameHeader, IntPtr pcontext)
{
}
DVSNETClient.FileSearchCallback File_SearchCallback; //远程视频回放
public void FileSearchCallback(IntPtr hSearch, ref DVSNET_FILE_TIME pFileTime, ref DVSNET_FILE_INFO pFileInfo, IntPtr pcontext)
{
OutText(hSearch, pFileTime, pFileInfo, pcontext);
}
public void OutText(IntPtr hSearch, DVSNET_FILE_TIME pFileTime, DVSNET_FILE_INFO pFileInfo, IntPtr pcontext)
{
if (lvVideo.InvokeRequired)
{
OutDelegate outdelegate = new OutDelegate(OutText);
this.BeginInvoke(outdelegate, new object[] { hSearch, pFileTime, pFileInfo, pcontext });
return;
}
lvVideo.View = View.Details;
lvVideo.FullRowSelect = true;
for (int i = 0; i < pFileInfo.sFileName.Count(); i++)
{
ListViewItem item1 = new ListViewItem(pFileInfo.sFileName, i);
lvVideo.Items.AddRange(new ListViewItem[] { item1 });
}
}
private void pb_del_video_Click(object sender, EventArgs e)
{
this.Close();
}
public void SearchNotify(int nDevType,string sServName,string sServSerial,string sUrl,IntPtr MacAddr,ushort wWebPort,ushort wDataPort,string sSubNetMask,string sGateWay,string sMultiIp,string sDNS,ushort wMultiPort,int nChCount,uint bDHCP,IntPtr pcontext)
{
string str = Convert.ToString(wDataPort);
//string str2 = sUrl + "(" + str + ")";
string str2 = sUrl;
this.AddText(str2);
}
//赋值给cmbIP
private void AddText(string str)
{
MyInvoke method = new MyInvoke(this.AddTextToCmb);
base.BeginInvoke(method, new object[] { str });
}
public void AddTextToCmb(string str)
{
this.CmbIP.Items.Add(str);
}
//s搜索服务器
private void btnVideoSearck_Click(object sender, EventArgs e)
{
this.CmbIP.Items.Clear();
errornum errornum = DVSNETClient.DVSNET_SearchServers(0xbb8, 1, new DVSNETClient.SearchNotify(this.SearchNotify), base.Handle, IntPtr.Zero);
if (errornum == errornum.DVSNET_ERR_OK)
{
timerVideo.Enabled = true;
btnVideoSearck.Enabled = false;
}
else
{
}
}
//打开链接
private void btnVideoOpen_Click(object sender, EventArgs e)
{
string text = this.CmbIP.Text;
string sServIp = text;
int index = text.IndexOf('(');
if (index != -1)
{
string str3 = new string(text.ToCharArray(), 0, index);
sServIp = str3;
}
errornum errornum = DVSNETClient.DVSNET_OpenServer(this.EdtName.Text, sServIp, Convert.ToUInt16(this.EdtPort.Text), this.EdtUser.Text, this.EdtPassword.Text, ref this.m_hServer, 0, 0);
this.btnVideoOpen.Enabled = false; //不能点击
if (errornum == errornum.DVSNET_ERR_OK)
{
DVSNET_SERVER_INFO dvsnet_server_info = new DVSNET_SERVER_INFO();
MessageBox.Show("打开链接成功!");
dvsnet_server_info = new DVSNET_SERVER_INFO
{
lStructSize = Marshal.SizeOf(dvsnet_server_info)
};
DVSNETClient.DVSNET_GetServerInfo(this.m_hServer, ref dvsnet_server_info);
// this.CmbChannel.Items.Clear();
for (int i = 0; i < dvsnet_server_info.nChnNum; i++)
{
int num3 = i + 1;
// this.CmbChannel.Items.Add(num3.ToString());
}
// this.CmbChannel.SelectedIndex = 0;
}
else
{
MessageBox.Show("打开链接失败,错误码为:" + errornum.ToString());
this.btnVideoOpen.Enabled = true; //不能点击
}
}
//关闭链接
private void btnVideoClose_Click(object sender, EventArgs e)
{
if (this.m_hChannel != IntPtr.Zero)
{
MessageBox.Show("请关闭通道!再关闭链接");
}
else
{
if (this.m_hServer != IntPtr.Zero)
{
DVSNETClient.DVSNET_CloseServer(this.m_hServer);
this.m_hServer = IntPtr.Zero;
this.btnVideoOpen.Enabled = true; //不能点击
}
}