Led区编辑

This commit is contained in:
zqm
2026-03-25 15:09:30 +08:00
parent 2a72eab101
commit 72b96782d0
3 changed files with 435 additions and 92 deletions

View File

@@ -1,4 +1,6 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Drawing;
using System.Timers;
using System.Net;
@@ -25,9 +27,9 @@ namespace Camera
// 配置数据
private Rectangle _detectionZone;
private Rectangle _ledZone;
private ConcurrentDictionary<int, Rectangle> _ledZones = new ConcurrentDictionary<int, Rectangle>();
private ConcurrentDictionary<int, Color> _ledZoneColors = new ConcurrentDictionary<int, Color>();
private Color _detectionZoneColor = Color.Black;
private Color _ledZoneColor = Color.Lime;
// 定时器
private System.Timers.Timer _timer;
@@ -135,11 +137,61 @@ namespace Camera
}
/// <summary>
/// 获取Led区域
/// 获取Led区域列表
/// </summary>
public ConcurrentDictionary<int, Rectangle> GetLedZones()
{
return _ledZones;
}
/// <summary>
/// 获取指定索引的Led区域
/// </summary>
public Rectangle GetLedZone(int index)
{
if (_ledZones.ContainsKey(index))
return _ledZones[index];
return new Rectangle(0, 0, 0, 0);
}
/// <summary>
/// 获取默认Led区域索引为1
/// </summary>
public Rectangle GetLedZone()
{
return _ledZone;
if (_ledZones.ContainsKey(1))
return _ledZones[1];
return new Rectangle(0, 0, 0, 0);
}
/// <summary>
/// 设置默认Led区域索引为1
/// </summary>
public void SetLedZone(Rectangle zone)
{
_ledZones[1] = zone;
if (!_ledZoneColors.ContainsKey(1))
{
_ledZoneColors[1] = Color.Lime;
}
}
/// <summary>
/// 获取默认Led区域颜色索引为1
/// </summary>
public Color GetLedZoneColor()
{
if (_ledZoneColors.ContainsKey(1))
return _ledZoneColors[1];
return Color.Lime;
}
/// <summary>
/// 设置默认Led区域颜色索引为1
/// </summary>
public void SetLedZoneColor(Color color)
{
_ledZoneColors[1] = color;
}
/// <summary>
@@ -151,11 +203,53 @@ namespace Camera
}
/// <summary>
/// 设置Led区域
/// 添加Led区域
/// </summary>
public void SetLedZone(Rectangle zone)
public void AddLedZone(int index, Rectangle zone, Color color)
{
_ledZone = zone;
_ledZones[index] = zone;
_ledZoneColors[index] = color;
}
/// <summary>
/// 设置指定索引的Led区域
/// </summary>
public void SetLedZone(int index, Rectangle zone)
{
if (_ledZones.ContainsKey(index))
{
_ledZones[index] = zone;
}
}
/// <summary>
/// 移除Led区域
/// </summary>
public void RemoveLedZone(int index)
{
Rectangle r;
Color c;
_ledZones.TryRemove(index, out r);
_ledZoneColors.TryRemove(index, out c);
}
/// <summary>
/// 设置指定索引的Led区域颜色
/// </summary>
public void SetLedZoneColor(int index, Color color)
{
if (_ledZoneColors.ContainsKey(index))
{
_ledZoneColors[index] = color;
}
}
/// <summary>
/// 获取Led区域数量
/// </summary>
public int GetLedZoneCount()
{
return _ledZones.Count;
}
public Color GetDetectionZoneColor()
@@ -163,19 +257,24 @@ namespace Camera
return _detectionZoneColor;
}
public ConcurrentDictionary<int, Color> GetLedZoneColors()
{
return _ledZoneColors;
}
public void SetDetectionZoneColor(Color color)
{
_detectionZoneColor = color;
}
public Color GetLedZoneColor()
/// <summary>
/// 获取指定索引的Led区域颜色
/// </summary>
public Color GetLedZoneColor(int index)
{
return _ledZoneColor;
}
public void SetLedZoneColor(Color color)
{
_ledZoneColor = color;
if (_ledZoneColors.ContainsKey(index))
return _ledZoneColors[index];
return Color.Lime;
}
/// <summary>
@@ -202,10 +301,13 @@ namespace Camera
writer.WriteLine("索引,X坐标,Y坐标,宽度,高度,颜色");
if (_ledZone.Width > 0 && _ledZone.Height > 0)
foreach (var kvp in _ledZones)
{
string colorStr = ColorTranslator.ToHtml(_ledZoneColor);
writer.WriteLine(string.Format("1,{0},{1},{2},{3},{4}", _ledZone.X, _ledZone.Y, _ledZone.Width, _ledZone.Height, colorStr));
int index = kvp.Key;
Rectangle ledZone = kvp.Value;
Color ledColor = _ledZoneColors[index];
string colorStr = ColorTranslator.ToHtml(ledColor);
writer.WriteLine(string.Format("{0},{1},{2},{3},{4},{5}", index, ledZone.X, ledZone.Y, ledZone.Width, ledZone.Height, colorStr));
}
}
}
@@ -220,9 +322,9 @@ namespace Camera
if (string.IsNullOrEmpty(_configPath)) return;
_detectionZone = new Rectangle(0, 0, 2880, 1620);
_ledZone = new Rectangle(0, 0, 0, 0);
_ledZones.Clear();
_ledZoneColors.Clear();
_detectionZoneColor = Color.Black;
_ledZoneColor = Color.Lime;
string configFile = Path.Combine(_configPath, "Camera.csv");
try
@@ -263,17 +365,19 @@ namespace Camera
else
{
if (!int.TryParse(parts[0], out int index)) continue;
if (index == 1 && parts.Length >= 5)
if (parts.Length >= 6)
{
if (!int.TryParse(parts[1], out int x)) continue;
if (!int.TryParse(parts[2], out int y)) continue;
if (!int.TryParse(parts[3], out int width)) continue;
if (!int.TryParse(parts[4], out int height)) continue;
_ledZone = new Rectangle(x, y, width, height);
Color zoneColor = Color.Lime;
if (parts.Length >= 6 && !string.IsNullOrEmpty(parts[5]))
{
try { _ledZoneColor = ColorTranslator.FromHtml(parts[5]); } catch { }
try { zoneColor = ColorTranslator.FromHtml(parts[5]); } catch { }
}
_ledZones[index] = new Rectangle(x, y, width, height);
_ledZoneColors[index] = zoneColor;
}
}
}

View File

@@ -25,6 +25,7 @@ namespace Camera
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.toolStripButton2 = new System.Windows.Forms.ToolStripButton();
this.toolStripButton3 = new System.Windows.Forms.ToolStripButton();
this.toolStripButton4 = new System.Windows.Forms.ToolStripButton();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
@@ -99,7 +100,8 @@ namespace Camera
this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripButton1,
this.toolStripButton2,
this.toolStripButton3});
this.toolStripButton3,
this.toolStripButton4});
this.toolStrip1.Location = new System.Drawing.Point(0, 0);
this.toolStrip1.Name = "toolStrip1";
this.toolStrip1.Size = new System.Drawing.Size(80, 40);
@@ -133,6 +135,15 @@ namespace Camera
this.toolStripButton3.Visible = false;
this.toolStripButton3.Click += new System.EventHandler(this.ToolStripButton3_Click);
//
// toolStripButton4
//
this.toolStripButton4.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.toolStripButton4.Name = "toolStripButton4";
this.toolStripButton4.Size = new System.Drawing.Size(36, 36);
this.toolStripButton4.ToolTipText = "删除Led区";
this.toolStripButton4.Visible = false;
this.toolStripButton4.Click += new System.EventHandler(this.ToolStripButton4_Click);
//
// dataGridView1
//
this.dataGridView1.AllowUserToAddRows = false;
@@ -187,6 +198,7 @@ namespace Camera
private System.Windows.Forms.ToolStripButton toolStripButton1;
private System.Windows.Forms.ToolStripButton toolStripButton2;
private System.Windows.Forms.ToolStripButton toolStripButton3;
private System.Windows.Forms.ToolStripButton toolStripButton4;
private System.Windows.Forms.DataGridView dataGridView1;
}
}

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
@@ -18,15 +19,17 @@ namespace Camera
private bool _isMoving = false;
private bool _isResizing = false;
private Point _startPoint;
private Point _currentMousePoint;
private Rectangle _originalZone;
private Rectangle _tempZone;
private bool _isEditingDetectionZone = false;
private bool _isEditingLedZone = false;
private int _ledZoneState = 0;
private Color _detectionZoneColor = Color.Black;
private Color _ledZoneColor = Color.Lime;
private ConcurrentDictionary<int, Color> _ledZoneColors = new ConcurrentDictionary<int, Color>();
private Point _resizeStartPoint;
private Rectangle _originalResizeRect;
private int _hoveredHandle = -1;
private int _currentLedIndex = -1;
private int[] _handleOffsetsX = { 0, 1, 2, 2, 2, 1, 0, 0 };
private int[] _handleOffsetsY = { 0, 0, 0, 1, 2, 2, 2, 1 };
@@ -94,7 +97,7 @@ namespace Camera
{
_camera.ImageCaptured += Camera_ImageCaptured;
_detectionZoneColor = _camera.GetDetectionZoneColor();
_ledZoneColor = _camera.GetLedZoneColor();
_ledZoneColors = _camera.GetLedZoneColors();
}
picBoxCamera.BackColor = Color.Gray;
splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel2;
@@ -182,6 +185,21 @@ namespace Camera
}
toolStripButton2.Image = ledIcon;
toolStripButton2.ImageTransparentColor = Color.Transparent;
Bitmap deleteIcon = new Bitmap(32, 32);
using (Graphics g = Graphics.FromImage(deleteIcon))
{
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.Clear(Color.Transparent);
using (Pen pen = new Pen(Color.Red, 3))
{
g.DrawLine(pen, 8, 8, 24, 24);
g.DrawLine(pen, 24, 8, 8, 24);
}
}
toolStripButton4.Image = deleteIcon;
toolStripButton4.ImageTransparentColor = Color.Transparent;
}
catch { }
}
@@ -222,17 +240,17 @@ namespace Camera
if (_camera != null)
{
Rectangle detectionZone = _camera.GetDetectionZone();
Rectangle ledZone = _camera.GetLedZone();
if (detectionZone.Width > 0 && detectionZone.Height > 0)
var ledZones = _camera.GetLedZones();
foreach (var kvp in ledZones)
{
dataGridView1.Rows.Add(0, detectionZone.X, detectionZone.Y, detectionZone.Width, detectionZone.Height, "#FF0000");
}
int index = kvp.Key;
Rectangle ledZone = kvp.Value;
Color ledColor = _camera.GetLedZoneColor(index);
if (ledZone.Width > 0 && ledZone.Height > 0)
{
dataGridView1.Rows.Add(1, ledZone.X, ledZone.Y, ledZone.Width, ledZone.Height, "#00FF00");
if (ledZone.Width > 0 && ledZone.Height > 0)
{
dataGridView1.Rows.Add(index, ledZone.X, ledZone.Y, ledZone.Width, ledZone.Height, ColorTranslator.ToHtml(ledColor));
}
}
}
@@ -281,7 +299,6 @@ namespace Camera
float scaleY = (float)picBoxCamera.ClientSize.Height / imageHeight;
Rectangle detectionZone = _camera != null ? _camera.GetDetectionZone() : new Rectangle(0, 0, 0, 0);
Rectangle ledZone = _camera != null ? _camera.GetLedZone() : new Rectangle(0, 0, 0, 0);
Rectangle scaledDetectionZone = new Rectangle(
(int)(detectionZone.X * scaleX),
@@ -290,13 +307,6 @@ namespace Camera
(int)(detectionZone.Height * scaleY)
);
Rectangle scaledLedZone = new Rectangle(
(int)(ledZone.X * scaleX),
(int)(ledZone.Y * scaleY),
(int)(ledZone.Width * scaleX),
(int)(ledZone.Height * scaleY)
);
Rectangle currentEditZone = new Rectangle(0, 0, 0, 0);
bool isEditing = false;
Color editColor = _detectionZoneColor;
@@ -307,14 +317,87 @@ namespace Camera
isEditing = true;
editColor = _detectionZoneColor;
}
else if (_isEditingLedZone)
else if (_isEditingLedZone && _currentLedIndex >= 0)
{
currentEditZone = scaledLedZone;
Rectangle ledZone = _camera.GetLedZone(_currentLedIndex);
currentEditZone = new Rectangle(
(int)(ledZone.X * scaleX),
(int)(ledZone.Y * scaleY),
(int)(ledZone.Width * scaleX),
(int)(ledZone.Height * scaleY)
);
isEditing = true;
editColor = _ledZoneColor;
editColor = _camera.GetLedZoneColor(_currentLedIndex);
}
if (isEditing && currentEditZone.Width > 0 && currentEditZone.Height > 0)
if (_isDrawing && _isEditingLedZone && _startPoint != null && _currentMousePoint != null)
{
using (Pen pen = new Pen(_detectionZoneColor, 2))
{
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
g.DrawRectangle(pen, scaledDetectionZone);
}
int x = (int)(Math.Min(_startPoint.X, _startPoint.X + (_currentMousePoint.X - _resizeStartPoint.X) / scaleX) * scaleX);
int y = (int)(Math.Min(_startPoint.Y, _startPoint.Y + (_currentMousePoint.Y - _resizeStartPoint.Y) / scaleY) * scaleY);
int w = (int)(Math.Abs(_currentMousePoint.X - _resizeStartPoint.X));
int h = (int)(Math.Abs(_currentMousePoint.Y - _resizeStartPoint.Y));
if (w > 5 && h > 5)
{
Rectangle drawRect = new Rectangle(x, y, w, h);
Color currentLedColor = _currentLedIndex >= 0 && _camera != null ? _camera.GetLedZoneColor(_currentLedIndex) : Color.Lime;
using (Pen pen = new Pen(currentLedColor, 2))
{
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
g.DrawRectangle(pen, drawRect);
}
}
}
else if (_isEditingLedZone && _currentLedIndex >= 0)
{
using (Pen pen = new Pen(_detectionZoneColor, 2))
{
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
g.DrawRectangle(pen, scaledDetectionZone);
}
var ledZones = _camera.GetLedZones();
foreach (var kvp in ledZones)
{
int index = kvp.Key;
Rectangle ledZone = kvp.Value;
Color ledColor = _camera.GetLedZoneColor(index);
Rectangle scaledLed = new Rectangle(
(int)(ledZone.X * scaleX),
(int)(ledZone.Y * scaleY),
(int)(ledZone.Width * scaleX),
(int)(ledZone.Height * scaleY)
);
using (Pen pen = new Pen(ledColor, 2))
{
g.DrawRectangle(pen, scaledLed);
}
using (Font font = new Font("Arial", 10))
{
g.DrawString(index.ToString(), font, new SolidBrush(ledColor), scaledLed.X, scaledLed.Y - 15);
}
}
if (currentEditZone.Width > 0 && currentEditZone.Height > 0)
{
using (Pen pen = new Pen(editColor, 2))
{
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
g.DrawRectangle(pen, currentEditZone);
}
DrawEditHandles(g, currentEditZone, editColor);
}
}
else if (isEditing && currentEditZone.Width > 0 && currentEditZone.Height > 0)
{
using (Pen pen = new Pen(editColor, 2))
{
@@ -331,14 +414,32 @@ namespace Camera
g.DrawRectangle(pen, scaledDetectionZone);
}
using (Pen pen = new Pen(_ledZoneColor, 2))
if (_camera != null)
{
g.DrawRectangle(pen, scaledLedZone);
}
var ledZones = _camera.GetLedZones();
foreach (var kvp in ledZones)
{
int index = kvp.Key;
Rectangle ledZone = kvp.Value;
Color ledColor = _camera.GetLedZoneColor(index);
using (Font font = new Font("Arial", 10))
{
g.DrawString("Led区", font, new SolidBrush(_ledZoneColor), scaledLedZone.X, scaledLedZone.Y - 15);
Rectangle scaledLed = new Rectangle(
(int)(ledZone.X * scaleX),
(int)(ledZone.Y * scaleY),
(int)(ledZone.Width * scaleX),
(int)(ledZone.Height * scaleY)
);
using (Pen pen = new Pen(ledColor, 2))
{
g.DrawRectangle(pen, scaledLed);
}
using (Font font = new Font("Arial", 10))
{
g.DrawString(index.ToString(), font, new SolidBrush(ledColor), scaledLed.X, scaledLed.Y - 15);
}
}
}
}
}
@@ -416,12 +517,69 @@ namespace Camera
(int)(detectionZone.Height * scaleY)
);
Rectangle scaledLedZone = new Rectangle(
(int)(ledZone.X * scaleX),
(int)(ledZone.Y * scaleY),
(int)(ledZone.Width * scaleX),
(int)(ledZone.Height * scaleY)
);
var ledZones = _camera.GetLedZones();
bool clickedOnLedZone = false;
int clickedLedIndex = -1;
foreach (var kvp in ledZones)
{
Rectangle led = kvp.Value;
Rectangle scaledLed = new Rectangle(
(int)(led.X * scaleX),
(int)(led.Y * scaleY),
(int)(led.Width * scaleX),
(int)(led.Height * scaleY)
);
if (scaledLed.Width > 0 && scaledLed.Height > 0 && scaledLed.Contains(controlPoint))
{
if (!_isEditingDetectionZone && !_isEditingLedZone)
{
clickedOnLedZone = true;
clickedLedIndex = kvp.Key;
ledZone = led;
break;
}
else if (_isEditingLedZone)
{
clickedOnLedZone = true;
clickedLedIndex = kvp.Key;
ledZone = led;
break;
}
}
}
if (clickedOnLedZone)
{
_isEditingLedZone = true;
_currentLedIndex = clickedLedIndex;
_selectedZoneIndex = clickedLedIndex;
_ledZoneState = 1; // 选中状态
toolStripButton2.Checked = true;
toolStripButton2.Visible = false;
toolStripButton1.Visible = false;
UpdateLedZoneButtonsVisibility(1);
UpdateColorButtonIcon();
_isMoving = true;
_startPoint = imagePoint;
_resizeStartPoint = controlPoint;
_originalZone = ledZone;
picBoxCamera.Invalidate();
return;
}
Rectangle scaledLedZone = new Rectangle(0, 0, 0, 0);
if (_isEditingLedZone && _currentLedIndex >= 0)
{
Rectangle led = _camera.GetLedZone(_currentLedIndex);
scaledLedZone = new Rectangle(
(int)(led.X * scaleX),
(int)(led.Y * scaleY),
(int)(led.Width * scaleX),
(int)(led.Height * scaleY)
);
}
if (_isEditingDetectionZone || _isEditingLedZone)
{
@@ -434,13 +592,21 @@ namespace Camera
_resizeStartPoint = controlPoint;
_originalResizeRect = editRect;
}
else if (editRect.Contains(controlPoint))
else if (editRect.Width > 0 && editRect.Height > 0 && editRect.Contains(controlPoint))
{
_isMoving = true;
_startPoint = imagePoint;
_resizeStartPoint = controlPoint;
_originalZone = _isEditingDetectionZone ? detectionZone : ledZone;
}
else if (_isEditingLedZone)
{
_currentLedIndex = -1;
_selectedZoneIndex = -1;
_ledZoneState = 2; // 切换到绘制状态
UpdateLedZoneButtonsVisibility(2);
picBoxCamera.Invalidate();
}
}
else
{
@@ -452,14 +618,6 @@ namespace Camera
_resizeStartPoint = controlPoint;
_originalZone = detectionZone;
}
else if (ledZone.Contains(imagePoint))
{
_selectedZoneIndex = 1;
_isMoving = true;
_startPoint = imagePoint;
_resizeStartPoint = controlPoint;
_originalZone = ledZone;
}
else
{
_selectedZoneIndex = -1;
@@ -474,6 +632,7 @@ namespace Camera
private void PicBoxCamera_MouseMove(object sender, MouseEventArgs e)
{
_currentMousePoint = e.Location;
Image currentImage = picBoxCamera.Image;
if (currentImage == null || _camera == null) return;
@@ -501,12 +660,17 @@ namespace Camera
(int)(detectionZone.Height * scaleY)
);
Rectangle scaledLedZone = new Rectangle(
(int)(ledZone.X * scaleX),
(int)(ledZone.Y * scaleY),
(int)(ledZone.Width * scaleX),
(int)(ledZone.Height * scaleY)
);
Rectangle scaledLedZone = new Rectangle(0, 0, 0, 0);
if (_currentLedIndex >= 0)
{
Rectangle led = _camera.GetLedZone(_currentLedIndex);
scaledLedZone = new Rectangle(
(int)(led.X * scaleX),
(int)(led.Y * scaleY),
(int)(led.Width * scaleX),
(int)(led.Height * scaleY)
);
}
if (_isEditingDetectionZone || _isEditingLedZone)
{
@@ -540,7 +704,7 @@ namespace Camera
}
else
{
_camera.SetLedZone(newZone);
_camera.SetLedZone(_currentLedIndex, newZone);
}
picBoxCamera.Update();
}
@@ -569,7 +733,7 @@ namespace Camera
}
else
{
_camera.SetLedZone(newZone);
_camera.SetLedZone(_currentLedIndex, newZone);
}
picBoxCamera.Update();
}
@@ -612,9 +776,9 @@ namespace Camera
{
_camera.SetDetectionZone(newZone);
}
else if (_selectedZoneIndex == 1)
else if (_selectedZoneIndex > 0)
{
_camera.SetLedZone(newZone);
_camera.SetLedZone(_selectedZoneIndex, newZone);
}
}
@@ -644,10 +808,13 @@ namespace Camera
_isEditingLedZone = false;
_isMoving = false;
_isResizing = false;
_currentLedIndex = -1;
_selectedZoneIndex = -1;
_ledZoneState = 0; // 就绪状态
toolStripButton2.Checked = false;
toolStripButton2.ToolTipText = "绘制Led区(点击开启)";
toolStripButton1.Visible = true;
toolStripButton3.Visible = false;
UpdateLedZoneButtonsVisibility(0);
picBoxCamera.Invalidate();
return;
}
@@ -682,8 +849,14 @@ namespace Camera
{
if (toolStripButton2.Checked)
{
_camera.SetLedZone(new Rectangle(x, y, width, height));
_selectedZoneIndex = 1;
int newIndex = _camera.GetLedZoneCount() + 1;
_camera.AddLedZone(newIndex, new Rectangle(x, y, width, height), Color.Lime);
_selectedZoneIndex = newIndex;
_currentLedIndex = newIndex;
_isEditingLedZone = true;
_ledZoneState = 1; // 选中状态
UpdateLedZoneButtonsVisibility(1);
UpdateColorButtonIcon();
}
else
{
@@ -817,7 +990,6 @@ namespace Camera
toolStripButton3.Visible = true;
UpdateColorButtonIcon();
toolStripButton2.ToolTipText = "绘制Led区(点击开启/关闭)";
_tempZone = _camera.GetDetectionZone();
}
else
{
@@ -837,22 +1009,25 @@ namespace Camera
{
_isEditingLedZone = true;
_isEditingDetectionZone = false;
_selectedZoneIndex = 1;
_selectedZoneIndex = -1;
_currentLedIndex = -1;
_ledZoneState = 2; // 绘制状态
toolStripButton2.ToolTipText = "绘制Led区(点击关闭)";
toolStripButton1.Checked = false;
toolStripButton1.Visible = false;
toolStripButton3.Visible = true;
UpdateLedZoneButtonsVisibility(2);
UpdateColorButtonIcon();
toolStripButton1.ToolTipText = "修改检测区(点击开启/关闭)";
_tempZone = _camera.GetLedZone();
}
else
{
_isEditingLedZone = false;
_currentLedIndex = -1;
_selectedZoneIndex = -1;
_ledZoneState = 0; // 就绪状态
toolStripButton2.ToolTipText = "绘制Led区(点击开启)";
toolStripButton1.Visible = true;
toolStripButton3.Visible = false;
UpdateLedZoneButtonsVisibility(0);
}
picBoxCamera.Invalidate();
UpdateDataGridView();
@@ -867,9 +1042,9 @@ namespace Camera
{
colorDialog.Color = _detectionZoneColor;
}
else if (_isEditingLedZone)
else if (_isEditingLedZone && _currentLedIndex >= 0)
{
colorDialog.Color = _ledZoneColor;
colorDialog.Color = _camera.GetLedZoneColor(_currentLedIndex);
}
if (colorDialog.ShowDialog() == DialogResult.OK)
@@ -878,9 +1053,9 @@ namespace Camera
{
_detectionZoneColor = colorDialog.Color;
}
else if (_isEditingLedZone)
else if (_isEditingLedZone && _currentLedIndex >= 0)
{
_ledZoneColor = colorDialog.Color;
_camera.SetLedZoneColor(_currentLedIndex, colorDialog.Color);
}
UpdateColorButtonIcon();
picBoxCamera.Invalidate();
@@ -888,9 +1063,40 @@ namespace Camera
}
}
private void ToolStripButton4_Click(object sender, EventArgs e)
{
if (_isEditingLedZone && _currentLedIndex >= 0)
{
_camera.RemoveLedZone(_currentLedIndex);
_currentLedIndex = -1;
_selectedZoneIndex = -1;
_isEditingLedZone = false;
_ledZoneState = 0; // 就绪状态
toolStripButton2.Checked = false;
toolStripButton2.Visible = true;
toolStripButton2.ToolTipText = "绘制Led区(点击开启)";
toolStripButton1.Visible = true;
UpdateLedZoneButtonsVisibility(0);
UpdateDataGridView();
picBoxCamera.Invalidate();
}
}
private void UpdateColorButtonIcon()
{
Color currentColor = _isEditingDetectionZone ? _detectionZoneColor : _ledZoneColor;
Color currentColor;
if (_isEditingDetectionZone)
{
currentColor = _detectionZoneColor;
}
else if (_isEditingLedZone && _currentLedIndex >= 0)
{
currentColor = _camera.GetLedZoneColor(_currentLedIndex);
}
else
{
currentColor = _camera.GetLedZoneColor();
}
Bitmap bmp = new Bitmap(24, 24);
using (Graphics g = Graphics.FromImage(bmp))
{
@@ -903,12 +1109,33 @@ namespace Camera
toolStripButton3.Image = bmp;
}
private void UpdateLedZoneButtonsVisibility(int state)
{
switch (state)
{
case 0: // 就绪状态
toolStripButton2.Visible = true;
toolStripButton3.Visible = false;
toolStripButton4.Visible = false;
break;
case 1: // 选中状态
toolStripButton2.Visible = false;
toolStripButton3.Visible = true;
toolStripButton4.Visible = true;
break;
case 2: // 绘制状态
toolStripButton2.Visible = true;
toolStripButton3.Visible = true;
toolStripButton4.Visible = false;
break;
}
}
private void Setting_FormClosed(object sender, FormClosedEventArgs e)
{
if (_camera != null)
{
_camera.SetDetectionZoneColor(_detectionZoneColor);
_camera.SetLedZoneColor(_ledZoneColor);
_camera.SaveConfig();
_camera.ImageCaptured -= Camera_ImageCaptured;
}