ImageProcessingAlgorithmHikVisionFactory.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // ImageProcessingAlgorithmFactory.cs 识别算法工厂,用于注册算法实现
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace MvvmScaffoldFrame48.DLL.ThreadManager
  8. {
  9. public static class ImageProcessingAlgorithmHikVisionFactory
  10. {
  11. #region 变量与实例
  12. // 存储所有算法实现类型的字典
  13. private static readonly Dictionary<string, Type> _algorithmTypes = new Dictionary<string, Type>();
  14. #endregion
  15. #region 构造方法
  16. /// <summary>
  17. /// 静态构造函数,在类加载时注册所有可用的算法实现
  18. /// </summary>
  19. static ImageProcessingAlgorithmHikVisionFactory()
  20. {
  21. // 注册所有可用的算法实现
  22. RegisterAlgorithm<ProcessingAlgorithm>("ProcessingAlgorithm");
  23. // 可以继续注册更多算法...
  24. }
  25. #endregion
  26. #region 私有方法
  27. /// <summary>
  28. /// 注册算法实现
  29. /// </summary>
  30. /// <typeparam name="T">算法实现类型</typeparam>
  31. /// <param name="algorithmName">算法名称</param>
  32. private static void RegisterAlgorithm<T>(string algorithmName) where T : IImageProcessingAlgorithmHikVision, new()
  33. {
  34. _algorithmTypes[algorithmName] = typeof(T);
  35. }
  36. #endregion
  37. #region 公有方法
  38. /// <summary>
  39. /// 根据算法名称创建算法实例
  40. /// </summary>
  41. /// <param name="algorithmName">算法名称</param>
  42. /// <returns>算法实例</returns>
  43. public static IImageProcessingAlgorithmHikVision CreateAlgorithm(string algorithmName)
  44. {
  45. if (_algorithmTypes.ContainsKey(algorithmName))
  46. {
  47. return (IImageProcessingAlgorithmHikVision)Activator.CreateInstance(_algorithmTypes[algorithmName]);
  48. }
  49. else
  50. {
  51. Console.WriteLine($"未找到名为 '{algorithmName}' 的算法实现");
  52. return null;
  53. }
  54. }
  55. /// <summary>
  56. /// 获取所有已注册的算法名称
  57. /// </summary>
  58. /// <returns>算法名称列表</returns>
  59. public static List<string> GetAvailableAlgorithms()
  60. {
  61. return _algorithmTypes.Keys.ToList();
  62. }
  63. /// <summary>
  64. /// 检查算法是否存在
  65. /// </summary>
  66. /// <param name="algorithmName">算法名称</param>
  67. /// <returns>是否存在</returns>
  68. public static bool AlgorithmExists(string algorithmName)
  69. {
  70. return _algorithmTypes.ContainsKey(algorithmName);
  71. }
  72. #endregion
  73. }
  74. }