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

CameraHelper.cs 4.1KB

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