从我的摄像头在WPF中显示一个视频流?

从我的摄像头在WPF中显示一个视频流?

问题描述:

我有一个问题,我如何显示从我的摄像头在WPF中的图像容器中的流?我已经与Emgu获得流,并将它转换成一个的BitmapSource,但现在我不知道如何将图像从我的摄像头到WPF结合,与卡利(MVVM),每X毫秒....从我的摄像头在WPF中显示一个视频流?

<Image x:Name="imageWebcam" /> 

好的解决方案中。 我不能显示整个代码,但我可以解释一下,到WPF查看绑定:

<Image x:Name="ImageWebcam" /> 

在我的ViewModel(使用卡利框架,但是这可以很容易地适应一个MVVM模式),我必须图像绑定具有相同名称的功能:

public WriteableBitmap ImageWebcam 
    { 
     get 
     { 
      return this.imageWebcam; 
     } 
     set 
     { 
      this.imageWebcam = value; 
      this.NotifyOfPropertyChange(() => this.ImageWebcam); 
     } 
    } 

,并更新这个图片我用一个定时器,即必须初始化到视图模型构造:

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 WriteableBitmapNotifyOfPropertyChange(() => ImageWebcam)通知更新。

如果您有任何问题,请随时与我联系或发表评论。 如果您发现任何错误,请毫不犹豫地纠正我(语法&代码)。

您应该使用MediaElement显示视频,而不是Image控制:

XAML:

<MediaElement Source="{Binding VideoAddress}" /> 

视图模型:

private URI videoAddress=new URI("C:\video.wmv"); 
public URI VideoAddress 
{ 
    get { return videoAddress; } 
    set 
    { 
     videoAddress = value; 
     OnPropertyChanged("LeafName"); 
    } 
} 

此外,您可以使用WPF-MediaKit来显示来自WebCam的视频。或see this tutorial

更新:

您应该使用Image,显示的图像:

<Image x:Name="imageWebcam" /> 

和C#:

BitmapImage logo = new BitmapImage(); 
logo.BeginInit(); 
logo.UriSource = new Uri("C:/1.png"); 
logo.EndInit(); 

imageWebcam.Source = logo; 
+0

EmguCV捕获一个“Bgr图像帧”,所以我真的可以使用它作为视频? –

+0

只是为了显示图像,请参阅更新部分 – StepUp

+0

@EstebanChamard随时问任何问题。如果您觉得我的回复对您有帮助,那么您可以将我的回复标记为答案,以简化未来搜索其他人的过程。请阅读http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – StepUp

使用WriteableBitmap的为你的图像元素的来源。您应该在视图模型中提供一个回调函数,以传递每个新帧的帧数据。在后面的代码中从您的视图中订阅此回调。然后,您可以将每个帧写入位图。在这种情况下,我认为数据绑定不是一个好方法。

当视频帧的流WriteableBitmap的是迄今为止最好的选择在WPF处理:

WriteableBitmap