diff --git a/Windows/CS/Framework4.0/Camera/Camera/Camera.cs b/Windows/CS/Framework4.0/Camera/Camera/Camera.cs index 3fb1554..530d875 100644 --- a/Windows/CS/Framework4.0/Camera/Camera/Camera.cs +++ b/Windows/CS/Framework4.0/Camera/Camera/Camera.cs @@ -29,8 +29,13 @@ namespace Camera private Rectangle _detectionZone; private ConcurrentDictionary _ledZones = new ConcurrentDictionary(); private ConcurrentDictionary _ledZoneColors = new ConcurrentDictionary(); + private ConcurrentDictionary _ledZoneDetectionResults = new ConcurrentDictionary(); private Color _detectionZoneColor = Color.Black; + // 检测状态 + private bool _isDetecting = false; + private readonly object _detectionLock = new object(); + // 定时器 private System.Timers.Timer _timer; @@ -256,8 +261,10 @@ namespace Camera { Rectangle r; Color c; + string s; _ledZones.TryRemove(index, out r); _ledZoneColors.TryRemove(index, out c); + _ledZoneDetectionResults.TryRemove(index, out s); } /// @@ -304,6 +311,32 @@ namespace Camera return Color.Lime; } + /// + /// 获取LED区域检测结果 + /// + public ConcurrentDictionary GetLedZoneDetectionResults() + { + return _ledZoneDetectionResults; + } + + /// + /// 获取指定索引的LED区域检测结果 + /// + public string GetLedZoneDetectionResult(int index) + { + if (_ledZoneDetectionResults.ContainsKey(index)) + return _ledZoneDetectionResults[index]; + return ""; + } + + /// + /// 设置LED区域检测结果 + /// + public void SetLedZoneDetectionResult(int index, string result) + { + _ledZoneDetectionResults[index] = result; + } + /// /// 保存配置 /// @@ -427,10 +460,28 @@ namespace Camera System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " Timer_Elapsed 开始"); try { + lock (_detectionLock) + { + if (_isDetecting) + { + System.Diagnostics.Debug.WriteLine("Camera: 正在检测,跳过本次"); + if (_timer != null) + { + _timer.Stop(); + _timer.Start(); + } + return; + } + _isDetecting = true; + } + Image image = GetCameraImage(); if (image != null) { System.Diagnostics.Debug.WriteLine("Camera: 捕获图像 Size=" + image.Width + "x" + image.Height); + + DetectLedZones(image); + Image oldImage = null; lock (_imageLock) { @@ -451,6 +502,10 @@ namespace Camera } finally { + lock (_detectionLock) + { + _isDetecting = false; + } if (_timer != null) { System.Diagnostics.Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + " 重新启动定时器"); @@ -460,6 +515,66 @@ namespace Camera } } + /// + /// 检测LED区域颜色 + /// + private void DetectLedZones(Image image) + { + if (image == null || _ledZones.Count == 0) return; + + try + { + using (Bitmap bmp = new Bitmap(image)) + { + foreach (var kvp in _ledZones) + { + int index = kvp.Key; + Rectangle zone = kvp.Value; + + if (zone.Width <= 0 || zone.Height <= 0) continue; + if (zone.X < 0 || zone.Y < 0 || zone.X + zone.Width > bmp.Width || zone.Y + zone.Height > bmp.Height) + continue; + + int redPixelCount = 0; + int greenPixelCount = 0; + int totalPixels = 0; + + for (int x = zone.X; x < zone.X + zone.Width; x++) + { + for (int y = zone.Y; y < zone.Y + zone.Height; y++) + { + Color pixel = bmp.GetPixel(x, y); + if (pixel.R > 150 && pixel.G < 100 && pixel.B < 100) + { + redPixelCount++; + } + else if (pixel.G > 150 && pixel.R < 100 && pixel.B < 100) + { + greenPixelCount++; + } + totalPixels++; + } + } + + string result = ""; + if (totalPixels > 0) + { + if (redPixelCount * 100 / totalPixels > 30) + result = "Red"; + else if (greenPixelCount * 100 / totalPixels > 30) + result = "Green"; + } + + _ledZoneDetectionResults[index] = result; + } + } + } + catch (Exception ex) + { + System.Diagnostics.Debug.WriteLine("Camera: 检测LED区域失败: " + ex.Message); + } + } + /// /// 获取摄像头图像 /// diff --git a/Windows/CS/Framework4.0/Camera/Camera/Setting.Designer.cs b/Windows/CS/Framework4.0/Camera/Camera/Setting.Designer.cs index b61989c..497ade5 100644 --- a/Windows/CS/Framework4.0/Camera/Camera/Setting.Designer.cs +++ b/Windows/CS/Framework4.0/Camera/Camera/Setting.Designer.cs @@ -61,7 +61,7 @@ namespace Camera // splitContainer1.Panel2 // this.splitContainer1.Panel2.Controls.Add(this.toolStripContainer1); - this.splitContainer1.Panel2MinSize = 457; + this.splitContainer1.Panel2MinSize = 460; this.splitContainer1.Size = new System.Drawing.Size(1263, 515); this.splitContainer1.SplitterDistance = 700; this.splitContainer1.TabIndex = 0; diff --git a/Windows/CS/Framework4.0/Camera/Camera/Setting.cs b/Windows/CS/Framework4.0/Camera/Camera/Setting.cs index 2c9b643..49a23dc 100644 --- a/Windows/CS/Framework4.0/Camera/Camera/Setting.cs +++ b/Windows/CS/Framework4.0/Camera/Camera/Setting.cs @@ -131,6 +131,7 @@ namespace Camera private int _ledZoneState = 0; private Color _detectionZoneColor = Color.Black; private ConcurrentDictionary _ledZoneColors = new ConcurrentDictionary(); + private ConcurrentDictionary _ledZoneDetectionResults = new ConcurrentDictionary(); private Point _resizeStartPoint; private Rectangle _originalResizeRect; private int _hoveredHandle = -1; @@ -183,6 +184,7 @@ namespace Camera dataGridView1.Columns.Add("Width", "宽度"); dataGridView1.Columns.Add("Height", "高度"); dataGridView1.Columns.Add("Color", "颜色"); + dataGridView1.Columns.Add("Detection", "检测结果"); dataGridView1.Columns["Index"].Width = 50; dataGridView1.Columns["X"].Width = 60; @@ -190,6 +192,7 @@ namespace Camera dataGridView1.Columns["Width"].Width = 60; dataGridView1.Columns["Height"].Width = 60; dataGridView1.Columns["Color"].Width = 70; + dataGridView1.Columns["Detection"].Width = 80; } } @@ -205,8 +208,7 @@ namespace Camera } picBoxCamera.BackColor = Color.Gray; splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel2; - splitContainer1.Panel2MinSize = 380; - splitContainer1.SplitterDistance = splitContainer1.Width - 380; + splitContainer1.SplitterDistance = splitContainer1.Width - splitContainer1.Panel2MinSize; UpdateDataGridView(); UpdateLedZoneButtonsVisibility(0); } @@ -313,10 +315,12 @@ namespace Camera if (picBoxCamera.InvokeRequired) { picBoxCamera.Invoke(new Action(UpdateImage), e.Image); + picBoxCamera.Invoke(new Action(UpdateDataGridView)); } else { UpdateImage(e.Image); + UpdateDataGridView(); } } @@ -350,12 +354,13 @@ namespace Camera int index = kvp.Key; Rectangle ledZone = kvp.Value; Color ledColor = _camera.GetLedZoneColor(index); + string detectionResult = _camera.GetLedZoneDetectionResult(index); 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)); + dataGridView1.Rows.Add(index, relativeX, relativeY, ledZone.Width, ledZone.Height, ColorTranslator.ToHtml(ledColor), detectionResult); } } }