AFORGE库对摄像头操作的封装(续)

来源:互联网 发布:杯装茶 知乎 编辑:程序博客网 时间:2024/06/07 06:57

    下午做的封装嘛。 感觉不是很好。 操作还是太多。但是我觉得 适用的还是比较广。刚刚手残, 把下午的代码都删了,现在都没法贴上下午笔记中的图了。

    我这里重新做了次封装。

----------------------------封装约定

     按照下面的这个模式封装的:

    1, open

     2,capture

     3, close

----------------------------准备工作

     ------A

     1, 去AFORGE下载DLL。我这里丢个链接。AFORGE_DLL (所有的)

     -----B

       在封装 DLL的工程中添加要用到AFORGE 的DLL 的 引用

AForge.Video.dll;

AForge.Video.DirectShow.dll;

system.drawing.dll 


      接着添加名称空间:

///---using AForge.Video;using AForge.Video.DirectShow;using System.Drawing;using System.Windows.Forms;

    -------C

    在测试用例中添加自己写的DLL后,添加名称空间:

using Webcam;

----------------------------上码

    下面是使用我自己封装的DLL 的测试结果(晚上拍的, 我的摄像头的分比率也不高):

     我用的是控件pictureBox承载图像的。DLL 中也是的回调函数也是用的pictureBox



                                   不废话了, 直接上码。

 -----------------DLL源码:

/*     使用该DLL操作摄像头, 操作结束后,一定要使用 close()函数断开摄像头的连接。 */using System;using System.Collections.Generic;using System.Linq;using System.Text;///---using AForge.Video;using AForge.Video.DirectShow;using System.Drawing;using System.Windows.Forms;namespace Webcam{    public class WebcamHelper    {        #region 变量区        private FilterInfoCollection videoDevices;        private VideoCaptureDevice WebCam;        private int pic_NewWidth;       ///--指定图片的宽度,        private int pic_NewHeight;      ///---指定图片的高度度        private PictureBox picturebox;  ///--承载图像        private string filepath;            ///---保存图片的路径,  路径中不需要指明 图片的格式。                                                    ///---例如:filepath = C:                                                    ///--不能传: filepath = C:1.jpg。        private Bitmap bitmap;          ///--保存抓拍截图        #endregion        #region 方法区        ///--构造函数        ///--picturebox是承载图像的,        ///--picturepath 是抓图的存储路径,        ///---newWidth 是指定存图的宽度        ///---newHeight 是指定存图的高度        public WebcamHelper(ref PictureBox pictureBox, string picturePath, int newWidth, int newHeight)        {            ///--指定图片缩放的宽度和高度            this.pic_NewHeight = newHeight;            this.pic_NewWidth = newWidth;            ///--图片路径            this.filepath = picturePath;            ///---picturebox,            this.picturebox = new PictureBox();            this.picturebox = pictureBox;        }        ///--函数功能:枚举输入设备;选择摄像头输入设备;保存图片的路径;        public void Open(string videoDeviceName = "")        {            ///--枚举可用的摄像头输入设备            this.videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);            if (0 == videoDevices.Count)            {                ///---没有可用的输入设备                throw new Exception("当前没有可用的摄像头输入设备");            }            ///--保存当前输入设备的名称            string[] videoDevicesName = new string[videoDevices.Count];            ///---计数器            int count = 0;            ///---获取设备名称            foreach (FilterInfo item in videoDevices)            {                ///--将设备名称返回到数组中                videoDevicesName[count++] = item.Name;            }            ///--计数器清零            count = 0;            ///---判断传入的指定的设备名是否为空            ///---为空,默认使用第一个设备            if (string.Empty == videoDeviceName)            {                this.WebCam = new VideoCaptureDevice(videoDevices[0].MonikerString);            }            else            {                ///-设备索引                int index = 0;                ///--使用指定的输入设备名称                ///---查找当前指定的输入设备在输入设备中的索引                for (int i = 0; i < videoDevices.Count; i++)                {                    if (videoDeviceName == videoDevicesName[i])                    {                        index = i;                        break;                    }                }                ///---连接指定的设备                this.WebCam = new VideoCaptureDevice(videoDevices[index].MonikerString);             }            ///--设定摄像头的分辨率为默认使用的分辨率            this.WebCam.VideoResolution = this.WebCam.VideoCapabilities[0];            ///--打开摄像头            this.WebCam.Start();            this.WebCam.NewFrame += new             NewFrameEventHandler(WebcamNewFrameCallBack);        }        ///---回调函数        private void WebcamNewFrameCallBack(object obj, NewFrameEventArgs eventArgs)        {            bitmap = (Bitmap)eventArgs.Frame.Clone();            this.picturebox.Image = bitmap;            GC.Collect();        }        ///--函数功能:抓拍图片,并保存        ///---成功返回:true. 失败返回:false、        public bool Capture()        {            if (bitmap != null)            {                ///--缩放图片                Zoom(ref this.bitmap, this.pic_NewWidth, this.pic_NewHeight).Save(this.filepath+".jpg");                return true;            }            else            {                ///--存储失败                return false;            }        }        ///--函数功能:实现抓拍的图像缩放        ///---返回缩放的图片。        private Bitmap Zoom(ref Bitmap bitmap, int new_Width, int new_Height)        {            ///--新建一个bitmap对象            Bitmap newBitmap = new Bitmap(new_Width, new_Width);            Graphics newG = Graphics.FromImage(newBitmap);            ///--插入算法的质量            newG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;            ///--重绘            newG.DrawImage(bitmap, new Rectangle(0, 0, new_Width, new_Width), new Rectangle(0, 0, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel);            ///--释放绘图工具            newG.Dispose();            ///---返回新结果            return newBitmap;        }        ///---函数功能:断开摄像头连接        ///---参数,无        public void Close()        {            if (this.WebCam != null)            {                if (this.WebCam.IsRunning)                {                    this.WebCam.Stop();                }            }        }        #endregion 方法区    }}

------------使用DLL 测试源码

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using Webcam;namespace WindowsFormsApplication1{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private WebcamHelper newW;        /// <summary>        /// --打开按钮被单击事件        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void openBtn_Click(object sender, EventArgs e)        {            string filepath =Application.StartupPath + DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss");            newW = new WebcamHelper(ref this.pictureBox1, filepath, 200, 100);            newW.Open("");        }        /// <summary>        /// ---窗口加载事件        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void Form1_Load(object sender, EventArgs e)        {        }        /// <summary>        /// --关闭按钮被单击事件        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void 关闭_Click(object sender, EventArgs e)        {            newW.Close();        }        /// <summary>        /// --抓图按钮被单击事件        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void captureBtn_Click(object sender, EventArgs e)        {            if (true == newW.Capture())            {                MessageBox.Show("拍照成功");            }        }    }}

----------------------------下载

    上面的 DLL  +  DLL 源码 + 测试用例源码 下载链接:

     点我试试


0 0
原创粉丝点击