From 452caa6d89537b828f2e64069e672429d9aca62a Mon Sep 17 00:00:00 2001 From: zqm Date: Thu, 26 Mar 2026 22:03:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=9D=E5=AD=98=E7=9B=B8=E5=AF=B9=E5=9D=90?= =?UTF-8?q?=E6=A0=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CS/Framework4.0/Camera/Camera/Camera.cs | 8 +- .../CS/Framework4.0/Camera/Camera/Setting.cs | 164 ++++++++++++++---- 2 files changed, 138 insertions(+), 34 deletions(-) diff --git a/Windows/CS/Framework4.0/Camera/Camera/Camera.cs b/Windows/CS/Framework4.0/Camera/Camera/Camera.cs index 530d875..0f1cb06 100644 --- a/Windows/CS/Framework4.0/Camera/Camera/Camera.cs +++ b/Windows/CS/Framework4.0/Camera/Camera/Camera.cs @@ -20,7 +20,7 @@ namespace Camera private const string PASSWD = "Yexian.net.168"; // 调试模式 - private const Boolean IsDebug = false; + private const Boolean IsDebug = true; // 配置文件目录 private string _configPath; @@ -69,13 +69,15 @@ namespace Camera } /// - /// 获取当前图像 + /// 获取当前图像(返回副本,避免线程冲突) /// public Image GetCurrentImage() { lock (_imageLock) { - return _currentImage; + if (_currentImage != null) + return _currentImage.Clone() as Image; + return null; } } diff --git a/Windows/CS/Framework4.0/Camera/Camera/Setting.cs b/Windows/CS/Framework4.0/Camera/Camera/Setting.cs index 49a23dc..2811e34 100644 --- a/Windows/CS/Framework4.0/Camera/Camera/Setting.cs +++ b/Windows/CS/Framework4.0/Camera/Camera/Setting.cs @@ -315,12 +315,12 @@ namespace Camera if (picBoxCamera.InvokeRequired) { picBoxCamera.Invoke(new Action(UpdateImage), e.Image); - picBoxCamera.Invoke(new Action(UpdateDataGridView)); + picBoxCamera.Invoke(new Action(UpdateDetectionResults)); } else { UpdateImage(e.Image); - UpdateDataGridView(); + UpdateDetectionResults(); } } @@ -358,9 +358,7 @@ namespace Camera if (ledZone.Width > 0 && ledZone.Height > 0) { - int relativeX = ledZone.X - detectionZone.X; - int relativeY = ledZone.Y - detectionZone.Y; - dataGridView1.Rows.Add(index, relativeX, relativeY, ledZone.Width, ledZone.Height, ColorTranslator.ToHtml(ledColor), detectionResult); + dataGridView1.Rows.Add(index, ledZone.X, ledZone.Y, ledZone.Width, ledZone.Height, ColorTranslator.ToHtml(ledColor), detectionResult); } } } @@ -368,6 +366,49 @@ namespace Camera _isUpdatingDataGridView = false; } + private void UpdateDetectionResults() + { + if (_isUpdatingDataGridView) return; + if (_camera == null || dataGridView1.Rows.Count == 0) return; + + try + { + _isUpdatingDataGridView = true; + int selectedIndex = -1; + if (dataGridView1.SelectedRows.Count > 0) + { + selectedIndex = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["Index"].Value); + } + + var allLedZones = _camera.GetLedZones(); + foreach (DataGridViewRow row in dataGridView1.Rows) + { + int index = Convert.ToInt32(row.Cells["Index"].Value); + if (allLedZones.ContainsKey(index)) + { + string detectionResult = _camera.GetLedZoneDetectionResult(index); + row.Cells["Detection"].Value = detectionResult; + } + } + + if (selectedIndex >= 0) + { + foreach (DataGridViewRow row in dataGridView1.Rows) + { + if (Convert.ToInt32(row.Cells["Index"].Value) == selectedIndex) + { + row.Selected = true; + break; + } + } + } + } + finally + { + _isUpdatingDataGridView = false; + } + } + private void PicBoxCamera_Paint(object sender, PaintEventArgs e) { Image currentImage = null; @@ -673,8 +714,8 @@ namespace Camera { Rectangle led = kvp.Value; Rectangle scaledLed = new Rectangle( - (int)(led.X * scaleX), - (int)(led.Y * scaleY), + (int)((detectionZone.X + led.X) * scaleX), + (int)((detectionZone.Y + led.Y) * scaleY), (int)(led.Width * scaleX), (int)(led.Height * scaleY) ); @@ -818,8 +859,8 @@ namespace Camera { Rectangle led = _camera.GetLedZone(_currentLedIndex); scaledLedZone = new Rectangle( - (int)(led.X * scaleX), - (int)(led.Y * scaleY), + (int)((detectionZone.X + led.X) * scaleX), + (int)((detectionZone.Y + led.Y) * scaleY), (int)(led.Width * scaleX), (int)(led.Height * scaleY) ); @@ -1013,7 +1054,9 @@ namespace Camera newIndex++; } Rectangle detectionZone = _camera.GetDetectionZone(); - _camera.AddLedZone(newIndex, new Rectangle(detectionZone.X + x, detectionZone.Y + y, width, height), Color.Lime); + int relativeX = x - detectionZone.X; + int relativeY = y - detectionZone.Y; + _camera.AddLedZone(newIndex, new Rectangle(relativeX, relativeY, width, height), Color.Lime); _selectedZoneIndex = newIndex; _currentLedIndex = newIndex; _isEditingLedZone = true; @@ -1145,9 +1188,8 @@ namespace Camera if (_selectedZoneIndex >= 0 && _camera != null) { Rectangle zone = _camera.GetLedZone(_selectedZoneIndex); - Rectangle detectionZone = _camera.GetDetectionZone(); - toolStripNumericUpDown1.Value = zone.X - detectionZone.X; - toolStripNumericUpDown2.Value = zone.Y - detectionZone.Y; + toolStripNumericUpDown1.Value = zone.X; + toolStripNumericUpDown2.Value = zone.Y; toolStripNumericUpDown3.Value = zone.Width; toolStripNumericUpDown4.Value = zone.Height; } @@ -1349,17 +1391,31 @@ namespace Camera toolStripButton4.Visible = false; toolStripTextBox1.Visible = false; toolStripTextBox1.Text = ""; - // 显示位置和宽高设置框 - toolStrip2.Visible = true; - // 更新位置和宽高值 - if (_selectedZoneIndex >= 0 && _camera != null) + // 如果有LED区域则显示位置和宽高设置框 + if (_camera != null && _camera.GetLedZoneCount() > 0) { - Rectangle zone = _camera.GetLedZone(_selectedZoneIndex); - Rectangle detectionZone = _camera.GetDetectionZone(); - toolStripNumericUpDown1.Value = zone.X - detectionZone.X; - toolStripNumericUpDown2.Value = zone.Y - detectionZone.Y; - toolStripNumericUpDown3.Value = zone.Width; - toolStripNumericUpDown4.Value = zone.Height; + toolStrip2.Visible = true; + int targetIndex = -1; + if (dataGridView1.SelectedRows.Count > 0) + { + targetIndex = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["Index"].Value); + } + else if (_selectedZoneIndex >= 0) + { + targetIndex = _selectedZoneIndex; + } + if (targetIndex >= 0) + { + Rectangle zone = _camera.GetLedZone(targetIndex); + toolStripNumericUpDown1.Value = zone.X; + toolStripNumericUpDown2.Value = zone.Y; + toolStripNumericUpDown3.Value = zone.Width; + toolStripNumericUpDown4.Value = zone.Height; + } + } + else + { + toolStrip2.Visible = false; } break; case 1: // 选中状态 @@ -1400,13 +1456,24 @@ namespace Camera // X坐标相关事件 private void ToolStripNumericUpDown1_ValueChanged(object sender, EventArgs e) { - int index = _currentLedIndex >= 0 ? _currentLedIndex : _selectedZoneIndex; + int index = -1; + if (dataGridView1.SelectedRows.Count > 0) + { + index = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["Index"].Value); + } + else if (_currentLedIndex >= 0) + { + index = _currentLedIndex; + } + else if (_selectedZoneIndex >= 0) + { + index = _selectedZoneIndex; + } if (index < 0 || _camera == null) return; int x = (int)toolStripNumericUpDown1.Value; Rectangle zone = _camera.GetLedZone(index); - Rectangle detectionZone = _camera.GetDetectionZone(); - zone.X = detectionZone.X + x; + zone.X = x; _camera.SetLedZone(index, zone); picBoxCamera.Invalidate(); } @@ -1414,13 +1481,24 @@ namespace Camera // Y坐标相关事件 private void ToolStripNumericUpDown2_ValueChanged(object sender, EventArgs e) { - int index = _currentLedIndex >= 0 ? _currentLedIndex : _selectedZoneIndex; + int index = -1; + if (dataGridView1.SelectedRows.Count > 0) + { + index = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["Index"].Value); + } + else if (_currentLedIndex >= 0) + { + index = _currentLedIndex; + } + else if (_selectedZoneIndex >= 0) + { + index = _selectedZoneIndex; + } if (index < 0 || _camera == null) return; int y = (int)toolStripNumericUpDown2.Value; Rectangle zone = _camera.GetLedZone(index); - Rectangle detectionZone = _camera.GetDetectionZone(); - zone.Y = detectionZone.Y + y; + zone.Y = y; _camera.SetLedZone(index, zone); picBoxCamera.Invalidate(); } @@ -1428,7 +1506,19 @@ namespace Camera // 宽度相关事件 private void ToolStripNumericUpDown3_ValueChanged(object sender, EventArgs e) { - int index = _currentLedIndex >= 0 ? _currentLedIndex : _selectedZoneIndex; + int index = -1; + if (dataGridView1.SelectedRows.Count > 0) + { + index = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["Index"].Value); + } + else if (_currentLedIndex >= 0) + { + index = _currentLedIndex; + } + else if (_selectedZoneIndex >= 0) + { + index = _selectedZoneIndex; + } if (index < 0 || _camera == null) return; int width = (int)toolStripNumericUpDown3.Value; @@ -1441,7 +1531,19 @@ namespace Camera // 高度相关事件 private void ToolStripNumericUpDown4_ValueChanged(object sender, EventArgs e) { - int index = _currentLedIndex >= 0 ? _currentLedIndex : _selectedZoneIndex; + int index = -1; + if (dataGridView1.SelectedRows.Count > 0) + { + index = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["Index"].Value); + } + else if (_currentLedIndex >= 0) + { + index = _currentLedIndex; + } + else if (_selectedZoneIndex >= 0) + { + index = _selectedZoneIndex; + } if (index < 0 || _camera == null) return; int height = (int)toolStripNumericUpDown4.Value;