From bf336fdb104a86ecb2f7e603c17d8d74dd9660eb Mon Sep 17 00:00:00 2001 From: CJKmkp <2564608840@qq.com> Date: Thu, 2 Oct 2025 02:11:54 +0800 Subject: [PATCH] add:issue #214 --- Ink Canvas/Helpers/CameraService.cs | 92 ++++++++++++++++-- Ink Canvas/Resources/Settings.cs | 17 ++++ .../Windows/ScreenshotSelectorWindow.xaml | 74 +++++++++++---- .../Windows/ScreenshotSelectorWindow.xaml.cs | 94 ++++++++++++++++++- 4 files changed, 249 insertions(+), 28 deletions(-) diff --git a/Ink Canvas/Helpers/CameraService.cs b/Ink Canvas/Helpers/CameraService.cs index 449e31fe..e0c4c68c 100644 --- a/Ink Canvas/Helpers/CameraService.cs +++ b/Ink Canvas/Helpers/CameraService.cs @@ -18,6 +18,11 @@ namespace Ink_Canvas.Helpers private readonly object _frameLock = new object(); private Dispatcher _dispatcher; + // 新增属性 + private int _rotationAngle = 0; // 0=0度,1=90度,2=180度,3=270度 + private int _resolutionWidth = 640; + private int _resolutionHeight = 480; + public event EventHandler FrameReceived; public event EventHandler ErrorOccurred; @@ -25,6 +30,25 @@ namespace Ink_Canvas.Helpers public List AvailableCameras { get; private set; } public FilterInfo CurrentCamera { get; private set; } + // 新增属性 + public int RotationAngle + { + get => _rotationAngle; + set => _rotationAngle = Math.Max(0, Math.Min(3, value)); + } + + public int ResolutionWidth + { + get => _resolutionWidth; + set => _resolutionWidth = Math.Max(320, Math.Min(1920, value)); + } + + public int ResolutionHeight + { + get => _resolutionHeight; + set => _resolutionHeight = Math.Max(240, Math.Min(1080, value)); + } + public CameraService() { _dispatcher = Dispatcher.CurrentDispatcher; @@ -32,6 +56,16 @@ namespace Ink_Canvas.Helpers RefreshCameraList(); } + public CameraService(int rotationAngle, int resolutionWidth, int resolutionHeight) + { + _dispatcher = Dispatcher.CurrentDispatcher; + AvailableCameras = new List(); + _rotationAngle = rotationAngle; + _resolutionWidth = resolutionWidth; + _resolutionHeight = resolutionHeight; + RefreshCameraList(); + } + /// /// 刷新可用摄像头列表 /// @@ -242,14 +276,16 @@ namespace Ink_Canvas.Helpers var width = sourceFrame.Width; var height = sourceFrame.Height; - if (width > 0 && height > 0) - { - _currentFrame = new Bitmap(width, height, PixelFormat.Format24bppRgb); - using (var graphics = Graphics.FromImage(_currentFrame)) - { - graphics.DrawImage(sourceFrame, 0, 0); - } - } + if (width > 0 && height > 0) + { + // 应用旋转 + Bitmap rotatedFrame = ApplyRotation(sourceFrame); + + // 应用分辨率调整 + _currentFrame = ResizeImage(rotatedFrame, _resolutionWidth, _resolutionHeight); + + rotatedFrame?.Dispose(); + } else { _currentFrame = null; @@ -300,6 +336,46 @@ namespace Ink_Canvas.Helpers return AvailableCameras.Count > 0; } + /// + /// 应用旋转到图像 + /// + private Bitmap ApplyRotation(Bitmap source) + { + if (_rotationAngle == 0) + return new Bitmap(source); + + var rotationType = RotateFlipType.RotateNoneFlipNone; + switch (_rotationAngle) + { + case 1: rotationType = RotateFlipType.Rotate90FlipNone; break; + case 2: rotationType = RotateFlipType.Rotate180FlipNone; break; + case 3: rotationType = RotateFlipType.Rotate270FlipNone; break; + } + + var rotated = new Bitmap(source); + rotated.RotateFlip(rotationType); + return rotated; + } + + /// + /// 调整图像大小 + /// + private Bitmap ResizeImage(Bitmap source, int width, int height) + { + if (source.Width == width && source.Height == height) + return new Bitmap(source); + + var resized = new Bitmap(width, height, PixelFormat.Format24bppRgb); + using (var graphics = Graphics.FromImage(resized)) + { + graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; + graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; + graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; + graphics.DrawImage(source, 0, 0, width, height); + } + return resized; + } + public void Dispose() { StopPreview(); diff --git a/Ink Canvas/Resources/Settings.cs b/Ink Canvas/Resources/Settings.cs index 8180f5f3..73cd49ed 100644 --- a/Ink Canvas/Resources/Settings.cs +++ b/Ink Canvas/Resources/Settings.cs @@ -27,6 +27,8 @@ namespace Ink_Canvas public RandSettings RandSettings { get; set; } = new RandSettings(); [JsonProperty("modeSettings")] public ModeSettings ModeSettings { get; set; } = new ModeSettings(); + [JsonProperty("camera")] + public CameraSettings Camera { get; set; } = new CameraSettings(); } public class Canvas @@ -682,4 +684,19 @@ namespace Ink_Canvas [JsonProperty("isPPTOnlyMode")] public bool IsPPTOnlyMode { get; set; } = false; // 是否为仅PPT模式,默认为false(正常模式) } + + public class CameraSettings + { + [JsonProperty("rotationAngle")] + public int RotationAngle { get; set; } = 0; + + [JsonProperty("resolutionWidth")] + public int ResolutionWidth { get; set; } = 1920; + + [JsonProperty("resolutionHeight")] + public int ResolutionHeight { get; set; } = 1080; + + [JsonProperty("selectedCameraIndex")] + public int SelectedCameraIndex { get; set; } = 0; + } } diff --git a/Ink Canvas/Windows/ScreenshotSelectorWindow.xaml b/Ink Canvas/Windows/ScreenshotSelectorWindow.xaml index b4832ff9..97c4804f 100644 --- a/Ink Canvas/Windows/ScreenshotSelectorWindow.xaml +++ b/Ink Canvas/Windows/ScreenshotSelectorWindow.xaml @@ -181,29 +181,65 @@ VerticalAlignment="Center" /> - - - + + + + + + +