cambooth/src/CamBooth/CamBooth.App/Features/LiveView/LiveViewPage.xaml.cs
2026-02-28 23:15:59 +01:00

98 lines
2.6 KiB
C#
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using CamBooth.App.Core.AppSettings;
using CamBooth.App.Core.Logging;
using CamBooth.App.Features.Camera;
using EOSDigital.API;
namespace CamBooth.App.Features.LiveView;
public partial class LiveViewPage : Page
{
private readonly AppSettingsService _appSettings;
private readonly CameraService _cameraService;
private readonly Logger _logger;
private readonly ImageBrush bgbrush = new();
private readonly Action<BitmapImage> SetImageAction;
public LiveViewPage(Logger logger, AppSettingsService appSettings, CameraService cameraService)
{
this._logger = logger;
this._appSettings = appSettings;
this._cameraService = cameraService;
this.InitializeComponent();
this.SetImageAction = img => { this.bgbrush.ImageSource = img; };
// Configure the image brush
this.bgbrush.Stretch = Stretch.UniformToFill;
this.bgbrush.AlignmentX = AlignmentX.Center;
this.bgbrush.AlignmentY = AlignmentY.Center;
this.LVCanvas.Background = this.bgbrush;
// Apply horizontal flip on the Canvas using RenderTransform
TransformGroup transformGroup = new();
transformGroup.Children.Add(new ScaleTransform { ScaleX = -1, ScaleY = 1 });
transformGroup.Children.Add(new TranslateTransform { X = 1, Y = 0 });
this.LVCanvas.RenderTransform = transformGroup;
this.LVCanvas.RenderTransformOrigin = new Point(0.5, 0.5);
try
{
cameraService.ConnectCamera();
// Verify that camera session is open before subscribing to events
if (cameraService._mainCamera != null && cameraService._mainCamera.SessionOpen)
{
cameraService._mainCamera.LiveViewUpdated += this.MainCamera_OnLiveViewUpdated;
this._logger.Info("LiveViewPage initialized successfully");
}
else
{
this._logger.Error("Camera session is not open after connection attempt");
throw new InvalidOperationException("Camera session failed to open");
}
}
catch (Exception ex)
{
this._logger.Error($"Failed to initialize LiveViewPage: {ex.Message}");
throw;
}
}
private void MainCamera_OnLiveViewUpdated(ICamera sender, Stream img)
{
try
{
using WrapStream s = new(img);
img.Position = 0;
BitmapImage EvfImage = new();
EvfImage.BeginInit();
EvfImage.StreamSource = s;
EvfImage.CacheOption = BitmapCacheOption.OnLoad;
EvfImage.EndInit();
EvfImage.Freeze();
Application.Current.Dispatcher.BeginInvoke(this.SetImageAction, EvfImage);
}
catch (Exception ex)
{
this._logger.Error(ex.Message);
}
}
public void Dispose()
{
this._cameraService.Dispose();
}
}