-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCameraSelectWindow.axaml.cs
More file actions
151 lines (131 loc) · 4.78 KB
/
CameraSelectWindow.axaml.cs
File metadata and controls
151 lines (131 loc) · 4.78 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
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Interactivity;
using Avalonia.Layout;
using Avalonia.Markup.Xaml;
using Avalonia.Media;
using Avalonia.Threading;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace ShowWrite;
public partial class CameraSelectWindow : Avalonia.Controls.Window
{
private readonly CameraService? _cameraService;
private List<int> _availableCameras = new();
private int _selectedCameraIndex = -1;
public event Action<int>? CameraSelected;
public CameraSelectWindow()
{
InitializeComponent();
}
public CameraSelectWindow(CameraService cameraService) : this()
{
_cameraService = cameraService;
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
protected override void OnOpened(EventArgs e)
{
base.OnOpened(e);
LoadCameras();
}
private async void LoadCameras()
{
var cameraList = this.FindControl<StackPanel>("CameraList");
var noCameraText = this.FindControl<TextBlock>("NoCameraText");
if (cameraList == null || noCameraText == null) return;
cameraList.Children.Clear();
noCameraText.Text = "正在扫描摄像头...";
noCameraText.IsVisible = true;
_availableCameras = await Task.Run(() =>
{
var cameras = new List<int>();
for (int i = 0; i < 10; i++)
{
try
{
using var test = new VideoCapture(i);
if (test.IsOpened())
cameras.Add(i);
}
catch { }
}
return cameras;
});
await Dispatcher.UIThread.InvokeAsync(() =>
{
cameraList.Children.Clear();
if (_availableCameras.Count == 0)
{
noCameraText.Text = "未检测到摄像头";
noCameraText.IsVisible = true;
return;
}
noCameraText.IsVisible = false;
int currentIndex = _cameraService?.CurrentCameraIndex ?? 0;
for (int i = 0; i < _availableCameras.Count; i++)
{
var cameraIndex = _availableCameras[i];
var btn = new Button
{
Classes = { "camera-btn" },
Tag = cameraIndex,
Content = new StackPanel
{
Orientation = Orientation.Horizontal,
Spacing = 12,
Children =
{
new Viewbox
{
Width = 24,
Height = 24,
Child = new Path
{
Fill = Brushes.White,
Data = Geometry.Parse("M16 7a1 1 0 1 0 0 2h1a1 1 0 1 0 0-2zm-4 0a1 1 0 1 0 0 2h1a1 1 0 1 0 0-2zM7 8a1 1 0 0 1 1-1h1a1 1 0 1 1 0 2H8a1 1 0 0 1-1-1M5 4a3 3 0 0 0-3 3v6a3 3 0 0 0 3 3h11a3 3 0 0 0 3-3V7a3 3 0 0 0-3-3zm0 2h11a1 1 0 0 1 1 1v6a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1m6.5 11a.5.5 0 0 0-.5.5v1a.5.5 0 0 0 .5.5h3a.5.5 0 0 0 .5-.5v-1a.5.5 0 0 0-.5-.5z")
}
},
new TextBlock
{
Text = $"摄像头 {cameraIndex}",
FontSize = 14,
Foreground = Brushes.White,
VerticalAlignment = VerticalAlignment.Center
},
new TextBlock
{
Text = cameraIndex == currentIndex ? "(当前)" : "",
FontSize = 12,
Foreground = Brush.Parse("#0078D4"),
VerticalAlignment = VerticalAlignment.Center
}
}
}
};
btn.Click += (s, e) =>
{
if (s is Button button && button.Tag is int idx)
{
_selectedCameraIndex = idx;
CameraSelected?.Invoke(idx);
Close();
}
};
cameraList.Children.Add(btn);
}
});
}
private void RefreshBtn_Click(object? sender, RoutedEventArgs e)
{
LoadCameras();
}
private void CancelBtn_Click(object? sender, RoutedEventArgs e)
{
Close();
}
}