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; //不能点击
   }
  }
 }
 //打开通道
 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#程序设计有所帮助。

(0)

相关推荐

  • C#实现用于操作wav声音文件的类实例

    本文实例讲述了C#实现用于操作wav声音文件的类.分享给大家供大家参考.具体如下: 有了这个C#类,我们可以很轻易的调用本地wav文件进行同步播放或者异步播放,大大简化了对wav文件的操作过程,如果你需要在项目中调用wav文件进行声音播放,可以使用这个C#类. using System; using System.Media; namespace DotNet.Utilities { /// <summary> /// 处理多媒体的公共类 /// </summary> public

  • C#实现调用本机摄像头实例

    本文实例源自一个项目,其中需要调用本机的摄像头进行拍照,分享给大家供大家参考之用.具体步骤如下: 硬件环境:联想C360一体机,自带摄像头 编写环境:vs2010 语言:C# WPF 实现步骤: 下载AForge类库,并添加引用: using AForge; using AForge.Controls; using AForge.Video; using AForge.Video.DirectShow; using Size = System.Drawing.Size; 在xaml界面中添加Vi

  • C#实现操作MySql数据层类MysqlHelper实例

    本文实例讲述了C#实现操作MySql数据层类MysqlHelper.分享给大家供大家参考.具体如下: using System; using System.Data; using System.Configuration; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; using MySql.Data; using MySql.Data.MySqlCli

  • c#实现摄像头拍照功能示例

    复制代码 代码如下: using System.Drawing;using System.Drawing.Imaging;using System;using System.Runtime.InteropServices; 复制代码 代码如下: private const int WM_USER = 0x400;private const int WS_CHILD = 0x40000000;private const int WS_VISIBLE = 0x10000000;private con

  • C#操作Access通用类实例

    本文实例讲述了C#操作Access通用类.分享给大家供大家参考.具体如下: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using Syst

  • C#实现的调用DOS命令操作类实例

    本文实例讲述了C#实现的调用DOS命令操作类.分享给大家供大家参考.具体如下: /*********************************** * All Rights Reserved , Copyright (C) 2012 , EricHu. * 作 者: EricHu * 创建时间: 2012-5-4 15:29:35 ***********************************/ using System; using System.Diagnostics; usi

  • C#操作ftp类完整实例

    本文实例讲述了C#操作ftp类.分享给大家供大家参考.具体如下: using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Net; using System.Globalization; namespace FtpTest1 { public class FtpWeb { string ftpServerIP; string ftpRemotePath; st

  • C#实现控制摄像头的类

    本文实例讲述了C#实现控制摄像头的类.分享给大家供大家参考.具体如下: /// <summary> /// 一个控制摄像头的类 /// </summary> public class Pick { private const int WM_USER = 0x400; private const int WS_CHILD = 0x40000000; private const int WS_VISIBLE = 0x10000000; private const int WM_CAP_

  • 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(Int

  • jsp获取客户端IP地址的方法

    本文实例讲述了jsp获取客户端IP地址的方法.分享给大家供大家参考,具体如下: public static String getIpAddr(HttpServletRequest request) { String ip = request.getHeader("X-Forwarded-For"); if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { ip = request

  • python生成IP段的方法

    本文实例讲述了python生成IP段的方法.分享给大家供大家参考.具体实现方法如下: #!/usr/local/bin/python #-*- coding: UTF-8 -*- ############################################################# # python 生成IP段 ############################################################# if __name__ == '__mai

  • Linux通过命令仅获取IP地址的方法

    一同事的朋友正在参加笔试,遇到这么一个问题让他帮忙解决,结果同事又找到我帮他搞定.真是感慨:通讯发达在某些方面来说,真不知是不是好事啊!题目大致如下所示,一般我们使用ifconfig查看网卡信息,请问你可以通过什么命令,让其只输出IP地址192.168.42.128 看似简单的问题,实现起来也不是太简单.看看下面的思路吧 [root@DB-Server ~]# ifconfig eth0 eth0 Link encap:Ethernet HWaddr 00:0C:29:9E:70:0E inet

  • TCP Wrappers防火墙介绍与封锁IP地址的方法

    Tcp_Wrappers是一个用来分析TCP/IP封包的软件,类似的IP封包软件还有iptables,linux默认都安装了此软件,作为一个安全的系统,Linux本身有两层安全防火墙,通过IP过滤机制的iptables实现第一层防护,iptables防火墙通过直观地监视系统的运行状况,阻挡网络中的一些恶意攻击,保护整个系统正常运行,免遭攻击和破坏.如果通过了第一层防护,那么下一层防护就是tcp_wrappers了,通过Tcp_Wrappers可以实现对系统中提供的某些服务的开放与关闭.允许和禁止

  • python使用Flask框架获取用户IP地址的方法

    本文实例讲述了python使用Flask框架获取用户IP地址的方法.分享给大家供大家参考.具体如下: 下面的代码包含了html页面和python代码,非常详细,如果你正使用Flask,也可以学习一下最基本的Flask使用方法. python代码如下: from flask import Flask, render_template, request # Initialize the Flask application app = Flask(__name__) # Default route,

  • Python使用django获取用户IP地址的方法

    本文实例讲述了Python使用django获取用户IP地址的方法.分享给大家供大家参考.具体如下: 函数实现: def get_client_ip(request): try: real_ip = request.META['HTTP_X_FORWARDED_FOR'] regip = real_ip.split(",")[0] except: try: regip = request.META['REMOTE_ADDR'] except: regip = "" r

  • python实现分析apache和nginx日志文件并输出访客ip列表的方法

    本文实例讲述了python实现分析apache和nginx日志文件并输出访客ip列表的方法.分享给大家供大家参考.具体如下: 这里使用python分析apache和nginx日志文件输出访客ip列表 ips = {} fh = open("/var/log/nginx/access.log", "r").readlines() for line in fh: ip = line.split(" ")[0] if 6 < len(ip) &l

  • JS简单获取客户端IP地址的方法【调用搜狐接口】

    本文实例讲述了JS简单获取客户端IP地址的方法.分享给大家供大家参考,具体如下: <!doctype html> <html> <head> <meta charset="utf-8"> <title>使用搜狐接口获取客户端IP地址</title> </head> <body> <script src="http://pv.sohu.com/cityjson?ie=utf-8

  • asp.net实现拒绝频繁的IP访问的方法

    本文实例讲述了asp.net实现拒绝频繁的IP访问的方法.分享给大家供大家参考,具体如下: 首先我们要实现 IHttpModule接口 using System; using System.Collections.Generic; using System.Text; using System.Web; using System.Web.UI; using System.Web.SessionState; using System.Configuration; namespace MyHttp

随机推荐