星火微课系统客户端
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. namespace XHWK.WKTool.system
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Drawing.Imaging;
  6. using System.IO;
  7. using System.Windows.Media.Imaging;
  8. using System.Windows.Threading;
  9. using AForge.Video.DirectShow;
  10. public static class CameraHelper
  11. {
  12. private static List<FilterInfo> _cameraDevices;
  13. private static VideoCaptureDevice _vedioCapture;
  14. private static Dispatcher _dispatcher;
  15. public static Action<BitmapImage> imageCallback;
  16. /// <summary>
  17. /// 获取或设置摄像头设备,无设备为null
  18. /// </summary>
  19. public static List<FilterInfo> CameraDevices
  20. {
  21. get => _cameraDevices;
  22. set => _cameraDevices = value;
  23. }
  24. /// <summary>
  25. /// 更新摄像头设备信息
  26. /// </summary>
  27. public static void UpdateCameraDevices()
  28. {
  29. // ReSharper disable once CollectionNeverUpdated.Local
  30. var devs = new FilterInfoCollection(FilterCategory.VideoInputDevice); //获取摄像头列表
  31. List<FilterInfo> allCamera = new List<FilterInfo>();
  32. foreach (FilterInfo dev in devs)
  33. {
  34. if (dev.Name != "screen-capture-recorder" && dev.Name != "OBS Virtual Camera")
  35. {
  36. allCamera.Add(dev);
  37. }
  38. }
  39. _cameraDevices = allCamera;
  40. }
  41. /// <summary>
  42. /// 设置使用的摄像头设备
  43. /// </summary>
  44. /// <param name="index">设备在CameraDevices中的索引</param>
  45. /// <param name="dispatcher"></param>
  46. /// <returns><see cref="bool"/></returns>
  47. public static bool StartCameraDevice(int index, Dispatcher dispatcher)
  48. {
  49. _dispatcher = dispatcher;
  50. //无设备,返回false
  51. if (_cameraDevices.Count <= 0 || index < 0)
  52. {
  53. return false;
  54. }
  55. if (index > _cameraDevices.Count - 1)
  56. {
  57. return false;
  58. }
  59. // 设定初始视频设备
  60. _vedioCapture = new VideoCaptureDevice(_cameraDevices[index].MonikerString);
  61. _vedioCapture.NewFrame += VedioCaptureNewFrame;
  62. _vedioCapture.Start();
  63. return true;
  64. }
  65. private static void VedioCaptureNewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
  66. {
  67. _dispatcher?.Invoke
  68. (
  69. () =>
  70. {
  71. using (MemoryStream ms = new MemoryStream())
  72. {
  73. eventArgs.Frame.Save(ms, ImageFormat.Bmp);
  74. BitmapImage image = new BitmapImage { CacheOption = BitmapCacheOption.OnLoad };
  75. image.BeginInit();
  76. image.StreamSource = new MemoryStream(ms.GetBuffer());
  77. image.EndInit();
  78. imageCallback?.Invoke(image);
  79. }
  80. }
  81. );
  82. }
  83. /// <summary>
  84. /// 关闭摄像头设备
  85. /// </summary>
  86. public static void CloseDevice()
  87. {
  88. try
  89. {
  90. if (_vedioCapture != null)
  91. {
  92. _vedioCapture.NewFrame -= VedioCaptureNewFrame;
  93. if (_vedioCapture.IsRunning)
  94. {
  95. _vedioCapture.SignalToStop();
  96. }
  97. _vedioCapture = null;
  98. }
  99. }
  100. catch (Exception)
  101. {
  102. // ignored
  103. }
  104. }
  105. }
  106. }