| | |
| | | InitializeComponent(); |
| | | } |
| | | |
| | | public BarcodeReaderForm(IBarcodeReader reader) |
| | | { |
| | | InitializeComponent(); |
| | | _reader = reader; |
| | | _reader.BarcodeRead += Reader_BarcodeRead; |
| | | _currentSN = _reader.IsConnected ? "设备已在线" : ""; |
| | | |
| | | // 同步 UI 状态 |
| | | cmbBrand.Text = _reader.Brand.ToString(); |
| | | cmbSN.Text = _reader.IsConnected ? "在线设备" : ""; |
| | | } |
| | | |
| | | public BarcodeReaderForm(LB_VisionProcesses.Processes.BarcodeReaderProcess process, string processPath) |
| | | { |
| | | InitializeComponent(); |
| | |
| | | { |
| | | // 更新结果列表 |
| | | lstBarcodes.Items.Clear(); |
| | | if (e.Barcodes != null && e.Barcodes.Count > 0) |
| | | if (e.BarcodeInfos != null && e.BarcodeInfos.Count > 0) |
| | | { |
| | | foreach (var code in e.Barcodes) |
| | | foreach (var info in e.BarcodeInfos) |
| | | { |
| | | lstBarcodes.Items.Add($"{DateTime.Now:HH:mm:ss} - {code}"); |
| | | lstBarcodes.Items.Add($"{DateTime.Now:HH:mm:ss} - {info.Text}"); |
| | | } |
| | | } |
| | | else |
| | |
| | | lstBarcodes.Items.Add($"{DateTime.Now:HH:mm:ss} - 未读到条码"); |
| | | } |
| | | |
| | | // 更新预览图像 |
| | | // 更新预览图像并绘制标注 |
| | | if (e.Image != null) |
| | | { |
| | | // 创建新位图用于绘图,不直接修改 SDK 传入的原图 |
| | | 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 = picPreview.Image; |
| | | picPreview.Image = new Bitmap(e.Image); // 拷贝一份防止被 SDK 内部释放 |
| | | picPreview.Image = drawImg; |
| | | oldImg?.Dispose(); |
| | | } |
| | | } |