c# - 在 WPF 中显示来 self 的网络摄像头的视频流?

标签 c# wpf mvvm caliburn.micro emgucv

我有一个问题,如何在 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/

相关文章:

c# - 如何更改 ASP.NET 中的应用程序语言?

c# - 如何仅获取图像最后一行的像素并将其显示在图片框中。 C#

c# - 使用多播委托(delegate)链接函数

c# - MVVM 的上下文相关命令

c# - 使用 MVVM 进行正确验证

具有不同文档的 WPF Devexpress Documentgroup

wpf - 当我在 Treeview 项目中使用它时,内容绑定(bind)不适用于文本 block

c# - DateTime.TryParse() 和 DateTime.TryParseExact() 无法在 linux/macos 上解析日期并在 windows 上正常工作

c# - 在 C# 中从更接近今天的列表中获取下一个日期

c# - 设置资源字典的源时无法识别 URI 前缀