chore: move CameraView logic to itself

This commit is contained in:
2026-01-23 08:42:58 +03:00
parent 8896629ce2
commit d4e7c300f4
8 changed files with 100 additions and 139 deletions
+31 -5
View File
@@ -72,7 +72,7 @@ class VkRenderer
SType = StructureType.FenceCreateInfo
};
context.Api.CreateFence(context.Device, &fenceInfo, null, out var fence).ThrowOnError();
var (importImage, importMemoryReqirements, importMemory) = ImportImage(fdDup, width, height, stride);
var (exportImage, exportMemoryReqirements, exportMemory) = CreateExportImage(width, height);
@@ -380,6 +380,7 @@ public partial class CameraView : Control
private bool _compositionRequested;
private VkRenderer? _renerer;
private VkImage? _latestImage;
private System.Timers.Timer? _updateTimer;
public CameraView()
{ }
@@ -388,11 +389,40 @@ public partial class CameraView : Control
{
base.OnAttachedToVisualTree(e);
Initialize();
if (!_initialized)
return;
int i = 0;
_updateTimer = new System.Timers.Timer(100)
{
AutoReset = true
};
_updateTimer.Elapsed += async (_, _) =>
{
await Dispatcher.UIThread.InvokeAsync(async () =>
{
var viewModel = DataContext as CameraViewModel;
if (viewModel is null)
return;
var result = await viewModel.CaptureImage(i++);
if (result is not null)
{
var (frameBuffer, streamConfiguration) = result.Value;
if (frameBuffer.Metadata.Status == FrameMetadata.StatusEnum.FrameSuccess)
Update(frameBuffer, streamConfiguration);
}
});
};
_updateTimer.Start();
}
protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e)
{
if (_initialized)
{
_updateTimer?.Stop();
_updateTimer?.Dispose();
Surface?.Dispose();
}
_initialized = false;
base.OnDetachedFromLogicalTree(e);
@@ -425,10 +455,6 @@ public partial class CameraView : Control
_visual.Size = new Vector(Bounds.Width, Bounds.Height);
Update();
}
if (change.Property == DataContextProperty)
{
(DataContext as CameraViewModel)?.Update += (_, ea) => Update(ea.FrameBuffer, ea.StreamConfiguration);
}
base.OnPropertyChanged(change);
}
+17 -17
View File
@@ -1,32 +1,32 @@
using GSS2.Core.Hardware;
using LibCameraSharp;
namespace GSS2.Test;
public class CameraViewModel : ViewModelBase
{
public class UpdateEventArgs : EventArgs
{
public FrameBuffer? FrameBuffer { get; }
public StreamConfiguration? StreamConfiguration { get; }
public UpdateEventArgs(FrameBuffer? frameBuffer = null, StreamConfiguration? streamConfiguration = null)
{
FrameBuffer = frameBuffer;
StreamConfiguration = streamConfiguration;
}
}
private readonly CameraService _cameraService;
private readonly ILogger<CameraViewModel> _logger;
public event EventHandler<UpdateEventArgs>? Update;
public CameraViewModel(ILogger<CameraViewModel> logger)
public CameraViewModel(CameraService cameraService, ILogger<CameraViewModel> logger)
{
_cameraService = cameraService;
_logger = logger;
_logger.LogInformation("Initialized");
}
public void CallUpdate(FrameBuffer? frameBuffer = null, StreamConfiguration? streamConfiguration = null)
public async Task<(FrameBuffer, StreamConfiguration)?> CaptureImage(int bufferId)
{
Update?.Invoke(this, new UpdateEventArgs(frameBuffer, streamConfiguration));
(FrameBuffer, StreamConfiguration)? result = null;
try
{
result = await _cameraService.CaptureViewFinderBuffer(bufferId, CancellationToken.None);
}
catch (Exception e)
{
_logger.LogError(e, "Error while image capture");
}
return result;
}
}
+1 -17
View File
@@ -1,4 +1,5 @@
using Avalonia.Controls;
using Avalonia.Threading;
namespace GSS2.Test;
@@ -7,22 +8,5 @@ public partial class MainWindow : Window
public MainWindow()
{
InitializeComponent();
int i = 0;
PointerPressed += async (_, _) =>
{
var viewModel = DataContext as MainWindowViewModel;
if (viewModel is null)
return;
var result = await viewModel.CaptureImage(i++);
if (result is not null)
{
var (frameBuffer, streamConfiguration) = result.Value;
if (frameBuffer.Metadata.Status == LibCameraSharp.FrameMetadata.StatusEnum.FrameSuccess)
// cameraView.Update(frameBuffer, streamConfiguration);
(cameraView.Content as CameraViewModel)?.CallUpdate(frameBuffer, streamConfiguration);
}
};
}
}
-35
View File
@@ -27,40 +27,5 @@ public partial class MainWindowViewModel : ViewModelBase
_lightsService = lightsService;
_logger = logger;
CameraViewModel = cameraViewModel;
_logger.LogInformation("Initialized");
}
public async Task<(LibCameraSharp.FrameBuffer, LibCameraSharp.StreamConfiguration)?> CaptureImage(int bufferId)
{
_logger.LogInformation("Capture image");
_lightsService.CpuLoad(0.5, 1, System.Drawing.Color.Aqua);
(LibCameraSharp.FrameBuffer, LibCameraSharp.StreamConfiguration)? result = null;
try
{
// _illuminatorService.SetIntensity(0.01, 0, 0);
// _illuminatorService.TurnOn();
result = await _cameraService.CaptureViewFinderBuffer(bufferId, CancellationToken.None);
_lightsService.Flash(0.5, 2, System.Drawing.Color.Green);
}
catch (Exception e)
{
_logger.LogError(e, "Error while image capture");
_lightsService.Flash(0.5, 2, System.Drawing.Color.Red);
}
finally
{
// _illuminatorService.TurnOff();
}
var snowfallTimer = new System.Timers.Timer(3000);
snowfallTimer.Elapsed += (_, _) =>
{
_lightsService.Snowfall(0.3, 1, System.Drawing.Color.Aqua);
snowfallTimer.Dispose();
};
snowfallTimer.Start();
return result;
}
}
+37 -37
View File
@@ -1,49 +1,49 @@
using System.Drawing;
// using System.Drawing;
using GSS2.Core.Analysis.AmineContent.Database;
using GSS2.Core.Hardware;
// using GSS2.Core.Analysis.AmineContent.Database;
// using GSS2.Core.Hardware;
namespace GSS2.Test;
// namespace GSS2.Test;
public class Worker(
ILogger<Worker> logger,
IHostApplicationLifetime applicationLifetime,
AmineContentCalibrationContext amineCalibrationContext,
AmineContentResultsContext amineResultsContext,
LightsService lightsService,
IlluminatorService illuminatorService,
CameraService cameraService
) : BackgroundService
{
private readonly ILogger<Worker> _logger = logger;
private readonly IHostApplicationLifetime _applicationLifetime = applicationLifetime;
private readonly AmineContentCalibrationContext _amineCalibrationContext = amineCalibrationContext;
private readonly AmineContentResultsContext _amineResultsContext = amineResultsContext;
private readonly LightsService _lightsService = lightsService;
private readonly IlluminatorService _illuminatorService = illuminatorService;
private readonly CameraService _cameraService = cameraService;
// public class Worker(
// ILogger<Worker> logger,
// IHostApplicationLifetime applicationLifetime,
// AmineContentCalibrationContext amineCalibrationContext,
// AmineContentResultsContext amineResultsContext,
// LightsService lightsService,
// IlluminatorService illuminatorService,
// CameraService cameraService
// ) : BackgroundService
// {
// private readonly ILogger<Worker> _logger = logger;
// private readonly IHostApplicationLifetime _applicationLifetime = applicationLifetime;
// private readonly AmineContentCalibrationContext _amineCalibrationContext = amineCalibrationContext;
// private readonly AmineContentResultsContext _amineResultsContext = amineResultsContext;
// private readonly LightsService _lightsService = lightsService;
// private readonly IlluminatorService _illuminatorService = illuminatorService;
// private readonly CameraService _cameraService = cameraService;
protected override async Task ExecuteAsync(CancellationToken cancellationToken)
{
_cameraService.Test();
// protected override async Task ExecuteAsync(CancellationToken cancellationToken)
// {
// // _cameraService.Test();
// _ = _lightsService.Run(cancellationToken);
// _ = _lightsService.SnowfallAsync(0.3, 1, Color.Aqua, cancellationToken);
// // _ = _lightsService.Run(cancellationToken);
// // _ = _lightsService.SnowfallAsync(0.3, 1, Color.Aqua, cancellationToken);
// _illuminatorService.SetIntensity(0.01, 0, 0);
// _illuminatorService.TurnOn();
// // _illuminatorService.SetIntensity(0.01, 0, 0);
// // _illuminatorService.TurnOn();
// await Task.Delay(1000);
// // await Task.Delay(1000);
// var img = await _cameraService.CaptureImage(cancellationToken, 50);
// // img?.SaveImage("IMAGE.png");
// // var img = await _cameraService.CaptureImage(cancellationToken, 50);
// // // img?.SaveImage("IMAGE.png");
// await Task.Delay(1000);
// // await Task.Delay(1000);
// _illuminatorService.TurnOff();
// // _illuminatorService.TurnOff();
// await _lightsService.DisconnectAsync(cancellationToken);
// // await _lightsService.DisconnectAsync(cancellationToken);
applicationLifetime.StopApplication();
}
}
// applicationLifetime.StopApplication();
// }
// }
+1 -1
View File
@@ -38,7 +38,7 @@
},
"Camera": {
"CameraId": "",
"ViewFinderFrameBufferCount": 2,
"ViewFinderFrameBufferCount": 30,
"ViewFinderWidth": 1920,
"ViewFinderHeight": 1080,
"ViewFinderFps": 30,