我有一个问题,如何在 WPF 的图像容器中显示来自网络摄像头的流?我已经使用 Emgu 获取流并将其转换为 BitmapSource,但现在我不明白如何使用 calibburn (MVVM) 每 X 毫秒将图像从网络摄像头绑定(bind)到 WPF ....
<Image x:Name="imageWebcam" />
最佳答案
找到了好的解决方案。 我无法显示整个代码,但我可以解释一下,绑定(bind) wpf View :
<Image x:Name="ImageWebcam" />
对于我的 ViewModel(使用 Caliburn FrameWork,但这可以轻松适应 MVVM 模式),我必须将图像与具有相同名称的函数绑定(bind):
public WriteableBitmap ImageWebcam
{
get
{
return this.imageWebcam;
}
set
{
this.imageWebcam = value;
this.NotifyOfPropertyChange(() => this.ImageWebcam);
}
}
为了更新此图像,我使用了一个计时器,它必须初始化到 ViewModel 构造函数中:
public MachinBidule()
{
timeIsRunningOut= new System.Timers.Timer();
timeIsRunningOut.AutoReset = true;
timeIsRunningOut.Interval = 10;
timeIsRunningOut.Elapsed += (sender, e) => { UpdateImage(); };
capture = new Capture(); // Capture the webcam using EmguCV
}
现在您可以使用调度程序管理更新:
// Update the image, method called by the timer every 10 ms
private void UpdateImage()
{
// If capture is working
if(capture.QueryFrame()!= null)
{
//capture an Image from webcam stream and
Image<Bgr, Byte> currentFrame = capture.QueryFrame().ToImage<Bgr, Byte>();
if (currentFrame != null)
{
Application.Current.Dispatcher.BeginInvoke(new System.Action(() => { imageWebcam = new WriteableBitmap(BitmapSourceConvert.ToBitmapSource(currentFrame)); NotifyOfPropertyChange(() => ImageWebcam); }));
}
}
}
你可以注意到我们需要从 emguCV Image<Bgr,Byte>
进行转换到 WPF WriteableBitmap
并通过 NotifyOfPropertyChange(() => ImageWebcam)
通知更新.
如果您有任何问题,请随时与我联系或发表评论。 如果您发现任何错误,请随时纠正我(语法和代码)。
关于c# - 在 WPF 中显示来 self 的网络摄像头的视频流?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36890103/