ImageProcessingAlgorithmHikVisionFactory.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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.ImageAlgorithm
  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. RegisterAlgorithm<ProcessingAlgorithm_CCDShuLi>("ProcessingAlgorithm_CCDShuLi");
  24. // 可以继续注册更多算法...
  25. }
  26. #endregion
  27. #region 私有方法
  28. /// <summary>
  29. /// 注册算法实现
  30. /// </summary>
  31. /// <typeparam name="T">算法实现类型</typeparam>
  32. /// <param name="algorithmName">算法名称</param>
  33. private static void RegisterAlgorithm<T>(string algorithmName) where T : IImageProcessingAlgorithmHikVision, new()
  34. {
  35. _algorithmTypes[algorithmName] = typeof(T);
  36. }
  37. #endregion
  38. #region 公有方法
  39. /// <summary>
  40. /// 根据算法名称创建算法实例
  41. /// </summary>
  42. /// <param name="algorithmName">算法名称</param>
  43. /// <returns>算法实例</returns>
  44. public static IImageProcessingAlgorithmHikVision CreateAlgorithm(string algorithmName)
  45. {
  46. if (_algorithmTypes.ContainsKey(algorithmName))
  47. {
  48. return (IImageProcessingAlgorithmHikVision)Activator.CreateInstance(_algorithmTypes[algorithmName]);
  49. }
  50. else
  51. {
  52. Console.WriteLine($"未找到名为 '{algorithmName}' 的算法实现");
  53. return null;
  54. }
  55. }
  56. /// <summary>
  57. /// 获取所有已注册的算法名称
  58. /// </summary>
  59. /// <returns>算法名称列表</returns>
  60. public static List<string> GetAvailableAlgorithms()
  61. {
  62. return _algorithmTypes.Keys.ToList();
  63. }
  64. /// <summary>
  65. /// 检查算法是否存在
  66. /// </summary>
  67. /// <param name="algorithmName">算法名称</param>
  68. /// <returns>是否存在</returns>
  69. public static bool AlgorithmExists(string algorithmName)
  70. {
  71. return _algorithmTypes.ContainsKey(algorithmName);
  72. }
  73. #endregion
  74. }
  75. }