Avalonia学习(十七)-AForge(视频)

今天开始继续Avalonia练习。

本节:AForge

1.引入

AForge.Video.DirectShow

2.项目引入

前台代码(用image控件显示)



    
        
        
    
  
    
    
    
  
 
    

后台代码

using AForge.Video.DirectShow;
using Avalonia.Controls;
using System.Drawing;
using System.IO;
using System;
using System.Drawing.Imaging;
using AForge.Video;
using Avalonia.Threading;

namespace AvaloniaApplication1.Views
{
    public partial class MainWindow : Window
    {
        private FilterInfoCollection videoDevices;
        private VideoCaptureDevice videoDevice;
        private VideoCapabilities[] videoCapabilities;
        private VideoCaptureDevice videoSource;

        public MainWindow()
        {
            InitializeComponent();
            videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            if (videoDevices.Count == 0)
                throw new ApplicationException();
            ComboName.Items.Clear();
            foreach (FilterInfo device in videoDevices)
            {
                ComboName.Items.Add(device.Name);
            }
            ComboName.SelectedIndex = 0;
            //不是全局变量不成功
            videoSource = new VideoCaptureDevice(videoDevices[ComboName.SelectedIndex].MonikerString);
            for (int i = 0; i < videoSource.VideoCapabilities.Length; i++)
            {
                cbo_rate.Items.Add(videoSource.VideoCapabilities[i].FrameSize.Width + "*" + videoSource.VideoCapabilities[i].FrameSize.Height);
            }
            cbo_rate.SelectedIndex = 0;
            videoSource.VideoResolution = videoSource.VideoCapabilities[cbo_rate.SelectedIndex];
            videoSource.NewFrame += new NewFrameEventHandler(videoSource_NewFrame);
            videoSource.Start();

        }

        private void videoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone();
            Dispatcher.UIThread.Invoke(() =>
            {
               

                using (MemoryStream memory = new MemoryStream())
                {
                    bitmap.Save(memory, ImageFormat.Png);
                    memory.Position = 0;

                    //AvIrBitmap is our new Avalonia compatible image. You can pass this to your view
                    Avalonia.Media.Imaging.Bitmap AvIrBitmap = new Avalonia.Media.Imaging.Bitmap(memory);
                    Avalonia.Media.IImage? bitmapSource = null;
                    pictureBox1.Source = AvIrBitmap;
                }

               
            });
          
        }

       

        private void Cbo_camera_SelectionChanged(object? sender, SelectionChangedEventArgs e)
        {

            if (videoDevices.Count != 0)
            {
                //获取每个摄像头支持的分辨率
                videoDevice = new VideoCaptureDevice(videoDevices[ComboName.SelectedIndex].MonikerString);
                GetDeviceResolution(videoDevice);
            }
        }
        /// 
        /// 获取每个摄像头支持的分辨率
        /// 
        /// 
        private void GetDeviceResolution(VideoCaptureDevice videoCaptureDevice)
        {
            cbo_rate.Items.Clear();
            videoCapabilities = videoCaptureDevice.VideoCapabilities;
            foreach (VideoCapabilities capabilty in videoCapabilities)
            {
                cbo_rate.Items.Add($"{capabilty.FrameSize.Width} x {capabilty.FrameSize.Height}");
            }
            cbo_rate.SelectedIndex = 0;
        }
    }
}

运行效果(我遮挡了哈,长的太.....)

Avalonia学习(十七)-AForge(视频)_第1张图片

另外我封装了控件库,使用方法和网上旧版本一样。可以引入

AForgeNet.AvaloniaVideo

注意:旧版库不能使用,是netframework的,我已经编译新的版本,注意版本。

AForgeNet.AvaloniaVideo使用方法

1.引用包



    
        
        
    
  
    
    
    

后台代码

using AForge.Imaging.Filters;
using AForge.Video;
using AForge.Video.DirectShow;
using Avalonia.Controls;
using Avalonia.Threading;
using SkiaSharp;
using System;

namespace AvaloniaApplication2.Views
{
    public partial class MainWindow : Window
    {
        private FilterInfoCollection videoDevices;
        private VideoCaptureDevice videoDevice;
        private VideoCapabilities[] videoCapabilities;
        private VideoCaptureDevice videoSource;
        public MainWindow()
        {
            InitializeComponent();
            videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            if (videoDevices.Count == 0)
                throw new ApplicationException();
            VideolistBox.Items.Clear();
            foreach (FilterInfo device in videoDevices)
            {
                VideolistBox.Items.Add(device.Name);
            }
            VideolistBox.SelectedIndex = 0;
            videoSource = new VideoCaptureDevice(videoDevices[VideolistBox.SelectedIndex].MonikerString);
            for (int i = 0; i < videoSource.VideoCapabilities.Length; i++)
            {
                cbo_rate.Items.Add(videoSource.VideoCapabilities[i].FrameSize.Width + "*" + videoSource.VideoCapabilities[i].FrameSize.Height);
            }
            cbo_rate.SelectedIndex = 0;
            VideoSourcePlayer.NewFrame += VideoSourcePlayer_NewFrame;


        }

        private void VideoSourcePlayer_NewFrame(object sender, ref System.Drawing.Bitmap image)
        {
            //图像过滤
            FiltersSequence filter = new FiltersSequence();
            filter.Add(new Grayscale(0.2125, 0.7154, 0.0721));
            filter.Add(new Threshold(128));
            image=  filter.Apply(image);
        }

        private void Button_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
        {
            videoSource.VideoResolution = videoSource.VideoCapabilities[cbo_rate.SelectedIndex];
            VideoSourcePlayer.VideoSource = videoSource;
            VideoSourcePlayer.Start();
        }

        private void Button_Click_1(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
        {
            VideoSourcePlayer.Stop();
        }
    }
}

运行效果

Avalonia学习(十七)-AForge(视频)_第2张图片

注释图像滤波处理

Avalonia学习(十七)-AForge(视频)_第3张图片

运行效果

Avalonia学习(十七)-AForge(视频)_第4张图片

你可能感兴趣的:(学习,Avalonia)