温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#怎样实现视频监控系统

发布时间:2021-02-03 11:33:04 来源:亿速云 阅读:266 作者:小新 栏目:开发技术

这篇文章主要介绍C#怎样实现视频监控系统,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

  本系统监控系统,主要核心是使用AForge.NET提供的接口和插件(dll),感兴趣的朋友也可以去他们官网查看文档http://www.aforgenet.com/framework/documentation.html

  Talk is cheap,show me the code!

  系统初始化时,首先检查工位的机台是否开启了摄像头,具体检测代码如下:

/// <summary>
/// 监控bind
/// </summary>
private void bind()
{
  try
  {
    FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
    if (videoDevices.Count <= 0)
    {
      MessageBox.Show("请连接摄像头");
      return;
    }
    else
    {
      CloseCaptureDevice();
      if (!Directory.Exists(path)) Directory.CreateDirectory(path);
 
      videoSource = new VideoCaptureDevice(videoDevices[0].MonikerString);
      videoSource.VideoResolution = videoSource.VideoCapabilities[0];
      sourcePlayer.VideoSource = videoSource;
      sourcePlayer.Start();
    }
  }
  catch (Exception ex)
  {
    MessageBox.Show(ex.Message);
  }
}

  好了,摄像头没问题,咱在检查网络是否正常(这事儿可以交给运维,当然也可以通过程序控制,具体校验网络代码比比皆是,此处忽略,如有兴趣的朋友可以在公众号Call我一起探讨),至于为什么要校验网络,一部分是用于机台系统的数据采集,另一部分就是录制的视频文件不可能存储在工位机台上,不然流水线和工位足够多,岂不是一个工位一个几天的查看视频监控嘛!咱这都是智能化时代,录制的视频可以保存在本地,不过为了方便起见,需要定时清理,定时上传到服务器便于领导审查。视频上传到服务器一般用到最多的莫非两种情况,1.网络足够稳定,足够快的可以直接和服务器开个磁盘映射(共享目录),视频录制完后系统直接剪切到服务器保存即可。2.把不同时段录制的视频先存储到本地,然后单独开发个定时任务FTP定时上传即可。今天先跟大家分享下第一种方法,第二种方法也比较简单,有兴趣的朋友可以公众号call我一起探讨。

  不知不觉又扯了一堆废话,都是实在人,直接上源码吧:

/// <summary>
/// 开启或者关闭程序后将多余文件copy到相应目录,并开启磁盘映射上传到共享目录
/// </summary>
private void CopyFilesToServer()
{
  try
  {
    //遍历 当前PC文件夹外是否存在视频文件,如存在,移动到目标目录 
    string newPath = path + MacAddressPath + @"-Video\";
    if (!Directory.Exists(newPath)) Directory.CreateDirectory(newPath);
    //将上一次最后一个视频文件转入目录
    var files = Directory.GetFiles(path, "*.wmv");
    foreach (var file in files)
    {
      FileInfo fi = new FileInfo(file);
      string filesName = file.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries).LastOrDefault();
      fi.MoveTo(newPath + filesName);
    } 
  }
  catch (Exception ex)
  {
    //TODO:异常抛出
  }
  finally
  {
    uint state = 0;
    if (!Directory.Exists("Z:"))
    {
      //计算机名
      string computerName = System.Net.Dns.GetHostName();
      //为网络共享目录添加磁盘映射 
      state = WNetHelper.WNetAddConnection(computerName + @"\" + netWorkUser, netWorkPwd, netWorkPath, "Z:");
    }
    if (state.Equals(0))
    {
      //本地磁盘视频文件copy到网络共享目录
      CopyFolder(path + MacAddressPath + @"-Video\", zPath); 
    }
    else
    {
      WNetHelper.WinExec("NET USE * /DELETE /Y", 0);
      throw new Exception("添加网络驱动器错误,错误号:" + state.ToString());
    }
  }
}

  其中CopyFolder方法代码如下:

#region 通过共享网络磁盘映射的方式,讲文件copy到指定网盘
 /// <summary>
 /// 通过共享网络磁盘映射的方式,讲文件copy到指定网盘
 /// </summary>
 /// <param name="strFromPath"></param>
 /// <param name="strToPath"></param>
 public static void CopyFolder(string strFromPath, string strToPath)
 {
   //如果源文件夹不存在,则创建
   if (!Directory.Exists(strFromPath))
   {
     Directory.CreateDirectory(strFromPath);
   } 
   if (!Directory.Exists(strToPath))
   {
     Directory.CreateDirectory(strToPath);
   } 
   //直接剪切moveto,本地不留副本
   string[] strFiles = Directory.GetFiles(strFromPath);
   //循环剪切文件,此处循环是考虑每日工作站最后一个文件无法存储到根目录,导致出现两个视频文件的问题
   for (int i = 0; i < strFiles.Length; i++)
   {
     //取得文件名,只取文件名,地址截掉。
     string strFileName = strFiles[i].Substring(strFiles[i].LastIndexOf("\\") + 1, strFiles[i].Length - strFiles[i].LastIndexOf("\\") - 1);
     File.Move(strFiles[i], strToPath + "DT-" + strFileName);
   } 
 }
 #endregion

   做完机台检查工作,也做好了视频传输的工作,接下来就是视频录制的主角戏了,完整录制视频源码如下

/// <summary>
    /// videosouceplayer 录像
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="image"></param>
    private void sourcePlayer_NewFrame(object sender, ref Bitmap image)
    {
      try
      { 
        //写到屏幕上的时间
        g = Graphics.FromImage(image);
        SolidBrush drawBrush = new SolidBrush(Color.Yellow);

        Font drawFont = new Font("Arial", 6, System.Drawing.FontStyle.Bold, GraphicsUnit.Millimeter);
        int xPos = image.Width - (image.Width - 15);
        int yPos = 10;

        string drawDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        g.DrawString(drawDate, drawFont, drawBrush, xPos, yPos);

        //save content
        string videoFileName = dt.ToString("yyyy-MM-dd HHmm") + ".wmv";

        if (TestDriveInfo(videoFileName)) //检测硬盘空间足够
        {
          if (!stopREC)
          {
            stopREC = true;
            createNewFile = true; //这里要设置为true表示要创建新文件
            if (videoWriter != null) videoWriter.Close();
          }
          else
          {
            //开始录像
            if (createNewFile)
            {
              //第二次录像不一定是第二次开启软件时间(比如:连续多小时录制),所以应该重新给新录制视频文件重新赋值命名
              dt = DateTime.Now;
              videoFileFullPath = path + dt.ToString("yyyy-MM-dd HHmm") + ".wmv";//videoFileName;

              createNewFile = false;
              if (videoWriter != null)
              {
                videoWriter.Close();
                videoWriter.Dispose();
              }

              videoWriter = new VideoFileWriter();
              //这里必须是全路径,否则会默认保存到程序运行根据录下了
              videoWriter.Open(videoFileFullPath, image.Width, image.Height, 30, VideoCodec.WMV1);
              videoWriter.WriteVideoFrame(image);
            }
            else
            {
              if (videoWriter.IsOpen)
              {
                videoWriter.WriteVideoFrame(image);
              }
              if (dt.AddMinutes(1) <= DateTime.Now)
              { 
                createNewFile = true;
                //modify by stephen,每次写入视频文件后,即刻更新结束时间戳,并存入指定文件夹(目的:如果只有关闭的时候处理此操作,就会出现大于1小时的视频文件无法更新结束时间戳,且无法转入指定文件夹)
                if (videoWriter != null)
                {
                  videoWriter.Close();
                  videoWriter.Dispose();
                }
                string newPath = path + MacAddressPath + @"-Video\";
                if (!Directory.Exists(newPath)) Directory.CreateDirectory(newPath);
                string newStr = newPath + dt.ToString("yyyyMMddHHmm") + "-" + DateTime.Now.ToString("yyyyMMddHHmm") + ".wmv";
                FileInfo fi = new FileInfo(videoFileFullPath);
                fi.MoveTo(newStr); 
                ////转移到网路目录
                //CopyFilesToServer();
              }
            }
          }
        }

      }
      catch (Exception ex)
      {
        videoWriter.Close();
        videoWriter.Dispose();
      }
      finally
      {

        if (this.g != null) this.g.Dispose();
      }

    }

      其中TestDriveInfo方法是用来获取保存视频的磁盘信息的,具体代码如下:

#region 获取保存视频的磁盘信息
 /// <summary>
 /// 获取保存视频的磁盘信息
 /// </summary>
 bool TestDriveInfo(string n)
 {
   try
   {
     DriveInfo D = DriveInfo.GetDrives().Where(a => a.Name == path.Substring(0, 3).ToUpper()).FirstOrDefault();
     Int64 i = D.TotalFreeSpace, ti = unchecked(50 * 1024 * 1024 * 1024);
     if (i < ti)
     {
       DirectoryInfo folder = new DirectoryInfo(path + MacAddressPath + @"-Video\");
       //modify by stephen,验证当前指定文件夹是否存在元素
       if (folder.Exists)
       { 
         var fisList = folder.GetFiles("*.wmv").OrderBy(a => a.CreationTime); 
         if (fisList.Any())
         {
           List<FileInfo> fis = fisList.ToList();
           if (fis.Count > 0 && fis[0].Name != n)
           {
             File.Delete(fis[0].FullName);
           }
         }
       } 
     }
   }
   catch (Exception ex)
   {
     MessageBox.Show(ex.Message, "处理硬盘信息出错");
     return false;
   }
   return true;
 }
 #endregion

    当然,如果工位师傅录入产品信息有疑问的话,也可以利用系统截图来保留证据,这个是我自己画蛇添足的功能,反正是为了方便嘛,别耽误了工位师傅的办事效率,利用摄像头截图代码如下:

try
{ 
  string pathp = $@"{Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)}\";
  if (!Directory.Exists(pathp)) Directory.CreateDirectory(pathp);
  if (sourcePlayer.IsRunning)
  {
    BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
   sourcePlayer.GetCurrentVideoFrame().GetHbitmap(),
   IntPtr.Zero,
   Int32Rect.Empty,
   BitmapSizeOptions.FromEmptyOptions());
    PngBitmapEncoder pE = new PngBitmapEncoder();
    pE.Frames.Add(BitmapFrame.Create(bitmapSource));
    string picName = $"{pathp}{DateTime.Now.ToString("yyyyMMddHHmmssffffff")}.jpg";
    if (File.Exists(picName))
    {
      File.Delete(picName);
    }
    using (Stream stream = File.Create(picName))
    {
      pE.Save(stream);
    }
  }
}
catch (Exception ex)
{
  MessageBox.Show(ex.Message);
}

    代码比较简单,就不写备注了。当然部署系统的时候也不是一帆风顺,有的工厂或者仓库会购买第三方的摄像头,碍于工位环境,摄像头有可能与机台角度偏差较大,所以我又画蛇添足的了校验摄像头的小功能,可以左右90°上下180°画面翻转,具体代码如下:

#region 设置摄像头旋转调整
 if (image != null)
 {
   RotateFlipType pType = RotateFlipType.RotateNoneFlipNone;
   if (dAngle == 0)
   {
     pType = RotateFlipType.RotateNoneFlipNone;
   }
   else if (dAngle == 90)
   {
     pType = RotateFlipType.Rotate90FlipNone;
   }
   else if (dAngle == 180)
   {
     pType = RotateFlipType.Rotate180FlipNone;
   }
   else if (dAngle == 270)
   {
     pType = RotateFlipType.Rotate270FlipNone;
   } 
   // 实时按角度绘制
   image.RotateFlip(pType);
 }
 #endregion

  当然,站在公司角度,为了防止工位师傅手误(诚心)关掉视频监控程序,我们也可以从程序的角度来防患于未然,比如禁用程序的关闭按钮,禁用工具栏右键程序图标关闭程序的操作。

   我们可以重写窗口句柄来防止,具体代码如下:

#region 窗口句柄重写,禁用窗体的关闭按钮   
private const int CP_NOCLOSE_BUTTON = 0x200;
protected override CreateParams CreateParams
{
  get
  {
    CreateParams myCp = base.CreateParams;
    myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON;
    return myCp;
  }
}

以上是“C#怎样实现视频监控系统”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI