From 19bf97fd8ec4f435e9111d72969c4ebf00d97991 Mon Sep 17 00:00:00 2001
From: C3032 <1057644574@qq.com>
Date: 星期五, 20 三月 2026 11:10:02 +0800
Subject: [PATCH] 读码器功能集成完成

---
 LB_SmartVision/Forms/Pages/BarcodeReaderPage/BarcodeReaderForm.cs |  207 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 207 insertions(+), 0 deletions(-)

diff --git a/LB_SmartVision/Forms/Pages/BarcodeReaderPage/BarcodeReaderForm.cs b/LB_SmartVision/Forms/Pages/BarcodeReaderPage/BarcodeReaderForm.cs
new file mode 100644
index 0000000..66da515
--- /dev/null
+++ b/LB_SmartVision/Forms/Pages/BarcodeReaderPage/BarcodeReaderForm.cs
@@ -0,0 +1,207 @@
+using LB_VisionProcesses.BarcodeReaders;
+using System;
+using System.Diagnostics;
+using System.Drawing;
+using System.Windows.Forms;
+
+namespace LB_SmartVision.Forms.Pages.BarcodeReaderPage
+{
+    public partial class BarcodeReaderForm : Form
+    {
+        BarcodeReaderBase barcodeReader { get; set; }
+        PictureBox pictureBox1 { get; set; }
+
+        public BarcodeReaderForm()
+        {
+            InitializeComponent();
+        }
+
+        public BarcodeReaderForm(BarcodeReaderBase reader)
+        {
+            InitializeComponent();
+
+            // 鍒涘缓鍥惧儚鏄剧ず鎺т欢
+            pictureBox1 = new PictureBox();
+            pictureBox1.Dock = DockStyle.Fill;
+            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
+            uiGroupBoxImage.Controls.Add(pictureBox1);
+
+            if (reader == null)
+                return;
+
+            this.barcodeReader = reader;
+            this.Text = reader.SN;
+
+            // 娣诲姞鍝佺墝閫夐」
+            foreach (BarcodeReaderBrand brand in Enum.GetValues(typeof(BarcodeReaderBrand)))
+            {
+                if (brand != BarcodeReaderBrand.Unsupported)
+                    uiComboBoxBrand.Items.Add(brand.ToString());
+            }
+
+            // 璁剧疆鍝佺墝
+            uiComboBoxBrand.Text = reader.Brand.ToString();
+
+            // 鍔犺浇鍥炶皟鍑芥暟
+            Subscribe();
+        }
+
+        private void BarcodeReaderForm_FormClosing(object sender, FormClosingEventArgs e)
+        {
+            if (barcodeReader == null)
+                return;
+
+            barcodeReader.StopGrabbing();
+            Unsubscribe();
+        }
+
+        public void Unsubscribe()
+        {
+            try
+            {
+                if (barcodeReader == null)
+                    return;
+                // 鍙栨秷鍥炶皟鍑芥暟
+                barcodeReader.BarcodeRead -= OnBarcodeRead;
+            }
+            catch (Exception ex)
+            {
+                // 璁板綍閿欒淇℃伅
+                Debug.WriteLine($"閿欒: {ex.Message}");
+            }
+        }
+
+        public void Subscribe()
+        {
+            try
+            {
+                if (barcodeReader == null)
+                    return;
+
+                // 鍙栨秷鍥炶皟鍑芥暟
+                barcodeReader.BarcodeRead -= OnBarcodeRead;
+                // 鍔犺浇鍥炶皟鍑芥暟
+                barcodeReader.BarcodeRead += OnBarcodeRead;
+            }
+            catch (Exception ex)
+            {
+                // 璁板綍閿欒淇℃伅
+                Debug.WriteLine($"閿欒: {ex.Message}");
+            }
+        }
+
+        private void OnBarcodeRead(object sender, BarcodeEventArgs e)
+        {
+            if (e.Barcodes == null || e.Barcodes.Count == 0)
+                return;
+
+            if (this.InvokeRequired)
+            {
+                this.Invoke(new Action(() =>
+                {
+                    DisplayBarcodeResult(e);
+                }));
+            }
+            else
+            {
+                DisplayBarcodeResult(e);
+            }
+        }
+
+        private void DisplayBarcodeResult(BarcodeEventArgs e)
+        {
+            uiListBoxBarcodes.Items.Clear();
+            if (e.BarcodeInfos != null && e.BarcodeInfos.Count > 0)
+            {
+                foreach (var info in e.BarcodeInfos)
+                {
+                    uiListBoxBarcodes.Items.Add(info.Text);
+                }
+            }
+
+            if (e.Image != null)
+            {
+                // 鍒涘缓鍥惧儚鍓湰杩涜缁樺埗
+                Bitmap drawImg = new Bitmap(e.Image);
+                using (Graphics g = Graphics.FromImage(drawImg))
+                {
+                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
+                    using (Pen pen = new Pen(Color.LimeGreen, 3))
+                    using (SolidBrush brush = new SolidBrush(Color.LimeGreen))
+                    using (Font font = new Font("Arial", 16, FontStyle.Bold))
+                    {
+                        foreach (var info in e.BarcodeInfos)
+                        {
+                            if (info.Points != null && info.Points.Length >= 4)
+                            {
+                                // 缁樺埗妗嗙嚎
+                                g.DrawPolygon(pen, info.Points);
+                                // 缁樺埗鏂囨湰
+                                Point textPos = info.Points[0];
+                                textPos.Y -= 25;
+                                g.DrawString(info.Text, font, brush, textPos);
+                            }
+                        }
+                    }
+                }
+
+                // 閲婃斁鏃у浘鍍�
+                var oldImg = pictureBox1.Image;
+                pictureBox1.Image = drawImg;
+                oldImg?.Dispose();
+            }
+
+            uiTextBoxResult.Text = $"璇诲彇鍒皗e.BarcodeInfos.Count}涓潯鐮�";
+        }
+
+        private void uiRadioButtonSoftTrigger_CheckedChanged(object sender, EventArgs e)
+        {
+            uiButtonSoftTrigger.Enabled = uiRadioButtonSoftTrigger.Checked;
+
+            if (barcodeReader == null)
+                return;
+
+            barcodeReader.SetTriggerMode(uiRadioButtonSoftTrigger.Checked);
+        }
+
+        private void uiButtonStartGrabbing_Click(object sender, EventArgs e)
+        {
+            if (barcodeReader == null)
+                return;
+
+            if (barcodeReader.StartGrabbing())
+            {
+                uiButtonStartGrabbing.Enabled = false;
+                uiButtonStopGrabbing.Enabled = true;
+            }
+        }
+
+        private void uiButtonStopGrabbing_Click(object sender, EventArgs e)
+        {
+            if (barcodeReader == null)
+                return;
+
+            if (barcodeReader.StopGrabbing())
+            {
+                uiButtonStartGrabbing.Enabled = true;
+                uiButtonStopGrabbing.Enabled = false;
+            }
+        }
+
+        private void uiButtonSoftTrigger_Click(object sender, EventArgs e)
+        {
+            if (barcodeReader == null)
+                return;
+
+            barcodeReader.SoftTrigger();
+        }
+
+        private void uiButtonClear_Click(object sender, EventArgs e)
+        {
+            uiListBoxBarcodes.Items.Clear();
+            uiTextBoxResult.Text = "";
+            if (pictureBox1 != null)
+                pictureBox1.Image = null;
+        }
+    }
+}

--
Gitblit v1.9.3