1
0

IDCardRecog.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using AI.Common.Crypto;
  2. using System;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5. namespace IDCardRecognitionLibs
  6. {
  7. public class IDCardRecog : IIDCardRecog
  8. {
  9. #region dll import
  10. [DllImport(@"libIDCardRecognition.so", CallingConvention = CallingConvention.Cdecl)]
  11. private static extern IntPtr CreateIDCardRecognition();
  12. [DllImport(@"libIDCardRecognition.so", CallingConvention = CallingConvention.Cdecl)]
  13. private static extern void ReleaseIDCardRecognition(IntPtr hIDCardRecognition);
  14. [DllImport(@"libIDCardRecognition.so", CallingConvention = CallingConvention.Cdecl)]
  15. [return: MarshalAs(UnmanagedType.I1)]
  16. private static extern bool LoadNetWorks(IntPtr hIDCardRecognition, StructModelDataInfor modelDataInfor);
  17. [DllImport(@"libIDCardRecognition.so", CallingConvention = CallingConvention.Cdecl)]
  18. [return: MarshalAs(UnmanagedType.I1)]
  19. private static extern StructIDCardRecogResult EvaluateOneImage(IntPtr hIDCardRecognition, StructImageInfo imgInfor);
  20. #endregion dll import
  21. #region private variable
  22. private IntPtr _hIDCardRecognition = IntPtr.Zero;
  23. #endregion private variable
  24. void IDisposable.Dispose()
  25. {
  26. throw new NotImplementedException();
  27. }
  28. public bool LoadNetWorks(byte[] detBufferEmd, byte[] recBufferEmd, byte[] recKeyBufferEmd, byte[] clsBufferEmd)
  29. {
  30. StructModelDataInfor structModelDataInfor = new StructModelDataInfor(detBufferEmd, detBufferEmd.Length,
  31. recBufferEmd, recBufferEmd.Length,
  32. recKeyBufferEmd, recKeyBufferEmd.Length,
  33. clsBufferEmd, clsBufferEmd.Length);
  34. bool ret = LoadNetWorks(_hIDCardRecognition, structModelDataInfor);
  35. return ret;
  36. }
  37. public ClassIDCardRecogResult EvaluateOneImage(byte[] imageData, int width, int height, EnumColorType colorType)
  38. {
  39. StructImageInfo imgInfor = new StructImageInfo(width, height, 4, imageData);
  40. StructIDCardRecogResult result = EvaluateOneImage(_hIDCardRecognition, imgInfor);
  41. if (result.Status == 1)
  42. {
  43. ClassIDCardRecogResult classResult = new ClassIDCardRecogResult(result);
  44. return classResult;
  45. }
  46. else
  47. {
  48. StructIDCardRecogResult structResult = new StructIDCardRecogResult();
  49. ClassIDCardRecogResult classResult = new ClassIDCardRecogResult(structResult);
  50. return classResult;
  51. }
  52. }
  53. // 实现 IDisposable 接口
  54. public void Dispose()
  55. {
  56. ReleaseIDCardRecognition(_hIDCardRecognition);
  57. DoDispose(true);
  58. GC.SuppressFinalize(this);
  59. }
  60. public static byte[] ReadNetworkDataFromFile(byte[] input, string netHashCode)
  61. {
  62. if (input == null || input.Length == 0)
  63. {
  64. throw new FileNotFoundException("The input byteData is empty.");
  65. }
  66. if (HashCode.ComputeHashCode(input) != netHashCode)
  67. {
  68. throw new ArgumentException("Unexpected parameter data file for the current version of AIDiagSystem");
  69. }
  70. return AES.AESDecrypt(input);
  71. }
  72. protected virtual void DoDispose(bool disposing)
  73. {
  74. if (_hIDCardRecognition != IntPtr.Zero)
  75. {
  76. _hIDCardRecognition = IntPtr.Zero;
  77. }
  78. }
  79. #region consdtuctor
  80. public IDCardRecog()
  81. {
  82. if (_hIDCardRecognition == IntPtr.Zero)
  83. {
  84. _hIDCardRecognition = CreateIDCardRecognition();
  85. }
  86. }
  87. #endregion consdtuctor
  88. }
  89. }