您的位置:首页 > 其它

Aforge的一点简单应用

2016-08-02 21:33 176 查看
    今天正好需要将一些无标识的图片按照甲方给的编号将图片命名,我了个去,2340张,2340张啊!!!还是无偿的啊。。。。。心中无数只草尼草呼啸而过。。真是上面运动嘴,下面跑断腿啊。。啥也别说了,整吧。。要不不能怎么着。说实话,以前的项目也没有图片处理的经验,百度上神游一圈,别说,还真有点眉目了。四下对此下来,一个叫AForge的控件还挺对胃口的。再一圈参考下来,打开VS2008,洗个澡,let's go !

首先添加一些引用:

using AForge;

using AForge.Controls;

using AForge.Video;

using AForge.Video.DirectShow;

using AForge.Imaging;

using AForge.Imaging.Filters;

然后新建一个选项卡(千万别告诉我不会 - -!),然后把Aforge.Controls.dll拖放在上面,会出现几个工具,如下图所示:

将VideoSourcePlayer控件拖放在form上,出会现控件区域。

然后该coding了,在form_load中整代码如下:

        private void Form1_Load(object sender, EventArgs e)

        {

            count = 1;

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

            videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

            if (videoDevices.Count == 0)

            {

                MessageBox.Show("未发现视频设备!");

                return;

            }

           

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

            videoSource.DesiredFrameSize = new Size(800, 600);

            videoSource.DesiredFrameRate = 1;

            videoSourcePlayer1.VideoSource = videoSource;

        }

先枚举视频设备,并定义大小和帧数。

然后打开摄像头:

 videoSourcePlayer1.Start();

然后在videoSourcePlayer事件中找到NewFrame事件并双击:

在里面编辑代码如下:

                Bitmap bitMap = image;

                bitMap.Save(@"D:\" + textBox1.Text.Trim() + "_" + count.ToString() + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);

//如果没有下面这行代码,将连续拍照,此行代码关键!!!!

                videoSourcePlayer1.NewFrame -= new VideoSourcePlayer.NewFrameHandler(videoSourcePlayer1_NewFrame);

然后在拍照按钮中代码如下:

注册这个事件,

  videoSourcePlayer1.NewFrame +=new VideoSourcePlayer.NewFrameHandler(videoSourcePlayer1_NewFrame);

基本代码就完成了,但是有个坑一直没有解决,望哪位大量指点,这个坑是:

一打开视频自动就会照一张图片是为什么呢??求解决啊求解决!

海纳百川,有容乃大;壁立千仞,无欲则钢!

测试代码链接:http://download.csdn.net/my/uploads
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  AForge 图像处理