-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
4387 lines (3760 loc) · 162 KB
/
MainWindow.xaml.cs
File metadata and controls
4387 lines (3760 loc) · 162 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using AForge.Imaging.Filters;
using Newtonsoft.Json;
using ShowWrite.Models;
using ShowWrite.Services;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using Button = System.Windows.Controls.Button;
using MessageBox = System.Windows.MessageBox;
using WinBrush = System.Windows.Media.Brush;
using WinBrushes = System.Windows.Media.Brushes;
using WinButton = System.Windows.Controls.Button;
using WinComboBox = System.Windows.Controls.ComboBox;
using WinCursors = System.Windows.Input.Cursors;
using WinOrientation = System.Windows.Controls.Orientation;
using WinMouseEventArgs = System.Windows.Input.MouseEventArgs;
using WinMouseButtonEventArgs = System.Windows.Input.MouseButtonEventArgs;
using WinPoint = System.Windows.Point;
using WinImage = System.Windows.Controls.Image;
using System.Windows.Controls.Primitives;
using ListBox = System.Windows.Controls.ListBox;
namespace ShowWrite
{
public partial class MainWindow : Window
{
// 管理器实例
private readonly VideoService _videoService = new();
private DrawingManager _drawingManager;
private CameraManager _cameraManager;
private PanZoomManager _panZoomManager;
private MemoryManager _memoryManager;
private FrameProcessor _frameProcessor;
private TouchManager _touchManager;
private LogManager _logManager;
private PhotoPopupManager _photoPopupManager;
private Services.DeviceConnectionManager _deviceConnectionManager;
private LanguageManager _languageManager;
// 数据集合
private readonly ObservableCollection<PhotoWithStrokes> _photos = new();
private StrokeCollection _liveStrokes = new StrokeCollection();
// 状态变量
private bool _isLiveMode = true;
private bool _isClosing = false;
private AppConfig config = new AppConfig();
// 视频帧接收状态
private bool _isFirstFrameProcessed = false;
// UI相关
private SolidColorBrush _noCameraBackground = new SolidColorBrush(System.Windows.Media.Color.FromRgb(40, 40, 40));
private Button _currentSelectedColorButton = null;
private string _currentPenColor = "Black";
// 配置文件路径
private readonly string configPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config.json");
// 双击检测
private DateTime _lastClickTime = DateTime.MinValue;
private const int DoubleClickDelay = 300; // 毫秒
// 画面调节参数
private double _brightness = 0.0;
private double _contrast = 0.0;
private int _rotation = 0;
private bool _mirrorHorizontal = false;
private bool _mirrorVertical = false;
// 梯形校正相关
private bool _isPerspectiveCorrectionMode = false;
private bool _isEnteringCorrectionMode = false; // 防止重复进入的保护机制
private System.Drawing.Bitmap _originalCorrectionFrame = null;
private int _draggingPointIndex = -1;
private WinPoint[] _correctionPoints = new WinPoint[4];
private bool _isCorrectionModeInitialized = false;
// 启动图相关 - 由App.xaml.cs控制
private bool _shouldShowSplash = false;
// 主题相关
private ResourceDictionary _currentTheme;
// 清屏确认滑块相关
private bool _isSliderDragging = false;
private double _sliderStartX = 0;
private double _sliderMaxDistance = 0;
private bool _sliderReachedEnd = false;
/// <summary>
/// 主构造函数 - 由App.xaml.cs调用
/// </summary>
/// <param name="shouldShowSplash">是否显示启动图</param>
public MainWindow(bool shouldShowSplash = false)
{
_shouldShowSplash = shouldShowSplash;
// 如果App.xaml.cs要求显示启动图,这里才显示
if (_shouldShowSplash)
{
ShowSplashScreen();
}
// 初始化日志系统
Logger.Initialize(minLogLevel: LogLevel.Debug);
Logger.Info("MainWindow", "主窗口初始化开始");
_logManager = new LogManager();
InitializeComponent();
// 初始化管理器
InitializeManagers();
// 初始化UI和数据绑定
InitializeUI();
// 加载配置和启动
LoadAndStart();
// 添加窗口加载完成事件
this.Loaded += MainWindow_Loaded;
this.SizeChanged += MainWindow_SizeChanged;
Logger.Info("MainWindow", "主窗口初始化完成");
// 如果显示了启动图,现在关闭它
if (_shouldShowSplash)
{
CloseSplashScreen();
}
}
/// <summary>
/// 默认构造函数 - 保留供WPF设计器使用
/// </summary>
public MainWindow() : this(false)
{
}
/// <summary>
/// 显示启动图
/// </summary>
private void ShowSplashScreen()
{
try
{
Logger.Debug("MainWindow", "显示启动图");
// 注意:这里我们不实际创建启动窗口
// 启动图由App.xaml.cs控制
Logger.Debug("MainWindow", "启动图由App.xaml.cs控制");
}
catch (Exception ex)
{
Logger.Error("MainWindow", $"显示启动图失败: {ex.Message}", ex);
}
}
/// <summary>
/// 关闭启动图
/// </summary>
private void CloseSplashScreen()
{
try
{
Logger.Debug("MainWindow", "关闭启动图");
// 启动图由App.xaml.cs控制,这里只是记录
}
catch (Exception ex)
{
Logger.Error("MainWindow", $"关闭启动图失败: {ex.Message}", ex);
}
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
try
{
// 记录进程ID
var process = System.Diagnostics.Process.GetCurrentProcess();
Logger.Info("MainWindow", $"主窗口加载完成,进程ID: {process.Id}, 进程名: {process.ProcessName}");
// 确保校正画布有正确的尺寸
Dispatcher.BeginInvoke(new Action(() =>
{
if (CorrectionCanvas != null && FindName("VideoArea") != null)
{
var videoArea = (Grid)FindName("VideoArea");
CorrectionCanvas.Width = videoArea.ActualWidth;
CorrectionCanvas.Height = videoArea.ActualHeight;
}
}), DispatcherPriority.Loaded);
// 确保主窗口在前台
this.Activate();
this.Topmost = true;
this.Topmost = false;
this.Focus();
// 检查是否有多余进程
CheckForDuplicateProcesses();
}
catch (Exception ex)
{
Logger.Error("MainWindow", $"主窗口加载事件失败: {ex.Message}", ex);
}
}
/// <summary>
/// 检查重复进程
/// </summary>
private void CheckForDuplicateProcesses()
{
try
{
var currentProcess = System.Diagnostics.Process.GetCurrentProcess();
var processes = System.Diagnostics.Process.GetProcessesByName(currentProcess.ProcessName);
if (processes.Length > 1)
{
Logger.Warning("MainWindow", $"检测到多个进程: {processes.Length} 个同名进程");
foreach (var process in processes)
{
if (process.Id != currentProcess.Id)
{
Logger.Warning("MainWindow", $"发现其他进程: ID={process.Id}, 启动时间={process.StartTime}");
}
}
}
else
{
Logger.Info("MainWindow", "进程检查正常: 只有一个进程运行");
}
}
catch (Exception ex)
{
Logger.Error("MainWindow", $"检查重复进程失败: {ex.Message}", ex);
}
}
private void SwitchTheme(bool useDarkTheme)
{
// 清除现有资源
this.Resources.MergedDictionaries.Clear();
// 创建新的资源字典
var resourceDictionary = new ResourceDictionary();
// 根据主题加载对应的资源文件
if (useDarkTheme)
{
resourceDictionary.MergedDictionaries.Add(
new ResourceDictionary() { Source = new Uri("themes/DarkTheme.xaml", UriKind.Relative) });
}
else
{
resourceDictionary.MergedDictionaries.Add(
new ResourceDictionary() { Source = new Uri("themes/LightTheme.xaml", UriKind.Relative) });
}
// 添加画笔设置按钮样式
var penSettingsStyle = new Style(typeof(Button));
penSettingsStyle.Setters.Add(new Setter(Button.WidthProperty, 32.0));
penSettingsStyle.Setters.Add(new Setter(Button.HeightProperty, 32.0));
penSettingsStyle.Setters.Add(new Setter(Button.MarginProperty, new Thickness(2)));
penSettingsStyle.Setters.Add(new Setter(Button.BorderThicknessProperty, new Thickness(1)));
penSettingsStyle.Setters.Add(new Setter(Button.BorderBrushProperty, new SolidColorBrush(System.Windows.Media.Color.FromRgb(85, 85, 85))));
resourceDictionary.Add("PenSettingsButtonStyle", penSettingsStyle);
// 应用新的资源字典
this.Resources = resourceDictionary;
}
private void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
{
// 如果在校正模式下,重新初始化校正点位置
if (_isPerspectiveCorrectionMode && _isCorrectionModeInitialized)
{
InitializeCorrectionPoints();
}
// 照片栏固定在右侧,不需要重新定位
}
private void InitializeManagers()
{
try
{
Logger.Info("MainWindow", "开始初始化管理器");
if (config == null) config = new AppConfig();
_drawingManager = new DrawingManager((InkCanvas)FindName("Ink"), (Grid)FindName("VideoArea"), this);
var eraserOverlayCanvas = (System.Windows.Controls.Canvas)FindName("EraserOverlayCanvas");
if (eraserOverlayCanvas != null)
{
_drawingManager.InitializeEraserOverlay(eraserOverlayCanvas);
}
var overlayInkCanvas = (InkCanvas)FindName("OverlayInkCanvas");
var zoomTransform = (ScaleTransform)FindName("ZoomTransform");
var panTransform = (TranslateTransform)FindName("PanTransform");
if (overlayInkCanvas != null && zoomTransform != null && panTransform != null)
{
_drawingManager.SetOverlayInkCanvas(overlayInkCanvas, zoomTransform, panTransform);
Logger.Info("MainWindow", "OverlayInkCanvas 已设置到 DrawingManager");
}
_cameraManager = new CameraManager(_videoService, config);
_memoryManager = new MemoryManager();
_frameProcessor = new FrameProcessor(_cameraManager, _memoryManager);
_panZoomManager = new PanZoomManager((ScaleTransform)FindName("ZoomTransform"), (TranslateTransform)FindName("PanTransform"), (Grid)FindName("VideoArea"), _drawingManager);
_touchManager = new TouchManager(_drawingManager);
InitializePhotoPopupManager();
SubscribeToEvents();
Logger.Info("MainWindow", "管理器初始化完成");
}
catch (Exception ex)
{
Logger.Error("MainWindow", $"初始化管理器失败: {ex.Message}", ex);
throw;
}
}
/// <summary>
/// 初始化照片悬浮窗管理器
/// </summary>
private void InitializePhotoPopupManager()
{
try
{
_photoPopupManager = new PhotoPopupManager(
null,
PhotoList,
this,
_photos,
_drawingManager,
_cameraManager,
_memoryManager,
_frameProcessor,
_panZoomManager,
_logManager);
// 订阅照片悬浮窗管理器事件
_photoPopupManager.PhotoSelected += OnPhotoSelected;
_photoPopupManager.BackToLiveRequested += OnBackToLiveRequested;
_photoPopupManager.SaveImageRequested += OnSaveImageRequested;
Logger.Info("MainWindow", "照片悬浮窗管理器初始化完成");
}
catch (Exception ex)
{
Logger.Error("MainWindow", $"初始化照片悬浮窗管理器失败: {ex.Message}", ex);
throw;
}
}
/// <summary>
/// 照片选择事件处理(修复版)
/// </summary>
private void OnPhotoSelected(PhotoWithStrokes photo)
{
Dispatcher.Invoke(() =>
{
try
{
if (photo == null)
{
Logger.Warning("MainWindow", "照片选择事件收到空照片对象");
return;
}
if (photo.Image == null)
{
Logger.Warning("MainWindow", "照片对象的Image属性为空");
return;
}
Logger.Info("MainWindow", $"切换到照片查看模式,照片尺寸: {photo.Image.Width}x{photo.Image.Height}");
// 1. 先停止摄像头,确保视频帧不再生成
if (_cameraManager != null && _cameraManager.IsCameraAvailable)
{
_cameraManager.PauseCamera();
Logger.Debug("MainWindow", "摄像头已暂停");
}
// 2. 设置到非实时模式
_isLiveMode = false;
// 3. 显示选中的照片
var videoImage = (WinImage)FindName("VideoImage");
var videoArea = (Grid)FindName("VideoArea");
if (videoImage != null)
{
videoImage.Source = photo.Image;
}
if (videoArea != null)
{
videoArea.Background = WinBrushes.Transparent;
}
// 4. 切换到照片对应的笔迹
if (photo.Strokes != null)
{
_drawingManager.SwitchToPhotoStrokes(photo.Strokes);
Logger.Debug("MainWindow", $"已切换到照片笔迹,包含 {photo.Strokes.Count} 个笔迹");
}
else
{
Logger.Warning("MainWindow", "照片没有关联的笔迹");
_drawingManager.SwitchToPhotoStrokes(new StrokeCollection());
}
// 5. 更新UI状态
UpdateUIModeForPhotoView();
// 6. 触发内存清理
Dispatcher.BeginInvoke(new Action(() =>
{
_memoryManager?.TriggerMemoryCleanup();
}), DispatcherPriority.Background);
Logger.Info("MainWindow", "已成功切换到照片查看模式");
}
catch (Exception ex)
{
Logger.Error("MainWindow", $"处理照片选择事件失败: {ex.Message}", ex);
// 出错时尝试恢复实时模式
try
{
_isLiveMode = true;
if (_cameraManager != null && _cameraManager.IsCameraAvailable)
{
_cameraManager.RestartCamera();
}
var videoImage = (WinImage)FindName("VideoImage");
if (videoImage != null)
{
videoImage.Source = null;
}
}
catch (Exception innerEx)
{
Logger.Error("MainWindow", $"恢复实时模式失败: {innerEx.Message}", innerEx);
}
}
});
}
/// <summary>
/// 为照片查看模式更新UI状态
/// </summary>
private void UpdateUIModeForPhotoView()
{
try
{
// 1. 设置窗口标题显示照片模式
this.Title = $"ShowWrite - 照片查看模式";
// 2. 关闭可能的悬浮窗
if (PenSettingsPopup.IsOpen)
{
PenSettingsPopup.IsOpen = false;
}
Logger.Debug("MainWindow", "UI状态已更新为照片查看模式");
}
catch (Exception ex)
{
Logger.Error("MainWindow", $"更新UI状态失败: {ex.Message}", ex);
}
}
/// <summary>
/// 返回实时模式请求处理(修复版)
/// </summary>
private void OnBackToLiveRequested()
{
Dispatcher.Invoke(() =>
{
try
{
// ---------------------------------------------------------
// [新增修复 3] 清除列表选中状态
// 这样下次点击同一张照片时,SelectionChanged 事件才能再次触发
if (PhotoList != null)
{
PhotoList.SelectedIndex = -1;
}
// ---------------------------------------------------------
// 1. 重置视频帧记录状态
_isFirstFrameProcessed = false;
Logger.ResetVideoFrameLogging();
// 2. 重新启动摄像头
if (_cameraManager != null && _cameraManager.IsCameraAvailable)
{
_cameraManager.RestartCamera();
}
// 3. 设置为实时模式
_isLiveMode = true;
// 4. 清空视频图像,让摄像头帧重新显示
var videoImage = (WinImage)FindName("VideoImage");
var videoArea = (Grid)FindName("VideoArea");
if (videoImage != null)
{
videoImage.Source = null;
}
if (videoArea != null)
{
videoArea.Background = _noCameraBackground;
}
// 5. 切换回实时笔迹
_drawingManager.SwitchToPhotoStrokes(_liveStrokes);
// 6. 更新UI状态
this.Title = "ShowWrite";
Logger.Info("MainWindow", "已返回实时模式");
}
catch (Exception ex)
{
Logger.Error("MainWindow", $"处理返回实时模式请求失败: {ex.Message}", ex);
}
});
}
/// <summary>
/// 保存图片请求处理
/// </summary>
private void OnSaveImageRequested()
{
Dispatcher.Invoke(() =>
{
try
{
// 调用原有的保存图片逻辑
SaveImage_Click(null, null);
Logger.Debug("MainWindow", "保存图片请求处理完成");
}
catch (Exception ex)
{
Logger.Error("MainWindow", $"处理保存图片请求失败: {ex.Message}", ex);
}
});
}
/// <summary>
/// 订阅管理器事件
/// </summary>
private void SubscribeToEvents()
{
try
{
Logger.Debug("MainWindow", "开始订阅事件");
// 摄像头帧事件
_cameraManager.OnNewFrameProcessed += OnCameraFrameReceived;
// 绘制管理器事件
_drawingManager.OnSDKTouchAreaChanged += OnSDKTouchAreaChanged;
// 触控管理器事件
_touchManager.OnTouchCountChanged += OnTouchCountChanged;
_touchManager.OnTouchAreaChanged += OnTouchAreaChanged;
_touchManager.OnTouchCenterChanged += OnTouchCenterChanged;
Logger.Debug("MainWindow", "事件订阅完成");
}
catch (Exception ex)
{
Logger.Error("MainWindow", $"订阅事件失败: {ex.Message}", ex);
}
}
/// <summary>
/// 取消所有事件订阅
/// </summary>
private void UnsubscribeAllEvents()
{
try
{
Logger.Info("MainWindow", "开始取消所有事件订阅");
// 取消摄像头管理器事件
if (_cameraManager != null)
{
_cameraManager.OnNewFrameProcessed -= OnCameraFrameReceived;
}
// 取消绘制管理器事件
if (_drawingManager != null)
{
_drawingManager.OnSDKTouchAreaChanged -= OnSDKTouchAreaChanged;
}
// 取消触控管理器事件
if (_touchManager != null)
{
_touchManager.OnTouchCountChanged -= OnTouchCountChanged;
_touchManager.OnTouchAreaChanged -= OnTouchAreaChanged;
_touchManager.OnTouchCenterChanged -= OnTouchCenterChanged;
}
// 取消照片悬浮窗管理器事件
if (_photoPopupManager != null)
{
_photoPopupManager.PhotoSelected -= OnPhotoSelected;
_photoPopupManager.BackToLiveRequested -= OnBackToLiveRequested;
_photoPopupManager.SaveImageRequested -= OnSaveImageRequested;
}
// 取消窗口事件
this.Loaded -= MainWindow_Loaded;
this.SizeChanged -= MainWindow_SizeChanged;
Logger.Info("MainWindow", "所有事件订阅已取消");
}
catch (Exception ex)
{
Logger.Error("MainWindow", $"取消事件订阅失败: {ex.Message}", ex);
}
}
/// <summary>
/// 初始化UI和数据绑定
/// </summary>
private void InitializeUI()
{
try
{
Logger.Debug("MainWindow", "开始初始化UI");
// 初始化语言管理器
_languageManager = LanguageManager.Instance;
_languageManager.LanguageChanged += UpdateLanguageUI;
// 初始化实时模式笔迹
_drawingManager.SwitchToPhotoStrokes(_liveStrokes);
// 应用窗口设置
WindowStyle = WindowStyle.None;
WindowState = config.StartMaximized ? WindowState.Maximized : WindowState.Normal;
// 应用绘制管理器配置
_drawingManager.ApplyConfig(config);
// 初始化UI组件
InitializePenSettingsPopup();
InitializeTouchInfoPopup();
// 初始化画笔颜色选择器
InitializePenColorSelector();
if (PhotoList != null)
{
PhotoList.SelectionChanged -= PhotoList_SelectionChanged; // 防止重复绑定
PhotoList.SelectionChanged += PhotoList_SelectionChanged;
}
// 开始触控跟踪
_touchManager.StartTracking();
// 更新语言UI
UpdateLanguageUI();
Logger.Debug("MainWindow", "UI初始化完成");
}
catch (Exception ex)
{
Logger.Error("MainWindow", $"初始化UI失败: {ex.Message}", ex);
}
}
private void PhotoList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// 如果选中项是 PhotoWithStrokes 类型,则调用切换逻辑
if (PhotoList.SelectedItem is PhotoWithStrokes photo)
{
// 调用现有的照片选择处理逻辑
OnPhotoSelected(photo);
// 确保照片栏保持展开
var photoPanelBorder = FindName("PhotoPanelBorder") as Border;
if (photoPanelBorder != null && photoPanelBorder.Visibility != Visibility.Visible)
{
photoPanelBorder.Visibility = Visibility.Visible;
}
}
}
/// <summary>
/// 加载配置和启动应用
/// </summary>
private void LoadAndStart()
{
try
{
Logger.Debug("MainWindow", "开始加载配置和启动");
// 加载配置
LoadConfig();
// 应用主题
ApplyTheme();
// 检查摄像头可用性
if (!_cameraManager.CheckCameraAvailability())
{
ShowNoCameraBackground();
}
else if (config.AutoStartCamera)
{
StartCameraWithFallback();
// 启动后应用摄像头配置
ApplyCameraConfigOnStartup();
}
// 显示 TouchSDK 状态
UpdateTouchSDKStatus();
// 调试图层可见性
TestLayerVisibility();
Logger.Debug("MainWindow", "配置加载和启动完成");
}
catch (Exception ex)
{
Logger.Error("MainWindow", $"加载配置和启动失败: {ex.Message}", ex);
}
}
#region 初始化方法
/// <summary>
/// 初始化画笔设置悬浮窗
/// </summary>
private void InitializePenSettingsPopup()
{
try
{
// 设置初始笔宽并保存原始宽度
_panZoomManager.SetOriginalPenWidth(_drawingManager.UserPenWidth);
PenWidthSlider.Value = _drawingManager.UserPenWidth;
PenWidthValue.Text = _drawingManager.UserPenWidth.ToString("0");
Logger.Debug("MainWindow", "画笔设置悬浮窗初始化完成");
}
catch (Exception ex)
{
Logger.Error("MainWindow", $"初始化画笔设置悬浮窗失败: {ex.Message}", ex);
}
}
/// <summary>
/// 初始化触控信息悬浮窗
/// </summary>
private void InitializeTouchInfoPopup()
{
try
{
// 设置悬浮窗初始位置在右上角
TouchInfoPopup.HorizontalOffset = SystemParameters.PrimaryScreenWidth - 200;
TouchInfoPopup.VerticalOffset = 50;
Logger.Debug("MainWindow", "触控信息悬浮窗初始化完成");
}
catch (Exception ex)
{
Logger.Error("MainWindow", $"初始化触控信息悬浮窗失败: {ex.Message}", ex);
}
}
/// <summary>
/// 图层可见性测试方法
/// </summary>
private void TestLayerVisibility()
{
Dispatcher.BeginInvoke(new Action(() =>
{
try
{
Logger.Debug("MainWindow", "=== 图层可见性测试 ===");
var videoArea = (Grid)FindName("VideoArea");
Logger.Debug("MainWindow", $"VideoArea 子元素数量: {VisualTreeHelper.GetChildrenCount(videoArea)}");
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(videoArea); i++)
{
var child = VisualTreeHelper.GetChild(videoArea, i);
Logger.Debug("MainWindow", $"子元素 {i}: {child.GetType().Name}, 可见性: {((UIElement)child).Visibility}");
}
var videoImage = (WinImage)FindName("VideoImage");
var ink = (InkCanvas)FindName("Ink");
Logger.Debug("MainWindow", $"VideoImage 源: {videoImage?.Source}");
Logger.Debug("MainWindow", $"VideoImage 渲染尺寸: {videoImage?.RenderSize}");
Logger.Debug("MainWindow", $"InkCanvas 背景: {ink?.Background}");
Logger.Debug("MainWindow", $"InkCanvas 默认绘制属性: {ink?.DefaultDrawingAttributes.Color}, {ink?.DefaultDrawingAttributes.Width}");
Logger.Debug("MainWindow", "=== 图层可见性测试结束 ===");
}
catch (Exception ex)
{
Logger.Error("MainWindow", $"图层可见性测试失败: {ex.Message}", ex);
}
}), DispatcherPriority.Loaded);
}
#endregion
#region 事件处理方法
/// <summary>
/// 摄像头帧接收事件(修复版)
/// </summary>
private void OnCameraFrameReceived(System.Drawing.Bitmap frame)
{
// 如果不是实时模式或正在关闭,不处理帧
if (_isClosing || !_isLiveMode || _isPerspectiveCorrectionMode)
{
_memoryManager?.DisposeFrame(frame, true);
return;
}
Dispatcher.Invoke(() =>
{
if (_isLiveMode && !_isClosing && !_isPerspectiveCorrectionMode)
{
try
{
// 记录第一次视频帧接收状态
if (!_isFirstFrameProcessed)
{
bool frameValid = frame != null && frame.Width > 0 && frame.Height > 0;
string frameInfo = frameValid ?
$"帧尺寸: {frame.Width}x{frame.Height}" :
"无效帧";
Logger.LogVideoFrameStatus("Camera", frameValid, frameInfo);
_isFirstFrameProcessed = true;
}
// 处理并显示帧
var bitmapImage = _frameProcessor.ProcessFrameToBitmapImage(frame);
var videoImage = (WinImage)FindName("VideoImage");
if (bitmapImage != null && videoImage != null)
{
videoImage.Source = bitmapImage;
}
// 更新内存管理
_memoryManager?.UpdateLastProcessedFrame(frame);
}
catch (Exception ex)
{
Logger.Error("MainWindow", $"视频帧处理错误: {ex.Message}", ex);
}
finally
{
// 释放当前帧
_memoryManager?.DisposeFrame(frame);
}
}
else
{
_memoryManager?.DisposeFrame(frame, true);
}
});
}
/// <summary>
/// TouchSDK 面积变化事件
/// </summary>
private void OnSDKTouchAreaChanged(double area)
{
if (_isClosing) return;
Dispatcher.Invoke(() =>
{
_touchManager.UpdateSDKTouchArea(area);
UpdateSDKTouchAreaDisplay();
});
}
/// <summary>
/// 触控点数变化事件
/// </summary>
private void OnTouchCountChanged(int count)
{
Dispatcher.Invoke(() =>
{
UpdateTouchInfoDisplay();
});
}
/// <summary>
/// 触控面积变化事件
/// </summary>
private void OnTouchAreaChanged(double area)
{
Dispatcher.Invoke(() =>
{
UpdateTouchInfoDisplay();
});
}
/// <summary>
/// 触控中心变化事件
/// </summary>
private void OnTouchCenterChanged(WinPoint center)
{
Dispatcher.Invoke(() =>
{
UpdateTouchInfoDisplay();
});
}
/// <summary>
/// 更新触控信息显示
/// </summary>
private void UpdateTouchInfoDisplay()
{
try
{
if (TouchCountText != null)
{
TouchCountText.Text = _touchManager.GetTouchSDKStatusText();
}
if (TouchAreaText != null)
{
var area = _touchManager.TouchCount >= 3 ?
_touchManager.CalculatePolygonArea(_touchManager.GetCurrentTouchPoints()) : 0;
TouchAreaText.Text = $"面积: {area:F0} 像素²";
}
if (TouchCenterText != null)
{
var center = _touchManager.CalculateTouchCenter();
TouchCenterText.Text = $"中心: ({center.X:F0}, {center.Y:F0})";
}
}
catch (Exception ex)
{
Logger.Error("MainWindow", $"更新触控信息显示失败: {ex.Message}", ex);
}
}
#endregion
#region UI事件处理
#region 画笔设置悬浮窗交互逻辑
/// <summary>
/// 画笔按钮点击事件(修改版)
/// </summary>
private void PenBtn_Click(object sender, RoutedEventArgs e)
{
// 如果当前不是画笔模式,切换到画笔模式
if (_drawingManager.CurrentMode != DrawingManager.ToolMode.Pen)
{
SetMode(DrawingManager.ToolMode.Pen);
Logger.Debug("MainWindow", "切换到画笔模式");
}
else
{
// 如果已经是画笔模式,切换悬浮窗的显示状态
PenSettingsPopup.IsOpen = !PenSettingsPopup.IsOpen;
// 确保按钮保持选中状态(因为 ToggleButton 点击会自动切换状态)
PenBtn.IsChecked = true;