在项目里有个功能需要调取监视器实现摄像功能,在网上找到了一些Demo虽然也没看太懂,但是基本功能还是做到了,在此做个笔记。
本文源码下载地址:https://download.csdn.net/download/qq_42757964/12259466
AForge引用类包下载地址:https://download.csdn.net/download/qq_42757964/12255300
简单示例:
1,首先我们创建Monitorr项目:
2.将需要的dll文件复制到Debug目录下。
3.将引用添加进项目(选择浏览找到你刚刚存放的对应位置的.dll文件)
4.添加成功后,在右侧工具栏创建一个Camera文件夹(也可以不创建直接将这些别人写好的自己定义控件拖拽到这里),将其拖拽过来。这一步的目的是将dll文件转化为可直接拖拽到Form窗口的控件
拖拽过来之后你就能直接看到控件了,但据部分同学反映有的是拖拽不过来,有的是拖拽过来之后并没有变成控件形式,而是变成了一个个文本文件像下面这样:
因为每个VS版本或者电脑系统有差异性,在此我也不能完全料想到有哪些错误,因此大家如果做到这一步发现出现某些问题的话,请自行百度 谷歌。在这给大家例举一些我在网上看到的一些方法吧~(仅供参考):
5.总之正常来说,拖拽到工具栏之后,就你就能看到:
那么接下来,你需要把VideoSourcePlayer控件拖拽到窗口上。
在拖拽我们需要的三个button和一个combobox
6.我们计划在button1的事件中去搜寻当前设备连接上的所有监控设施,并将其列举在下拉列表中。
工具类:
public FilterInfoCollection GetDevices()
{
try
{
//枚举所有视频输入设备
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (videoDevices.Count != 0)
{
// LogClass.WriteFile("已找到视频设备.");
MessageBox.Show("已经找到设备");
return videoDevices;
}
else
return null;
}
catch (Exception ex)
{
// LogClass.WriteFile("error:没有找到视频设备!具体原因:" + ex.Message);
MessageBox.Show("没有找到视频设备");
return null;
}
}
Button1点击事件:
// 枚举所有视频输入设备
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (videoDevices.Count == 0)
throw new ApplicationException();
foreach (FilterInfo device in videoDevices)
{
comboBox1.Items.Add(device.Name);
}
comboBox1.SelectedIndex = 0;
}
Button2中去打开你在列表中选择的设备:
//连接摄像头
private void CameraConn()
{
VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
videoSource.DesiredFrameSize = new System.Drawing.Size(320, 240);
videoSource.DesiredFrameRate = 1;
videoSourcePlayer1.VideoSource = videoSource;
videoSourcePlayer1.Start();
}
Button3中选择让其停止且等待:
videoSourcePlayer1.SignalToStop();
videoSourcePlayer1.WaitForStop();//而非彻底关闭
然后就OK啦!(在下拉搜索列表里,你能看的你当前设备上所有链接到的监控设施,ps:因为当前我的笔记本上只有这一个摄像头所以就索引到了这一个~):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge;
using AForge.Controls;
using AForge.Imaging;
using AForge.Video;
using AForge.Video.DirectShow;
using System.Drawing.Imaging;
namespace Monitorr
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//监控设施的选择
private FilterInfoCollection videoDevices;
private VideoCaptureDevice videoSource;
public int selectedDeviceIndex = 0;
public FilterInfoCollection GetDevices()
{
try
{
//枚举所有视频输入设备
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (videoDevices.Count != 0)
{
// LogClass.WriteFile("已找到视频设备.");
MessageBox.Show("已经找到设备");
return videoDevices;
}
else
return null;
}
catch (Exception ex)
{
// LogClass.WriteFile("error:没有找到视频设备!具体原因:" + ex.Message);
MessageBox.Show("没有找到视频设备");
return null;
}
}
//连接摄像头
private void CameraConn()
{
VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
videoSource.DesiredFrameSize = new System.Drawing.Size(320, 240);
videoSource.DesiredFrameRate = 1;
videoSourcePlayer1.VideoSource = videoSource;
videoSourcePlayer1.Start();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
// 枚举所有视频输入设备
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (videoDevices.Count == 0)
throw new ApplicationException();
foreach (FilterInfo device in videoDevices)
{
comboBox1.Items.Add(device.Name);
}
comboBox1.SelectedIndex = 0;
}
catch (ApplicationException)
{
comboBox1.Items.Add("No local capture devices");
videoDevices = null;
}
}
private void button2_Click(object sender, EventArgs e)
{
if (button2.Text == "打开监控")
{
if (comboBox1.Text != null)
{
CameraConn();
}
else
{
MessageBox.Show("---------");
}
}
}
private void button3_Click(object sender, EventArgs e)
{
videoSourcePlayer1.SignalToStop();
videoSourcePlayer1.WaitForStop();
}
}
}
参考博客:
https://blog.csdn.net/special00/article/details/82629916
https://blog.csdn.net/chenhongwu666/article/details/40594365
https://blog.csdn.net/zwb_578209160/article/details/80309377