1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
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;
        }
    }
}