DynamicFaImport.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace MeterVision.FreeAi
  8. {
  9. public static class DynamicFaImport
  10. {
  11. private static readonly object _lock = new object();
  12. private static IntPtr _hModule;
  13. private static RecognitionDelegate _recognitionFunc;
  14. private static bool _isInitialized;
  15. [DllImport("kernel32.dll", SetLastError = true)]
  16. private static extern IntPtr LoadLibrary(string dllToLoad);
  17. [DllImport("kernel32.dll", SetLastError = true)]
  18. private static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
  19. [DllImport("kernel32.dll", SetLastError = true)]
  20. [return: MarshalAs(UnmanagedType.Bool)]
  21. private static extern bool FreeLibrary(IntPtr hModule);
  22. [UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
  23. private delegate int RecognitionDelegate(
  24. [In] short[] rgb565,
  25. ref FaImport.BeforeAI beforeAI,
  26. ref FaImport.AfterAI afterAI,
  27. ref FaImport.SmallImage smallJpg,
  28. ref FaImport.BigImage bigJpg,
  29. [MarshalAs(UnmanagedType.LPWStr)] string modelPath,
  30. FaImport.PrintfCallback callback
  31. );
  32. /// <summary>
  33. /// 初始化并加载 freeAI.dll
  34. /// </summary>
  35. /// <param name="dllPath">DLL 文件路径</param>
  36. /// <returns>是否成功加载</returns>
  37. public static bool Initialize(string dllPath)
  38. {
  39. lock (_lock)
  40. {
  41. if (_isInitialized) return true;
  42. _hModule = LoadLibrary(dllPath);
  43. if (_hModule == IntPtr.Zero)
  44. {
  45. throw new Exception($"Failed to load DLL: {Marshal.GetLastWin32Error()}");
  46. }
  47. IntPtr procAddress = GetProcAddress(_hModule, "recognition");
  48. if (procAddress == IntPtr.Zero)
  49. {
  50. throw new Exception($"Failed to find function 'recognition': {Marshal.GetLastWin32Error()}");
  51. }
  52. _recognitionFunc = Marshal.GetDelegateForFunctionPointer<RecognitionDelegate>(procAddress);
  53. _isInitialized = true;
  54. return true;
  55. }
  56. }
  57. /// <summary>
  58. /// 调用 recognition 函数
  59. /// </summary>
  60. /// <param name="rgb565">RGB565 格式的图像数据</param>
  61. /// <param name="beforeAI">BeforeAI 结构体</param>
  62. /// <param name="afterAI">AfterAI 结构体</param>
  63. /// <param name="smallJpg">SmallImage 结构体</param>
  64. /// <param name="bigJpg">BigImage 结构体</param>
  65. /// <param name="modelPath">模型文件路径</param>
  66. /// <param name="callback">回调函数</param>
  67. /// <returns>返回值</returns>
  68. public static int Recognition(
  69. short[] rgb565,
  70. ref FaImport.BeforeAI beforeAI,
  71. ref FaImport.AfterAI afterAI,
  72. ref FaImport.SmallImage smallJpg,
  73. ref FaImport.BigImage bigJpg,
  74. string modelPath,
  75. FaImport.PrintfCallback callback
  76. )
  77. {
  78. if (!_isInitialized)
  79. {
  80. throw new InvalidOperationException("The 'recognition' function has not been initialized.");
  81. }
  82. return _recognitionFunc(rgb565, ref beforeAI, ref afterAI, ref smallJpg, ref bigJpg, modelPath, callback);
  83. }
  84. /// <summary>
  85. /// 卸载 freeAI.dll
  86. /// </summary>
  87. public static void Unload()
  88. {
  89. lock (_lock)
  90. {
  91. if (_hModule != IntPtr.Zero)
  92. {
  93. FreeLibrary(_hModule);
  94. _hModule = IntPtr.Zero;
  95. _recognitionFunc = null;
  96. _isInitialized = false;
  97. }
  98. }
  99. }
  100. //--------------------------------------------------------
  101. }
  102. }