123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace MeterVision.Mark
- {
- /// <summary>
- /// UCMark.xaml 的交互逻辑
- /// </summary>
- public partial class UCMark : UserControl
- {
- //当前的放大倍率
- //private double _sacle_value = 1.5f;
- //标注的类型: 4:表盘; 3:数字域; 2:首位指针; 0:未选中
- private int _mark_type = 0;
- //是否已加载图片
- //private bool _loadedImage = false;
- private MarkManager _markManager;
- private int _meterType = -1;
- public int MeterType
- {
- private get => _meterType;
- set
- {
- //if(_meterType != value)
- {
- _meterType = value;
- SetFeatureRegion(_meterType);
- }
- }
- }
- public event Action<string,int> MeterRegion_MarkFinished;
- public event Action<string,int> FeatureRegion_MarkFinished;
- BitmapImage bitmap;
- public UCMark()
- {
- InitializeComponent();
- _mark_type = 0;
- _markManager = new MarkManager(drawingCanvas);
- _markManager.MarkError += _markManager_MarkError;
- _markManager.FinishMark += _markManager_FinishMark;
- //初始化一些控件的默认显示值
- txtCurPoint.Text = "";
- this.Loaded += UCMark_Loaded;
- }
- private void UCMark_Loaded(object sender, RoutedEventArgs e)
- {
- //throw new NotImplementedException();
- //ChangeScale();
- }
- private void PnlCanvas_SizeChanged(object sender, SizeChangedEventArgs e)
- {
- ChangeScale();
- if (_markManager != null)
- {
- _markManager.ClearMark();
- }
- }
- public void SetImagePath(string imgPath)
- {
- //MeterType = meterType;
- //加载照片
- LoadImage(imgPath);
- //设置放大倍率
- //_sacle_value = 1.92f;
- //SetScaleTrnsform(_sacle_value);
- }
- //private void SetScaleTrnsform(double scale)
- //{
- // scaleTransform.ScaleX = scale;
- // scaleTransform.ScaleY = scale;
- // _markManager.ClearMark();
- //}
- private void LoadImage(string filePath)
- {
- // 创建一个BitmapImage对象,并加载图片
- bitmap = new BitmapImage(new Uri(filePath));
- if (bitmap.Width != 320 || bitmap.Height != 240)
- {
- MessageBox.Show(Application.Current.MainWindow, "图片尺寸必须为320*240!", "提示",
- MessageBoxButton.OK, MessageBoxImage.Information);
- return;
- }
- imgControl.Source = bitmap; // 将图片显示到Image控件中
- drawingCanvas.Width = bitmap.PixelWidth;
- drawingCanvas.Height = bitmap.PixelHeight;
- _markManager.InitMark();
- }
- private void ChangeScale()
- {
- // 获取图片原始尺寸
- double imageWidth = bitmap.PixelWidth;
- double imageHeight = bitmap.PixelHeight;
- // 获取pnlCanvas的实际可用区域(考虑Margin和Padding)
- double containerWidth = pnlCanvas.ActualWidth - pnlCanvas.Padding.Left - pnlCanvas.Padding.Right;
- double containerHeight = pnlCanvas.ActualHeight - pnlCanvas.Padding.Top - pnlCanvas.Padding.Bottom;
- // 计算缩放比例,留5%边距
- double scaleX = (containerWidth * 0.95) / imageWidth;
- double scaleY = (containerHeight * 0.95) / imageHeight;
- double scale = Math.Min(scaleX, scaleY);
- // 应用缩放
- scaleTransform.ScaleX = scale;
- scaleTransform.ScaleY = scale;
- // 计算图片缩放后的尺寸
- double scaledWidth = imageWidth * scale;
- double scaledHeight = imageHeight * scale;
- // 将图片居中于Canvas
- //Canvas.SetLeft(imgControl, (containerWidth - scaledWidth) / 2);
- //Canvas.SetTop(imgControl, (containerHeight - scaledHeight) / 2);
- // 关键步骤:强制Canvas与Image的渲染区域一致
- drawingCanvas.Width = scaledWidth; //containerWidth; // 使Canvas宽度与Border可用区域一致
- drawingCanvas.Height = scaledHeight; //containerHeight; // 使Canvas高度与Border可用区域一致
- }
- private void _markManager_FinishMark(int pointCount, List<Point> points, int angle)
- {
- double scaleX = scaleTransform.ScaleX;
- double scaleY = scaleTransform.ScaleY;
- string regions = "";
- if (_mark_type == 4)
- {
- //表盘标注完成(获取到2组坐标)
- Point point1 = points[0];
- Point point2 = points[1];
- int xMin = (int)(point1.X / scaleX);
- int yMin = (int)(point1.Y / scaleY);
- int xMax = (int)(point2.X / scaleX);
- int yMax = (int)(point2.Y / scaleY);
- regions = $"{xMin},{yMin} {xMax},{yMax}";
- MeterRegion_MarkFinished?.Invoke(regions,MeterType);
- }
- else if (_mark_type == 3)
- {
- //数字区域标注完成
- regions = $"" +
- $"{(int)(points[0].X / scaleX)}," +
- $"{(int)(points[0].Y / scaleY)} " +
- $"{(int)(points[1].X / scaleX)}," +
- $"{(int)(points[1].Y / scaleY)} " +
- $"{(int)(points[2].X / scaleX)}," +
- $"{(int)(points[2].Y / scaleY)} " +
- $"{(int)(points[3].X / scaleX)}," +
- $"{(int)(points[3].Y / scaleY)}";
- FeatureRegion_MarkFinished?.Invoke(regions,MeterType);
- }
- else if (_mark_type == 2)
- {
- //指针首位标注完成
- regions = $"" +
- $"{(int)(points[0].X / scaleX)}," +
- $"{(int)(points[0].Y / scaleY)} " +
- $"{(int)(points[1].X / scaleX)}," +
- $"{(int)(points[1].Y / scaleY)}";
- FeatureRegion_MarkFinished?.Invoke(regions, MeterType);
- }
- }
- private void _markManager_MarkError(string message)
- {
- MessageBox.Show(message);
- }
- private void SetFeatureRegion(int meterType)
- {
- if(meterType == 1 || meterType == 3)
- {
- pnlMarkFunc.Visibility = Visibility.Visible;
- btnFeatureRegion.Visibility = txtFeatureRegion.Visibility = Visibility.Visible;
- btnFeatureRegion.Content = "标注数据域(4点)";
- }
- else if(meterType == 2)
- {
- pnlMarkFunc.Visibility = Visibility.Visible;
- btnFeatureRegion.Visibility = txtFeatureRegion.Visibility = Visibility.Visible;
- btnFeatureRegion.Content = "标注首位指针(2点)";
- }
- else
- {
- pnlMarkFunc.Visibility = Visibility.Hidden;
- //btnFeatureRegion.Visibility = txtFeatureRegion.Visibility = Visibility.Collapsed;
- }
- }
- private void SetBtnMark(int mark_type)
- {
- btnMeterRegion.Foreground = btnFeatureRegion.Foreground = Brushes.Black;
- if(mark_type == 4)
- {
- btnMeterRegion.Foreground = Brushes.Red;
- }
- else if(mark_type == 2 || mark_type == 3)
- {
- btnFeatureRegion.Foreground = Brushes.Red;
- }
- }
- private void BtnMeterRegion_Click(object sender, RoutedEventArgs e)
- {
- if (MeterType <= 0) return;
- if(_mark_type != 4)
- {
- _mark_type = 4;
- _markManager.ReadyMark(_mark_type);
- }
- else
- {
- _mark_type = 0;
- _markManager.StopMark();
- }
- SetBtnMark(_mark_type);
- }
- private void BtnFeatureRegion_Click(object sender, RoutedEventArgs e)
- {
- if(MeterType == 1 || MeterType == 3)
- {
- //数字域标注
- if(_mark_type != 3)
- {
- _mark_type = 3;
- _markManager.ReadyMark(_mark_type);
- }
- else
- {
- _mark_type = 0;
- _markManager.StopMark();
- }
- }
- else if(MeterType == 2)
- {
- //首位指针标注
- if(_mark_type != 2)
- {
- _mark_type = 2;
- _markManager.ReadyMark(_mark_type);
- }
- else
- {
- _mark_type = 2;
- _markManager.StopMark();
- }
- }
- SetBtnMark(_mark_type);
- }
- private void BtnClearMark_Click(object sender, RoutedEventArgs e)
- {
- _markManager.ClearMark();
- }
- private void DrawingCanvas_MouseDown(object sender, MouseButtonEventArgs e)
- {
- if (_markManager.IsMarking && _mark_type > 0)
- {
- //获取点击位置
- Point clickPoint = e.GetPosition(drawingCanvas);
- _markManager.PointMark(clickPoint);
- }
- }
- private void DrawingCanvas_MouseMove(object sender, MouseEventArgs e)
- {
- Point mousePos = e.GetPosition(drawingCanvas);
- // 将鼠标位置转换为原图的坐标
- double originalX = mousePos.X / scaleTransform.ScaleX;
- double originalY = mousePos.Y / scaleTransform.ScaleY;
- int mouseX = (int)originalX;
- int mouseY = (int)originalY;
- if(_markManager != null && _markManager.PointCount == 3) {
- _markManager.MouseDrawLine(mousePos);
- }
- //this.Title = $"Mouse Position : {mouseX},{mouseY}";
- txtCurPoint.Text = $"鼠标坐标: {mouseX},{mouseY}";
- }
- private void DrawingCanvas_MouseUp(object sender, MouseButtonEventArgs e)
- {
- }
- private void DrawingCanvas_MouseEnter(object sender, MouseEventArgs e)
- {
- if (_mark_type > 0)
- {
- this.Cursor = Cursors.Hand;
- }
- else
- {
- this.Cursor = Cursors.Arrow;
- }
- }
- private void DrawingCanvas_MouseLeave(object sender, MouseEventArgs e)
- {
- txtCurPoint.Text = "";
- this.Cursor = Cursors.Arrow;
- }
- //-----------------------------------------------------------------------
- }
- }
|