• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C#调用AForge类库操作摄像头

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

很多都是C#调用window API 发送SendMessage,实现操作摄像头,但是C#调用window API的时候因为驱动的问题,总是弹出视频选择对话框,让人很是无语,看到大牛们有的截获到了window消息,然后模拟点击确定按钮,这是在是不敢恭维啊,还有的大牛根据API原型重写了,至于我是一只IT小小鸟了,然后在继续百度,找到了一个AForge强大的C#类库,最后终于搞定了,接下来将我拙劣的代码部分贴出来,以便同行或者需要的朋友学习交流,

首先用到AForge类库下载地址:http://www.aforgenet.com/

然后引用AForge,AForge.Controls(这个是控件,可以添加到工具箱中),AForge.Imaging,AForge.Video,AForge.Video.DirectShow;

然后直接上代码

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("已找到视频设备.");
                    return videoDevices;
                }
                else
                    return null;
            }
            catch (Exception ex)
            {
                LogClass.WriteFile("error:没有找到视频设备!具体原因:" + ex.Message);
                return null;
            }
        }

选择设备,然后连接摄像头

<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>
//抓图,拍照,单帧

public void GrabBitmap(string path)
{
if (videoSource == null)
return;
g_Path = path;
videoSource.NewFrame += new NewFrameEventHandler(videoSource_NewFrame);
}

 

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.Controls,然后添加到工具箱,然后将VideoSourcePlayer控件拖到窗体中,想要得到单张图像处理:

Bitmap bmp = videoSourcePlayer1.GetCurrentFrame();

这样就可以拿来处理了,AForge类库是非常的强大,这里只是冰山一角,文章不足之处还请大家多多指正,欢迎提出宝贵意见和建议。谢谢。。。
---------------------
作者:ching126
来源:CSDN
原文:https://blog.csdn.net/chenhongwu666/article/details/40594365
版权声明:本文为博主原创文章,转载请附上博文链接!


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#中三种弹出信息窗口的方式发布时间:2022-07-10
下一篇:
C#9.0新特性详解系列之五:记录(record)和with表达式发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap