
首先用到AForge类库下载地址
然后引用AForge,AForge.Controls(这个是控件,可以添加到工具箱中),AForge.Imaging,AForge.Video,AForge.Video.DirectShow
然后直接上代码
[csharp] view plain copy print?
private FilterInfoCollection videoDevices
private VideoCaptureDevice videoSource
public int selectedDeviceIndex = 0
private FilterInfoCollection videoDevicesprivate VideoCaptureDevice videoSource
public int selectedDeviceIndex = 0
下面是获取设备
[csharp] view plain copy print?
public FilterInfoCollection GetDevices()
{
try
{
//枚举所有视频输入设备
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice)
if (videoDevices.Count != 0)
{
LogClass.WriteFile("已找到视频设备.")
return videoDevices
}
else
return null
}
catch (Exception ex)
{
LogClass.WriteFile("error:没有找到视频设备!具体原因:" + ex.Message)
return null
}
}
public FilterInfoCollection GetDevices(){
try
{
//枚举所有视频输入设备
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice)
if (videoDevices.Count != 0)
{
LogClass.WriteFile("已找到视频设备.")
return videoDevices
}
else
return null
}
catch (Exception ex)
{
LogClass.WriteFile("error:没有找到视频设备!具体原因:" + ex.Message)
return null
}
}
选择设备,然后连接摄像头
[csharp] view plain copy print?
<p> /// <summary>
/// 连接视频摄像头
/// </summary>
/// <param name="deviceIndex"></param>
/// <param name="resolutionIndex"></param>
/// <returns></returns>
public VideoCaptureDevice VideoConnect(int deviceIndex = 0, int resolutionIndex = 0)
{
if (videoDevices.Count <= 0)
return null
selectedDeviceIndex = deviceIndex
videoSource = new VideoCaptureDevice(videoDevices[deviceIndex].MonikerString)</p><p> return videoSource
}</p>
<p> /// <summary>/// 连接视频摄像头
/// </summary>
/// <param name="deviceIndex"></param>
/// <param name="resolutionIndex"></param>
/// <returns></returns>
public VideoCaptureDevice VideoConnect(int deviceIndex = 0, int resolutionIndex = 0)
{
if (videoDevices.Count <= 0)
return null
selectedDeviceIndex = deviceIndex
videoSource = new VideoCaptureDevice(videoDevices[deviceIndex].MonikerString)</p><p> return videoSource
}</p>
[csharp] view plain copy print?
//抓图,拍照,单帧
public void GrabBitmap(string path)
{
if (videoSource == null)
return
g_Path = path
videoSource.NewFrame += new NewFrameEventHandler(videoSource_NewFrame)
}
//抓图,拍照,单帧public void GrabBitmap(string path)
{
if (videoSource == null)
return
g_Path = path
videoSource.NewFrame += new NewFrameEventHandler(videoSource_NewFrame)
}
[csharp] view plain copy print?
void videoSource_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
{
Bitmap bmp = (Bitmap)eventArgs.Frame.Clone()
string fullPath = path + "temp\\"
if (!Directory.Exists(fullPath))
Directory.CreateDirectory(fullPath)
string img = fullPath + DateTime.Now.ToString("yyyyMMdd hhmmss") + ".bmp"
bmp.Save(img)
void videoSource_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs){
Bitmap bmp = (Bitmap)eventArgs.Frame.Clone()
string fullPath = path + "temp\\"
if (!Directory.Exists(fullPath))
Directory.CreateDirectory(fullPath)
string img = fullPath + DateTime.Now.ToString("yyyyMMdd hhmmss") + ".bmp"
bmp.Save(img)
[csharp] view plain copy print?
//如果这里不写这个,一会儿会不停的拍照,
videoSource.NewFrame -= new NewFrameEventHandler(videoSource_NewFrame)
}
//如果这里不写这个,一会儿会不停的拍照,videoSource.NewFrame -= new NewFrameEventHandler(videoSource_NewFrame)
}
这样就完成了 *** 作摄像头的工作
但是发现一个问题,如果要拍照得到的照片先要处理在保存,这里就有问题了,所以需要在界面前台中添加控件,医用AForge.Controls,然后添加到工具箱,然后将VideoSourcePlayer控件拖到窗体中,想要得到单张图像处理:
Bitmap bmp = videoSourcePlayer1.GetCurrentFrame()
这样就可以拿来处理了,AForge类库是非常的强大,这里只是冰山一角,文章不足之处还请大家多多指正,欢迎提出宝贵意见和建议。谢谢。。。
系统故障。系统故障导致vb.net工程添加引用时找不到microsoftspeec,是需要等待系统恢复的,此软件是一种简单,现代,面向对象的计算机编程语言,它由微软开发,将NETFramework和公共语言运行库的强大功能相结合。dll 动态连接库mml 不知道
olb 类型库
pip 个人信息配置文件
acl 不知道
xml 和网页有关
ocx 系统文件
Com选项卡多为组件,而另外的多为类库,其实区别不区别的没什么,只要是自己用到就可以进行引用
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)