109 lines
6.4 KiB
C#
109 lines
6.4 KiB
C#
using Avalonia;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Headless;
|
|
using Avalonia.Markup.Xaml.Styling;
|
|
using Avalonia.Themes.Fluent;
|
|
using Avalonia.Threading;
|
|
using Avalonia.VisualTree;
|
|
using OpenCvSharp;
|
|
using Prism.Dialogs;
|
|
using SopHelper.Engine.Sop;
|
|
using SopHelper.Engine.Recipes;
|
|
using SopHelper.VVMs.RecipeEditor;
|
|
using Window = Avalonia.Controls.Window;
|
|
|
|
internal static class Program
|
|
{
|
|
[STAThread]
|
|
public static void Main()
|
|
{
|
|
var root = Path.Combine(Path.GetTempPath(), "region-video-capture-" + Guid.NewGuid().ToString("N"));
|
|
var previousRoot = Environment.GetEnvironmentVariable("SOPHELPER_WORKSPACE_ROOT");
|
|
Environment.SetEnvironmentVariable("SOPHELPER_WORKSPACE_ROOT", root);
|
|
try
|
|
{
|
|
AppBuilder.Configure<Application>().UseSkia().UseHeadless(new AvaloniaHeadlessPlatformOptions { UseHeadlessDrawing = false }).SetupWithoutStarting();
|
|
Application.Current!.Styles.Add(new FluentTheme());
|
|
Application.Current.Styles.Add(new StyleInclude(new Uri("avares://SopHelper.Avalonia/")) { Source = new Uri("avares://SopHelper.Avalonia/Themes/Styles.axaml") });
|
|
CheckRegionVideoCapture(root);
|
|
}
|
|
finally
|
|
{
|
|
Environment.SetEnvironmentVariable("SOPHELPER_WORKSPACE_ROOT", previousRoot);
|
|
if (Directory.Exists(root)) Directory.Delete(root, true);
|
|
}
|
|
}
|
|
private static void CheckRegionVideoCapture(string root)
|
|
{
|
|
var store = new RecipeWorkspace(root);
|
|
var config = new SopConfig(); config.Sop.Name = "区域视频取图"; config.Sop.Parts = [];
|
|
var vm = new RecipeEditorViewModel();
|
|
vm.OnDialogOpened(new DialogParameters { { "directory", store.RecipesDirectory }, { "config", config }, { "new", true } });
|
|
vm.CaptureStandardVideoImageCommand.Execute();
|
|
Check(vm.PreviewStatus.Contains("先保存配方"), "Unsaved recipe explains how to enable standard video capture");
|
|
vm.SaveCommand.Execute();
|
|
var editor = new RecipeEditor { DataContext = vm };
|
|
var window = new Window { Content = editor, Width = 1040, Height = 760 };
|
|
window.Show(); window.UpdateLayout();
|
|
editor.GetVisualDescendants().OfType<TabControl>().Single().SelectedIndex = 1;
|
|
window.UpdateLayout(); Dispatcher.UIThread.RunJobs();
|
|
var button = editor.GetVisualDescendants().OfType<Button>().Single(b => ReferenceEquals(b.Command, vm.CaptureStandardVideoImageCommand));
|
|
Check(button.Content?.ToString() == "从标准视频取图" && button.IsEffectivelyVisible, "Region page exposes the standard video capture button");
|
|
var secondsBox = editor.FindControl<NumericUpDown>("StandardVideoSecondsBox")!;
|
|
secondsBox.Value = 1.2m; Dispatcher.UIThread.RunJobs();
|
|
Check(vm.StandardVideoSeconds == 1.2m, "Region time input updates the capture time");
|
|
vm.CaptureStandardVideoImageCommand.Execute();
|
|
Check(vm.PreviewStatus.Contains("没有标准视频"), "Missing standard video explains where to record it");
|
|
var path = vm.SopRecording!.StandardVideoPath;
|
|
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
|
|
using (var writer = new VideoWriter(path, FourCC.MJPG, 10, new OpenCvSharp.Size(64, 48)))
|
|
{
|
|
Check(writer.IsOpened(), "Synthetic standard video writer opens");
|
|
for (var i = 0; i < 20; i++)
|
|
{
|
|
using var frame = new Mat(48, 64, MatType.CV_8UC3, i < 10 ? new Scalar(255, 0, 0) : new Scalar(0, 0, 255));
|
|
writer.Write(frame);
|
|
}
|
|
}
|
|
void Capture(decimal seconds)
|
|
{
|
|
vm.StandardVideoSeconds = seconds;
|
|
vm.CaptureStandardVideoImageCommand.Execute();
|
|
Check(!vm.IsCapturing || (!vm.CaptureStandardVideoImageCommand.CanExecute() && !vm.CaptureImageCommand.CanExecute() && !vm.OpenImageCommand.CanExecute() && !vm.CanCloseDialog()), "Extraction blocks concurrent image operations and dialog close");
|
|
var wait = System.Diagnostics.Stopwatch.StartNew();
|
|
while (vm.IsCapturing && wait.Elapsed < TimeSpan.FromSeconds(10)) { Dispatcher.UIThread.RunJobs(); Thread.Sleep(1); }
|
|
Check(!vm.IsCapturing && vm.CaptureStandardVideoImageCommand.CanExecute(), "Video capture finishes and restores commands");
|
|
}
|
|
Capture(0);
|
|
Check(vm.PreviewImage is { PixelWidth: 64, PixelHeight: 48 } && vm.PreviewStatus.Contains("标准视频 0 秒"), "First frame retains the original video dimensions without a camera");
|
|
var imagePath = Path.Combine(root, "region-frame.png");
|
|
vm.PreviewImage!.Save(imagePath);
|
|
using (var image = Cv2.ImRead(imagePath)) Check(image.At<Vec3b>(20, 20).Item0 > 240, "Default capture reads the blue first frame");
|
|
Capture(1.2m);
|
|
Check(ReferenceEquals(editor.GetVisualDescendants().OfType<RegionCanvas>().Single().Image, vm.PreviewImage), "Captured standard video frame is displayed on the region drawing canvas");
|
|
vm.PreviewImage!.Save(imagePath);
|
|
using (var image = Cv2.ImRead(imagePath)) Check(image.At<Vec3b>(20, 20).Item2 > 240, "Capture at a chosen time reads the later red frame");
|
|
var previous = vm.PreviewImage;
|
|
Capture(2);
|
|
Check(ReferenceEquals(vm.PreviewImage, previous) && vm.PreviewStatus.Contains("视频时长"), "Out-of-range capture preserves the previous region image and reports duration");
|
|
using (var exclusive = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
|
|
Check(exclusive.Length > 0, "Capturing releases the standard video file immediately");
|
|
File.WriteAllText(path, "damaged-video");
|
|
Capture(0);
|
|
Check(ReferenceEquals(vm.PreviewImage, previous) && vm.PreviewStatus.Contains("失败"), "Damaged video reports an error and preserves the previous image");
|
|
Pump(vm.ShutdownAsync()); window.Close(); vm.OnDialogClosed();
|
|
}
|
|
private static void Pump(Task task)
|
|
{
|
|
var wait = System.Diagnostics.Stopwatch.StartNew();
|
|
while (!task.IsCompleted && wait.Elapsed < TimeSpan.FromSeconds(10)) { Dispatcher.UIThread.RunJobs(); Thread.Sleep(1); }
|
|
Check(task.IsCompleted, "Editor shutdown completes");
|
|
task.GetAwaiter().GetResult();
|
|
}
|
|
private static void Check(bool value, string message)
|
|
{
|
|
if (!value) throw new Exception(message);
|
|
Console.WriteLine("PASS: " + message);
|
|
}
|
|
}
|