前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >摄像头拍照功能是怎样实现的?自己动手做一个!

摄像头拍照功能是怎样实现的?自己动手做一个!

作者头像
跋扈洋
发布2021-07-19 10:25:37
7740
发布2021-07-19 10:25:37
举报
文章被收录于专栏:物联网知识物联网知识

需求分析

现如今,拍照已经融入我们的日常生活中了。我们在日常的工作生活中很多场景都会用到拍照功能。比如在登录网页或者设备时,密码错误进行拍照,防止被盗。日常进行图像识别或者图像处理前的图像获取。都需要用到我们的摄像头进行图像的获取。

前期准备

  • 带摄像头的电脑
  • Visual Studio 2019
  • AForge.NET Framework库文件

设计流程

1.首先我们在Visual Studio 2019创建一个工程

2.添加引用文件(不会使用AForge.NET Framework可以搜一下,网上例子很多)

3.设计自己的相关页面,其中关键在于videoSourcePlayer。这是引用文件里的

进行相关程序的编写,程序我放在后面。

4.运行打包

实现效果

页面布局

点击连接,连接到自己电脑的摄像头

点击拍照,拍照成的图像,将保存在你防止的文件夹里。

保存的文件夹在 GetImagePath()函数里

可以这样写

代码语言:javascript
复制
                private string GetImagePath()
        {
      
            string personImgPath = "D:\\图片";
            if (!Directory.Exists(personImgPath))
            {
                Directory.CreateDirectory(personImgPath);
            }

            return personImgPath;
        }

命名(string picName = GetImagePath() + "\\" + "xiaosy" + ".jpg";

重要代码

代码语言:javascript
复制

        private void btnConnect_Click(object sender, EventArgs e)
        {
            CameraConn();
           
        }
        //连接摄像头
        private void CameraConn()
        {
            VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[tscbxCameras.SelectedIndex].MonikerString);
            videoSource.DesiredFrameSize = new System.Drawing.Size(320, 240);
            videoSource.DesiredFrameRate = 1;

            videoSourcePlayer.VideoSource = videoSource;
            videoSourcePlayer.Start();
        }

        //关闭摄像头
        private void btnClose_Click(object sender, EventArgs e)
        {
            videoSourcePlayer.SignalToStop();
            videoSourcePlayer.WaitForStop();
        }

        //主窗体关闭
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            btnClose_Click(null, null);
        }

        //拍照
        private void Photograph_Click(object sender, EventArgs e)
        {
            try
            {
                if (videoSourcePlayer.IsRunning)
                {
                    BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                                    videoSourcePlayer.GetCurrentVideoFrame().GetHbitmap(),
                                    IntPtr.Zero,
                                     Int32Rect.Empty,
                                    BitmapSizeOptions.FromEmptyOptions());
                    PngBitmapEncoder pE = new PngBitmapEncoder();
                    pE.Frames.Add(BitmapFrame.Create(bitmapSource));
                    string picName = GetImagePath() + "\\" + "xiaosy" + ".jpg";
                    if (File.Exists(picName))
                    {
                        File.Delete(picName);
                    }
                    using (Stream stream = File.Create(picName))
                    {
                        pE.Save(stream);
                    }
                    //拍照完成后关摄像头并刷新同时关窗体
                    if (videoSourcePlayer != null && videoSourcePlayer.IsRunning)
                    {
                        videoSourcePlayer.SignalToStop();
                        videoSourcePlayer.WaitForStop();
                    }
                    
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("摄像头异常:" + ex.Message);
            }
        }

        private string GetImagePath()
        {
            string personImgPath = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory)
                         + Path.DirectorySeparatorChar.ToString() + "PersonImg";
            if (!Directory.Exists(personImgPath))
            {
                Directory.CreateDirectory(personImgPath);
            }

            return personImgPath;
        }
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-06-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 物联网知识 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档