zoukankan      html  css  js  c++  java
  • winform 引用AForge调用摄像头拍照

    Nuget安装这个2个:

    AForge.Controls; 

    AForge.Video.DirectShow;

    code:

    namespace WindowsFormsApp1
    {
        partial class FormCameraVideo
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows Form Designer generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.tscbxCameras = new System.Windows.Forms.ComboBox();
                this.panelVideo = new System.Windows.Forms.Panel();
                this.button1 = new System.Windows.Forms.Button();
                this.button2 = new System.Windows.Forms.Button();
                this.SuspendLayout();
                // 
                // tscbxCameras
                // 
                this.tscbxCameras.FormattingEnabled = true;
                this.tscbxCameras.Location = new System.Drawing.Point(55, 50);
                this.tscbxCameras.Name = "tscbxCameras";
                this.tscbxCameras.Size = new System.Drawing.Size(379, 21);
                this.tscbxCameras.TabIndex = 0;
                this.tscbxCameras.SelectedIndexChanged += new System.EventHandler(this.tscbxCameras_SelectedIndexChanged);
                // 
                // panelVideo
                // 
                this.panelVideo.Location = new System.Drawing.Point(55, 94);
                this.panelVideo.Name = "panelVideo";
                this.panelVideo.Size = new System.Drawing.Size(379, 282);
                this.panelVideo.TabIndex = 1;
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(64, 392);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(128, 23);
                this.button1.TabIndex = 2;
                this.button1.Text = "btnPhotograph";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.btnPhotograph_Click);
                // 
                // button2
                // 
                this.button2.Location = new System.Drawing.Point(335, 392);
                this.button2.Name = "button2";
                this.button2.Size = new System.Drawing.Size(99, 23);
                this.button2.TabIndex = 3;
                this.button2.Text = "btnClose";
                this.button2.UseVisualStyleBackColor = true;
                this.button2.Click += new System.EventHandler(this.btnClose_Click);
                // 
                // FormCameraVideo
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(540, 451);
                this.Controls.Add(this.button2);
                this.Controls.Add(this.button1);
                this.Controls.Add(this.panelVideo);
                this.Controls.Add(this.tscbxCameras);
                this.Name = "FormCameraVideo";
                this.Text = "FormCameraVideo";
                this.Load += new System.EventHandler(this.FormCameraVideo_Load);
                this.ResumeLayout(false);
    
            }
    
            #endregion
    
            private System.Windows.Forms.ComboBox tscbxCameras;
            private System.Windows.Forms.Panel panelVideo;
            private System.Windows.Forms.Button button1;
            private System.Windows.Forms.Button button2;
        }
    }
    
    
    
    using AForge.Controls;
    using AForge.Video.DirectShow;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Forms;
    using System.Windows.Media.Imaging;
    
    namespace WindowsFormsApp1
    {
        public partial class FormCameraVideo : Form
        {
            public FormCameraVideo()
            {
                InitializeComponent();
                panelVideo.Controls.Add(videoSourcePlayer);
                videoSourcePlayer.Dock = DockStyle.Fill;
    
            }
            private VideoSourcePlayer videoSourcePlayer = new VideoSourcePlayer();
            private FilterInfoCollection videoDevices;
            private void FormCameraVideo_Load(object sender, EventArgs e)
            {
    
                try
                {
                    // 枚举所有视频输入设备
                    videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
    
                    if (videoDevices.Count == 0)
                        throw new ApplicationException();
    
                    foreach (FilterInfo device in videoDevices)
                    {
                        tscbxCameras.Items.Add(device.Name);
                    }
    
                    tscbxCameras.SelectedIndex = 0;
    
                }
                catch (ApplicationException)
                {
                    tscbxCameras.Items.Add("No local capture devices");
                    videoDevices = null;
                }
            }
    
    
    
            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 tscbxCameras_SelectedIndexChanged(object sender, EventArgs e)
            {
                CameraConn();
            }
    
    
    
    
            private void btnPhotograph_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() + "\CameraPic" + ".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)
                {
                    System.Windows.Forms.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;
            }
    
    
            /// <summary>
            /// Close the camera
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnClose_Click(object sender, EventArgs e)
            {
                videoSourcePlayer.SignalToStop();
                videoSourcePlayer.WaitForStop();
    
            }
    
    
        }
    }
    

      

    以上是全部代码。测试笔记本电脑摄像头OK。

  • 相关阅读:
    数据分析项目补充:医院药品销售数据分析
    会计实务会计分录——资产之流动资产篇
    HTTP请求过程和状态响应码
    网络爬虫深究-初识HTTP和https常识
    数字分析之一元线性回归及多元线性回归
    Navicat15激活(仅供学习使用,严禁任何商业用途)
    pandas补充(其二)与matplotlib补充
    pandas模块补充
    阿里云ecs服务器公网ip除了能telnet通22端口,其他如tomcat的8080端口telnet不通,原来需要添加安全组规则
    maven依赖里redis的依赖spring-boot-starter-data-redis和spring-boot-starter-redis有什么区别?
  • 原文地址:https://www.cnblogs.com/wgscd/p/13924657.html
Copyright © 2011-2022 走看看