using AI.Common.Crypto; using System; using System.IO; using System.Runtime.InteropServices; namespace IDCardRecognitionLibs { public class IDCardRecog : IIDCardRecog { #region dll import [DllImport(@"libIDCardRecognition.so", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr CreateIDCardRecognition(); [DllImport(@"libIDCardRecognition.so", CallingConvention = CallingConvention.Cdecl)] private static extern void ReleaseIDCardRecognition(IntPtr hIDCardRecognition); [DllImport(@"libIDCardRecognition.so", CallingConvention = CallingConvention.Cdecl)] [return: MarshalAs(UnmanagedType.I1)] private static extern bool LoadNetWorks(IntPtr hIDCardRecognition, StructModelDataInfor modelDataInfor); [DllImport(@"libIDCardRecognition.so", CallingConvention = CallingConvention.Cdecl)] [return: MarshalAs(UnmanagedType.I1)] private static extern StructIDCardRecogResult EvaluateOneImage(IntPtr hIDCardRecognition, StructImageInfo imgInfor); #endregion dll import #region private variable private IntPtr _hIDCardRecognition = IntPtr.Zero; #endregion private variable void IDisposable.Dispose() { throw new NotImplementedException(); } public bool LoadNetWorks(byte[] detBufferEmd, byte[] recBufferEmd, byte[] recKeyBufferEmd, byte[] clsBufferEmd) { StructModelDataInfor structModelDataInfor = new StructModelDataInfor(detBufferEmd, detBufferEmd.Length, recBufferEmd, recBufferEmd.Length, recKeyBufferEmd, recKeyBufferEmd.Length, clsBufferEmd, clsBufferEmd.Length); bool ret = LoadNetWorks(_hIDCardRecognition, structModelDataInfor); return ret; } public ClassIDCardRecogResult EvaluateOneImage(byte[] imageData, int width, int height, EnumColorType colorType) { StructImageInfo imgInfor = new StructImageInfo(width, height, 4, imageData); StructIDCardRecogResult result = EvaluateOneImage(_hIDCardRecognition, imgInfor); if (result.Status == 1) { ClassIDCardRecogResult classResult = new ClassIDCardRecogResult(result); return classResult; } else { StructIDCardRecogResult structResult = new StructIDCardRecogResult(); ClassIDCardRecogResult classResult = new ClassIDCardRecogResult(structResult); return classResult; } } // 实现 IDisposable 接口 public void Dispose() { ReleaseIDCardRecognition(_hIDCardRecognition); DoDispose(true); GC.SuppressFinalize(this); } public static byte[] ReadNetworkDataFromFile(byte[] input, string netHashCode) { if (input == null || input.Length == 0) { throw new FileNotFoundException("The input byteData is empty."); } if (HashCode.ComputeHashCode(input) != netHashCode) { throw new ArgumentException("Unexpected parameter data file for the current version of AIDiagSystem"); } return AES.AESDecrypt(input); } protected virtual void DoDispose(bool disposing) { if (_hIDCardRecognition != IntPtr.Zero) { _hIDCardRecognition = IntPtr.Zero; } } #region consdtuctor public IDCardRecog() { if (_hIDCardRecognition == IntPtr.Zero) { _hIDCardRecognition = CreateIDCardRecognition(); } } #endregion consdtuctor } }