wpf使用Aforge控件实现多摄像头的展示与拍照

首先安装相关NuGet包:右击项目→管理NuGet包→点击【浏览】→搜索【Aforge】→安装以下项目

wpf使用Aforge控件实现多摄像头的展示与拍照

MainWindow中引用控件

        xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
        xmlns:aforge ="clr-namespace:AForge.Controls;assembly=AForge.Controls"

MainWindow中添加控件,多个摄像头命名则是名称+***,后台可通过FindName找到对应控件,简化代码。

    <wfi:WindowsFormsHost Margin="5">
                <aforge:VideoSourcePlayer x:Name="sourcePlayer0" Width="640" Height="480"></aforge:VideoSourcePlayer>
            </wfi:WindowsFormsHost>

后台程序加载完成后得到摄像头列表,并展现(注意:高清摄像头需设置分辨率,否则一直显示connecting)

 void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            videoCount = videoDevices.Count;
            if (videoCount > maxCameraCount)
                videoCount = maxCameraCount;
            VideoCaptureDevice videoDevice = null;
            object box;
            for (int i = 0; i < videoCount; i++)
            {
                box = this.FindName("sourcePlayer" + i);
                if (box is AForge.Controls.VideoSourcePlayer)
                {
                    videoDevice = new VideoCaptureDevice(videoDevices[i].MonikerString);
                    //高清摄像头需设置支持的分辨率,否则无法显示,一直connecting
                    videoDevice.VideoResolution = videoDevice.VideoCapabilities[0];
                    (box as AForge.Controls.VideoSourcePlayer).VideoSource = videoDevice;
                    (box as AForge.Controls.VideoSourcePlayer).Start();

                    Thread thread = new Thread(new ParameterizedThreadStart(CompareFaceImg));
                    thread.IsBackground = true;
                    thread.Start(i);
                }
            }

        }

拍照并保存至指定路径

     private void CompareFaceImg(object threadIndexobj)
        {
            Thread.Sleep(5000);
            int threadIndex = (int)threadIndexobj;
            while (true)
            {
                string file1 = AppDomain.CurrentDomain.BaseDirectory + "CurrentPhoto\\" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".bmp";

                this.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
                    (ThreadStart)delegate ()
                    {
                        object box = this.FindName("sourcePlayer" + threadIndex);
                        if ((box as AForge.Controls.VideoSourcePlayer).IsRunning)
                        {
                            System.Drawing.Bitmap bitmap = sourcePlayer0.GetCurrentVideoFrame();
                            bitmap.Save(file1, ImageFormat.Bmp);
                            bitmap.Dispose();
                        }

                    }
                    );
                Thread.Sleep(1000);
            }
        }

关闭时停止视频

   void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            object box;
            for (int i = 0; i < videoCount; i++)
            {
                box = this.FindName("sourcePlayer" + i);
                if (box is AForge.Controls.VideoSourcePlayer)
                {
                    if ((box as AForge.Controls.VideoSourcePlayer).IsRunning)
                    {
                        (box as AForge.Controls.VideoSourcePlayer).SignalToStop();
                        (box as AForge.Controls.VideoSourcePlayer).WaitForStop();
                    }
                }
            }

        }