CreatCamera.cs 2.8 KB

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