欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现IP摄像头的方法

程序员文章站 2022-05-15 15:15:08
本文实例讲述了c#实现ip摄像头的方法。分享给大家供大家参考。具体实现方法如下: #region 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; //不能点击
   }
  }
 }
 //打开通道
 intptr selserver = intptr.zero;
 private void btnvideoopenchnnel_click(object sender, eventargs e)
 {
  this.panelvideo.visible = true;
  this.pannelvideotwo.visible = false;
  if (this.m_hserver != intptr.zero)
  {
   this.btnvideoopenchnnel.enabled = false; //不能点击
   dvsnet_channel_info dvsnet_channel_info = new dvsnet_channel_info();
   messagebox.show("打开通道成功!请稍候");
   dvsnet_channel_info = new dvsnet_channel_info
   {
    lstructsize = marshal.sizeof(dvsnet_channel_info),
    nprotocol = 0,
    hwnddisplay = this.panelvideo.handle,
    bplaystart = 1,
    dwstreamno = 0
   };
   dvsnetclient.dvsnet_openchannel(this.m_hserver, convert.touint16(0), ref dvsnet_channel_info, ref this.m_hchannel);
  }
  else
  {
   messagebox.show("请打开链接!");
  }
 }
 //关闭通道
 private void btnvideoclosechnnel_click(object sender, eventargs e)
 {
  if (this.m_hchannel != intptr.zero)
  {
   dvsnetclient.dvsnet_closechannel(this.m_hchannel);
   messagebox.show("关闭通道成功!请稍候");
   this.m_hchannel = intptr.zero;
   this.btnvideoopenchnnel.enabled = true; //不能点击
  }
 }
 //开始录像
 private void btnvideoon_click(object sender, eventargs e)
 {
  try
  {
   string ss = datetime.now.tostring().replace(":", "-").replace(" ", "-").replace("/", "-") + ".asf";  
   if (this.panelvideo.visible == false)
   {
    messagebox.show("请切换到打开通道");
   }
   else
   {
    if (this.m_hchannel != intptr.zero)
    {
     this.btnvideoon.enabled = false; //不能点击
     //dvsnetclient.dvsnet_startrecord(this.m_hchannel, 1, filetype.filetype_asf, ss, new dvsnetclient.beforerecordframe(this.beforerecordframe), base.handle);
     dvsnetclient.dvsnet_startrecord(this.m_hchannel, 1, filetype.filetype_asf, ss,before_recordframe, base.handle);
    }
    else
    {
     messagebox.show("请打开通道!");
    }
   }
  }
  catch (exception ex)
  {
   messagebox.show(ex.tostring());
  }  
 }
 //停止录像
 private void btnstoprec_click(object sender, eventargs e)
 {
  dvsnetclient.dvsnet_stoprecord(this.m_hchannel);
  messagebox.show("已停止录像!");
  this.btnvideoon.enabled = true; //不能点击
 }
 //打开本地视频
 intptr phplayer;
 private void btnsearch_click(object sender, eventargs e)
 {
  try
  {
   if (m_hserver != intptr.zero)
   {
    string fname = "";
    openfiledialog openfiledialog = new openfiledialog();
    openfiledialog.initialdirectory = application.startuppath;
    //注意这里写路径时要用c:\而不是c:
    openfiledialog.filter = "文本文件|*.asf|所有文件|*.asf";
    openfiledialog.restoredirectory = true;
    openfiledialog.filterindex = 1;
    if (openfiledialog.showdialog() == dialogresult.ok)
    {
     this.panelvideo.visible = false;
     this.pannelvideotwo.visible = true;
     this.btnsearch.enabled = false;
     fname = openfiledialog.filename;
     intptr hdisplaywnd = pannelvideotwo.handle;
     phplayer = (intptr)0;
     dvsnetclient.dvsplayer_releaseinstance(phplayer); //释放
     dvsnetclient.dvsplayer_stop(phplayer); //释放本地
     errornum aa = dvsnetclient.dvsplayer_createinstance(hdisplaywnd, ref phplayer, 0);      
     aa = dvsnetclient.dvsplayer_openfile(phplayer,ref fname, 1);
     dvsnetclient.dvsplayer_start(phplayer);
    }
    else
    {
     this.panelvideo.visible = true;
     this.pannelvideotwo.visible = false;
    }
   }
   else
   {
    messagebox.show("请打开通道!");
   }
  }
  catch (exception)
  {
   throw;
  }   
 }
 //停止视频播放
 private void btnhf_click(object sender, eventargs e)
 {
  this.panelvideo.visible = true;
  this.pannelvideotwo.visible = false;
  intptr phplayer = (intptr)0;
  dvsnetclient.dvsplayer_stop(phplayer);
  messagebox.show("已停止回放录像!");
  this.btnsearch.enabled = true; ;  
 }
 //查找远程视频
 private void btnsearchbyname_click(object sender, eventargs e)
 {
  if (m_hserver == intptr.zero)
  {
   messagebox.show("请先打开链接登录服务器!");
   return;
  }
  this.btnsearchbyname.enabled = false;
  dvsnet_file_time pstarttime = new dvsnet_file_time();
  pstarttime.lyear = (uint)this.datetimepicker1.value.year;
  pstarttime.lmonth = (uint)this.datetimepicker1.value.month;
  pstarttime.lday = (uint)this.datetimepicker1.value.day;
  pstarttime.nhour = 0;
  pstarttime.nminute = 0;
  dvsnet_file_time pendtime = new dvsnet_file_time();
  pendtime.lyear = (uint)this.datetimepicker1.value.year;
  pendtime.lmonth = (uint)this.datetimepicker1.value.month;
  pendtime.lday = (uint)this.datetimepicker1.value.day;
  pendtime.nhour = 23;
  pendtime.nminute = 59;
  intptr phsearch = intptr.zero;
  //if(){}
  errornum bak = dvsnetclient.dvsnet_serverfindfile(m_hserver, 0, 0, (char)0xff, ref pstarttime, ref pendtime, file_searchcallback, (intptr)0, ref phsearch);
  if (bak != errornum.dvsnet_err_ok)
  {
   messagebox.show("查找远程视频失败!");
  }   
  this.btnsearchbyname.enabled = true;
 }
 //鼠标双击事件
 private void lvvideo_mousedoubleclick(object sender, mouseeventargs e)
 {
  this.panelvideo.visible = false;
  this.pannelvideotwo.visible = true;   
  string sa = "";
  intptr hwnd = pannelvideotwo.handle;
  if (lvvideo.selecteditems.count > 0)
  { //this.listview1.selectedindices
   sa = lvvideo.selecteditems[0].text.tostring();
  }
  
  dvsnetclient.dvsplayer_stop(phplayer); //释放本地回放
  dvsnetclient.dvsnet_closeplayfile(phplay); //释放远程
  phplay = intptr.zero;
  errornum bak = dvsnetclient.dvsnet_playfilebyname(m_hserver, sa, 1, hwnd, ref phplay);
  if (bak == errornum.dvsnet_err_ok)
  {
   uint32 value = 0;
   dvsnetclient.dvsnet_controlplayfile(phplay, 1, 0, ref value);
  }
  else
  {
   messagebox.show("回放远程文件 失败,错误码为" + bak.tostring());
  }
 }
 #endregion
 //时间控件 1s
 private void timervideo_tick(object sender, eventargs e)
 {
  int nstatus = 0;
  errornum errnum = dvsnetclient.dvsnet_getsearchstatus(ref nstatus);
  if (errnum == errornum.dvsnet_err_ok)
  {
   if (nstatus == 1)
   {
    btnvideosearck.enabled = true;
    timervideo.enabled = false;
    messagebox.show("没有启动搜索");
   }
   if (nstatus == 2)
   {
    //messagebox.show("正在搜索!");
   }
   if (nstatus == 3)
   {
    timervideo.enabled = false;
    btnvideosearck.enabled = true;
    if (this.cmbip.items.count == 0)
    {
     messagebox.show("没有找到设备!请接上设备");
    }
    else
    {
     messagebox.show("已找到设备,请选择ip地址");
    }
   }
  }
  else
  {
   messagebox.show("dvsnet_getsearchstatus 失败,错误码为" + errnum.tostring());
  }
}

希望本文所述对大家的c#程序设计有所帮助。