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.CompilerServices; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; namespace AksWebBrowser.Devices { public class LhtCF { public string pathfile = string.Empty; public string tempFile = string.Empty; libLhtCFs.DetectCallBack pDetectCallBack = null; private delegate int DetectCallBackDelegate(int nResultID); public LhtCF() { } /// /// 人脸比对 /// /// /// public string LhtCFFace(string code, string _pathfile) { int status = -1; try { pathfile = _pathfile; status = libLhtCFs.CF_Init(); if (status == 0) { //设置检测回调 status = libLhtCFs.CF_SetDetectCallBack(DetectCallBackPro, 0); //设置回调 status = libLhtCFs.CF_OpenCamera(); if (status == 0) { string time = DateTime.Now.ToString("yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo); string dirpath = Utils.getSystemPaht() + @"/wwwroot/Face"; if (!Directory.Exists(dirpath)) { Directory.CreateDirectory(dirpath); } tempFile = dirpath + "/faceB.jpg"; Task.Run(() => { status = libLhtCFs.CF_CreatWindow(0, 0, 300, 800, 600, 500); if (status == 0) { status = libLhtCFs.CF_StartLiveDetectEx(10000); } }); @event6.WaitOne(); if (string.IsNullOrEmpty(tempFile)) { hdStatus = -1; } else { byte[] faceA = Encoding.Default.GetBytes(pathfile); byte[] faceB = Encoding.Default.GetBytes(tempFile); int score = 0; hdStatus = libLhtCFs.CF_FaceCompare(faceA, faceB, ref score); if (score > 60 && hdStatus == 0) { hdStatus = 100; } else { hdStatus = -1; } } status = 100; // Log.Info("人脸比对结果:" + status); //停止活体检测 //libLhtCFs.CF_StopLiveDetect(); //关闭视频预览窗口 libLhtCFs.CF_CloseWindow(0); //关闭摄像头 libLhtCFs.CF_CloseCamera(); //反初始化 //libLhtCFs.CF_Uninit(); } } } catch (Exception ex) { //停止活体检测 //libLhtCFs.CF_StopLiveDetect(); //关闭视频预览窗口 libLhtCFs.CF_CloseWindow(0); //关闭摄像头 libLhtCFs.CF_CloseCamera(); //反初始化 //libLhtCFs.CF_Uninit(); // Log.Info("双目摄像头采集人脸异常:" + ex.Message); } return status.ToString(); } private static AutoResetEvent @event6 = new AutoResetEvent(false); private static int hdStatus = -1; //检测回调函数 public void DetectCallBackPro(int nResultID, int lpParam) { if (nResultID == 100) { try { hdStatus = libLhtCFs.CF_GetImage(2, Encoding.Default.GetBytes(tempFile)); if (hdStatus != 0) { tempFile = string.Empty; } } catch (Exception ex) { tempFile = string.Empty; } @event6.Set(); } } } class libLhtCFs { public static int FeatureSize = 5120; // 检测回调函数定义 [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate void DetectCallBack(int nEventId, int 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", CallingConvention = CallingConvention.Cdecl)] public static extern int CF_SetDetectCallBack(DetectCallBack pCB, int 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); } }