您的位置:首页 > 编程语言 > C#

C#读取摄像头处理图片AForge

2015-12-08 21:27 435 查看
上一篇文章是用avicap读取摄像头处理图片的,很麻烦,而本篇文章是用新的方法AForge,该方法是很方便很好用的,其中有一篇文章很好,链接如下:
http://blog.csdn.net/chenhongwu666/article/details/40594365
该文章是讲如何使用AForge的,而且也有程序,主要分几步:

(1)到该网址
http://www.aforgenet.com/ 下载并安装AForge.NETFramework.exe文件

(2)在安装路径下的AForge.NET文件夹---Framework---Release文件夹中,把AForge.Controls.dll,AForge.dll,AForge.Imaging.dll,AForge.Video.dll,AForge.Video.DirectShow.dll等几个库文件copy到我们工程根目录下。

(3)对于VS系列软件的使用者来说,要把AForge.Controls.dll(工具)添加到左侧的工具箱中,方法如下:

A.工具箱右键---选择项----.NET Framework组件----浏览---到根目录下选择上面dll库中的,AForge.Controls.dll库,因为这是一个控件。

(4)添加成功后,会出现VideoSourcePlayer控件,我们主要用的就是他,该控件的功能比pictureBox还要强大。


(5)右键工程项目----添加引用---把根目录下的几个dll文件都加进来。

(6)在using中加入这个几个dll引用。

基本上配置完成了,下面就是程序:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Drawing.Imaging;

using System.Text;

using System.Windows.Forms;

using System.Threading;

using AForge;

using AForge.Video;

using AForge.Video.DirectShow;

using AForge.Imaging;

using AForge.Imaging.Filters;

namespace Camera

{

    public partial class Form1 : Form

    {

        private FilterInfoCollection videoDevices;

        public Form1()

        {

            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)

        {

            this.Location = new System.Drawing.Point(0, 0);

            this.WindowState = FormWindowState.Maximized;

            try

            {

                // 枚举所有视频输入设备

                videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

                if (videoDevices.Count == 0)

                    throw new ApplicationException();

                foreach (FilterInfo device in videoDevices)

                {

                    tscbxCameras.Items.Add(device.Name);

                }

                tscbxCameras.SelectedIndex = 0;

            }

            catch (ApplicationException)

            {

                tscbxCameras.Items.Add("No local capture devices");

                videoDevices = null;

            }

        }

        private void toolStripButton1_Click(object sender, EventArgs e)

        {

            CameraConn();

        }

        //测试

        int count;

        //测试

        private void CameraConn()

        {

            VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[tscbxCameras.SelectedIndex].MonikerString);

            videoSource.DesiredFrameSize = new Size(640,640);//320, 240

            videoSource.DesiredFrameRate = 1;

            videPlayer.VideoSource = videoSource;

            videPlayer.Start();

            //测试

            count = videoSource.BytesReceived;

            //测试

        }

        private void toolStripButton2_Click(object sender, EventArgs e)

        {

            videPlayer.SignalToStop();

            videPlayer.WaitForStop();

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)

        {

            toolStripButton2_Click(null, null);

        }

        private void videPlayer_MouseDown(object sender, MouseEventArgs e)

        {

            this.textBox1.Text = count.ToString();

        }

    }

}

还有其他功能的需求的话就就参考最上面分享的那个链接就行了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息