Winform的条形码识别

 代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge.Video;
using AForge.Video.DirectShow;
using ZXing;
using System.IO;
using System.Diagnostics;
namespace WindowsFormsApp6
{
    public partial class Form1 : Form
    {
        FilterInfoCollection videoDevices;//摄像头设备集合
        VideoCaptureDevice videoSource;//捕获设备源
        const int delaytime = 500; //扫码间隔时间 ms
        int totalTime; //扫码最长时间
        public Form1()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            comboBox1.Items.Clear();
            videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);//获取拍照设备列表
            for (int i = 0; i < videoDevices.Count; i++)
                comboBox1.Items.Add(videoDevices[i].Name);
            comboBox1.Text = comboBox1.Items[0].ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text == null)
                return;
            ShutCamera();
            videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
            videoSourcePlayer1.VideoSource = videoSource;
            videoSourcePlayer1.Start();
            textBox1.Text = "开始识读";

            timer1.Interval = delaytime;
            timer1.Start();
            totalTime = 0;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ShutCamera();
            timer1.Stop();
        }

        // 关闭并释放摄像头
        public void ShutCamera()
        {
            if (videoSourcePlayer1.VideoSource != null)
            {
                videoSourcePlayer1.SignalToStop();
                videoSourcePlayer1.WaitForStop();
                videoSourcePlayer1.VideoSource = null;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (totalTime >= 20000)// 最多读20秒
            {
                timer1.Stop();
                ShutCamera();
                textBox1.Text = "未识别到二维码";
                return;
            }

            Bitmap barcodeImage;
            barcodeImage = videoSourcePlayer1.GetCurrentVideoFrame();//拍摄

            if (barcodeImage != null)
            {
                // 识读条形码
                BarcodeReader reader = new BarcodeReader();
                reader.Options.CharacterSet = "UTF-8";
                reader.Options.PossibleFormats = new List<BarcodeFormat>() { BarcodeFormat.CODE_128 };
                Result resultBarcode = reader.Decode(barcodeImage);
                if (resultBarcode != null)
                {
                    textBox1.Text = "";
                    textBox1.AppendText(resultBarcode.Text);
                    timer1.Stop();
                    ShutCamera();
                    return;
                }
            }
            totalTime += delaytime;
            textBox1.Text = totalTime.ToString() + " ms";
            //if (totalTime == 3000)
            //    picture.Save("测试图片.bmp");

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            string searchNum = textBox1.Text.Trim();
            //string folderPath = "C:\Users\HUI.CAI\Desktop\HELLO"; // 指定要搜索的文件夹路径
            string folderPath;
            folderPath = textBox2.Text.Trim();

            foreach (string filePath in Directory.GetFiles(folderPath, "*.pdf"))
            {
                string fileName = Path.GetFileNameWithoutExtension(filePath);
                if (fileName.Length >= 8)
                {
                    string firstEightDigits = fileName.Substring(0, 8);
                    if (firstEightDigits == searchNum)
                    {
                        Process.Start(filePath);
                        return; // 打开第一个匹配的pdf文件后退出循环
                    }
                }
            }
            MessageBox.Show("未找到匹配的pdf文件。");
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

               private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text.Length > 9)
            {
                textBox1.Text = textBox1.Text.Substring(0, 9);
            }
            else if (textBox1.Text.Length == 9)
            {
                textBox1.Text = textBox1.Text.Substring(0, 8);
            }
        }
    }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值