65 lines
1.9 KiB
C#
65 lines
1.9 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Text.Json;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace DH.Commons.Base
|
|||
|
{
|
|||
|
[Serializable]
|
|||
|
public class VisualLocalization
|
|||
|
{
|
|||
|
// 必须包含公共无参构造函数
|
|||
|
public VisualLocalization() { }
|
|||
|
|
|||
|
public string CameraName { get; set; }
|
|||
|
public string ModelPath { get; set; }
|
|||
|
public string ImgPath { get; set; }
|
|||
|
public string Threshold { get; set; }
|
|||
|
public string Direction { get; set; }
|
|||
|
public string Speed { get; set; }
|
|||
|
|
|||
|
// 保存到文件
|
|||
|
public void SaveToFile(string filePath)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
var options = new JsonSerializerOptions
|
|||
|
{
|
|||
|
WriteIndented = true, // 美化格式
|
|||
|
IgnoreNullValues = true // 忽略空值
|
|||
|
};
|
|||
|
|
|||
|
string json = JsonSerializer.Serialize(this, options);
|
|||
|
File.WriteAllText(filePath, json);
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
throw new InvalidOperationException($"保存失败: {ex.Message}");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// 从文件加载
|
|||
|
public static VisualLocalization LoadFromFile(string filePath)
|
|||
|
{
|
|||
|
if (!File.Exists(filePath))
|
|||
|
throw new FileNotFoundException("文件不存在", filePath);
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
string json = File.ReadAllText(filePath);
|
|||
|
return JsonSerializer.Deserialize<VisualLocalization>(json);
|
|||
|
}
|
|||
|
catch (JsonException ex)
|
|||
|
{
|
|||
|
throw new InvalidOperationException($"JSON解析错误: {ex.Message}");
|
|||
|
}
|
|||
|
catch (IOException ex)
|
|||
|
{
|
|||
|
throw new InvalidOperationException($"文件读取失败: {ex.Message}");
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|