当前位置:   article > 正文

Avalonia学习(十七)-AForge(视频)_avalonia 调用摄像头

avalonia 调用摄像头

今天开始继续Avalonia练习。

本节:AForge

1.引入

AForge.Video.DirectShow

2.项目引入

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

  1. <Window xmlns="https://github.com/avaloniaui"
  2. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  3. xmlns:vm="using:AvaloniaApplication1.ViewModels"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
  7. x:Class="AvaloniaApplication1.Views.MainWindow"
  8. x:DataType="vm:MainWindowViewModel"
  9. Icon="/Assets/avalonia-logo.ico"
  10. Title="AvaloniaApplication1">
  11. <Design.DataContext>
  12. <!-- This only sets the DataContext for the previewer in an IDE,
  13. to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
  14. <vm:MainWindowViewModel/>
  15. </Design.DataContext>
  16. <Grid RowDefinitions="*,auto" ColumnDefinitions="*,auto">
  17. <ComboBox Name="ComboName" Width="120"/>
  18. <ComboBox Name="cbo_rate" Width="120"/>
  19. <Image Name="pictureBox1" Width="630"/>
  20. </Grid>
  21. </Window>

后台代码

  1. using AForge.Video.DirectShow;
  2. using Avalonia.Controls;
  3. using System.Drawing;
  4. using System.IO;
  5. using System;
  6. using System.Drawing.Imaging;
  7. using AForge.Video;
  8. using Avalonia.Threading;
  9. namespace AvaloniaApplication1.Views
  10. {
  11. public partial class MainWindow : Window
  12. {
  13. private FilterInfoCollection videoDevices;
  14. private VideoCaptureDevice videoDevice;
  15. private VideoCapabilities[] videoCapabilities;
  16. private VideoCaptureDevice videoSource;
  17. public MainWindow()
  18. {
  19. InitializeComponent();
  20. videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
  21. if (videoDevices.Count == 0)
  22. throw new ApplicationException();
  23. ComboName.Items.Clear();
  24. foreach (FilterInfo device in videoDevices)
  25. {
  26. ComboName.Items.Add(device.Name);
  27. }
  28. ComboName.SelectedIndex = 0;
  29. //不是全局变量不成功
  30. videoSource = new VideoCaptureDevice(videoDevices[ComboName.SelectedIndex].MonikerString);
  31. for (int i = 0; i < videoSource.VideoCapabilities.Length; i++)
  32. {
  33. cbo_rate.Items.Add(videoSource.VideoCapabilities[i].FrameSize.Width + "*" + videoSource.VideoCapabilities[i].FrameSize.Height);
  34. }
  35. cbo_rate.SelectedIndex = 0;
  36. videoSource.VideoResolution = videoSource.VideoCapabilities[cbo_rate.SelectedIndex];
  37. videoSource.NewFrame += new NewFrameEventHandler(videoSource_NewFrame);
  38. videoSource.Start();
  39. }
  40. private void videoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
  41. {
  42. Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone();
  43. Dispatcher.UIThread.Invoke(() =>
  44. {
  45. using (MemoryStream memory = new MemoryStream())
  46. {
  47. bitmap.Save(memory, ImageFormat.Png);
  48. memory.Position = 0;
  49. //AvIrBitmap is our new Avalonia compatible image. You can pass this to your view
  50. Avalonia.Media.Imaging.Bitmap AvIrBitmap = new Avalonia.Media.Imaging.Bitmap(memory);
  51. Avalonia.Media.IImage? bitmapSource = null;
  52. pictureBox1.Source = AvIrBitmap;
  53. }
  54. });
  55. }
  56. private void Cbo_camera_SelectionChanged(object? sender, SelectionChangedEventArgs e)
  57. {
  58. if (videoDevices.Count != 0)
  59. {
  60. //获取每个摄像头支持的分辨率
  61. videoDevice = new VideoCaptureDevice(videoDevices[ComboName.SelectedIndex].MonikerString);
  62. GetDeviceResolution(videoDevice);
  63. }
  64. }
  65. /// <summary>
  66. /// 获取每个摄像头支持的分辨率
  67. /// </summary>
  68. /// <param name="videoCaptureDevice"></param>
  69. private void GetDeviceResolution(VideoCaptureDevice videoCaptureDevice)
  70. {
  71. cbo_rate.Items.Clear();
  72. videoCapabilities = videoCaptureDevice.VideoCapabilities;
  73. foreach (VideoCapabilities capabilty in videoCapabilities)
  74. {
  75. cbo_rate.Items.Add($"{capabilty.FrameSize.Width} x {capabilty.FrameSize.Height}");
  76. }
  77. cbo_rate.SelectedIndex = 0;
  78. }
  79. }
  80. }

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

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

AForgeNet.AvaloniaVideo

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

AForgeNet.AvaloniaVideo使用方法

1.引用包

  1. <Window xmlns="https://github.com/avaloniaui"
  2. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  3. xmlns:vm="using:AvaloniaApplication2.ViewModels"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
  7. xmlns:uc="clr-namespace:AForgeNet.AvaloniaVideo;assembly=AForgeNet.AvaloniaVideo"
  8. x:Class="AvaloniaApplication2.Views.MainWindow"
  9. x:DataType="vm:MainWindowViewModel"
  10. Icon="/Assets/avalonia-logo.ico"
  11. Title="AvaloniaApplication2">
  12. <Design.DataContext>
  13. <!-- This only sets the DataContext for the previewer in an IDE,
  14. to set the actual DataContext for runtime, set the DataContext property in code (look at App.axaml.cs) -->
  15. <vm:MainWindowViewModel/>
  16. </Design.DataContext>
  17. <Grid RowDefinitions="200,*" ColumnDefinitions="200,200,200,200,*">
  18. <ComboBox Name="VideolistBox" Width="120" Grid.Row="0" Grid.Column="0"/>
  19. <ComboBox Name="cbo_rate" Width="120" Grid.Row="0" Grid.Column="1"/>
  20. <Button Name="buttonStart" Click="Button_Click" Width="120" Grid.Row="0" Grid.Column="2"/>
  21. <Button Name="buttonStop" Click="Button_Click_1" Width="120" Grid.Row="0" Grid.Column="3"/>
  22. <uc:VideoPlayer Name = "VideoSourcePlayer" Grid.Row="1" Grid.ColumnSpan="4"/>
  23. </Grid>
  24. </Window>

后台代码

  1. using AForge.Imaging.Filters;
  2. using AForge.Video;
  3. using AForge.Video.DirectShow;
  4. using Avalonia.Controls;
  5. using Avalonia.Threading;
  6. using SkiaSharp;
  7. using System;
  8. namespace AvaloniaApplication2.Views
  9. {
  10. public partial class MainWindow : Window
  11. {
  12. private FilterInfoCollection videoDevices;
  13. private VideoCaptureDevice videoDevice;
  14. private VideoCapabilities[] videoCapabilities;
  15. private VideoCaptureDevice videoSource;
  16. public MainWindow()
  17. {
  18. InitializeComponent();
  19. videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
  20. if (videoDevices.Count == 0)
  21. throw new ApplicationException();
  22. VideolistBox.Items.Clear();
  23. foreach (FilterInfo device in videoDevices)
  24. {
  25. VideolistBox.Items.Add(device.Name);
  26. }
  27. VideolistBox.SelectedIndex = 0;
  28. videoSource = new VideoCaptureDevice(videoDevices[VideolistBox.SelectedIndex].MonikerString);
  29. for (int i = 0; i < videoSource.VideoCapabilities.Length; i++)
  30. {
  31. cbo_rate.Items.Add(videoSource.VideoCapabilities[i].FrameSize.Width + "*" + videoSource.VideoCapabilities[i].FrameSize.Height);
  32. }
  33. cbo_rate.SelectedIndex = 0;
  34. VideoSourcePlayer.NewFrame += VideoSourcePlayer_NewFrame;
  35. }
  36. private void VideoSourcePlayer_NewFrame(object sender, ref System.Drawing.Bitmap image)
  37. {
  38. //图像过滤
  39. FiltersSequence filter = new FiltersSequence();
  40. filter.Add(new Grayscale(0.2125, 0.7154, 0.0721));
  41. filter.Add(new Threshold(128));
  42. image= filter.Apply(image);
  43. }
  44. private void Button_Click(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
  45. {
  46. videoSource.VideoResolution = videoSource.VideoCapabilities[cbo_rate.SelectedIndex];
  47. VideoSourcePlayer.VideoSource = videoSource;
  48. VideoSourcePlayer.Start();
  49. }
  50. private void Button_Click_1(object? sender, Avalonia.Interactivity.RoutedEventArgs e)
  51. {
  52. VideoSourcePlayer.Stop();
  53. }
  54. }
  55. }

运行效果

注释图像滤波处理

运行效果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/889322
推荐阅读
相关标签
  

闽ICP备14008679号