using AksWebBrowser.Common; using AKSWebBrowser.Commen; using CPF.Linux; using CPF.Mac.CoreGraphics; using CPF.Windows; using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; namespace AksWebBrowser.Devices { public class LhtCF { public static string pathfile = string.Empty; libLhtCFs.DetectCallBack pDetectCallBack = null; private delegate int DetectCallBackDelegate(int nResultID); public LhtCF() { //设置检测回调 Log.Info("设置检测回调"); if (pDetectCallBack == null) { pDetectCallBack = new libLhtCFs.DetectCallBack(DetectCallBackPro); libLhtCFs.CF_SetDetectCallBack(pDetectCallBack, IntPtr.Zero); //设置回调 } } /// /// 人脸比对 /// /// /// public string LhtCFFace(string code, string _pathfile) { int status = -1; try { pathfile = _pathfile; status = libLhtCFs.CF_Init(); if (status == 0) { status = libLhtCFs.CF_OpenCamera(); if (status == 0) { status = libLhtCFs.CF_CreatWindow(0, 0, 300, 800, 600, 500); if (status == 0) { status = libLhtCFs.CF_StartLiveDetectEx(10000); if (status == 0) { } //关闭视频预览窗口 //Task.Run(() => { libLhtCFs.CF_CloseWindow(0); }); } //关闭摄像头 //Task.Run(() => { libLhtCFs.CF_CloseCamera(); }); } } } catch (Exception ex) { ////关闭视频预览窗口 //Task.Run(() => { libLhtCFs.CF_CloseWindow(0); }); ////关闭摄像头 //Task.Run(() => { libLhtCFs.CF_CloseCamera(); }); Log.Info("双目摄像头采集人脸异常:" + ex.Message); } return status.ToString(); } //检测回调函数 public void DetectCallBackPro(int nResultID, IntPtr lpParam) { DetectCallBackDelegate myDelegate = (message) => { Console.WriteLine($"Hello, {message}!"); return 0; // 返回值 }; //new DetectCallBackDelegate(DetectCallBackResultf, nResultID); //Task.Run(new DetectCallBackDelegate(DetectCallBackResult), nResultID); } public int DetectCallBackResult(int nResultID) { Log.Info("检测回调函数:" + nResultID); if (nResultID == 100) { string time = DateTime.Now.ToString("yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo); string dirpath = Utils.getSystemPaht() + @"/wwwroot/Face"; if (!Directory.Exists(dirpath)) { Directory.CreateDirectory(dirpath); } var tempFile = dirpath + "/" + time + ".jpg"; int status = libLhtCFs.CF_GetImage(0, Encoding.Default.GetBytes(tempFile)); Log.Info("人脸路径:" + tempFile); byte[] faceA = File.ReadAllBytes(pathfile); Log.Info("身份证图片路径:" + pathfile); byte[] faceB = File.ReadAllBytes(tempFile); int score = 0; status = libLhtCFs.CF_FaceCompare(faceA, faceB, ref score); Log.Info("人脸比对:" + status); Log.Info("人脸值:" + score); if (score > 60 && status == 0) { status = 0; } else { status = -1; } //关闭视频预览窗口 Task.Run(() => { libLhtCFs.CF_CloseWindow(0); }); //关闭摄像头 Task.Run(() => { libLhtCFs.CF_CloseCamera(); }); } return 0; } } class libLhtCFs { // 检测回调函数定义 [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate void DetectCallBack(int nEventId, IntPtr lpContext); //RGB数据回调函数定义 [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate void GetVideoStream(IntPtr buf, int width, int height); //mjpeg数据回调函数定义 [System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute(System.Runtime.InteropServices.CallingConvention.Cdecl)] public delegate void GetMjpegStream(byte[] mjpegData, int dataLen); //初始化 [DllImport("libLhtCF", EntryPoint = "CF_Init", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public static extern int CF_Init(); //反初始化函数 [DllImport("libLhtCF", EntryPoint = "CF_Uninit", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public static extern int CF_Uninit(); //反初始化函数 [DllImport("libLhtCF", EntryPoint = "CF_GetCameraStatus", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public static extern int CF_GetCameraStatus(int status); //设置检测回调函数 [DllImport("libLhtCF", EntryPoint = "CF_GetCameraStatus", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public static extern int CF_SetDetectCallBack(DetectCallBack pCB, IntPtr lpContext); //设置彩色摄像头视频流回调 [DllImport("libLhtCF", EntryPoint = "SetCamADataCallback", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public static extern int SetCamADataCallback(GetVideoStream cb); //设置红外摄像头视频流回调 [DllImport("libLhtCF", EntryPoint = "SetCamBDataCallback", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public static extern int SetCamBDataCallback(GetVideoStream cb); //设置彩色摄像头mjpeg数据流回调 [DllImport("libLhtCF", EntryPoint = "SetCamADataCallbackEx", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public static extern int SetCamADataCallbackEx(GetMjpegStream cb); //设置红外摄像头mjpeg数据流回调 [DllImport("libLhtCF", EntryPoint = "SetCamBDataCallbackEx", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public static extern int SetCamBDataCallbackEx(GetMjpegStream cb); //设置分辨率 [DllImport("libLhtCF", EntryPoint = "CF_SetResolution", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public static extern int CF_SetResolution(int width, int height); //打开摄像头 [DllImport("libLhtCF", EntryPoint = "CF_OpenCamera", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public static extern int CF_OpenCamera(); //关闭摄像头 [DllImport("libLhtCF", EntryPoint = "CF_CloseCamera", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public static extern int CF_CloseCamera(); //开始活体检测 [DllImport("libLhtCF", EntryPoint = "CF_StartLiveDetect", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public static extern int CF_StartLiveDetect(); //活体检测超时时间 [DllImport("libLhtCF", EntryPoint = "CF_StartLiveDetectEx", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public static extern int CF_StartLiveDetectEx(int timeout); //停止活体检测 [DllImport("libLhtCF", EntryPoint = "CF_StopLiveDetect", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public static extern int CF_StopLiveDetect(); //获取采集图像 [DllImport("libLhtCF", CallingConvention = CallingConvention.Cdecl)] public extern static int CF_GetImage(int nImageType, byte[] FilePath); //人脸比对 [DllImport("libLhtCF", CallingConvention = CallingConvention.Cdecl)] public extern static int CF_FaceCompare(byte[] picPathA, byte[] picPathB, ref int score); //创建视频预览窗口 [DllImport("libLhtCF", EntryPoint = "CF_CreatWindow", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public static extern int CF_CreatWindow(int nWndType, long hWnd, int iX, int iY, int iWidth, int iHeight); //关闭视频预览窗口 [DllImport("libLhtCF", EntryPoint = "CF_CloseWindow", SetLastError = true, CharSet = CharSet.Auto, ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public static extern int CF_CloseWindow(int nWndType); } }