保存相对坐标

This commit is contained in:
zqm
2026-03-26 22:03:42 +08:00
parent fdca167420
commit 452caa6d89
2 changed files with 138 additions and 34 deletions

View File

@@ -20,7 +20,7 @@ namespace Camera
private const string PASSWD = "Yexian.net.168"; private const string PASSWD = "Yexian.net.168";
// 调试模式 // 调试模式
private const Boolean IsDebug = false; private const Boolean IsDebug = true;
// 配置文件目录 // 配置文件目录
private string _configPath; private string _configPath;
@@ -69,13 +69,15 @@ namespace Camera
} }
/// <summary> /// <summary>
/// 获取当前图像 /// 获取当前图像(返回副本,避免线程冲突)
/// </summary> /// </summary>
public Image GetCurrentImage() public Image GetCurrentImage()
{ {
lock (_imageLock) lock (_imageLock)
{ {
return _currentImage; if (_currentImage != null)
return _currentImage.Clone() as Image;
return null;
} }
} }

View File

@@ -315,12 +315,12 @@ namespace Camera
if (picBoxCamera.InvokeRequired) if (picBoxCamera.InvokeRequired)
{ {
picBoxCamera.Invoke(new Action<Image>(UpdateImage), e.Image); picBoxCamera.Invoke(new Action<Image>(UpdateImage), e.Image);
picBoxCamera.Invoke(new Action(UpdateDataGridView)); picBoxCamera.Invoke(new Action(UpdateDetectionResults));
} }
else else
{ {
UpdateImage(e.Image); UpdateImage(e.Image);
UpdateDataGridView(); UpdateDetectionResults();
} }
} }
@@ -358,9 +358,7 @@ namespace Camera
if (ledZone.Width > 0 && ledZone.Height > 0) if (ledZone.Width > 0 && ledZone.Height > 0)
{ {
int relativeX = ledZone.X - detectionZone.X; dataGridView1.Rows.Add(index, ledZone.X, ledZone.Y, ledZone.Width, ledZone.Height, ColorTranslator.ToHtml(ledColor), detectionResult);
int relativeY = ledZone.Y - detectionZone.Y;
dataGridView1.Rows.Add(index, relativeX, relativeY, ledZone.Width, ledZone.Height, ColorTranslator.ToHtml(ledColor), detectionResult);
} }
} }
} }
@@ -368,6 +366,49 @@ namespace Camera
_isUpdatingDataGridView = false; _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) private void PicBoxCamera_Paint(object sender, PaintEventArgs e)
{ {
Image currentImage = null; Image currentImage = null;
@@ -673,8 +714,8 @@ namespace Camera
{ {
Rectangle led = kvp.Value; Rectangle led = kvp.Value;
Rectangle scaledLed = new Rectangle( Rectangle scaledLed = new Rectangle(
(int)(led.X * scaleX), (int)((detectionZone.X + led.X) * scaleX),
(int)(led.Y * scaleY), (int)((detectionZone.Y + led.Y) * scaleY),
(int)(led.Width * scaleX), (int)(led.Width * scaleX),
(int)(led.Height * scaleY) (int)(led.Height * scaleY)
); );
@@ -818,8 +859,8 @@ namespace Camera
{ {
Rectangle led = _camera.GetLedZone(_currentLedIndex); Rectangle led = _camera.GetLedZone(_currentLedIndex);
scaledLedZone = new Rectangle( scaledLedZone = new Rectangle(
(int)(led.X * scaleX), (int)((detectionZone.X + led.X) * scaleX),
(int)(led.Y * scaleY), (int)((detectionZone.Y + led.Y) * scaleY),
(int)(led.Width * scaleX), (int)(led.Width * scaleX),
(int)(led.Height * scaleY) (int)(led.Height * scaleY)
); );
@@ -1013,7 +1054,9 @@ namespace Camera
newIndex++; newIndex++;
} }
Rectangle detectionZone = _camera.GetDetectionZone(); 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; _selectedZoneIndex = newIndex;
_currentLedIndex = newIndex; _currentLedIndex = newIndex;
_isEditingLedZone = true; _isEditingLedZone = true;
@@ -1145,9 +1188,8 @@ namespace Camera
if (_selectedZoneIndex >= 0 && _camera != null) if (_selectedZoneIndex >= 0 && _camera != null)
{ {
Rectangle zone = _camera.GetLedZone(_selectedZoneIndex); Rectangle zone = _camera.GetLedZone(_selectedZoneIndex);
Rectangle detectionZone = _camera.GetDetectionZone(); toolStripNumericUpDown1.Value = zone.X;
toolStripNumericUpDown1.Value = zone.X - detectionZone.X; toolStripNumericUpDown2.Value = zone.Y;
toolStripNumericUpDown2.Value = zone.Y - detectionZone.Y;
toolStripNumericUpDown3.Value = zone.Width; toolStripNumericUpDown3.Value = zone.Width;
toolStripNumericUpDown4.Value = zone.Height; toolStripNumericUpDown4.Value = zone.Height;
} }
@@ -1349,17 +1391,31 @@ namespace Camera
toolStripButton4.Visible = false; toolStripButton4.Visible = false;
toolStripTextBox1.Visible = false; toolStripTextBox1.Visible = false;
toolStripTextBox1.Text = ""; toolStripTextBox1.Text = "";
// 显示位置和宽高设置框 // 如果有LED区域则显示位置和宽高设置框
toolStrip2.Visible = true; if (_camera != null && _camera.GetLedZoneCount() > 0)
// 更新位置和宽高值
if (_selectedZoneIndex >= 0 && _camera != null)
{ {
Rectangle zone = _camera.GetLedZone(_selectedZoneIndex); toolStrip2.Visible = true;
Rectangle detectionZone = _camera.GetDetectionZone(); int targetIndex = -1;
toolStripNumericUpDown1.Value = zone.X - detectionZone.X; if (dataGridView1.SelectedRows.Count > 0)
toolStripNumericUpDown2.Value = zone.Y - detectionZone.Y; {
toolStripNumericUpDown3.Value = zone.Width; targetIndex = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["Index"].Value);
toolStripNumericUpDown4.Value = zone.Height; }
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; break;
case 1: // 选中状态 case 1: // 选中状态
@@ -1400,13 +1456,24 @@ namespace Camera
// X坐标相关事件 // X坐标相关事件
private void ToolStripNumericUpDown1_ValueChanged(object sender, EventArgs e) 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; if (index < 0 || _camera == null) return;
int x = (int)toolStripNumericUpDown1.Value; int x = (int)toolStripNumericUpDown1.Value;
Rectangle zone = _camera.GetLedZone(index); Rectangle zone = _camera.GetLedZone(index);
Rectangle detectionZone = _camera.GetDetectionZone(); zone.X = x;
zone.X = detectionZone.X + x;
_camera.SetLedZone(index, zone); _camera.SetLedZone(index, zone);
picBoxCamera.Invalidate(); picBoxCamera.Invalidate();
} }
@@ -1414,13 +1481,24 @@ namespace Camera
// Y坐标相关事件 // Y坐标相关事件
private void ToolStripNumericUpDown2_ValueChanged(object sender, EventArgs e) 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; if (index < 0 || _camera == null) return;
int y = (int)toolStripNumericUpDown2.Value; int y = (int)toolStripNumericUpDown2.Value;
Rectangle zone = _camera.GetLedZone(index); Rectangle zone = _camera.GetLedZone(index);
Rectangle detectionZone = _camera.GetDetectionZone(); zone.Y = y;
zone.Y = detectionZone.Y + y;
_camera.SetLedZone(index, zone); _camera.SetLedZone(index, zone);
picBoxCamera.Invalidate(); picBoxCamera.Invalidate();
} }
@@ -1428,7 +1506,19 @@ namespace Camera
// 宽度相关事件 // 宽度相关事件
private void ToolStripNumericUpDown3_ValueChanged(object sender, EventArgs e) 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; if (index < 0 || _camera == null) return;
int width = (int)toolStripNumericUpDown3.Value; int width = (int)toolStripNumericUpDown3.Value;
@@ -1441,7 +1531,19 @@ namespace Camera
// 高度相关事件 // 高度相关事件
private void ToolStripNumericUpDown4_ValueChanged(object sender, EventArgs e) 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; if (index < 0 || _camera == null) return;
int height = (int)toolStripNumericUpDown4.Value; int height = (int)toolStripNumericUpDown4.Value;