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; } } }