ImageProcessingAlgorithmHikVisionFactory.cs 2.8 KB

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