ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-07-02 17:03:09  阅读:218  来源: 互联网

标签:动手做 拍照 videoSourcePlayer string private personImgPath 摄像头


 

需求分析

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

前期准备
  1. 带摄像头的电脑
  2. Visual Studio 2019
  3. AForge.NET Framework库文件
设计流程
  1. 首先我们在Visual Studio 2019创建一个工程
  2. 添加引用文件(不会使用AForge.NET Framework可以搜一下,网上例子很多)
  3. 设计自己的相关页面,其中关键在于videoSourcePlayer。这是引用文件里的
  4. 进行相关程序的编写,程序我放在后面。
  5. 运行打包(下面有我打包好的软件,可以点击看看运行结果)
实现效果
  1. 页面布局

  2. 点击连接,连接到自己电脑的摄像头
  3. 点击拍照,拍照成的图像,将保存在你防止的文件夹里。
    保存的文件夹在 GetImagePath()函数里
    可以这样写
                private string GetImagePath()
        {
      
            string personImgPath = "D:\\图片";
            if (!Directory.Exists(personImgPath))
            {
                Directory.CreateDirectory(personImgPath);
            }

            return personImgPath;
        }

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

重要代码
        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;
        }
源码及程序整合
  1. 工程源码
  2. 可直接点击进行运行的程序

 

编写不易,感谢支持。

 

标签:动手做,拍照,videoSourcePlayer,string,private,personImgPath,摄像头
来源: https://blog.51cto.com/u_15260779/2971530

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有