AForge引用
1.創建WPF項目, 在NuGet安裝AForge相關SDK包:
2.項目引用
2.1.由於在WPF當中使用AForge,需要通過WindowsFormsHost嵌入在WPF當中使用, 所以需要給項目添加相關引用:
2.2.頁面添加命名空間
.xaml文件中,添加以下命名空間:
xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:aforge="clr-namespace:AForge.Controls;assembly=AForge.Controls
3.創建控件
為XAML中添加一個WindowsFormsHost 嵌入一個VideoSourcePlayer
<wfi:WindowsFormsHost >
<aforge:VideoSourcePlayer x:Name="player" Dock="Fill" />
</wfi:WindowsFormsHost>
初始化
在后台代碼中, 添加初始化代碼, 以下代碼模擬設置的第一個攝像頭, FilterInfoCollection實際為一個集合。
FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (videoDevices.Count > 0)
{
var videoDevice = new VideoCaptureDevice(videoDevices[0].MonikerString);
videoDevice.VideoResolution = videoDevice.VideoCapabilities[0]; //設置分辨率
player.VideoSource = videoDevice; //設置源
player.Start(); //啟動
}
實際效果(演示):
拍照
player.GetCurrentVideoFrame();