CreatCamera2.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using SHJX.Service.Common.Logging;
  2. using Microsoft.Extensions.Logging;
  3. using SHJX.Service.Common.ReadXML;
  4. namespace shjxCamera
  5. {
  6. public class CreatCamera2
  7. {
  8. private static readonly ILogger logger = LogFactory.BuildLogger(typeof(CreatCamera2));
  9. private static ReadConfigUtil _config =ReadConfigUtil.Instance;
  10. /// <summary>
  11. /// 摄像头
  12. /// </summary>
  13. public WebCamera2 WebCam { get; set; }
  14. /// <summary>
  15. /// 是否检测到摄像头
  16. /// </summary>
  17. public static bool HadWebcams { get; set; }
  18. /// <summary>
  19. /// CreatCamera实例
  20. /// </summary>
  21. private static CreatCamera2 _camera;
  22. /// <summary>
  23. /// 锁
  24. /// </summary>
  25. private static readonly object obj_lock = new();
  26. /// <summary>
  27. /// 摄像头传感器名称
  28. /// </summary>
  29. /// "USB CAMERA"
  30. /// "Integrated Camera"
  31. private readonly string CamSensorName = _config.LogiCamera2Name;// "Logitech HD Webcam C270";// "Logi C270 HD WebCam";//
  32. /// <summary>
  33. /// 初始化
  34. /// </summary>
  35. /// <param name="log"></param>
  36. public CreatCamera2()
  37. {
  38. Titration2Settings.SensorPara = new SensorsParameter2();
  39. string camName = "";
  40. HadWebcams = false;
  41. for (int i = 0; i < WebCamera2.GetCameras().Count; i++)
  42. {
  43. // 部分型号摄像头名称包含特殊符号,难以输入,故修改为名称关键词识别匹配,且兼容之前版本
  44. if (!WebCamera2.GetCameraName(i).ToLower().Contains(CamSensorName.ToLower()))
  45. {
  46. continue;
  47. }
  48. WebCam = null;
  49. WebCam = new WebCamera2(WebCamera2.GetCameraMoniker(i));
  50. camName = WebCam.VidCapDevice2.Source.Split('&')[3];
  51. logger.LogInformation("相机2编号为" + camName + ",请配写入配置文件中");
  52. if (_config.LogiCamera2.Equals(camName))
  53. {
  54. HadWebcams = true;
  55. break;
  56. }
  57. }
  58. if (!HadWebcams)
  59. {
  60. logger.LogError("未找到摄像头2!");
  61. }
  62. else
  63. {
  64. logger.LogDebug($"找到摄像头2!");
  65. }
  66. }
  67. /// <summary>
  68. /// 默认值
  69. /// </summary>
  70. public static CreatCamera2 Instance
  71. {
  72. get
  73. {
  74. lock (obj_lock)
  75. {
  76. if (_camera is null)
  77. {
  78. _camera = new CreatCamera2();
  79. }
  80. return _camera;
  81. }
  82. }
  83. }
  84. }
  85. }