检测区颜色

This commit is contained in:
zqm
2026-03-25 14:52:47 +08:00
parent f33d70e429
commit 2a72eab101
3 changed files with 122 additions and 9 deletions

View File

@@ -26,6 +26,8 @@ namespace Camera
// 配置数据
private Rectangle _detectionZone;
private Rectangle _ledZone;
private Color _detectionZoneColor = Color.Black;
private Color _ledZoneColor = Color.Lime;
// 定时器
private System.Timers.Timer _timer;
@@ -156,6 +158,26 @@ namespace Camera
_ledZone = zone;
}
public Color GetDetectionZoneColor()
{
return _detectionZoneColor;
}
public void SetDetectionZoneColor(Color color)
{
_detectionZoneColor = color;
}
public Color GetLedZoneColor()
{
return _ledZoneColor;
}
public void SetLedZoneColor(Color color)
{
_ledZoneColor = color;
}
/// <summary>
/// 保存配置
/// </summary>
@@ -172,7 +194,8 @@ namespace Camera
if (_detectionZone.Width > 0 && _detectionZone.Height > 0)
{
writer.WriteLine(string.Format("{0},{1},{2},{3},#FF0000", _detectionZone.X, _detectionZone.Y, _detectionZone.Width, _detectionZone.Height));
string colorStr = ColorTranslator.ToHtml(_detectionZoneColor);
writer.WriteLine(string.Format("{0},{1},{2},{3},{4}", _detectionZone.X, _detectionZone.Y, _detectionZone.Width, _detectionZone.Height, colorStr));
}
writer.WriteLine();
@@ -181,7 +204,8 @@ namespace Camera
if (_ledZone.Width > 0 && _ledZone.Height > 0)
{
writer.WriteLine(string.Format("1,{0},{1},{2},{3},#00FF00", _ledZone.X, _ledZone.Y, _ledZone.Width, _ledZone.Height));
string colorStr = ColorTranslator.ToHtml(_ledZoneColor);
writer.WriteLine(string.Format("1,{0},{1},{2},{3},{4}", _ledZone.X, _ledZone.Y, _ledZone.Width, _ledZone.Height, colorStr));
}
}
}
@@ -197,6 +221,8 @@ namespace Camera
_detectionZone = new Rectangle(0, 0, 2880, 1620);
_ledZone = new Rectangle(0, 0, 0, 0);
_detectionZoneColor = Color.Black;
_ledZoneColor = Color.Lime;
string configFile = Path.Combine(_configPath, "Camera.csv");
try
@@ -225,6 +251,10 @@ namespace Camera
if (!int.TryParse(parts[2], out int width)) continue;
if (!int.TryParse(parts[3], out int height)) continue;
_detectionZone = new Rectangle(x, y, width, height);
if (parts.Length >= 5 && !string.IsNullOrEmpty(parts[4]))
{
try { _detectionZoneColor = ColorTranslator.FromHtml(parts[4]); } catch { }
}
}
else if (parts[0] == "索引")
{
@@ -240,6 +270,10 @@ namespace Camera
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);
if (parts.Length >= 6 && !string.IsNullOrEmpty(parts[5]))
{
try { _ledZoneColor = ColorTranslator.FromHtml(parts[5]); } catch { }
}
}
}
}
@@ -300,11 +334,13 @@ namespace Camera
/// </summary>
private Image GetCameraImage()
{
#pragma warning disable 0162
if (IsDebug)
{
if (File.Exists("test.jpg"))
return new Bitmap("test.jpg");
}
#pragma warning restore 0162
string url = "http://" + IP + SNAPSHOT_URI;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

View File

@@ -24,6 +24,7 @@ namespace Camera
this.toolStripButton1 = new System.Windows.Forms.ToolStripButton();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.toolStripButton2 = new System.Windows.Forms.ToolStripButton();
this.toolStripButton3 = new System.Windows.Forms.ToolStripButton();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
@@ -97,7 +98,8 @@ namespace Camera
this.toolStrip1.ImageScalingSize = new System.Drawing.Size(32, 32);
this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripButton1,
this.toolStripButton2});
this.toolStripButton2,
this.toolStripButton3});
this.toolStrip1.Location = new System.Drawing.Point(0, 0);
this.toolStrip1.Name = "toolStrip1";
this.toolStrip1.Size = new System.Drawing.Size(80, 40);
@@ -122,6 +124,15 @@ namespace Camera
this.toolStripButton2.ToolTipText = "绘制Led区(点击开启/关闭)";
this.toolStripButton2.Click += new System.EventHandler(this.ToolStripButton2_Click);
//
// toolStripButton3
//
this.toolStripButton3.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.toolStripButton3.Name = "toolStripButton3";
this.toolStripButton3.Size = new System.Drawing.Size(36, 36);
this.toolStripButton3.ToolTipText = "选择颜色";
this.toolStripButton3.Visible = false;
this.toolStripButton3.Click += new System.EventHandler(this.ToolStripButton3_Click);
//
// dataGridView1
//
this.dataGridView1.AllowUserToAddRows = false;
@@ -175,6 +186,7 @@ namespace Camera
private System.Windows.Forms.ToolStrip toolStrip1;
private System.Windows.Forms.ToolStripButton toolStripButton1;
private System.Windows.Forms.ToolStripButton toolStripButton2;
private System.Windows.Forms.ToolStripButton toolStripButton3;
private System.Windows.Forms.DataGridView dataGridView1;
}
}

View File

@@ -22,6 +22,8 @@ namespace Camera
private Rectangle _tempZone;
private bool _isEditingDetectionZone = false;
private bool _isEditingLedZone = false;
private Color _detectionZoneColor = Color.Black;
private Color _ledZoneColor = Color.Lime;
private Point _resizeStartPoint;
private Rectangle _originalResizeRect;
private int _hoveredHandle = -1;
@@ -91,6 +93,8 @@ namespace Camera
if (_camera != null)
{
_camera.ImageCaptured += Camera_ImageCaptured;
_detectionZoneColor = _camera.GetDetectionZoneColor();
_ledZoneColor = _camera.GetLedZoneColor();
}
picBoxCamera.BackColor = Color.Gray;
splitContainer1.FixedPanel = System.Windows.Forms.FixedPanel.Panel2;
@@ -295,19 +299,19 @@ namespace Camera
Rectangle currentEditZone = new Rectangle(0, 0, 0, 0);
bool isEditing = false;
Color editColor = Color.Red;
Color editColor = _detectionZoneColor;
if (_isEditingDetectionZone)
{
currentEditZone = scaledDetectionZone;
isEditing = true;
editColor = Color.Red;
editColor = _detectionZoneColor;
}
else if (_isEditingLedZone)
{
currentEditZone = scaledLedZone;
isEditing = true;
editColor = Color.Lime;
editColor = _ledZoneColor;
}
if (isEditing && currentEditZone.Width > 0 && currentEditZone.Height > 0)
@@ -321,20 +325,20 @@ namespace Camera
}
else
{
using (Pen pen = new Pen(Color.Red, 2))
using (Pen pen = new Pen(_detectionZoneColor, 2))
{
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
g.DrawRectangle(pen, scaledDetectionZone);
}
using (Pen pen = new Pen(Color.Green, 2))
using (Pen pen = new Pen(_ledZoneColor, 2))
{
g.DrawRectangle(pen, scaledLedZone);
}
using (Font font = new Font("Arial", 10))
{
g.DrawString("Led区", font, Brushes.Green, scaledLedZone.X, scaledLedZone.Y - 15);
g.DrawString("Led区", font, new SolidBrush(_ledZoneColor), scaledLedZone.X, scaledLedZone.Y - 15);
}
}
}
@@ -630,6 +634,8 @@ namespace Camera
_isResizing = false;
toolStripButton1.Checked = false;
toolStripButton1.ToolTipText = "修改检测区(点击开启)";
toolStripButton2.Visible = true;
toolStripButton3.Visible = false;
picBoxCamera.Invalidate();
return;
}
@@ -640,6 +646,8 @@ namespace Camera
_isResizing = false;
toolStripButton2.Checked = false;
toolStripButton2.ToolTipText = "绘制Led区(点击开启)";
toolStripButton1.Visible = true;
toolStripButton3.Visible = false;
picBoxCamera.Invalidate();
return;
}
@@ -805,6 +813,9 @@ namespace Camera
_selectedZoneIndex = 0;
toolStripButton1.ToolTipText = "修改检测区(点击关闭)";
toolStripButton2.Checked = false;
toolStripButton2.Visible = false;
toolStripButton3.Visible = true;
UpdateColorButtonIcon();
toolStripButton2.ToolTipText = "绘制Led区(点击开启/关闭)";
_tempZone = _camera.GetDetectionZone();
}
@@ -813,6 +824,8 @@ namespace Camera
_isEditingDetectionZone = false;
_selectedZoneIndex = -1;
toolStripButton1.ToolTipText = "修改检测区(点击开启)";
toolStripButton2.Visible = true;
toolStripButton3.Visible = false;
}
picBoxCamera.Invalidate();
UpdateDataGridView();
@@ -827,6 +840,9 @@ namespace Camera
_selectedZoneIndex = 1;
toolStripButton2.ToolTipText = "绘制Led区(点击关闭)";
toolStripButton1.Checked = false;
toolStripButton1.Visible = false;
toolStripButton3.Visible = true;
UpdateColorButtonIcon();
toolStripButton1.ToolTipText = "修改检测区(点击开启/关闭)";
_tempZone = _camera.GetLedZone();
}
@@ -835,15 +851,64 @@ namespace Camera
_isEditingLedZone = false;
_selectedZoneIndex = -1;
toolStripButton2.ToolTipText = "绘制Led区(点击开启)";
toolStripButton1.Visible = true;
toolStripButton3.Visible = false;
}
picBoxCamera.Invalidate();
UpdateDataGridView();
}
private void ToolStripButton3_Click(object sender, EventArgs e)
{
using (ColorDialog colorDialog = new ColorDialog())
{
colorDialog.FullOpen = true;
if (_isEditingDetectionZone)
{
colorDialog.Color = _detectionZoneColor;
}
else if (_isEditingLedZone)
{
colorDialog.Color = _ledZoneColor;
}
if (colorDialog.ShowDialog() == DialogResult.OK)
{
if (_isEditingDetectionZone)
{
_detectionZoneColor = colorDialog.Color;
}
else if (_isEditingLedZone)
{
_ledZoneColor = colorDialog.Color;
}
UpdateColorButtonIcon();
picBoxCamera.Invalidate();
}
}
}
private void UpdateColorButtonIcon()
{
Color currentColor = _isEditingDetectionZone ? _detectionZoneColor : _ledZoneColor;
Bitmap bmp = new Bitmap(24, 24);
using (Graphics g = Graphics.FromImage(bmp))
{
using (Brush brush = new SolidBrush(currentColor))
{
g.FillRectangle(Brushes.White, 0, 0, 24, 24);
g.FillRectangle(brush, 2, 2, 20, 20);
}
}
toolStripButton3.Image = bmp;
}
private void Setting_FormClosed(object sender, FormClosedEventArgs e)
{
if (_camera != null)
{
_camera.SetDetectionZoneColor(_detectionZoneColor);
_camera.SetLedZoneColor(_ledZoneColor);
_camera.SaveConfig();
_camera.ImageCaptured -= Camera_ImageCaptured;
}