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 face = string.Empty; public LhtCF() { } /// /// 人脸比对 /// /// /// public static string LhtCFFace(string code, string _face) { int status = -1; try { face = _face; 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) { //status = libLhtCFs.SetCamADataCallbackEx(GetMjpegStream); //Log.Info("设置回调:" + status); string time = DateTime.Now.ToString("yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo); string dirpath = System.IO.Directory.GetCurrentDirectory(); string[] array = dirpath.Split("/"); var groupCount = array.GroupBy(x => x).Select(group => new { Element = group.Key, Count = group.Count() }).Where(x => x.Count > 0); if (groupCount.Count() == 1) { dirpath = dirpath + "/aks"; } else if (groupCount.Count() == 0) { dirpath = dirpath + "/aks/aks"; } dirpath = dirpath + @"/wwwroot/Face"; if (!Directory.Exists(dirpath)) { Directory.CreateDirectory(dirpath); } Log.Info(dirpath); var tempFile = dirpath + "/" + time + ".jpg"; Log.Info("人脸路径:" + tempFile); status = libLhtCFs.CF_GetImage(0, Encoding.Default.GetBytes(tempFile)); Log.Info("获取人脸:" + status); } //关闭视频预览窗口 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(); } } class libLhtCFs { // 检测回调函数定义 [System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute(System.Runtime.InteropServices.CallingConvention.Cdecl)] public delegate void DetectCallBack(int nEventId, IntPtr lpContext); //RGB数据回调函数定义 [System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute(System.Runtime.InteropServices.CallingConvention.Cdecl)] public delegate void GetVideoStream(byte[] rgbData, 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("LhtCF.dll", 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); } }