赞
踩
今天开始继续Avalonia练习。
本节:AForge
1.引入
AForge.Video.DirectShow
2.项目引入
前台代码(用image控件显示)
- <Window xmlns="https://github.com/avaloniaui"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:vm="using:AvaloniaApplication1.ViewModels"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
- x:Class="AvaloniaApplication1.Views.MainWindow"
- x:DataType="vm:MainWindowViewModel"
- Icon="/Assets/avalonia-logo.ico"
- Title="AvaloniaApplication1">
-
- <Design.DataContext>
- <!-- This only sets the DataContext for the previewer in an IDE,
- to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
- <vm:MainWindowViewModel/>
- </Design.DataContext>
- <Grid RowDefinitions="*,auto" ColumnDefinitions="*,auto">
- <ComboBox Name="ComboName" Width="120"/>
- <ComboBox Name="cbo_rate" Width="120"/>
- <Image Name="pictureBox1" Width="630"/>
- </Grid>
-
-
- </Window>
后台代码
- 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);
- }
- }
- /// <summary>
- /// 获取每个摄像头支持的分辨率
- /// </summary>
- /// <param name="videoCaptureDevice"></param>
- 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;
- }
- }
- }
运行效果(我遮挡了哈,长的太.....)
另外我封装了控件库,使用方法和网上旧版本一样。可以引入
AForgeNet.AvaloniaVideo
注意:旧版库不能使用,是netframework的,我已经编译新的版本,注意版本。
AForgeNet.AvaloniaVideo使用方法
1.引用包
- <Window xmlns="https://github.com/avaloniaui"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:vm="using:AvaloniaApplication2.ViewModels"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
- xmlns:uc="clr-namespace:AForgeNet.AvaloniaVideo;assembly=AForgeNet.AvaloniaVideo"
- x:Class="AvaloniaApplication2.Views.MainWindow"
- x:DataType="vm:MainWindowViewModel"
- Icon="/Assets/avalonia-logo.ico"
- Title="AvaloniaApplication2">
-
- <Design.DataContext>
- <!-- This only sets the DataContext for the previewer in an IDE,
- to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
- <vm:MainWindowViewModel/>
- </Design.DataContext>
- <Grid RowDefinitions="200,*" ColumnDefinitions="200,200,200,200,*">
- <ComboBox Name="VideolistBox" Width="120" Grid.Row="0" Grid.Column="0"/>
- <ComboBox Name="cbo_rate" Width="120" Grid.Row="0" Grid.Column="1"/>
- <Button Name="buttonStart" Click="Button_Click" Width="120" Grid.Row="0" Grid.Column="2"/>
- <Button Name="buttonStop" Click="Button_Click_1" Width="120" Grid.Row="0" Grid.Column="3"/>
- <uc:VideoPlayer Name = "VideoSourcePlayer" Grid.Row="1" Grid.ColumnSpan="4"/>
- </Grid>
-
- </Window>
后台代码
- 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();
- }
- }
- }
运行效果
注释图像滤波处理
运行效果
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。