diff --git a/Windows/CS/Framework4.0/Toprie/Toprie/Camera.cs b/Windows/CS/Framework4.0/Toprie/Toprie/Camera.cs index ba34c1d..6d41a02 100644 --- a/Windows/CS/Framework4.0/Toprie/Toprie/Camera.cs +++ b/Windows/CS/Framework4.0/Toprie/Toprie/Camera.cs @@ -3199,6 +3199,7 @@ namespace JoyD.Windows.CS.Toprie { // 显示配置窗口,使用完整命名空间引用Setting类 JoyD.Windows.CS.Setting.Form.AutoConfig = this.AutoConfig; + JoyD.Windows.CS.Setting.Form.ProjectPath = this.ProjectPath; JoyD.Windows.CS.Setting.Form.ShowDialog(); } catch (Exception ex) diff --git a/Windows/CS/Framework4.0/Toprie/Toprie/Setting.cs b/Windows/CS/Framework4.0/Toprie/Toprie/Setting.cs index 5e2e0cf..6f1cb58 100644 --- a/Windows/CS/Framework4.0/Toprie/Toprie/Setting.cs +++ b/Windows/CS/Framework4.0/Toprie/Toprie/Setting.cs @@ -7,6 +7,7 @@ using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; +using System.Threading.Tasks; using System.Windows.Forms; namespace JoyD.Windows.CS @@ -46,9 +47,30 @@ namespace JoyD.Windows.CS _autoConfig = value; // 更新按钮可见性 UpdateButtonsVisibility(0); + // 重置配置加载状态 + _isConfigLoaded = false; } } + // 项目路径,用于获取配置文件 + private string _projectPath = ""; + /// + /// 获取或设置项目路径 + /// + public string ProjectPath + { + get { return _projectPath; } + set + { + _projectPath = value; + // 重置配置加载状态 + _isConfigLoaded = false; + } + } + + // 配置是否已经加载的标志位 + private bool _isConfigLoaded = false; + // 定时器字段 private readonly Timer _timer; @@ -4617,5 +4639,328 @@ namespace JoyD.Windows.CS MessageBox.Show($"保存失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } + + /// + /// 异步加载测温区配置 + /// + private void LoadTempRegionAsync() + { + // 检查配置是否已经加载,避免重复加载 + if (_isConfigLoaded) return; + + Task.Factory.StartNew(() => + { + try + { + // 从默认配置文件路径加载测温区 + if (string.IsNullOrEmpty(_projectPath)) return; + + string configDir = Path.Combine(_projectPath, "Config"); + string configPath = Path.Combine(configDir, "测温区信息.csv"); + + if (File.Exists(configPath)) + { + // 读取并加载测温区配置 + List regions = new List(); + int regionCounter = 0; + + using (StreamReader reader = new StreamReader(configPath, Encoding.UTF8)) + { + // 跳过标题行 + string headerLine = reader.ReadLine(); + + string line; + while ((line = reader.ReadLine()) != null) + { + try + { + // 分割CSV行数据 + string[] parts = line.Split(','); + + if (parts.Length >= 6) + { + // 解析数据 + int.TryParse(parts[1], out int x); + int.TryParse(parts[2], out int y); + int.TryParse(parts[3], out int width); + int.TryParse(parts[4], out int height); + + // 解析颜色 + Color color = Color.FromName(parts[5]); + + // 创建区域信息 + regions.Add(new RegionInfo + { + Index = ++regionCounter, + ImageRectangle = new Rectangle(x, y, width, height), + Color = color + }); + } + } + catch (Exception ex) + { + Console.WriteLine($"解析测温区配置行失败: {line}, 错误: {ex.Message}"); + } + } + } + + // 更新UI,显示加载的测温区 + if (regions.Count > 0) + { + this.Invoke((Action)(() => + { + try + { + // 清除现有区域 + _drawnRectangles.Clear(); + _selectedRegionIndex = -1; + txtRegionNumber.Text = ""; + + // 创建新的叠加层图像 + CreateRectangleOverlayImage(); + + // 添加加载的区域 + _drawnRectangles.AddRange(regions); + _regionCounter = regions.Count; + + // 绘制加载的区域到叠加层图像 + using (Graphics g = Graphics.FromImage(_rectangleOverlayImage)) + { + foreach (var region in regions) + { + using (Pen pen = new Pen(region.Color, 2)) + { + g.DrawRectangle(pen, region.ImageRectangle); + } + } + } + + // 刷新图像显示 + picBoxTemp.Invalidate(); + } + catch (Exception ex) + { + Console.WriteLine($"更新测温区UI失败: {ex.Message}"); + } + })); + } + } + } + catch (Exception ex) + { + Console.WriteLine($"异步加载测温区配置失败: {ex.Message}"); + } + finally + { + // 配置加载完成,设置标志位 + _isConfigLoaded = true; + } + }); + } + + /// + /// 异步加载温差图配置 + /// + private void LoadTempDiffAsync() + { + // 检查配置是否已经加载,避免重复加载 + if (_isConfigLoaded) return; + + Task.Factory.StartNew(() => + { + try + { + // 从默认配置文件路径加载温差图 + if (string.IsNullOrEmpty(_projectPath)) return; + + string configDir = Path.Combine(_projectPath, "Config"); + string configPath = Path.Combine(configDir, "温差数据.csv"); + + if (File.Exists(configPath)) + { + List> tempDiffDataList = new List>(); + Dictionary tempToColorMap = new Dictionary(); + bool readingLegend = false; + bool readingPixelData = false; + + // 读取温差数据文件 + using (StreamReader reader = new StreamReader(configPath, Encoding.UTF8)) + { + string line; + while ((line = reader.ReadLine()) != null) + { + line = line.Trim(); + + // 识别温差图例信息部分 + if (line == "温差图例信息") + { + readingLegend = true; + readingPixelData = false; + reader.ReadLine(); // 跳过表头行 + continue; + } + // 识别温差像素数据部分 + else if (line == "温差像素数据") + { + readingLegend = false; + readingPixelData = true; + reader.ReadLine(); // 跳过表头行 + continue; + } + // 温差图例读到空行就表示结束 + else if (readingLegend && string.IsNullOrEmpty(line)) + { + readingLegend = false; + continue; + } + // 跳过空行 + else if (string.IsNullOrEmpty(line)) + { + continue; + } + + // 读取温差图例信息 + if (readingLegend) + { + string[] parts = line.Split(','); + if (parts.Length >= 2) + { + // 解析温差值和颜色 + if (double.TryParse(parts[0].Replace("°C", ""), out double tempValue)) + { + // 使用ColorTranslator.FromHtml正确解析十六进制颜色代码 + Color color = ColorTranslator.FromHtml(parts[1]); + tempToColorMap[tempValue] = color; + + // 添加到数据列表 + tempDiffDataList.Add(new Dictionary + { + { "tempDiffValue", $"{tempValue}°C" }, + { "color", color } + }); + } + } + } + // 处理像素温度数据 + else if (readingPixelData) + { + string[] parts = line.Split(','); + if (parts.Length == 3 && parts[0] != "无温差图像数据") + { + try + { + int x = int.Parse(parts[0]); + int y = int.Parse(parts[1]); + double temperature = double.Parse(parts[2]); + + // 找到对应的颜色 - 尝试精确匹配 + Color pixelColor = Color.Transparent; + if (tempToColorMap.TryGetValue(temperature, out Color exactColor)) + { + pixelColor = exactColor; + } + else + { + // 如果没有精确匹配,尝试四舍五入匹配 + double roundedTemp = Math.Round(temperature, 1); + if (tempToColorMap.TryGetValue(roundedTemp, out Color roundedColor)) + { + pixelColor = roundedColor; + } + } + + if (pixelColor != Color.Transparent) + { + // 更新UI,绘制温差图 + this.Invoke((Action)(() => + { + try + { + // 确保温差图叠加层已初始化 + if (_tempDiffOverlayImage == null || + _tempDiffOverlayImage.Width != picBoxTemp.Image.Width || + _tempDiffOverlayImage.Height != picBoxTemp.Image.Height) + { + InitializeTempDiffOverlayImage(); + } + + if (_tempDiffOverlayImage is Bitmap bitmap) + { + // 直接设置像素点颜色 + bitmap.SetPixel(x, y, pixelColor); + } + } + catch (Exception ex) + { + Console.WriteLine($"绘制温差图像素失败: {ex.Message}"); + } + })); + } + } + catch (Exception ex) + { + Console.WriteLine($"解析像素数据失败: {ex.Message}"); + } + } + } + } + } + + // 更新UI,显示加载的温差图配置 + if (tempDiffDataList.Count > 0) + { + this.Invoke((Action)(() => + { + try + { + // 清除现有数据 + tempDiffData.Clear(); + dataGridViewTempDiff.Rows.Clear(); + + // 添加加载的数据 + tempDiffData.AddRange(tempDiffDataList); + + // 更新DataGridView + foreach (var item in tempDiffDataList) + { + dataGridViewTempDiff.Rows.Add(item["tempDiffValue"], ""); + } + + // 刷新显示 + dataGridViewTempDiff.Refresh(); + // 更新温差图显示 + picBoxTemp.Invalidate(); + } + catch (Exception ex) + { + Console.WriteLine($"更新温差图UI失败: {ex.Message}"); + } + })); + } + } + } + catch (Exception ex) + { + Console.WriteLine($"异步加载温差图配置失败: {ex.Message}"); + } + }); + } + + /// + /// 窗口显示事件,用于自动加载配置 + /// + /// + protected override void OnShown(EventArgs e) + { + base.OnShown(e); + + // 检查自动配置状态 + if (_autoConfig) + { + // 异步加载测温区和温差图,不等待完成,避免阻塞UI + LoadTempRegionAsync(); + LoadTempDiffAsync(); + } + } } } \ No newline at end of file