首先用到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类库是非常的强大,这里只是冰山一角,文章不足之处还请大家多多指正,欢迎提出宝贵意见和建议。谢谢。。。
先移步http://www.aforgenet.com/ 下载AForge类库。陵镇
然后引用AForge,AForge.Controls(这个是控件,可以添加到工具箱中),AForge.Imaging,AForge.Video,AForge.Video.DirectShow
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 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
}
拍照
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)
videoSource.NewFrame -= new NewFrameEventHandler(videoSource_NewFrame)
}
录像的话调用AForge中的录像函数即可。可以在官网看他的说明文档,很详细。
你好,方法如下:作为程序员现在应该用的是vs2015,添加依赖项和依赖库和13一样,这里以13为例;
1.首先,打开vs2013和你扰汪备的project
2.要添加依赖项和依赖库,就要找到【解决方案资源管理器】,也许你的【陵运解决方案资源管理器】在右边或左边隐藏,点缓毁开即可
它的快捷键是ctrl+Alt+L
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)