using System;
|
using System.Collections.Generic;
|
using System.ComponentModel;
|
using System.Drawing;
|
using System.Data;
|
using System.Linq;
|
using System.Text;
|
using System.Windows.Forms;
|
using Bro.UI.HalconDisplay;
|
using System.IO;
|
using Newtonsoft.Json;
|
using Bro.UI.HalconDisplay.ViewROI;
|
using HalconDotNet;
|
|
namespace M423project
|
{
|
/// <summary>
|
/// add by Patrick 2020-2-18
|
///
|
/// 编辑电池在位判断使用的框位置/大小和相关阈值
|
/// </summary>
|
public partial class CtrlSetBatteryCheckROI : UserControl
|
{
|
public static event Action<BatteryCheckSetting> OnSettingSaved;
|
|
public const string SettingPath = "BatteryCheckSetting.json";
|
|
HalconDisplay halconDisplay = new HalconDisplay();
|
BatteryCheckSetting batteryCheckSetting = new BatteryCheckSetting();
|
|
BatteryCheckSetting BatteryCheckSetting
|
{
|
get { return batteryCheckSetting; }
|
set
|
{
|
if (value != batteryCheckSetting)
|
{
|
batteryCheckSetting = value;
|
}
|
}
|
}
|
|
public CtrlSetBatteryCheckROI()
|
{
|
InitializeComponent();
|
|
halconDisplay.Dock = DockStyle.Fill;
|
plDisplay.Controls.Add(halconDisplay);
|
|
halconDisplay.OnROIChanged += halconDisplay_OnROIChanged;
|
}
|
|
private void halconDisplay_OnROIChanged(object sender, Bro.UI.HalconDisplay.ViewROI.ROI NewROI)
|
{
|
if (NewROI is ROIRectangle1)
|
{
|
BatteryCheckSetting.Rect = NewROI as ROIRectangle1;
|
|
SetROILabel();
|
}
|
}
|
|
private void SetROILabel()
|
{
|
RemoveEvent();
|
|
nudStartX.Value = (decimal)BatteryCheckSetting.StartPoint.X;
|
nudStartY.Value = (decimal)BatteryCheckSetting.StartPoint.Y;
|
nudEndX.Value = (decimal)BatteryCheckSetting.EndPoint.X;
|
nudEndY.Value = (decimal)BatteryCheckSetting.EndPoint.Y;
|
|
RegisterEvent();
|
}
|
|
private void nudStartY_ValueChanged(object sender, EventArgs e)
|
{
|
BatteryCheckSetting.StartPoint = new PointF((float)nudStartX.Value, (float)nudStartY.Value);
|
BatteryCheckSetting.EndPoint = new PointF((float)nudEndX.Value, (float)nudEndY.Value);
|
|
halconDisplay.ROIController.ROIList = new System.Collections.ArrayList();
|
halconDisplay.ROIController.ROIList.Add(BatteryCheckSetting.Rect);
|
|
halconDisplay.Refresh();
|
}
|
|
private void btnSave_Click(object sender, EventArgs e)
|
{
|
BatteryCheckSetting.IsEnabled = true;
|
|
BatteryCheckSetting.MinArea = (int)nudMin.Value;
|
BatteryCheckSetting.MaxArea = (int)nudMax.Value;
|
|
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, SettingPath);
|
|
using (StreamWriter writer = new StreamWriter(path, false, Encoding.UTF8))
|
{
|
writer.WriteLine(JsonConvert.SerializeObject(BatteryCheckSetting));
|
writer.Flush();
|
writer.Close();
|
}
|
|
OnSettingSaved?.Invoke(BatteryCheckSetting);
|
|
MessageBox.Show("配置保存完成!");
|
}
|
|
private void CtrlSetBatteryCheckROI_Load(object sender, EventArgs e)
|
{
|
LoadSetting();
|
}
|
|
private void LoadSetting()
|
{
|
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, SettingPath);
|
|
if (File.Exists(path))
|
{
|
using (StreamReader reader = new StreamReader(path, System.Text.Encoding.UTF8))
|
{
|
var dataLine = reader.ReadToEnd();
|
|
BatteryCheckSetting = JsonConvert.DeserializeObject<BatteryCheckSetting>(dataLine);
|
}
|
}
|
else
|
{
|
BatteryCheckSetting = new BatteryCheckSetting();
|
}
|
|
halconDisplay.ROIController.ROIList = new System.Collections.ArrayList();
|
halconDisplay.ROIController.ROIList.Add(BatteryCheckSetting.Rect);
|
SetROILabel();
|
|
nudMin.Value = BatteryCheckSetting.MinArea;
|
nudMax.Value = BatteryCheckSetting.MaxArea;
|
}
|
|
private void RegisterEvent()
|
{
|
nudStartX.ValueChanged += nudStartY_ValueChanged;
|
nudStartY.ValueChanged += nudStartY_ValueChanged;
|
nudEndX.ValueChanged += nudStartY_ValueChanged;
|
nudEndY.ValueChanged += nudStartY_ValueChanged;
|
}
|
|
private void RemoveEvent()
|
{
|
nudStartX.ValueChanged -= nudStartY_ValueChanged;
|
nudStartY.ValueChanged -= nudStartY_ValueChanged;
|
nudEndX.ValueChanged -= nudStartY_ValueChanged;
|
nudEndY.ValueChanged -= nudStartY_ValueChanged;
|
}
|
|
#region 图片载入
|
private void btnLoadSingleImage_Click(object sender, EventArgs e)
|
{
|
OpenFileDialog ofd = new OpenFileDialog();
|
ofd.Filter = "TIFF图片|*.tif|JPG图片|*.jpg|BMP图片|*.bmp";
|
ofd.Multiselect = false;
|
if (ofd.ShowDialog() != DialogResult.OK)
|
{
|
return;
|
}
|
|
LoadImagFile(ofd.FileName);
|
}
|
|
private void LoadImagFile(string imgPath)
|
{
|
HImage img = new HImage();
|
img.ReadImage(imgPath);
|
halconDisplay.Image = img;
|
halconDisplay.Refresh();
|
|
txtImgPath.Text = imgPath;
|
}
|
#endregion
|
|
int imgIndex = 0;
|
int ImgIndex
|
{
|
get { return imgIndex; }
|
set
|
{
|
imgIndex = value;
|
|
btnPre.Visible = imgIndex > 0;
|
btnNext.Visible = (imgFileList.Count > 0 && imgIndex < imgFileList.Count - 1);
|
|
if (imgFileList.Count > 0 && imgIndex >= 0 && imgIndex < imgFileList.Count)
|
{
|
LoadImagFile(imgFileList[imgIndex]);
|
}
|
}
|
}
|
|
List<string> imgFileList = new List<string>();
|
|
private void btnLoadImageFolder_Click(object sender, EventArgs e)
|
{
|
List<string> imgExtensions = new List<string>() { ".BMP", ".JPG", ".TIF" };
|
FolderBrowserDialog fbd = new FolderBrowserDialog();
|
|
if (fbd.ShowDialog() == DialogResult.OK)
|
{
|
imgFileList = Directory.GetFiles(fbd.SelectedPath).Where(u =>
|
{
|
string s = u.ToUpper();
|
|
return imgExtensions.Any(ext => s.EndsWith(ext));
|
}).ToList();
|
|
if (imgFileList.Count == 0)
|
{
|
MessageBox.Show($"当前文件夹中没有图片文件,目前支持{string.Join(",", imgExtensions)}格式");
|
return;
|
}
|
|
ImgIndex = 0;
|
}
|
}
|
|
private void btnPre_Click(object sender, EventArgs e)
|
{
|
ImgIndex--;
|
}
|
|
private void btnNext_Click(object sender, EventArgs e)
|
{
|
ImgIndex++;
|
}
|
}
|
}
|
|
public class BatteryCheckSetting
|
{
|
private ROIRectangle1 rect = new ROIRectangle1();
|
[JsonIgnore]
|
public ROIRectangle1 Rect
|
{
|
get
|
{
|
rect.SetRect(StartPoint, EndPoint);
|
return rect;
|
}
|
set
|
{
|
var data = rect.getModelData().DArr;
|
StartPoint = new PointF((float)data[1], (float)data[0]);
|
EndPoint = new PointF((float)data[3], (float)data[2]);
|
}
|
}
|
|
public PointF StartPoint { get; set; } = new PointF(50, 50);
|
|
public PointF EndPoint { get; set; } = new PointF(150, 150);
|
public int MinArea { get; set; } = 0;
|
public int MaxArea { get; set; } = 0;
|
|
public bool IsEnabled { get; set; } = false;
|
}
|