视觉定位提交保存
This commit is contained in:
parent
43e7f3009d
commit
74051a4287
@ -1,18 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
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; }
|
||||
@ -20,45 +16,62 @@ namespace DH.Commons.Base
|
||||
public string Direction { get; set; }
|
||||
public string Speed { get; set; }
|
||||
|
||||
// 保存到文件
|
||||
public void SaveToFile(string filePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
var options = new JsonSerializerOptions
|
||||
{
|
||||
WriteIndented = true, // 美化格式
|
||||
IgnoreNullValues = true // 忽略空值
|
||||
};
|
||||
// 配置文件路径
|
||||
private const string ConfigFile = "VisualConfigs.json";
|
||||
private static readonly object _fileLock = new object();
|
||||
|
||||
string json = JsonSerializer.Serialize(this, options);
|
||||
File.WriteAllText(filePath, json);
|
||||
}
|
||||
catch (Exception ex)
|
||||
/// <summary>
|
||||
/// 保存当前配置(存在则更新,不存在则新增)
|
||||
/// </summary>
|
||||
public void Save()
|
||||
{
|
||||
lock (_fileLock)
|
||||
{
|
||||
throw new InvalidOperationException($"保存失败: {ex.Message}");
|
||||
var list = LoadAll();
|
||||
var existing = list.FirstOrDefault(c => c.CameraName == CameraName);
|
||||
|
||||
if (existing != null)
|
||||
{
|
||||
// 更新现有配置
|
||||
existing.ModelPath = ModelPath;
|
||||
existing.ImgPath = ImgPath;
|
||||
existing.Threshold = Threshold;
|
||||
existing.Direction = Direction;
|
||||
existing.Speed = Speed;
|
||||
}
|
||||
else
|
||||
{
|
||||
list.Add(this);
|
||||
}
|
||||
|
||||
SaveAll(list);
|
||||
}
|
||||
}
|
||||
|
||||
// 从文件加载
|
||||
public static VisualLocalization LoadFromFile(string filePath)
|
||||
/// <summary>
|
||||
/// 获取全部配置列表
|
||||
/// </summary>
|
||||
public static List<VisualLocalization> LoadAll()
|
||||
{
|
||||
if (!File.Exists(filePath))
|
||||
throw new FileNotFoundException("文件不存在", filePath);
|
||||
lock (_fileLock)
|
||||
{
|
||||
if (!File.Exists(ConfigFile)) return new List<VisualLocalization>();
|
||||
|
||||
try
|
||||
{
|
||||
string json = File.ReadAllText(filePath);
|
||||
return JsonSerializer.Deserialize<VisualLocalization>(json);
|
||||
var json = File.ReadAllText(ConfigFile);
|
||||
return JsonSerializer.Deserialize<List<VisualLocalization>>(json)
|
||||
?? new List<VisualLocalization>();
|
||||
}
|
||||
catch (JsonException ex)
|
||||
}
|
||||
|
||||
private static void SaveAll(List<VisualLocalization> list)
|
||||
{
|
||||
var options = new JsonSerializerOptions
|
||||
{
|
||||
throw new InvalidOperationException($"JSON解析错误: {ex.Message}");
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
throw new InvalidOperationException($"文件读取失败: {ex.Message}");
|
||||
}
|
||||
WriteIndented = true,
|
||||
IgnoreNullValues = true
|
||||
};
|
||||
|
||||
File.WriteAllText(ConfigFile, JsonSerializer.Serialize(list, options));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,21 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Drawing;
|
||||
using System.Data;
|
||||
using System.Drawing.Imaging;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using AntdUI;
|
||||
using DH.Commons.Base;
|
||||
using DH.Commons.Enums;
|
||||
using DH.Devices.Camera;
|
||||
using HalconDotNet;
|
||||
using OpenCvSharp.Extensions;
|
||||
using Sunny.UI;
|
||||
using static System.Net.Mime.MediaTypeNames;
|
||||
|
||||
|
||||
namespace DHSoftware.Views
|
||||
{
|
||||
@ -36,7 +27,9 @@ namespace DHSoftware.Views
|
||||
//定时器
|
||||
private System.Threading.Timer Timer;
|
||||
|
||||
List<VisualLocalization> localizations = new List<VisualLocalization>();
|
||||
|
||||
|
||||
public VisualLocalizationWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
@ -51,10 +44,34 @@ namespace DHSoftware.Views
|
||||
btnReverse.MouseUp += BtnReverse_MouseUp;
|
||||
btnSaveImg.Click += BtnSaveImg_Click;
|
||||
btnSavePos.Click += BtnSavePos_Click;
|
||||
|
||||
sltCameraName.SelectedIndexChanged += SltCameraName_SelectedIndexChanged;
|
||||
|
||||
try
|
||||
{
|
||||
localizations = VisualLocalization.LoadAll();
|
||||
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void SltCameraName_SelectedIndexChanged(object sender, IntEventArgs e)
|
||||
{
|
||||
|
||||
string cameraName= sltCameraName.Text;
|
||||
VisualLocalization? visual= localizations.Where(it=>it.CameraName==cameraName).FirstOrDefault();
|
||||
if (visual != null)
|
||||
{
|
||||
iptModel.Text = visual.ModelPath;
|
||||
iptBackImg.Text = visual.ImgPath;
|
||||
iptThreshold.Text = visual.Threshold;
|
||||
sltDirection.SelectedIndex = visual.Direction == "正方向" ? 0 : 1;
|
||||
iptSpeed.Text = visual.Speed;
|
||||
}
|
||||
}
|
||||
|
||||
private void BtnSavePos_Click(object? sender, EventArgs e)
|
||||
{
|
||||
@ -65,15 +82,15 @@ namespace DHSoftware.Views
|
||||
});
|
||||
if (form.submit)
|
||||
{
|
||||
//保存用户操作到文件
|
||||
VisualLocalization visualLocalization = new VisualLocalization();
|
||||
//保存用户操作到文件
|
||||
visualLocalization.CameraName = sltCameraName.Text;
|
||||
visualLocalization.ModelPath=iptModel.Text;
|
||||
visualLocalization.ImgPath=iptBackImg.Text;
|
||||
visualLocalization.Threshold=iptThreshold.Text;
|
||||
visualLocalization.Direction=sltDirection.Text;
|
||||
visualLocalization.Speed=iptSpeed.Text;
|
||||
visualLocalization.SaveToFile("VisualLocalization.json");
|
||||
visualLocalization.Save();
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user