This commit is contained in:
2025-10-02 02:11:54 +08:00
parent c96c26288c
commit bf336fdb10
4 changed files with 249 additions and 28 deletions
@@ -181,29 +181,65 @@
VerticalAlignment="Center" />
<!-- 摄像头控制面板 -->
<StackPanel Orientation="Horizontal"
<StackPanel Orientation="Vertical"
HorizontalAlignment="Center"
VerticalAlignment="Bottom"
Margin="0,0,0,8">
<!-- 摄像头选择下拉框 -->
<ComboBox Name="CameraSelectionComboBox"
Width="200"
Margin="4,0"
Background="#2d2d2d"
Foreground="White"
BorderBrush="#404040"
SelectionChanged="CameraSelectionComboBox_SelectionChanged" />
<!-- 第一行:摄像头选择和切换 -->
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<!-- 摄像头选择下拉框 -->
<ComboBox Name="CameraSelectionComboBox"
Width="200"
Margin="4,0"
Background="#2d2d2d"
Foreground="White"
BorderBrush="#404040"
SelectionChanged="CameraSelectionComboBox_SelectionChanged" />
<!-- 切换摄像头按钮 -->
<Button Name="SwitchCameraButton"
Content="切换摄像头"
Margin="4,0"
Padding="8,4"
Background="#3b82f6"
Foreground="White"
BorderThickness="0"
FontWeight="Medium"
Click="SwitchCameraButton_Click" />
</StackPanel>
<!-- 切换摄像头按钮 -->
<Button Name="SwitchCameraButton"
Content="切换摄像头"
Margin="4,0"
Padding="8,4"
Background="#3b82f6"
Foreground="White"
BorderThickness="0"
FontWeight="Medium"
Click="SwitchCameraButton_Click" />
<!-- 第二行:旋转和分辨率控制 -->
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,4,0,0">
<!-- 旋转控制 -->
<StackPanel Orientation="Horizontal" Margin="4,0">
<TextBlock Text="旋转:" Foreground="White" VerticalAlignment="Center" Margin="0,0,4,0" FontSize="12"/>
<Button Name="RotateLeftButton" Content="⟲" Width="30" Height="30" Margin="2,0"
Background="#4b5563" Foreground="White" BorderThickness="0"
Click="RotateLeftButton_Click" FontSize="10" FontWeight="10"/>
<TextBlock Name="RotationAngleText" Text="0°" Foreground="White" VerticalAlignment="Center"
Margin="4,0" FontSize="12" MinWidth="30" TextAlignment="Center"/>
<Button Name="RotateRightButton" Content="⟳" Width="30" Height="30" Margin="2,0"
Background="#4b5563" Foreground="White" BorderThickness="0"
Click="RotateRightButton_Click" FontSize="10" FontWeight="10"/>
</StackPanel>
<!-- 分隔线 -->
<Rectangle Width="1" Height="20" Fill="#404040" Margin="8,0"/>
<!-- 分辨率控制 -->
<StackPanel Orientation="Horizontal" Margin="4,0">
<TextBlock Text="分辨率:" Foreground="White" VerticalAlignment="Center" Margin="0,0,4,0" FontSize="12"/>
<ComboBox Name="ResolutionComboBox" Width="120" Margin="2,0"
Background="#2d2d2d" Foreground="White" BorderBrush="#404040"
SelectionChanged="ResolutionComboBox_SelectionChanged">
<ComboBoxItem Content="640x480" Tag="640,480"/>
<ComboBoxItem Content="800x600" Tag="800,600"/>
<ComboBoxItem Content="1024x768" Tag="1024,768"/>
<ComboBoxItem Content="1280x720" Tag="1280,720"/>
<ComboBoxItem Content="1920x1080" Tag="1920,1080"/>
</ComboBox>
</StackPanel>
</StackPanel>
</StackPanel>
<!-- 摄像头状态指示 -->
@@ -104,12 +104,20 @@ namespace Ink_Canvas
{
try
{
_cameraService = new CameraService();
// 从设置中加载摄像头配置
var cameraSettings = MainWindow.Settings.Camera;
_cameraService = new CameraService(
cameraSettings.RotationAngle,
cameraSettings.ResolutionWidth,
cameraSettings.ResolutionHeight);
_cameraService.FrameReceived += CameraService_FrameReceived;
_cameraService.ErrorOccurred += CameraService_ErrorOccurred;
// 初始化摄像头选择下拉框
RefreshCameraComboBox();
// 初始化旋转和分辨率显示
InitializeCameraControls();
}
catch (Exception ex)
{
@@ -117,6 +125,26 @@ namespace Ink_Canvas
}
}
private void InitializeCameraControls()
{
if (_cameraService != null)
{
// 更新旋转角度显示
UpdateRotationDisplay();
// 设置分辨率下拉框
var currentResolution = $"{_cameraService.ResolutionWidth}x{_cameraService.ResolutionHeight}";
foreach (ComboBoxItem item in ResolutionComboBox.Items)
{
if (item.Tag?.ToString() == $"{_cameraService.ResolutionWidth},{_cameraService.ResolutionHeight}")
{
ResolutionComboBox.SelectedItem = item;
break;
}
}
}
}
private void RefreshCameraComboBox()
{
try
@@ -1213,6 +1241,70 @@ namespace Ink_Canvas
CameraModeButton.Background = new SolidColorBrush(Color.FromRgb(107, 114, 128)); // 灰色
}
#region
private void RotateLeftButton_Click(object sender, RoutedEventArgs e)
{
if (_cameraService != null)
{
_cameraService.RotationAngle = (_cameraService.RotationAngle - 1 + 4) % 4;
UpdateRotationDisplay();
SaveCameraSettings();
}
}
private void RotateRightButton_Click(object sender, RoutedEventArgs e)
{
if (_cameraService != null)
{
_cameraService.RotationAngle = (_cameraService.RotationAngle + 1) % 4;
UpdateRotationDisplay();
SaveCameraSettings();
}
}
private void ResolutionComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (_cameraService != null && ResolutionComboBox.SelectedItem is ComboBoxItem selectedItem)
{
var resolution = selectedItem.Tag?.ToString();
if (!string.IsNullOrEmpty(resolution))
{
var parts = resolution.Split(',');
if (parts.Length == 2 &&
int.TryParse(parts[0], out int width) &&
int.TryParse(parts[1], out int height))
{
_cameraService.ResolutionWidth = width;
_cameraService.ResolutionHeight = height;
SaveCameraSettings();
}
}
}
}
private void UpdateRotationDisplay()
{
if (_cameraService != null)
{
var angle = _cameraService.RotationAngle * 90;
RotationAngleText.Text = $"{angle}°";
}
}
private void SaveCameraSettings()
{
if (_cameraService != null)
{
MainWindow.Settings.Camera.RotationAngle = _cameraService.RotationAngle;
MainWindow.Settings.Camera.ResolutionWidth = _cameraService.ResolutionWidth;
MainWindow.Settings.Camera.ResolutionHeight = _cameraService.ResolutionHeight;
MainWindow.SaveSettingsToFile();
}
}
#endregion
protected override void OnClosed(EventArgs e)
{
try