我有一個問題,我如何顯示從我的攝像頭在WPF中的圖像容器中的流?我已經與Emgu獲得流,並將它轉換成一個的BitmapSource,但現在我不知道如何將圖像從我的攝像頭到WPF結合,與卡利(MVVM),每X毫秒....從我的攝像頭在WPF中顯示一個視頻流?
<Image x:Name="imageWebcam" />
我有一個問題,我如何顯示從我的攝像頭在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 WriteableBitmap
與NotifyOfPropertyChange(() => 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;
使用WriteableBitmap的爲你的圖像元素的來源。您應該在視圖模型中提供一個回調函數,以傳遞每個新幀的幀數據。在後面的代碼中從您的視圖中訂閱此回調。然後,您可以將每個幀寫入位圖。在這種情況下,我認爲數據綁定不是一個好方法。
當視頻幀的流WriteableBitmap的是迄今爲止最好的選擇在WPF處理:
EmguCV捕獲一個「Bgr圖像幀」,所以我真的可以使用它作爲視頻? –
只是爲了顯示圖像,請參閱更新部分 – StepUp
@EstebanChamard隨時問任何問題。如果您覺得我的回覆對您有幫助,那麼您可以將我的回覆標記爲答案,以簡化未來搜索其他人的過程。請閱讀http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – StepUp