星火微课系统客户端
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 4.2KB

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