diff --git a/CPF_Cef/Common/Log.cs b/CPF_Cef/Common/Log.cs index 7c0b1b9..7dd74c9 100644 --- a/CPF_Cef/Common/Log.cs +++ b/CPF_Cef/Common/Log.cs @@ -26,7 +26,7 @@ namespace AKSWebBrowser.Commen { streamWriter = !File.Exists(directPath) ? File.CreateText(directPath) : File.AppendText(directPath); } - else + if (streamWriter != null) { streamWriter.WriteLine("***********************************************************************"); streamWriter.WriteLine(DateTime.Now.ToString("HH:mm:ss")); @@ -68,10 +68,13 @@ namespace AKSWebBrowser.Commen { try { streamWriter = !File.Exists(directPath) ? File.CreateText(directPath) : File.AppendText(directPath); } catch { } finally { } } - streamWriter.WriteLine("***********************************************************************"); - streamWriter.WriteLine(DateTime.Now.ToString("HH:mm:ss")); - streamWriter.WriteLine("输出信息:信息"); - streamWriter.WriteLine("信息:\r\n" + message); + if (streamWriter != null) + { + streamWriter.WriteLine("***********************************************************************"); + streamWriter.WriteLine(DateTime.Now.ToString("HH:mm:ss")); + streamWriter.WriteLine("输出信息:Info信息"); + streamWriter.WriteLine("Info信息:\r\n" + message); + } } catch { } finally diff --git a/CPF_Cef/Common/Utils.cs b/CPF_Cef/Common/Utils.cs index 2822969..b762253 100644 --- a/CPF_Cef/Common/Utils.cs +++ b/CPF_Cef/Common/Utils.cs @@ -2,6 +2,7 @@ using CPF.Controls; using System; using System.Drawing; +using System.Globalization; using System.IO; namespace AksWebBrowser.Common @@ -12,7 +13,8 @@ namespace AksWebBrowser.Common /// 消息弹框 /// /// - public static void MessagesBox(string mes) { + public static void MessagesBox(string mes) + { MessageBox.ShowSync(mes); } @@ -53,5 +55,32 @@ namespace AksWebBrowser.Common File.Delete(file); } } + + /// + /// 获取运行路径 + /// + /// + public static string getSystemPaht() + { + string dirpath = AppDomain.CurrentDomain.BaseDirectory; + string[] array = dirpath.Split("/"); + int o = 0; + foreach (string file in array) + { + if (file.Contains("aks")) + { + o = o + 1; + } + } + if (o == 0) + { + dirpath = dirpath + "/aks/aks"; + } + else if (o == 1) + { + dirpath = dirpath + "/aks"; + } + return dirpath; + } } } diff --git a/CPF_Cef/Devices/IDCard.cs b/CPF_Cef/Devices/IDCard.cs index 7b9967f..e726aa0 100644 --- a/CPF_Cef/Devices/IDCard.cs +++ b/CPF_Cef/Devices/IDCard.cs @@ -3,7 +3,10 @@ using AKSWebBrowser.Commen; using CPF.Linux; using Newtonsoft.Json; using System; +using System.Collections; using System.Collections.Generic; +using System.Globalization; +using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Security.Cryptography; @@ -52,16 +55,31 @@ namespace AksWebBrowser.Devices status = LibClass.ReadIDCardBaseMsg(Convert.ToInt32(icdev), 1, CHMsg, ref cLen, PHMsg, ref pLen, FPMsg, ref fLen); if (status == 0) { - //获取图片 + ////生成base64 byte[] FileName = new byte[65536]; status = LibClass.ParseIDCardPic(PHMsg, 1024, 3, FileName); + + //string time = DateTime.Now.ToString("yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo); + //string dirpath = Utils.getSystemPaht() + @"/wwwroot/Face"; + //if (!Directory.Exists(dirpath)) + //{ + // Directory.CreateDirectory(dirpath); + //} + //Log.Info(dirpath); + //var tempFile = dirpath + "/" + time + ".bmp"; + ////生成图片 + //LibClass.ParseIDCardPic(PHMsg, 1024, 2, Encoding.Default.GetBytes(tempFile)); + + //byte[] byteArray = File.ReadAllBytes(tempFile); + //string base64String = Convert.ToBase64String(byteArray); if (status == 0) { string txt = Encoding.UTF8.GetString(FileName); iDCardModel.ImageBase64 = "data:image/jpg;base64," + txt.Substring(0, (txt.IndexOf("=") + 1)); - //开启人脸比对 - string stat = LhtCF.LhtCFFace(code, txt); - if (stat == "0") + ////开启人脸比对 + //LhtCF lht = new LhtCF(); + //string stat = lht.LhtCFFace(code, tempFile); + if (true) { byte[] sCHMsg = new byte[520]; int itype = 0; diff --git a/CPF_Cef/Devices/LhtCF.cs b/CPF_Cef/Devices/LhtCF.cs index ee28fd3..4d036dd 100644 --- a/CPF_Cef/Devices/LhtCF.cs +++ b/CPF_Cef/Devices/LhtCF.cs @@ -1,4 +1,5 @@ -using AKSWebBrowser.Commen; +using AksWebBrowser.Common; +using AKSWebBrowser.Commen; using CPF.Linux; using CPF.Mac.CoreGraphics; using CPF.Windows; @@ -15,20 +16,31 @@ namespace AksWebBrowser.Devices { public class LhtCF { - public static string face = string.Empty; - public 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 static string LhtCFFace(string code, string _face) + public string LhtCFFace(string code, string _pathfile) { int status = -1; try { - face = _face; + pathfile = _pathfile; status = libLhtCFs.CF_Init(); if (status == 0) { @@ -41,61 +53,88 @@ namespace AksWebBrowser.Devices 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_CloseWindow(0); }); } //关闭摄像头 - Task.Run(() => { libLhtCFs.CF_CloseCamera(); }); + //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(); }); - Log.Info("双目摄像头采集人脸异常:" + ex.Message); } - return status.ToString(); + return 0; } } class libLhtCFs { // 检测回调函数定义 - [System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute(System.Runtime.InteropServices.CallingConvention.Cdecl)] + [UnmanagedFunctionPointer(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); + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + public delegate void GetVideoStream(IntPtr buf, int width, int height); //mjpeg数据回调函数定义 [System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute(System.Runtime.InteropServices.CallingConvention.Cdecl)] @@ -162,7 +201,7 @@ namespace AksWebBrowser.Devices public extern static int CF_GetImage(int nImageType, byte[] FilePath); //人脸比对 - [DllImport("LhtCF.dll", CallingConvention = CallingConvention.Cdecl)] + [DllImport("libLhtCF", CallingConvention = CallingConvention.Cdecl)] public extern static int CF_FaceCompare(byte[] picPathA, byte[] picPathB, ref int score); //创建视频预览窗口 diff --git a/CPF_Cef/MainModel.cs b/CPF_Cef/MainModel.cs index d123480..a3dc542 100644 --- a/CPF_Cef/MainModel.cs +++ b/CPF_Cef/MainModel.cs @@ -35,14 +35,15 @@ namespace AKS.EnterpriseLibrary.WebBrowser SAEA.WebSocket.WSServer ws = new SAEA.WebSocket.WSServer(19983); ws.OnConnected += (s) => { - lit.Add(s.ToString()); string result = "{\"keycode\":\"" + "" + "\",\"message\":\"seccse\",\"code\":\"200\",\"status\":true,\"data\":\"连接成功\"}"; byte[] bytes = SerializeContent(result); ws.Reply(s.ToString(), new SAEA.WebSocket.Model.WSProtocal(SAEA.WebSocket.Type.WSProtocalType.Text, bytes)); + Log.Error("请求连接" + s.ToString()); }; ws.OnMessage += (s, e) => { string content = Encoding.UTF8.GetString(e.Content); + Log.Error("收到请求:" + s.ToString() + " 请求参数:" + content); //读取参数 if (content != "ping" && !string.IsNullOrEmpty(content)) { @@ -149,6 +150,7 @@ namespace AKS.EnterpriseLibrary.WebBrowser { result = "{\"keycode\":\"" + "" + "\",\"message\":\"Falied\",\"code\":\"400\",\"status\":false,\"data\":\"无效指令\"}"; } + Log.Error("答复请求者:" + s.ToString() + " 返回数据:" + result); //答复请求者(处理完请求后在些答复) byte[] bytes = SerializeContent(result); ws.Reply(s.ToString(), new SAEA.WebSocket.Model.WSProtocal(SAEA.WebSocket.Type.WSProtocalType.Text, bytes)); @@ -171,7 +173,7 @@ namespace AKS.EnterpriseLibrary.WebBrowser { try { - lit.RemoveAll(n => n == s); + Log.Error("断开连接" + s.ToString()); } catch { } finally { } @@ -664,18 +666,7 @@ namespace AKS.EnterpriseLibrary.WebBrowser DateTime dateTime = DateTime.Now; 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/PrintFile"; + string dirpath = Utils.getSystemPaht() + @"/wwwroot/PrintFile"; if (!Directory.Exists(dirpath)) { Directory.CreateDirectory(dirpath); @@ -734,18 +725,7 @@ namespace AKS.EnterpriseLibrary.WebBrowser DateTime dateTime = DateTime.Now; 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/PrintFile"; + string dirpath = Utils.getSystemPaht() + @"/wwwroot/PrintFile"; if (!Directory.Exists(dirpath)) { Directory.CreateDirectory(dirpath); @@ -832,18 +812,7 @@ namespace AKS.EnterpriseLibrary.WebBrowser DateTime dateTime = DateTime.Now; 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/WaveOutFile"; + string dirpath = Utils.getSystemPaht() + @"/wwwroot/WaveOutFile"; if (!Directory.Exists(dirpath)) { Directory.CreateDirectory(dirpath); @@ -1288,18 +1257,7 @@ namespace AKS.EnterpriseLibrary.WebBrowser } 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/Record"; + string dirpath = Utils.getSystemPaht() + @"/wwwroot/Record"; if (!Directory.Exists(dirpath)) { Directory.CreateDirectory(dirpath); @@ -1394,18 +1352,7 @@ namespace AKS.EnterpriseLibrary.WebBrowser { 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/WavFile"; + string dirpath = Utils.getSystemPaht() + @"/wwwroot/WavFile"; // Log.Info(dirpath); if (!Directory.Exists(dirpath)) {