C# 开启摄像头 拍照

引用 Aforge

Aforge 介绍:http://baike.baidu.com/link?url=T3XRoneXNTVU8Hoh2tNOtiBYXCPBYmslpVUWWPw4c2pxyclFtmMgAlTNlBhLiXoLN_GamxUpiI3x-UBAErSUNa

获得摄像设备

     public class FaceCommon
        {
            #region 方法
            /// <summary>
            /// 获取已插USB摄像头硬件Id
            /// </summary>
            /// <returns></returns>
            public static List<string> GetCameraDeviceId()
            {
                List<string> _cameraList = new List<string>();
                FilterInfoCollection _filterInfoCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);// 过滤信息  获取所有已插USB摄像头驱动信息
                if (_filterInfoCollection != null && _filterInfoCollection.Count > 0)
                {
                    for (int i = 0; i < _filterInfoCollection.Count; i++)
                    {
                        _cameraList.Add(_filterInfoCollection[i].MonikerString); //向集合中添加USB摄像头硬件Id
                    }
                    _cameraList.Remove(""); //移出空项
                    return _cameraList;
                }
                else
                {
                    return null;
                }
            }
            #endregion
        }
     
      /// <summary>
        /// 开始播放
        /// </summary>   

        private void StartCamera()
        {
            if (_videoCaptureDevice != null)
            {
                _videoCaptureDevice.Start();
            }
        }
        /// <summary>
        /// 停止播放
        /// </summary>
        private void StopCamera()
        {
            if (_videoCaptureDevice != null)
            {
                _videoCaptureDevice.SignalToStop();
            }
        }

        /// <summary>
        /// 初始化摄像头
        /// </summary>
        private void InitialCamera()
        {
            if (this.comboBox1.SelectedItem != null)
            {
                _videoCaptureDevice = new VideoCaptureDevice(this.comboBox1.SelectedItem.ToString());
                _videoCaptureDevice.NewFrame += HandNewFrame;
            }
        }
        private void HandNewFrame(object sender, NewFrameEventArgs args)
        {
            try
            {
                this.Invoke(new Action(() =>           //拉姆达表达式 =〉后面跟着函数体
                {
                    if (args != null)
                    {
                        this.pictureBox1.Image = args.Frame.Clone() as Image;
                        imgobj = args.Frame.Clone() as Image;
                    }
                }));

            }
            catch (Exception)
            {
                //throw;
            }

        }

开始摄像:

    private void button1_Click(object sender, EventArgs e)
        {
            InitialCamera();
            StartCamera();
        }

 拍照并将图片保存:

      StopCamera();
            pictureBox1.BackgroundImage = imgobj;
            if (pictureBox1.BackgroundImage != null)
            {
                Bitmap bmp = new Bitmap(pictureBox1.BackgroundImage);
                if (sm.textBox1.Text== String.Empty)
                {
                    MessageBox.Show("请先完善人员的基本信息", "警告");
                }
                else
                {
                    String number = sm.textBox1.Text.Trim();
                     String dir = System.Environment.CurrentDirectory + "\\Images\\" + number + ".bmp";
                     if (File.Exists(@dir))
                     {
                         File.Delete(@dir);
                         bmp.Save("Images//" + number + ".bmp");
                         bmp.Dispose();                          //记得释放 不然会提醒文件被另一进程使用。
                     }
                     else
                     {
                         bmp.Save("Images//" + number + ".bmp");
                         bmp.Dispose();
                     }
                    if (File.Exists(@dir))
                    {   
                        sm.pictureBox1.Image = Image.FromFile(dir);
                        sm.pictureBox1.ImageLocation = @dir;
                    }
                    else 
                    { 
                         //
                    }
                }
             }

界面截图:

posted @ 2016-04-12 14:53  暖暖要坚持  阅读(1932)  评论(0编辑  收藏  举报