星火微课系统客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CameraHelper.cs 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 VideoSourcePlayer _sourcePlayer = new VideoSourcePlayer();
  17. //指示_isDisplay设置为true后,是否设置了其他的sourcePlayer,若未设置则_isDisplay重设为false
  18. private static bool _isSet;
  19. private static Dispatcher _dispatcher;
  20. public static Action<BitmapImage> imageCallback;
  21. /// <summary>
  22. /// 获取或设置摄像头设备,无设备为null
  23. /// </summary>
  24. public static List<FilterInfo> CameraDevices
  25. {
  26. get => _cameraDevices;
  27. set => _cameraDevices = value;
  28. }
  29. /// <summary>
  30. /// 更新摄像头设备信息
  31. /// </summary>
  32. public static void UpdateCameraDevices()
  33. {
  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. MemoryStream ms = new MemoryStream();
  78. eventArgs.Frame.Save(ms, ImageFormat.Bmp);
  79. BitmapImage image = new BitmapImage();
  80. image.BeginInit();
  81. image.StreamSource = new MemoryStream(ms.GetBuffer());
  82. ms.Close();
  83. ms.Dispose();
  84. image.EndInit();
  85. imageCallback?.Invoke(image);
  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 && _div.IsRunning)
  140. {
  141. _div.NewFrame -= _div_NewFrame;
  142. _sourcePlayer.Stop();
  143. _div.SignalToStop();
  144. _div = null;
  145. }
  146. }
  147. }
  148. }