修复TemperatureData类命名冲突及构造函数参数问题
This commit is contained in:
@@ -1744,7 +1744,7 @@ namespace JoyD.Windows.CS.Toprie
|
||||
for (int j = 0; j < temperatureData.Width; j++)
|
||||
{
|
||||
// 添加温度值,使用逗号分隔
|
||||
lineBuilder.Append($"{temperatureData.Data[i, j]:F2}");
|
||||
lineBuilder.Append($"{temperatureData.TemperatureMatrix[i, j]:F2}");
|
||||
if (j < temperatureData.Width - 1)
|
||||
{
|
||||
lineBuilder.Append(",");
|
||||
|
||||
@@ -1147,8 +1147,10 @@ namespace JoyD.Windows.CS.Toprie
|
||||
// 获取温度补偿值
|
||||
float compensationValue = GetTemperatureCompensationValue();
|
||||
|
||||
// 创建温度数据对象
|
||||
TemperatureData temperatureData = new TemperatureData(temperatureFrame, compensationValue);
|
||||
// 创建温度数据对象,添加缺少的width和height参数
|
||||
const int width = 640; // 假设设备分辨率为640x480
|
||||
const int height = 480;
|
||||
TemperatureData temperatureData = new TemperatureData(temperatureFrame, width, height, compensationValue);
|
||||
|
||||
// 触发温度数据接收事件
|
||||
OnTemperatureReceived(new TemperatureReceivedEventArgs(temperatureData, temperatureFrame, compensationValue));
|
||||
@@ -4994,8 +4996,8 @@ namespace JoyD.Windows.CS.Toprie
|
||||
|
||||
SharedStructures.ImageTemp imageTemp = _a8Sdk.Get_all_temp();
|
||||
|
||||
// 检查是否获取到温度数据
|
||||
if (imageTemp == null)
|
||||
// 检查是否获取到温度数据(使用默认值比较而不是null比较)
|
||||
if (imageTemp.Equals(default(SharedStructures.ImageTemp)))
|
||||
{
|
||||
Log("未获取到温度数据,返回null");
|
||||
return null;
|
||||
@@ -5047,14 +5049,28 @@ namespace JoyD.Windows.CS.Toprie
|
||||
}
|
||||
|
||||
// 创建TemperatureData对象
|
||||
TemperatureData temperatureData = new TemperatureData
|
||||
// 使用Toprie.TemperatureData类的构造函数
|
||||
// 首先创建符合构造函数要求的rawData格式
|
||||
List<byte> rawDataList = new List<byte>();
|
||||
for (int i = 0; i < height; i++)
|
||||
{
|
||||
Width = width,
|
||||
Height = height,
|
||||
Data = temperatureMatrix,
|
||||
CompensationValue = compensationValue
|
||||
};
|
||||
temperatureData.CalculateStatistics();
|
||||
for (int j = 0; j < width; j++)
|
||||
{
|
||||
// 将温度值转换回原始格式(不包含补偿值)
|
||||
float tempValue = temperatureMatrix[i, j] - compensationValue;
|
||||
int tempInt = (int)(tempValue * 10.0f);
|
||||
rawDataList.Add((byte)((tempInt >> 8) & 0xFF)); // 高字节
|
||||
rawDataList.Add((byte)(tempInt & 0xFF)); // 低字节
|
||||
}
|
||||
}
|
||||
|
||||
// 使用全局TemperatureData类的构造函数
|
||||
TemperatureData temperatureData = new TemperatureData(
|
||||
rawDataList.ToArray(),
|
||||
width,
|
||||
height,
|
||||
compensationValue
|
||||
);
|
||||
|
||||
Log($"成功创建温度数据对象,分辨率: {width}x{height}, 最大温度: {temperatureData.MaxTemperature:F2}°C, 最小温度: {temperatureData.MinTemperature:F2}°C");
|
||||
return temperatureData;
|
||||
@@ -5108,14 +5124,27 @@ namespace JoyD.Windows.CS.Toprie
|
||||
}
|
||||
|
||||
// 创建并返回模拟温度数据对象
|
||||
TemperatureData temperatureData = new TemperatureData
|
||||
// 首先创建符合构造函数要求的rawData格式
|
||||
List<byte> rawDataList = new List<byte>();
|
||||
for (int i = 0; i < height; i++)
|
||||
{
|
||||
Width = width,
|
||||
Height = height,
|
||||
Data = temperatureMatrix,
|
||||
CompensationValue = 0
|
||||
};
|
||||
temperatureData.CalculateStatistics();
|
||||
for (int j = 0; j < width; j++)
|
||||
{
|
||||
// 将温度值转换回原始格式
|
||||
float tempValue = temperatureMatrix[i, j];
|
||||
int tempInt = (int)(tempValue * 10.0f);
|
||||
rawDataList.Add((byte)((tempInt >> 8) & 0xFF)); // 高字节
|
||||
rawDataList.Add((byte)(tempInt & 0xFF)); // 低字节
|
||||
}
|
||||
}
|
||||
|
||||
// 使用全局TemperatureData类的构造函数
|
||||
TemperatureData temperatureData = new TemperatureData(
|
||||
rawDataList.ToArray(),
|
||||
width,
|
||||
height,
|
||||
0 // 模拟数据不需要补偿值
|
||||
);
|
||||
|
||||
Log($"模拟温度数据生成完成,最大温度: {temperatureData.MaxTemperature:F2}°C, 最小温度: {temperatureData.MinTemperature:F2}°C");
|
||||
return temperatureData;
|
||||
@@ -5289,261 +5318,7 @@ namespace JoyD.Windows.CS.Toprie
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 温度数据类,用于存储和处理温度数据
|
||||
/// </summary>
|
||||
public class TemperatureData
|
||||
{
|
||||
/// <summary>
|
||||
/// 温度数据数组
|
||||
/// </summary>
|
||||
public float[,] Data { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 温度数据的宽度(像素)
|
||||
/// </summary>
|
||||
public int Width { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 温度数据的高度(像素)
|
||||
/// </summary>
|
||||
public int Height { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 温度补偿值
|
||||
/// </summary>
|
||||
public float CompensationValue { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 最大温度值
|
||||
/// </summary>
|
||||
public float MaxTemperature { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 最小温度值
|
||||
/// </summary>
|
||||
public float MinTemperature { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 平均温度值
|
||||
/// </summary>
|
||||
public float AverageTemperature { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="rawData">原始温度数据字节数组</param>
|
||||
/// <param name="compensationValue">温度补偿值</param>
|
||||
public TemperatureData(byte[] rawData, float compensationValue)
|
||||
{
|
||||
// 假设分辨率为256x192
|
||||
Width = 256;
|
||||
Height = 192;
|
||||
CompensationValue = compensationValue;
|
||||
|
||||
// 初始化温度数据数组
|
||||
Data = new float[Height, Width];
|
||||
|
||||
// 解析原始数据
|
||||
ParseRawData(rawData);
|
||||
|
||||
// 计算温度统计信息
|
||||
CalculateStatistics();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 解析原始温度数据
|
||||
/// </summary>
|
||||
/// <param name="rawData">原始温度数据字节数组</param>
|
||||
private void ParseRawData(byte[] rawData)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 假设每个温度值由2字节表示
|
||||
for (int i = 0; i < Height; i++)
|
||||
{
|
||||
for (int j = 0; j < Width; j++)
|
||||
{
|
||||
int index = (i * Width + j) * 2;
|
||||
if (index + 1 < rawData.Length)
|
||||
{
|
||||
// 读取2字节数据并转换为温度值
|
||||
short rawTempValue = (short)((rawData[index + 1] << 8) | rawData[index]);
|
||||
|
||||
// 转换为实际温度值(假设温度值为原始值除以某个系数)
|
||||
// 这里使用一个示例转换公式,实际转换方式需要根据设备的数据格式确定
|
||||
float temperature = rawTempValue / 100.0f + CompensationValue;
|
||||
|
||||
Data[i, j] = temperature;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 记录异常但不抛出,确保即使解析出错也能继续执行
|
||||
Console.WriteLine($"解析温度数据异常: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 计算温度统计信息
|
||||
/// </summary>
|
||||
private void CalculateStatistics()
|
||||
{
|
||||
if (Data == null || Data.Length == 0)
|
||||
{
|
||||
MaxTemperature = MinTemperature = AverageTemperature = 0.0f;
|
||||
return;
|
||||
}
|
||||
|
||||
float sum = 0.0f;
|
||||
MaxTemperature = float.MinValue;
|
||||
MinTemperature = float.MaxValue;
|
||||
int validCount = 0;
|
||||
|
||||
for (int i = 0; i < Height; i++)
|
||||
{
|
||||
for (int j = 0; j < Width; j++)
|
||||
{
|
||||
float temp = Data[i, j];
|
||||
// 跳过无效温度值
|
||||
if (!float.IsNaN(temp) && !float.IsInfinity(temp))
|
||||
{
|
||||
sum += temp;
|
||||
MaxTemperature = Math.Max(MaxTemperature, temp);
|
||||
MinTemperature = Math.Min(MinTemperature, temp);
|
||||
validCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 计算平均温度
|
||||
AverageTemperature = validCount > 0 ? sum / validCount : 0.0f;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定区域的温度数据
|
||||
/// </summary>
|
||||
/// <param name="x">区域左上角X坐标</param>
|
||||
/// <param name="y">区域左上角Y坐标</param>
|
||||
/// <param name="width">区域宽度</param>
|
||||
/// <param name="height">区域高度</param>
|
||||
/// <returns>区域温度数据,如果参数无效则返回null</returns>
|
||||
public TemperatureData GetRegionData(int x, int y, int width, int height)
|
||||
{
|
||||
// 验证参数
|
||||
if (x < 0 || y < 0 || width <= 0 || height <= 0 ||
|
||||
x + width > Width || y + height > Height)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// 创建区域温度数据数组
|
||||
float[,] regionData = new float[height, width];
|
||||
|
||||
// 复制区域数据
|
||||
for (int i = 0; i < height; i++)
|
||||
{
|
||||
for (int j = 0; j < width; j++)
|
||||
{
|
||||
regionData[i, j] = Data[y + i, x + j];
|
||||
}
|
||||
}
|
||||
|
||||
// 创建新的温度数据对象
|
||||
TemperatureData result = new TemperatureData
|
||||
{
|
||||
Width = width,
|
||||
Height = height,
|
||||
CompensationValue = CompensationValue,
|
||||
Data = regionData
|
||||
};
|
||||
result.CalculateStatistics();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定坐标的温度值(摄氏度)
|
||||
/// </summary>
|
||||
/// <param name="x">X坐标</param>
|
||||
/// <param name="y">Y坐标</param>
|
||||
/// <returns>温度值,如果坐标无效则返回NaN</returns>
|
||||
public float GetTemperatureAt(int x, int y)
|
||||
{
|
||||
if (x < 0 || x >= Width || y < 0 || y >= Height)
|
||||
{
|
||||
return float.NaN;
|
||||
}
|
||||
return Data[y, x];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将温度数据转换为指定的温度模式
|
||||
/// </summary>
|
||||
/// <param name="mode">目标温度模式</param>
|
||||
/// <returns>转换后的温度数据副本</returns>
|
||||
public TemperatureData ConvertTo(TemperatureMode mode)
|
||||
{
|
||||
// 创建新的温度数据对象
|
||||
TemperatureData result = new TemperatureData
|
||||
{
|
||||
Width = Width,
|
||||
Height = Height,
|
||||
CompensationValue = CompensationValue,
|
||||
Data = new float[Height, Width]
|
||||
};
|
||||
|
||||
// 根据目标模式进行转换
|
||||
switch (mode)
|
||||
{
|
||||
case TemperatureMode.Celsius:
|
||||
// 如果当前已经是摄氏度,直接复制数据
|
||||
for (int i = 0; i < Height; i++)
|
||||
{
|
||||
for (int j = 0; j < Width; j++)
|
||||
{
|
||||
result.Data[i, j] = Data[i, j];
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case TemperatureMode.Fahrenheit:
|
||||
// 摄氏度转华氏度: F = C * 9/5 + 32
|
||||
for (int i = 0; i < Height; i++)
|
||||
{
|
||||
for (int j = 0; j < Width; j++)
|
||||
{
|
||||
result.Data[i, j] = Data[i, j] * 9.0f / 5.0f + 32.0f;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case TemperatureMode.Kelvin:
|
||||
// 摄氏度转开尔文: K = C + 273.15
|
||||
for (int i = 0; i < Height; i++)
|
||||
{
|
||||
for (int j = 0; j < Width; j++)
|
||||
{
|
||||
result.Data[i, j] = Data[i, j] + 273.15f;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// 重新计算统计信息
|
||||
result.CalculateStatistics();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 私有构造函数,用于内部创建
|
||||
/// </summary>
|
||||
private TemperatureData()
|
||||
{}
|
||||
}
|
||||
// 移除内部的TemperatureData类定义,使用全局的TemperatureData类
|
||||
|
||||
#endregion 温度相关辅助类
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Toprie
|
||||
namespace JoyD.Windows.CS.Toprie
|
||||
{
|
||||
/// <summary>
|
||||
/// 温度数据模型类,用于存储和处理红外热像仪采集的温度数据
|
||||
|
||||
@@ -4,7 +4,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Toprie
|
||||
namespace JoyD.Windows.CS.Toprie
|
||||
{
|
||||
/// <summary>
|
||||
/// 温度数据接收事件参数类
|
||||
|
||||
Reference in New Issue
Block a user