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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. namespace XHWK.WKTool.system
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.Drawing.Imaging;
  7. using System.IO;
  8. using System.Windows.Media.Imaging;
  9. using System.Windows.Threading;
  10. using AForge.Controls;
  11. using AForge.Video.DirectShow;
  12. public static class CameraHelper
  13. {
  14. private static List<FilterInfo> _cameraDevices;
  15. private static VideoCaptureDevice _div;
  16. private static readonly VideoSourcePlayer SourcePlayer = new VideoSourcePlayer();
  17. //指示_isDisplay设置为true后,是否设置了其他的sourcePlayer,若未设置则_isDisplay重设为false
  18. private static Dispatcher _dispatcher;
  19. public static Action<BitmapImage> imageCallback;
  20. /// <summary>
  21. /// 获取或设置摄像头设备,无设备为null
  22. /// </summary>
  23. public static List<FilterInfo> CameraDevices
  24. {
  25. get => _cameraDevices;
  26. set => _cameraDevices = value;
  27. }
  28. /// <summary>
  29. /// 更新摄像头设备信息
  30. /// </summary>
  31. public static void UpdateCameraDevices()
  32. {
  33. // ReSharper disable once CollectionNeverUpdated.Local
  34. var devs = new FilterInfoCollection(FilterCategory.VideoInputDevice); //获取摄像头列表
  35. List<FilterInfo> allCamera = new List<FilterInfo>();
  36. foreach (FilterInfo dev in devs)
  37. {
  38. if (dev.Name != "screen-capture-recorder" && dev.Name != "OBS Virtual Camera")
  39. {
  40. allCamera.Add(dev);
  41. }
  42. }
  43. _cameraDevices = allCamera;
  44. }
  45. /// <summary>
  46. /// 设置使用的摄像头设备
  47. /// </summary>
  48. /// <param name="index">设备在CameraDevices中的索引</param>
  49. /// <param name="dispatcher"></param>
  50. /// <returns><see cref="bool"/></returns>
  51. public static bool StartCameraDevice(int index, Dispatcher dispatcher)
  52. {
  53. _dispatcher = dispatcher;
  54. //无设备,返回false
  55. if (_cameraDevices.Count <= 0 || index < 0)
  56. {
  57. return false;
  58. }
  59. if (index > _cameraDevices.Count - 1)
  60. {
  61. return false;
  62. }
  63. // 设定初始视频设备
  64. _div = new VideoCaptureDevice(_cameraDevices[index].MonikerString);
  65. _div.NewFrame += _div_NewFrame;
  66. SourcePlayer.VideoSource = _div;
  67. _div.Start();
  68. SourcePlayer.Start();
  69. return true;
  70. }
  71. private static void _div_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
  72. {
  73. _dispatcher?.Invoke
  74. (
  75. () =>
  76. {
  77. using (MemoryStream ms = new MemoryStream())
  78. {
  79. eventArgs.Frame.Save(ms, ImageFormat.Bmp);
  80. BitmapImage image = new BitmapImage { CacheOption = BitmapCacheOption.OnLoad };
  81. image.BeginInit();
  82. image.StreamSource = new MemoryStream(ms.GetBuffer());
  83. image.EndInit();
  84. imageCallback?.Invoke(image);
  85. }
  86. }
  87. );
  88. }
  89. /// <summary>
  90. /// 截取一帧图像并保存
  91. /// </summary>
  92. /// <param name="filePath">图像保存路径</param>
  93. /// <param name="fileName">保存的图像文件名</param>
  94. /// <returns>如果保存成功,则返回完整路径,否则为 null</returns>
  95. public static string CaptureImage(string filePath, string fileName = null)
  96. {
  97. if (SourcePlayer.VideoSource == null)
  98. {
  99. return null;
  100. }
  101. if (!Directory.Exists(filePath))
  102. {
  103. Directory.CreateDirectory(filePath);
  104. }
  105. try
  106. {
  107. Image bitmap = SourcePlayer.GetCurrentVideoFrame();
  108. if (bitmap != null)
  109. {
  110. if (fileName == null)
  111. {
  112. fileName = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
  113. }
  114. string fullPath = Path.Combine(filePath, fileName + "-cap.jpg");
  115. Bitmap objNewPic = new Bitmap
  116. (
  117. bitmap,
  118. 172,
  119. 124
  120. ); //图片保存的大小尺寸
  121. objNewPic.Save(fullPath, ImageFormat.Jpeg);
  122. //bitmap.Save(fullPath, ImageFormat.Jpeg);
  123. bitmap.Dispose();
  124. objNewPic.Dispose();
  125. return fullPath;
  126. }
  127. return null;
  128. }
  129. catch (Exception)
  130. {
  131. return null;
  132. }
  133. }
  134. /// <summary>
  135. /// 关闭摄像头设备
  136. /// </summary>
  137. public static void CloseDevice()
  138. {
  139. if (_div != null)
  140. {
  141. _div.NewFrame -= _div_NewFrame;
  142. if (SourcePlayer.IsRunning)
  143. {
  144. SourcePlayer.Stop();
  145. }
  146. if (_div.IsRunning)
  147. {
  148. _div.SignalToStop();
  149. }
  150. _div = null;
  151. }
  152. }
  153. }
  154. }