星火直播PC
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.

3 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using AForge.Controls;
  2. using AForge.Video.DirectShow;
  3. using System;
  4. using System.Drawing;
  5. using System.Drawing.Imaging;
  6. using System.IO;
  7. namespace Common.system
  8. {
  9. public static class CameraHelper
  10. {
  11. private static FilterInfoCollection _cameraDevices;
  12. private static VideoCaptureDevice div = null;
  13. private static VideoSourcePlayer sourcePlayer = new VideoSourcePlayer();
  14. private static bool _isDisplay = false;
  15. //指示_isDisplay设置为true后,是否设置了其他的sourcePlayer,若未设置则_isDisplay重设为false
  16. private static bool isSet = false;
  17. /// <summary>
  18. /// 获取或设置摄像头设备,无设备为null
  19. /// </summary>
  20. public static FilterInfoCollection CameraDevices
  21. {
  22. get => _cameraDevices;
  23. set => _cameraDevices = value;
  24. }
  25. /// <summary>
  26. /// 指示是否显示摄像头视频画面
  27. /// 默认false
  28. /// </summary>
  29. public static bool IsDisplay
  30. {
  31. get => _isDisplay;
  32. set => _isDisplay = value;
  33. }
  34. /// <summary>
  35. /// 获取或设置VideoSourcePlayer控件,
  36. /// 只有当IsDisplay设置为true时,该属性才可以设置成功
  37. /// </summary>
  38. public static VideoSourcePlayer SourcePlayer
  39. {
  40. get => sourcePlayer;
  41. set
  42. {
  43. if (_isDisplay)
  44. {
  45. sourcePlayer = value;
  46. isSet = true;
  47. }
  48. }
  49. }
  50. /// <summary>
  51. /// 更新摄像头设备信息
  52. /// </summary>
  53. public static void UpdateCameraDevices()
  54. {
  55. _cameraDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
  56. }
  57. /// <summary>
  58. /// 设置使用的摄像头设备
  59. /// </summary>
  60. /// <param name="index">设备在CameraDevices中的索引</param>
  61. /// <returns><see cref="bool"/></returns>
  62. public static bool SetCameraDevice(int index)
  63. {
  64. if (!isSet)
  65. {
  66. _isDisplay = false;
  67. }
  68. //无设备,返回false
  69. if (_cameraDevices.Count <= 0 || index < 0)
  70. {
  71. return false;
  72. }
  73. if (index > _cameraDevices.Count - 1)
  74. {
  75. return false;
  76. }
  77. // 设定初始视频设备
  78. div = new VideoCaptureDevice(_cameraDevices[index].MonikerString);
  79. sourcePlayer.VideoSource = div;
  80. div.Start();
  81. sourcePlayer.Start();
  82. return true;
  83. }
  84. /// <summary>
  85. /// 截取一帧图像并保存
  86. /// </summary>
  87. /// <param name="filePath">图像保存路径</param>
  88. /// <param name="fileName">保存的图像文件名</param>
  89. /// <returns>如果保存成功,则返回完整路径,否则为 null</returns>
  90. public static string CaptureImage(string filePath, string fileName = null)
  91. {
  92. if (sourcePlayer.VideoSource == null)
  93. {
  94. return null;
  95. }
  96. if (!Directory.Exists(filePath))
  97. {
  98. Directory.CreateDirectory(filePath);
  99. }
  100. try
  101. {
  102. Image bitmap = sourcePlayer.GetCurrentVideoFrame();
  103. if (bitmap != null)
  104. {
  105. if (fileName == null)
  106. {
  107. fileName = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
  108. }
  109. string fullPath = Path.Combine(filePath, fileName + "-cap.jpg");
  110. System.Drawing.Bitmap objNewPic = new System.Drawing.Bitmap(bitmap, 172, 124);//图片保存的大小尺寸
  111. objNewPic.Save(fullPath, ImageFormat.Jpeg);
  112. //bitmap.Save(fullPath, ImageFormat.Jpeg);
  113. bitmap.Dispose();
  114. objNewPic.Dispose();
  115. return fullPath;
  116. }
  117. return null;
  118. }
  119. catch (Exception)
  120. {
  121. return null;
  122. }
  123. }
  124. /// <summary>
  125. /// 关闭摄像头设备
  126. /// </summary>
  127. public static void CloseDevice()
  128. {
  129. if (div != null && div.IsRunning)
  130. {
  131. sourcePlayer.Stop();
  132. div.SignalToStop();
  133. div = null;
  134. _cameraDevices = null;
  135. }
  136. }
  137. }
  138. }