修复:使用独立帧快照刷新 Avalonia 相机预览

This commit is contained in:
tfarcraw 2026-11-08 21:07:25 +08:00
parent 4bbb6f8819
commit 5be8b8587f
2 changed files with 47 additions and 2 deletions

View File

@ -360,6 +360,12 @@ public class SopPageViewModel : ViewModelBase
_pending = null;
}
var previous = Frame as BitmapSource;
Frame = null;
previous?.Dispose();
_bitmap?.Dispose();
_bitmap = null;
// 相机与推理管线是应用级单例,由 App.OnExit 按顺序统一释放。
base.Destroy();
@ -684,6 +690,7 @@ public class SopPageViewModel : ViewModelBase
{
if (_bitmap is null || _bitmap.PixelWidth != src.Width || _bitmap.PixelHeight != src.Height)
{
_bitmap?.Dispose();
_bitmap = new WriteableBitmap(src.Cols, src.Rows, 96, 96, PixelFormats.Bgr24, null);
// ROI 覆盖层的坐标系就是帧像素坐标系,所以内层 Grid 尺寸必须跟着帧走
@ -702,7 +709,11 @@ public class SopPageViewModel : ViewModelBase
_bitmap.WritePixels(new Avalonia.PixelRect(0, 0, src.Cols, src.Rows), bytes, stride, 0);
}
Frame = _bitmap;
// 同一 IImage 引用不会触发 Avalonia Image 的重绘;覆盖层的局部刷新会留下旧画面。
// 发布独立快照,同时避免渲染线程读取正在写入的位图。
var previous = Frame as BitmapSource;
Frame = _bitmap.Snapshot();
previous?.Dispose();
// 帧率:这 40ms 里处理了几帧
var delta = _frameCount - _lastTickCount;

View File

@ -70,6 +70,7 @@ internal static class Smoke
await Task.Run(()=>{Camera.Emit();Camera.Emit();});
await Task.Delay(150);
Check(sop.Frame is not null&&sop.FrameWidth==640&&sop.FrameHeight==480,"Camera buffer renders through the shared Avalonia bitmap adapter");
await CheckMovingPreview(sop,window);
Save(window,"sop");
sop.StartStopCommand.Execute();for(var i=0;i<100&&sop.IsRunning;i++)await Task.Delay(20);
Check(!sop.IsRunning&&!Camera.IsConnected,"Stop command releases acquisition");
@ -140,6 +141,38 @@ internal static class Smoke
finally{lifetime.Shutdown(_exit);}
}
private static string Output {get{var p=Path.GetFullPath("artifacts/avalonia-smoke");Directory.CreateDirectory(p);return p;}}
private static async Task CheckMovingPreview(SopPageViewModel sop,Window window)
{
var image=window.GetVisualDescendants().OfType<SopPage>().Single()
.GetVisualDescendants().OfType<Image>().Single();
var changes=0;
System.ComponentModel.PropertyChangedEventHandler onChanged=(_,e)=>{if(e.PropertyName==nameof(sop.Frame))changes++;};
sop.PropertyChanged+=onChanged;
try
{
foreach(var x in new[]{40,240,40})
{
var previous=sop.Frame;
await Task.Run(()=>Camera.Emit(x));
for(var i=0;i<50&&ReferenceEquals(previous,sop.Frame);i++)await Task.Delay(20);
Check(!ReferenceEquals(previous,sop.Frame)&&ReferenceEquals(image.Source,sop.Frame),
"Each moving frame updates the bound Image source even at the same resolution");
using var rendered=new RenderTargetBitmap(new PixelSize(640,480));
var preview=new Image {Source=sop.Frame,Width=640,Height=480};
preview.Measure(new Size(640,480));preview.Arrange(new Avalonia.Rect(0,0,640,480));
rendered.Render(preview);
var path=Path.Combine(Output,"motion-"+changes+".png");rendered.Save(path);
using var decoded=Cv2.ImRead(path);
var clearedX=x==40?260:60;
Check(decoded.At<Vec3b>(60,x+20)==new Vec3b(0,0,255)&&decoded.At<Vec3b>(60,clearedX)==new Vec3b(35,60,90),
"Moving object renders at its new position and clears its previous position");
}
Check(changes==3,"All three same-size frames notify the UI");
var last=sop.Frame;await Task.Delay(120);
Check(ReferenceEquals(last,sop.Frame),"No new camera frame leaves the displayed image intact");
}
finally{sop.PropertyChanged-=onChanged;}
}
private static void Save(Window window,string name)
{
window.UpdateLayout();
@ -158,9 +191,10 @@ internal sealed class FakeCamera : ICameraService
public Task<bool> StartAsync(){IsGrabbing=true;return Task.FromResult(true);}
public void Stop(){IsGrabbing=false;}public void Disconnect(){Stop();IsConnected=false;ConnectionChanged?.Invoke(false);}public void Dispose()=>Disconnect();
private long _id;
public void Emit()
public void Emit(int? objectX=null)
{
using var frame=new CameraFrame {FrameId=++_id,Timestamp=DateTime.Now,Bgr=new Mat(480,640,MatType.CV_8UC3,new Scalar(35,60,90))};
if(objectX is int x)Cv2.Rectangle(frame.Bgr,new OpenCvSharp.Rect(x,40,60,60),new Scalar(0,0,255),-1);
FrameArrived?.Invoke(frame);
}
}