ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C#扫码枪触发拍照(扫码枪支持串口COM通讯)

2021-09-09 16:04:06  阅读:241  来源: 互联网

标签:扫码 C# System Forms Windows 串口 new Drawing Size


效果图

 

 

1.普通USB摄像头

2.扫码枪支持串口通讯

3.引用AFORGE

**************************************

引用的内容(自己下载)

 

 

 

 

**************************************

C#Form1.Designer.cs 窗口设计代码

namespace com_get
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.serialPort1 = new System.IO.Ports.SerialPort(this.components);
            this.button1 = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.comboBox1 = new System.Windows.Forms.ComboBox();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.button2 = new System.Windows.Forms.Button();
            this.label2 = new System.Windows.Forms.Label();
            this.comboBox2 = new System.Windows.Forms.ComboBox();
            this.button3 = new System.Windows.Forms.Button();
            this.button4 = new System.Windows.Forms.Button();
            this.button5 = new System.Windows.Forms.Button();
            this.pictureBox1 = new AForge.Controls.PictureBox();
            this.button6 = new System.Windows.Forms.Button();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
            this.videplayer = new AForge.Controls.VideoSourcePlayer();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // serialPort1
            // 
            this.serialPort1.DiscardNull = true;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(41, 634);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "启动";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Font = new System.Drawing.Font("宋体", 11F);
            this.label1.Location = new System.Drawing.Point(38, 588);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(46, 15);
            this.label1.TabIndex = 1;
            this.label1.Text = "COM口";
            // 
            // comboBox1
            // 
            this.comboBox1.FormattingEnabled = true;
            this.comboBox1.Location = new System.Drawing.Point(100, 583);
            this.comboBox1.Name = "comboBox1";
            this.comboBox1.Size = new System.Drawing.Size(58, 20);
            this.comboBox1.TabIndex = 2;
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(38, 663);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
            this.textBox1.Size = new System.Drawing.Size(185, 51);
            this.textBox1.TabIndex = 3;
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(122, 634);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(83, 23);
            this.button2.TabIndex = 5;
            this.button2.Text = "停止";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(92, 32);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(41, 12);
            this.label2.TabIndex = 6;
            this.label2.Text = "摄像头";
            // 
            // comboBox2
            // 
            this.comboBox2.FormattingEnabled = true;
            this.comboBox2.Location = new System.Drawing.Point(139, 29);
            this.comboBox2.Name = "comboBox2";
            this.comboBox2.Size = new System.Drawing.Size(121, 20);
            this.comboBox2.TabIndex = 7;
            // 
            // button3
            // 
            this.button3.Location = new System.Drawing.Point(318, 27);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(75, 23);
            this.button3.TabIndex = 8;
            this.button3.Text = "打开摄像头";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button3_Click);
            // 
            // button4
            // 
            this.button4.Cursor = System.Windows.Forms.Cursors.Default;
            this.button4.Location = new System.Drawing.Point(419, 27);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(75, 23);
            this.button4.TabIndex = 9;
            this.button4.Text = "关闭摄像头";
            this.button4.UseVisualStyleBackColor = true;
            this.button4.Click += new System.EventHandler(this.button4_Click);
            // 
            // button5
            // 
            this.button5.Location = new System.Drawing.Point(538, 27);
            this.button5.Name = "button5";
            this.button5.Size = new System.Drawing.Size(63, 23);
            this.button5.TabIndex = 11;
            this.button5.Text = "拍照";
            this.button5.UseVisualStyleBackColor = true;
            this.button5.Click += new System.EventHandler(this.button5_Click);
            // 
            // pictureBox1
            // 
            this.pictureBox1.BackColor = System.Drawing.SystemColors.ControlDark;
            this.pictureBox1.Image = null;
            this.pictureBox1.Location = new System.Drawing.Point(25, 186);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(198, 165);
            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            this.pictureBox1.TabIndex = 12;
            this.pictureBox1.TabStop = false;
            // 
            // button6
            // 
            this.button6.Location = new System.Drawing.Point(671, 26);
            this.button6.Name = "button6";
            this.button6.Size = new System.Drawing.Size(75, 23);
            this.button6.TabIndex = 13;
            this.button6.Text = "保存路径";
            this.button6.UseVisualStyleBackColor = true;
            this.button6.Click += new System.EventHandler(this.button6_Click);
            // 
            // textBox2
            // 
            this.textBox2.Location = new System.Drawing.Point(753, 28);
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(186, 21);
            this.textBox2.TabIndex = 14;
            // 
            // videplayer
            // 
            this.videplayer.BackColor = System.Drawing.SystemColors.ButtonShadow;
            this.videplayer.Location = new System.Drawing.Point(320, 60);
            this.videplayer.Name = "videplayer";
            this.videplayer.Size = new System.Drawing.Size(1116, 766);
            this.videplayer.TabIndex = 10;
            this.videplayer.Text = "videoSourcePlayer1";
            this.videplayer.VideoSource = null;
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Font = new System.Drawing.Font("宋体", 12F);
            this.label3.Location = new System.Drawing.Point(45, 399);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(88, 16);
            this.label3.TabIndex = 15;
            this.label3.Text = "流程单号码";
            this.label3.TextAlign = System.Drawing.ContentAlignment.TopCenter;
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.label4.Font = new System.Drawing.Font("宋体", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label4.Location = new System.Drawing.Point(41, 719);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(193, 107);
            this.label4.TabIndex = 18;
            this.label4.Text = "\r\n设置方法\r\n1.打开摄像头\r\n2.设置保存图片的路径\r\n3.选择扫描枪的 COM口  \r\n4.按下启动\r\n\r\n";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(1473, 862);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.button6);
            this.Controls.Add(this.pictureBox1);
            this.Controls.Add(this.button5);
            this.Controls.Add(this.videplayer);
            this.Controls.Add(this.button4);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.comboBox2);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.comboBox1);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "扫码枪通讯";
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.IO.Ports.SerialPort serialPort1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.ComboBox comboBox1;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.ComboBox comboBox2;
        private System.Windows.Forms.Button button3;
        private System.Windows.Forms.Button button5;
        private AForge.Controls.PictureBox pictureBox1;
        private System.Windows.Forms.Button button6;
        private System.Windows.Forms.TextBox textBox2;
        private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;
        public AForge.Controls.VideoSourcePlayer videplayer;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Button button4;
        private System.Windows.Forms.Label label4;
    }
}

  

**********************************

DOMO代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.IO;
using System.Windows.Media.Imaging;
using AForge;
using AForge.Controls;
using AForge.Video;
using AForge.Video.DirectShow;
using Size = System.Drawing.Size;


namespace com_get
{
    public partial class Form1 : Form
    {
        private FilterInfoCollection videoDevices;  //AFORGE摄像头
        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //枚举COM口
            #region  
            foreach (string com in System.IO.Ports.SerialPort.GetPortNames())
            {
                comboBox1.Items.Add(com);  //如果有COM口就添加进去
            }
            comboBox1.SelectedIndex = 1;  //COM选择默认第一个
            #endregion

            //枚举视频信号
            #region  
            try
            {
                // 枚举所有视频输入设备
                videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

                if (videoDevices.Count == 0)
                    throw new ApplicationException();

                foreach (FilterInfo device in videoDevices)
                {
                    comboBox2.Items.Add(device.Name);  
                }
                comboBox2.SelectedIndex = 0;
            }
            catch (ApplicationException)
            {
                comboBox2.Items.Add("没有视频设备");
                videoDevices = null;
            }
            #endregion

            button2.Enabled = false;                         //COM关闭不可以按
            button4.Enabled = false;                         //关闭视频不可按
            serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(ComPort_DataReceived);//定义串口接受数据的一个方法

            #region #判斷文件夾是否存在,對文件夾進行處理
            if (Directory.Exists(setting.FilePath) == false)
            {
                Directory.CreateDirectory(setting.FilePath);      //如果文件不存在则創建文件夾
            }
            #endregion
            Control.CheckForIllegalCrossThreadCalls = false;
        }


        #region #串口方法
        private void ComPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) //串口函数
        {
            string str = serialPort1.ReadExisting(); //定义字符型变量str,把COM读取到的赋值给str
            //textBox1.AppendText(str+"\r\n");               //str输出到屏幕框框内
            setting.FileName = str + ".jpg";
            getpicture();
        }
        #endregion
       

        private void button1_Click(object sender, EventArgs e)       //启动按钮
        {
            try
            {
                serialPort1.PortName = comboBox1.Text;                     //设置COM口
                serialPort1.BaudRate = 9600;                               //COM传输速度
                serialPort1.DataBits = 8;                                  //COM数据位
                serialPort1.Parity = System.IO.Ports.Parity.None;          //奇偶校验
                serialPort1.StopBits = System.IO.Ports.StopBits.One;       //COM停止位
                serialPort1.Open();                                        //打开端口
                textBox1.AppendText("启动成功" + "\r\n");
                button1.Enabled = false;                            
                button2.Enabled = true;
                comboBox1.Enabled = false;
            }
            catch
            {
                textBox1.AppendText("启动失败" + "\r\n");
                MessageBox.Show("COM打开失败");

            }

        }
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort1.Close();                                 //关闭COM口
                textBox1.AppendText("关闭成功" + "\r\n");
                button1.Enabled = true;
                button2.Enabled = false;
                comboBox1.Enabled = true;

                
            }
            catch
            {
                textBox1.AppendText("COM关闭失败" + "\r\n");
                MessageBox.Show("关闭失败");
            }
        }  //关闭按钮


        private void button3_Click(object sender, EventArgs e)
        {
            Camera_Connect();
        }

        private void Camera_Connect()
        {
            if (videoDevices == null)
            {
                MessageBox.Show("打开失败");
            }
            else
            {
                VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[comboBox2.SelectedIndex].MonikerString);//定义视频源
                //videoSource.DesiredFrameSize = new System.Drawing.Size(1280, 1024); //设置大小
                //videoSource.DesiredFrameRate = 1;  //设置帧率
                videplayer.VideoSource = videoSource; //控件与摄像头连接
                videplayer.Start();                     //控件开始将摄像头内容显示出来
                button4.Enabled = true;
                button3.Enabled = false;
            }

        }

        private void button4_Click(object sender, EventArgs e)
        {
            videplayer.SignalToStop(); //停止控件播放
            videplayer.WaitForStop();
            button3.Enabled = true;
            button4.Enabled = false;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //程序关闭前释放摄像头资源和COM断开
            videplayer.SignalToStop();
            videplayer.WaitForStop();
            serialPort1.Close(); 
        }

        private void button5_Click(object sender, EventArgs e)

        {
            if (videplayer.IsRunning)
            {
                getpicture();
            }
            else
            {
        
                pictureBox1.Image = null;
            }
        }

        private void getpicture()  //拍照
        {
          
            try
            {
                BitmapSource bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                                videplayer.GetCurrentVideoFrame().GetHbitmap(),
                                IntPtr.Zero,
                                 Int32Rect.Empty,
                                BitmapSizeOptions.FromEmptyOptions());
                PngBitmapEncoder pE = new PngBitmapEncoder();
                pE.Frames.Add(BitmapFrame.Create(bitmapSource));
                string paddress = setting.FilePath + "\\" + setting.FileName;
                if (File.Exists(paddress))
                {
                    File.Delete(paddress);
                }
                using (Stream stream = File.Create(paddress))
                {
                    pE.Save(stream);
                }
                pictureBox1.Image = videplayer.GetCurrentVideoFrame();//放图片到右边栏
                label3.Text = setting.FileName;

            }
            catch
            {
                MessageBox.Show("拍照失败");
                pictureBox1.Image = null;
            }
            
        }

       

        private void button6_Click(object sender, EventArgs e)
        {

            folderBrowserDialog1.Description = "请选择文件夹";
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK || folderBrowserDialog1.ShowDialog() == DialogResult.Yes)
            {
                textBox2.Text = folderBrowserDialog1.SelectedPath;
                setting.FilePath = folderBrowserDialog1.SelectedPath;
            }

        }

    }
}

  

***********************

setting.css

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace com_get
{
    class setting
    {
        //文件基本参数
        public static string FileName = "test.jpg";  //默认图片名称
        public static string FilePath = "D:\\picture";   //定义保存的文件夹
        public static int pz=0;


      
        
    }
}

 

标签:扫码,C#,System,Forms,Windows,串口,new,Drawing,Size
来源: https://www.cnblogs.com/ribi/p/15247352.html

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

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

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

ICode9版权所有