WPF攝像頭使用(AForge)


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();


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM