From 4b844e536b02095704e44cabed158e1e34e1c77d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=BD=87=E9=98=B3?= Date: Wed, 22 May 2024 16:26:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E7=AD=BE=E5=AD=97=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DevicesService/Commen/ScriptCallbackObject.cs | 58 +- DevicesService/Commen/TcpServer.cs | 575 +++++++++--------- DevicesService/Commen/Util.cs | 243 +++++++- DevicesService/Devices/HScamera.cs | 11 +- DevicesService/Devices/SignDll.cs | 259 ++++++-- DevicesService/DevicesService.csproj | 8 + DevicesService/Program.cs | 3 +- DevicesService/Properties/launchSettings.json | 12 + 8 files changed, 790 insertions(+), 379 deletions(-) create mode 100644 DevicesService/Properties/launchSettings.json diff --git a/DevicesService/Commen/ScriptCallbackObject.cs b/DevicesService/Commen/ScriptCallbackObject.cs index 5aeafb8..26cb113 100644 --- a/DevicesService/Commen/ScriptCallbackObject.cs +++ b/DevicesService/Commen/ScriptCallbackObject.cs @@ -28,6 +28,7 @@ using System.Numerics; using System.Web; using NAudio.Wave.SampleProviders; using System.Configuration; +using System.Diagnostics; namespace DevicesService.Common { @@ -147,11 +148,8 @@ namespace DevicesService.Common // } // } //} - Task.Run(() => - { - text = text.Replace("(", "").Replace(")", "、"); - speech.Speak(text); - }); + text = text.Replace("(", "").Replace(")", "、"); + speech.SpeakAsync(text); } catch { } } @@ -207,14 +205,6 @@ namespace DevicesService.Common string newdata = Util.CompressPictures(fileInfo); //转换 string base64 = Util.ImgToBase64String(newdata); - //Task.Run(async () => - //{ - // UploadInfoByFile(url, data); - //}); - //@event5.WaitOne(); - //Regex re = new Regex(@"(((?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(:[0-9]+)?|(?:ww‌​w.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?‌​(?:[\w]*))?)"); - //MatchCollection mc = re.Matches(url);//获取的是一个数组 - //string imgurl = mc[0].ToString() + "://" + mc[1].ToString() + urlP; return base64; } else @@ -222,16 +212,11 @@ namespace DevicesService.Common data = hScamera.openCamera2(); if (!string.IsNullOrEmpty(data)) { - string base64 = Util.ImgToBase64String(data); - //Task.Run(async () => - //{ - // UploadInfoByFile(url, data); - //}); - //@event5.WaitOne(); - //Regex re = new Regex(@"(((?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(:[0-9]+)?|(?:ww‌​w.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?‌​(?:[\w]*))?)"); - //MatchCollection mc = re.Matches(url);//获取的是一个数组 - //string imgurl = mc[0].ToString() + "://" + mc[1].ToString() + urlP; - + FileInfo fileInfo = new FileInfo(data); + //压缩 + string newdata = Util.CompressPictures(fileInfo); + //转换 + string base64 = Util.ImgToBase64String(newdata); return base64; } else @@ -331,13 +316,7 @@ namespace DevicesService.Common { FileStream fs1 = fileInfo.OpenRead(); fs1.Close(); - //string base64 = Util.ConvertImageToBase64(sourcepaht); - Task.Run(async () => - { - UploadInfoByFile(url, sourcepaht); - }); - @event5.WaitOne(); - File.Delete(sourcepaht); + string base64 = Util.ImgToBase64StringSign(sourcepaht); int width = 800; int height = 394; using (Bitmap blankImage = new Bitmap(width, height)) @@ -351,26 +330,22 @@ namespace DevicesService.Common // 保存图片到文件系统 blankImage.Save(sourcepaht); } - Regex re = new Regex(@"(((?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(:[0-9]+)?|(?:ww‌​w.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?‌​(?:[\w]*))?)"); - MatchCollection mc = re.Matches(url);//获取的是一个数组 - string imgurl = mc[0].ToString() + "://" + mc[1].ToString() + urlP; - //关闭签字版 - //SignDll.CloseComDevice(); - return "{\"callback\":\"" + callback + "\",\"message\":\"success\",\"code\":\"200\",\"status\":true,\"data\":\"" + imgurl + "\"}"; + return base64; } else { - return "{\"callback\":\"" + callback + "\",\"message\":\"success\",\"code\":\"400\",\"status\":false,\"data\":\"" + "用户未签字或签字不规范" + "\"}"; + return Util.str2Base64("400"); } } else { - return "{\"callback\":\"" + callback + "\",\"message\":\"success\",\"code\":\"400\",\"status\":false,\"data\":\"" + "用户未签字或签字不规范" + "\"}"; + return Util.str2Base64("400"); } } catch (Exception ex) { - return "{\"callback\":\"" + callback + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + ex.Message + "\"}"; + Log.Info("获取签字版数据异常:" + ex.Message); + return Util.str2Base64("400"); } } @@ -491,8 +466,9 @@ namespace DevicesService.Common if (!string.IsNullOrEmpty(url) && !isopen) { string filename = Path.GetFileName(srpath); - string path = ConfigurationManager.AppSettings["rootPath"].ToString(); - return path + "Record/" + filename; + string path = ConfigurationManager.AppSettings["rootPath"].ToString() + "Record/" + filename; + Log.Info("结束录音上传文件" + path); + return path; } else if (isopen)//开始录音 { diff --git a/DevicesService/Commen/TcpServer.cs b/DevicesService/Commen/TcpServer.cs index eaa4b52..813c42c 100644 --- a/DevicesService/Commen/TcpServer.cs +++ b/DevicesService/Commen/TcpServer.cs @@ -16,6 +16,8 @@ using System.Drawing; using System.IO.Ports; using System.IO; using System.Reflection; +using System.Buffers.Text; +using System.Globalization; namespace DevicesService.Commen { /// @@ -25,7 +27,7 @@ namespace DevicesService.Commen { public static List clients = new List(); private static ScriptCallbackObject scriptCallback = new ScriptCallbackObject(); - public static void Start() + public void Start() { try { @@ -55,13 +57,16 @@ namespace DevicesService.Commen { try { - byte[] messageBytes = Encoding.UTF8.GetBytes(message); - byte[] header = BitConverter.GetBytes(messageBytes.Length); - byte[] data = new byte[header.Length + messageBytes.Length]; - Array.Copy(header, 0, data, 0, header.Length); - Array.Copy(messageBytes, 0, data, header.Length, messageBytes.Length); - NetworkStream stream = client.GetStream(); - stream.Write(data, 0, data.Length); + if (client != null) + { + byte[] messageBytes = Encoding.UTF8.GetBytes(message); + byte[] header = BitConverter.GetBytes(messageBytes.Length); + byte[] data = new byte[header.Length + messageBytes.Length]; + Array.Copy(header, 0, data, 0, header.Length); + Array.Copy(messageBytes, 0, data, header.Length, messageBytes.Length); + NetworkStream stream = client.GetStream(); + stream.Write(data, 0, data.Length); + } } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { } @@ -73,19 +78,24 @@ namespace DevicesService.Commen /// public static void SendHelloMessage(object state) { - foreach (TcpClient client in clients) + try { - try - { - SendDataWithHeader(client, " heartbeatService"); - } - catch (Exception ex) + foreach (TcpClient client in clients) { - Console.WriteLine("向客户端 {0} 发送消息失败:{1}", client.Client.RemoteEndPoint, ex.Message); - clients.Remove(client); - break; + try + { + SendDataWithHeader(client, " heartbeatService"); + } + catch (Exception ex) + { + Console.WriteLine("向客户端 {0} 发送消息失败:{1}", client.Client.RemoteEndPoint, ex.Message); + clients.Remove(client); + break; + } } } + catch { } + finally { } } /// @@ -95,304 +105,275 @@ namespace DevicesService.Commen public static void ReceiveMessage(object state) { TcpClient client = (TcpClient)state; - NetworkStream stream = client.GetStream(); - - while (true) + if (client != null) { - try + NetworkStream stream = client.GetStream(); + + while (true) { - // 读取包头,获取消息的大小 - int messageSize = ReadHeader(stream); + try + { + // 读取包头,获取消息的大小 + int messageSize = ReadHeader(stream); - // 读取消息内容 - byte[] buffer = new byte[messageSize]; + // 读取消息内容 + byte[] buffer = new byte[messageSize]; - int bytesRead = ReadExactly(stream, buffer, 0, messageSize); + int bytesRead = ReadExactly(stream, buffer, 0, messageSize); - if (bytesRead < messageSize) - { - Console.WriteLine("无法读取消息,可能是连接断开:{0}", client.Client.RemoteEndPoint); - clients.Remove(client); - break; - } - string message = Encoding.UTF8.GetString(buffer, 0, bytesRead); - if (!message.Contains("heartbeatClient") || message != "keepAlive") - { - Console.WriteLine("收到客户端消息:{0}", message); - string indata = message; - if (!string.IsNullOrEmpty(indata)) + if (bytesRead < messageSize) { - - string result = Util.Base64str2(indata); - Log.Info("接收到COM口传来数据:" + result); - if (!string.IsNullOrEmpty(result)) + Console.WriteLine("无法读取消息,可能是连接断开:{0}", client.Client.RemoteEndPoint); + clients.Remove(client); + break; + } + string message = Encoding.UTF8.GetString(buffer, 0, bytesRead); + if (!message.Contains("heartbeatClient") || message != "keepAlive") + { + Console.WriteLine("收到客户端消息:{0}", message); + string indata = message; + if (!string.IsNullOrEmpty(indata)) { - JObject jo = (JObject)JsonConvert.DeserializeObject(result); - int type = Convert.ToInt32(jo["type"].ToString()); - string param = jo["param"].ToString(); - string resultback = string.Empty; - string base64 = string.Empty; - string data = string.Empty; - string callback = jo["callback"].ToString(); - JObject joparam; - switch (type) + + string result = Util.Base64str2(indata); + Log.Info("接收到COM口传来数据:" + result); + if (!string.IsNullOrEmpty(result)) { - //{"type":"1","param":{"data":""}} - case Func.IDCardRead: - #region 读取身份证 - try - { - joparam = (JObject)JsonConvert.DeserializeObject(param); - data = joparam["data"].ToString(); - resultback = scriptCallback.IDCardRead(data, callback); - //Console.WriteLine(resultback); - Log.Info("读取身份证 返回数据:" + resultback); - base64 = Util.str2Base64(resultback); - //向com对方发送数据 - SendDataWithHeader(client, base64); - } - catch (Exception ex) - { - string rest = "{\"callback\":\"" + "callback" + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + ex.Message + "\"}"; - base64 = Util.str2Base64(rest); - SendDataWithHeader(client, base64); - } - #endregion - break; - //{"type":"2","param":{"ph":"A012","ddrs":"10","qrcode":"www.baidu.com/s?wd=国家赔偿","ywmc":"国家赔偿"}} - case Func.SendByPrint: - #region 打印排队票据 - try - { - joparam = (JObject)JsonConvert.DeserializeObject(param); - string ph = joparam["ph"].ToString(); - string ddrs = joparam["ddrs"].ToString(); - string qrcode = joparam["qrcode"].ToString(); - string ywmc = joparam["ywmc"].ToString(); - resultback = scriptCallback.SendByPrint(ph, ddrs, qrcode, ywmc, callback); - Log.Info("打印排队票据 返回数据:" + resultback); - base64 = Util.str2Base64(resultback); - //向com对方发送数据 - SendDataWithHeader(client, base64); - } - catch (Exception ex) - { - string rest = "{\"callback\":\"" + "callback" + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + ex.Message + "\"}"; - base64 = Util.str2Base64(rest); - SendDataWithHeader(client, base64); - } - #endregion - break; - //{"type":"3","param":{"text":"欢迎使用阿凯思控诉业务一体机","ispaye":false}} - case Func.payleText: - #region 文字语音播报 - try - { - joparam = (JObject)JsonConvert.DeserializeObject(param); - data = joparam["text"].ToString(); - bool ispaye = Convert.ToBoolean(joparam["ispaye"].ToString()); - resultback = scriptCallback.payleText(data, ispaye, callback); - //Console.WriteLine(resultback); - Log.Info("文字语音播报 返回数据:" + resultback); - base64 = Util.str2Base64(resultback); - //向com对方发送数据 - SendDataWithHeader(client, base64); - } - catch (Exception ex) - { - string rest = "{\"callback\":\"" + "callback" + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + ex.Message + "\"}"; - base64 = Util.str2Base64(rest); - SendDataWithHeader(client, base64); - } - #endregion - break; - //{"type":"4","param":{"content":"欢迎使用阿凯思控诉业务一体机","phone":"13333333333"}} - case Func.SendSSM: - #region 发送短信 - try - { - joparam = (JObject)JsonConvert.DeserializeObject(param); - string content = joparam["content"].ToString(); - string phone = joparam["phone"].ToString(); - resultback = scriptCallback.SendSSM(content, phone, callback); - Log.Info("发送短信 返回数据:" + resultback); - base64 = Util.str2Base64(resultback); - //向com对方发送数据 - SendDataWithHeader(client, base64); - } - catch (Exception ex) - { - string rest = "{\"callback\":\"" + "callback" + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + ex.Message + "\"}"; - base64 = Util.str2Base64(rest); - SendDataWithHeader(client, base64); - } - #endregion - break; - //{"type":"5","param":{"url":"http://192.168.0.34:92/api/UploadFP/UploadFP"}} - case Func.openCamera: - #region 打开高拍仪并且进行快速扫描文件 - try - { - joparam = (JObject)JsonConvert.DeserializeObject(param); - data = joparam["url"].ToString(); - resultback = scriptCallback.openCamera(data, callback); - Log.Info("打开高拍仪并且进行快速扫描文件 返回数据:" + resultback); - //base64 = Util.str2Base64(resultback); - //向com对方发送数据 - SendDataWithHeader(client, resultback); - } - catch (Exception ex) - { - string rest = "{\"callback\":\"" + "callback" + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + ex.Message + "\"}"; - base64 = Util.str2Base64(rest); - SendDataWithHeader(client, base64); - } - #endregion - break; - //{"type":"6","param":{"data":""}} - case Func.OpenSign: - #region 打开签字版数据 - try - { - joparam = (JObject)JsonConvert.DeserializeObject(param); - data = joparam["data"].ToString(); - resultback = scriptCallback.OpenSign(data, callback); - Log.Info("打开签字版数据 返回数据:" + resultback); - base64 = Util.str2Base64(resultback); - //向com对方发送数据 - SendDataWithHeader(client, base64); - } - catch (Exception ex) - { - string rest = "{\"callback\":\"" + "callback" + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + ex.Message + "\"}"; - base64 = Util.str2Base64(rest); - SendDataWithHeader(client, base64); - } - #endregion - break; - //{"type":"7","param":{"data":""}} - case Func.CloseSign: - #region 关闭签字版数据 - try - { - joparam = (JObject)JsonConvert.DeserializeObject(param); - data = joparam["data"].ToString(); - resultback = scriptCallback.CloseSign(data, callback); - Log.Info("关闭签字版数据 返回数据:" + resultback); - base64 = Util.str2Base64(resultback); - //向com对方发送数据 - SendDataWithHeader(client, base64); - } - catch (Exception ex) - { - string rest = "{\"callback\":\"" + "callback" + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + ex.Message + "\"}"; - base64 = Util.str2Base64(rest); - SendDataWithHeader(client, base64); - } - #endregion - break; - //{"type":"8","param":{"url":"http://192.168.0.34:92/api/UploadFP/UploadFP"}} - case Func.GetSignData: - #region 获取签字版数据 - try - { - string sourcepaht = Environment.CurrentDirectory + "\\fiveInch.png"; - FileInfo fileInfo = new FileInfo(sourcepaht); - if (fileInfo.Exists) + JObject jo = (JObject)JsonConvert.DeserializeObject(result); + int type = Convert.ToInt32(jo["type"].ToString()); + string param = jo["param"].ToString(); + string resultback = string.Empty; + string base64 = string.Empty; + string data = string.Empty; + string callback = jo["callback"].ToString(); + JObject joparam; + switch (type) + { + //{"type":"1","param":{"data":""}} + case Func.IDCardRead: + #region 读取身份证 + try { - double size = fileInfo.Length / 1024.0; - if (size > 10) - { - FileStream fs1 = fileInfo.OpenRead(); - fs1.Close(); - resultback = Util.ImgToBase64String(sourcepaht); - int width = 800; - int height = 394; - using (Bitmap blankImage = new Bitmap(width, height)) - { - // 设置图片背景为完全透明 - using (Graphics g = Graphics.FromImage(blankImage)) - { - // 使用白色背景填充图片 - g.Clear(Color.Transparent); - } - // 保存图片到文件系统 - blankImage.Save(sourcepaht); - } - } - else - { - resultback = Util.str2Base64("400"); - } + joparam = (JObject)JsonConvert.DeserializeObject(param); + data = joparam["data"].ToString(); + resultback = scriptCallback.IDCardRead(data, callback); + //Console.WriteLine(resultback); + Log.Info("读取身份证 返回数据:" + resultback); + base64 = Util.str2Base64(resultback); + //向com对方发送数据 + SendDataWithHeader(client, base64); } - else + catch (Exception ex) { - resultback = Util.str2Base64("400"); + string rest = "{\"callback\":\"" + "callback" + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + ex.Message + "\"}"; + base64 = Util.str2Base64(rest); + SendDataWithHeader(client, base64); } - Log.Info("获取签字版数据 返回数据:" + resultback); - // base64 = Util.str2Base64(resultback); - //向com对方发送数据 - SendDataWithHeader(client, resultback); - } - catch (Exception ex) - { - string rest = "{\"callback\":\"" + "callback" + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + ex.Message + "\"}"; - base64 = Util.str2Base64(rest); - SendDataWithHeader(client, base64); - } - #endregion - break; - //开始录音:{"type":"11","param":{"url":"","isopen":true}} - //结束录音:{"type":"11","param":{"url":"http://192.168.0.34:92/api/UploadFP/UploadFP","isopen":false}} - //取消录音:{"type":"11","param":{"url":"","isopen":false}} - case Func.SoundRecording: - #region 开始录音、取消录音、结束录音 - try - { - joparam = (JObject)JsonConvert.DeserializeObject(param); - string url = joparam["url"].ToString(); - bool isopen = Convert.ToBoolean(joparam["isopen"].ToString()); - resultback = scriptCallback.SoundRecording(isopen, url, callback); - Log.Info("开始录音、取消录音、结束录音 返回数据:" + resultback); - base64 = Util.str2Base64(resultback); - //向com对方发送数据 - SendDataWithHeader(client, base64); - } - catch (Exception ex) - { - string rest = "{\"callback\":\"" + "callback" + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + ex.Message + "\"}"; - base64 = Util.str2Base64(rest); + #endregion + break; + //{"type":"2","param":{"ph":"A012","ddrs":"10","qrcode":"www.baidu.com/s?wd=国家赔偿","ywmc":"国家赔偿"}} + case Func.SendByPrint: + #region 打印排队票据 + try + { + joparam = (JObject)JsonConvert.DeserializeObject(param); + string ph = joparam["ph"].ToString(); + string ddrs = joparam["ddrs"].ToString(); + string qrcode = joparam["qrcode"].ToString(); + string ywmc = joparam["ywmc"].ToString(); + resultback = scriptCallback.SendByPrint(ph, ddrs, qrcode, ywmc, callback); + Log.Info("打印排队票据 返回数据:" + resultback); + base64 = Util.str2Base64(resultback); + //向com对方发送数据 + SendDataWithHeader(client, base64); + } + catch (Exception ex) + { + string rest = "{\"callback\":\"" + "callback" + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + ex.Message + "\"}"; + base64 = Util.str2Base64(rest); + SendDataWithHeader(client, base64); + } + #endregion + break; + //{"type":"3","param":{"text":"欢迎使用阿凯思控诉业务一体机","ispaye":false}} + case Func.payleText: + #region 文字语音播报 + try + { + joparam = (JObject)JsonConvert.DeserializeObject(param); + data = joparam["text"].ToString(); + bool ispaye = Convert.ToBoolean(joparam["ispaye"].ToString()); + resultback = scriptCallback.payleText(data, ispaye, callback); + //Console.WriteLine(resultback); + Log.Info("文字语音播报 返回数据:" + resultback); + base64 = Util.str2Base64(resultback); + //向com对方发送数据 + SendDataWithHeader(client, base64); + } + catch (Exception ex) + { + string rest = "{\"callback\":\"" + "callback" + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + ex.Message + "\"}"; + base64 = Util.str2Base64(rest); + SendDataWithHeader(client, base64); + } + #endregion + break; + //{"type":"4","param":{"content":"欢迎使用阿凯思控诉业务一体机","phone":"13333333333"}} + case Func.SendSSM: + #region 发送短信 + try + { + joparam = (JObject)JsonConvert.DeserializeObject(param); + string content = joparam["content"].ToString(); + string phone = joparam["phone"].ToString(); + resultback = scriptCallback.SendSSM(content, phone, callback); + Log.Info("发送短信 返回数据:" + resultback); + base64 = Util.str2Base64(resultback); + //向com对方发送数据 + SendDataWithHeader(client, base64); + } + catch (Exception ex) + { + string rest = "{\"callback\":\"" + "callback" + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + ex.Message + "\"}"; + base64 = Util.str2Base64(rest); + SendDataWithHeader(client, base64); + } + #endregion + break; + //{"type":"5","param":{"url":"http://192.168.0.34:92/api/UploadFP/UploadFP"}} + case Func.openCamera: + #region 打开高拍仪并且进行快速扫描文件 + try + { + joparam = (JObject)JsonConvert.DeserializeObject(param); + data = joparam["url"].ToString(); + resultback = scriptCallback.openCamera(data, callback); + Log.Info("打开高拍仪并且进行快速扫描文件 返回数据:" + resultback); + //base64 = Util.str2Base64(resultback); + //向com对方发送数据 + SendDataWithHeader(client, resultback); + } + catch (Exception ex) + { + string rest = "{\"callback\":\"" + "callback" + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + ex.Message + "\"}"; + base64 = Util.str2Base64(rest); + SendDataWithHeader(client, base64); + } + #endregion + break; + //{"type":"6","param":{"data":""}} + case Func.OpenSign: + #region 打开签字版数据 + try + { + joparam = (JObject)JsonConvert.DeserializeObject(param); + data = joparam["data"].ToString(); + resultback = scriptCallback.OpenSign(data, callback); + Log.Info("打开签字版数据 返回数据:" + resultback); + base64 = Util.str2Base64(resultback); + //向com对方发送数据 + SendDataWithHeader(client, base64); + } + catch (Exception ex) + { + string rest = "{\"callback\":\"" + "callback" + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + ex.Message + "\"}"; + base64 = Util.str2Base64(rest); + SendDataWithHeader(client, base64); + } + #endregion + break; + //{"type":"7","param":{"data":""}} + case Func.CloseSign: + #region 关闭签字版数据 + try + { + joparam = (JObject)JsonConvert.DeserializeObject(param); + data = joparam["data"].ToString(); + resultback = scriptCallback.CloseSign(data, callback); + Log.Info("关闭签字版数据 返回数据:" + resultback); + base64 = Util.str2Base64(resultback); + //向com对方发送数据 + SendDataWithHeader(client, base64); + } + catch (Exception ex) + { + string rest = "{\"callback\":\"" + "callback" + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + ex.Message + "\"}"; + base64 = Util.str2Base64(rest); + SendDataWithHeader(client, base64); + } + #endregion + break; + //{"type":"8","param":{"url":"http://192.168.0.34:92/api/UploadFP/UploadFP"}} + case Func.GetSignData: + #region 获取签字版数据 + try + { + resultback = scriptCallback.GetSignData(data, callback); + Log.Info("获取签字版数据 返回数据:" + resultback); + //base64 = Util.str2Base64(resultback); + //向com对方发送数据 + SendDataWithHeader(client, resultback); + } + catch (Exception ex) + { + string rest = "{\"callback\":\"" + "callback" + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + ex.Message + "\"}"; + base64 = Util.str2Base64(rest); + SendDataWithHeader(client, base64); + } + #endregion + break; + //开始录音:{"type":"11","param":{"url":"","isopen":true}} + //结束录音:{"type":"11","param":{"url":"http://192.168.0.34:92/api/UploadFP/UploadFP","isopen":false}} + //取消录音:{"type":"11","param":{"url":"","isopen":false}} + case Func.SoundRecording: + #region 开始录音、取消录音、结束录音 + try + { + joparam = (JObject)JsonConvert.DeserializeObject(param); + string url = joparam["url"].ToString(); + bool isopen = Convert.ToBoolean(joparam["isopen"].ToString()); + resultback = scriptCallback.SoundRecording(isopen, url, callback); + Log.Info("开始录音、取消录音、结束录音 返回数据:" + resultback); + base64 = Util.str2Base64(resultback); + //向com对方发送数据 + SendDataWithHeader(client, base64); + } + catch (Exception ex) + { + string rest = "{\"callback\":\"" + "callback" + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + ex.Message + "\"}"; + base64 = Util.str2Base64(rest); + SendDataWithHeader(client, base64); + } + #endregion + break; + default: + string str = "{\"callback\":\"" + "callback" + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + "调用方法不存在" + "\"}"; + base64 = Util.str2Base64(str); SendDataWithHeader(client, base64); - } - #endregion - break; - default: - string str = "{\"callback\":\"" + "callback" + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + "调用方法不存在" + "\"}"; - base64 = Util.str2Base64(str); - SendDataWithHeader(client, base64); - break; + break; + } + } + else + { + string str = "{\"callback\":\"" + "callback" + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + "参数不能为空" + "\"}"; + string base64 = Util.str2Base64(str); + SendDataWithHeader(client, base64); } } - else - { - string str = "{\"callback\":\"" + "callback" + "\",\"message\":\"fali\",\"code\":\"400\",\"status\":false,\"data\":\"" + "参数不能为空" + "\"}"; - string base64 = Util.str2Base64(str); - SendDataWithHeader(client, base64); - } - } + } + if (message == "keepAlive") + { + // 客户端发送心跳指令,关闭连接 + Console.WriteLine("客户端发送了心跳指令,关闭连接:{0}", client.Client.RemoteEndPoint); + clients.Remove(client); + client.Close(); + break; + } } - if (message == "keepAlive") - { - // 客户端发送心跳指令,关闭连接 - Console.WriteLine("客户端发送了心跳指令,关闭连接:{0}", client.Client.RemoteEndPoint); - clients.Remove(client); - client.Close(); - break; - } + catch { } + finally { } } - catch { } - finally { } } } diff --git a/DevicesService/Commen/Util.cs b/DevicesService/Commen/Util.cs index 0f9c731..1b7fc42 100644 --- a/DevicesService/Commen/Util.cs +++ b/DevicesService/Commen/Util.cs @@ -71,6 +71,78 @@ namespace DevicesService.Common } } + /// + /// 文件转Base64码 + /// + /// + /// + public static string ImgToBase64StringSign(string fileLocation) + { + MemoryStream ms = new MemoryStream(); + try + { + if (System.IO.File.Exists(fileLocation)) + { + Bitmap bmp = new Bitmap(@fileLocation); + //BitmapFlip(90, ref bmp); + bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); + byte[] arr = new byte[ms.Length]; + ms.Position = 0; + ms.Read(arr, 0, (int)ms.Length); + ms.Close(); + bmp.Dispose(); + File.Delete(fileLocation); + return Convert.ToBase64String(arr); + } + return ""; + } + catch (Exception ex) + { + return ex.Message; + } + finally + { + ms.Close(); + } + } + + /// + /// 文件转Base64码 + /// + /// + /// + public static string ImgToBase64StringBySign(string fileLocation) + { + MemoryStream ms = new MemoryStream(); + try + { + if (System.IO.File.Exists(fileLocation)) + { + Bitmap bmp1 = new Bitmap(fileLocation); + Bitmap bmp = CutImageWhitePart(bmp1, 10);//new Bitmap(@fileLocation); + BitmapFlip(90, ref bmp); + bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); + byte[] arr = new byte[ms.Length]; + ms.Position = 0; + ms.Read(arr, 0, (int)ms.Length); + ms.Close(); + bmp.Dispose(); + bmp1.Dispose(); + File.Delete(fileLocation); + return Convert.ToBase64String(arr); + } + return ""; + } + catch (Exception ex) + { + return ex.Message; + } + finally + { + ms.Close(); + } + } + /// /// 图像翻转,基于图像中心 /// @@ -321,7 +393,7 @@ namespace DevicesService.Common if (!file.Name.Contains("_compress")) { //压缩图片 - image =GetPicThumbnail(file.FullName); + image = GetPicThumbnail(file.FullName); if (image != null) { //压缩后的图片,图片名称增加_compress @@ -409,5 +481,174 @@ namespace DevicesService.Common string base64String = Convert.ToBase64String(fileBytes); return base64String; } + + /// + /// 图片处理成电子签名大小 + /// + /// + /// + public static void ResizeImage(string originalImagePath, string resizedImagePath) + { + int width = 72; + int height = 72; + using (Image originalImage = Image.FromFile(originalImagePath)) + { + // 创建一个新的画布,在其上绘制调整大小后的图片 + using (Bitmap resizedImage = new Bitmap(width, height)) + { + using (Graphics graphics = Graphics.FromImage(resizedImage)) + { + // 设置绘图质量 + graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; + graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; + graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; + + // 清除背景并填充白色 + graphics.Clear(Color.White); + graphics.DrawImage(originalImage, new Rectangle(0, 0, width, height), new Rectangle(0, 0, originalImage.Width, originalImage.Height), GraphicsUnit.Pixel); + + // 保存调整大小后的图片 + resizedImage.Save(resizedImagePath, ImageFormat.Png); + } + } + } + } + + /// + /// 剪去图片空余白边 + /// + /// 源文件 + /// 保留空白边比例 + public static Bitmap CutImageWhitePart(Bitmap bmp, int WhiteBarRate) + { + int top = 0, left = 0; + int right = bmp.Width, bottom = bmp.Height; + Color white = Color.White; + //寻找最上面的标线,从左(0)到右,从上(0)到下 + for (int i = 0; i < bmp.Height; i++)//行 + { + bool find = false; + for (int j = 0; j < bmp.Width; j++)//列 + { + Color c = bmp.GetPixel(j, i); + if (IsWhite(c)) + { + top = i; + find = true; + break; + } + } + if (find) break; + } + //寻找最左边的标线,从上(top位)到下,从左到右 + for (int i = 0; i < bmp.Width; i++)//列 + { + bool find = false; + for (int j = top; j < bmp.Height; j++)//行 + { + Color c = bmp.GetPixel(i, j); + if (IsWhite(c)) + { + left = i; + find = true; + break; + } + } + if (find) break; ; + } + //寻找最下边标线,从下到上,从左到右 + for (int i = bmp.Height - 1; i >= 0; i--)//行 + { + bool find = false; + for (int j = left; j < bmp.Width; j++)//列 + { + Color c = bmp.GetPixel(j, i); + if (IsWhite(c)) + { + bottom = i; + find = true; + break; + } + } + if (find) break; + } + //寻找最右边的标线,从上到下,从右往左 + for (int i = bmp.Width - 1; i >= 0; i--)//列 + { + bool find = false; + for (int j = 0; j <= bottom; j++)//行 + { + Color c = bmp.GetPixel(i, j); + if (IsWhite(c)) + { + right = i; + find = true; + break; + } + } + if (find) break; + } + int iWidth = right - left; + int iHeight = bottom - left; + int blockWidth = Convert.ToInt32(iWidth * WhiteBarRate / 100); + bmp = Cut(bmp, left - blockWidth, top - blockWidth, right - left + 2 * blockWidth, bottom - top + 2 * blockWidth); + return bmp; + } + + + /// + /// 2014.6.13 来源于网络的一个函数 + /// + /// + /// + /// + /// + /// + /// + public static Bitmap Cut(Bitmap b, int StartX, int StartY, int iWidth, int iHeight) + { + if (b == null) + { + return null; + } + int w = b.Width; + int h = b.Height; + if (StartX >= w || StartY >= h) + { + return null; + } + if (StartX + iWidth > w) + { + iWidth = w - StartX; + } + if (StartY + iHeight > h) + { + iHeight = h - StartY; + } + try + { + Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb); + Graphics g = Graphics.FromImage(bmpOut); + g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel); + g.Dispose(); + return bmpOut; + } + catch + { + return null; + } + } + + /// + /// 2014.6.12 判断白色与否,非纯白色 + /// + /// + /// + public static bool IsWhite(Color c) + { + if (c.R < 245 || c.G < 245 || c.B < 245) + return true; + else return false; + } } } diff --git a/DevicesService/Devices/HScamera.cs b/DevicesService/Devices/HScamera.cs index 323e36e..0b52489 100644 --- a/DevicesService/Devices/HScamera.cs +++ b/DevicesService/Devices/HScamera.cs @@ -77,7 +77,6 @@ namespace DevicesService.Devices this.modeIdx = selectMode; } - /// /// 打开高拍仪 /// @@ -87,19 +86,19 @@ namespace DevicesService.Devices try { string filename = string.Empty; - //初始化高拍仪 if (m_vDevice.Count == 0) { DevChangeCallBack = new ELOAM_DEVCHANGECALLBACK(DEVCHANGECALLBACK); EloamDll.EloamGlobal_InitDevs(DevChangeCallBack, IntPtr.Zero); EloamDll.EloamGlobal_VideoCapInit(); - EloamDll.EloamGlobal_InitFaceDetect(); + //EloamDll.EloamGlobal_InitFaceDetect(); } //打开高拍仪 EloamDll.EloamVideo_Release(m_hVideo); HELOAMDEVICE hDev = m_vDevice[devIdx]; m_hVideo = EloamDll.EloamDevice_CreateVideo( hDev, resIdx, modeIdx, null, (IntPtr)0, null, (IntPtr)0, 0, 2); + //开始拍照 HELOAMIMAGE hImg = EloamDll.EloamVideo_CreateImage( m_hVideo, 0, m_hView); @@ -197,7 +196,11 @@ namespace DevicesService.Devices } } - + /// + /// base64转文件 + /// + /// + /// public void ConvertFromBase64ToImage(string base64String, string filePath) { // 将Base64字符串转换为字节数组 diff --git a/DevicesService/Devices/SignDll.cs b/DevicesService/Devices/SignDll.cs index 04d012b..79d4259 100644 --- a/DevicesService/Devices/SignDll.cs +++ b/DevicesService/Devices/SignDll.cs @@ -1,4 +1,5 @@ -using Functions.FileExt; +using DevicesService.Commen; +using Functions.FileExt; using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http; using System; using System.Drawing; @@ -34,16 +35,21 @@ namespace DevicesService.Devices private static Bitmap bitmap = new Bitmap(594, 392); public SignDll() { - endPos = new PointF(-1F, -1f); - beginPos = new PointF(-1F, -1f); - frontPos = new PointF(-1F, -1f); - lastpointx[0] = -1; - lastpointy[0] = -1; - lastpointx[1] = -1; - lastpointy[1] = -1; - lastpointx[2] = -1; - lastpointy[2] = -1; - xypointcount = 0; + try + { + endPos = new PointF(-1F, -1f); + beginPos = new PointF(-1F, -1f); + frontPos = new PointF(-1F, -1f); + lastpointx[0] = -1; + lastpointy[0] = -1; + lastpointx[1] = -1; + lastpointy[1] = -1; + lastpointx[2] = -1; + lastpointy[2] = -1; + xypointcount = 0; + } + catch (Exception ex) { Log.Info(ex.Message); } + finally { } } public static void GetTouchNumber(int number) @@ -83,40 +89,218 @@ namespace DevicesService.Devices FiveInchDll.ComSetBackGroundAdv(); } } - catch { } + catch (Exception ex) { Log.Info(ex.Message); } finally { } } //0816加报点 public static void GetTouchPoint(TOUCH_INFO[] info1) - { } - - //打开签字版 - public static int OpenComDevice() { - int status = FiveInchDll.OpenComDevice(GetTouchNumber); - Console.WriteLine("OpenComDevice:" + status); - if (status == 0) + int x = 0, y = 0; + int pressurevl; + int dx = 0, dy = 0; + + for (int k = 0; k < 80; k++) { - //2022-08-16 加实时报点 - status = FiveInchDll.ComSendPoint(1, GetTouchPoint); - Console.WriteLine("ComSendPoint:" + status); - if (true) + x = info1[k].X; + y = info1[k].Y; + + //落笔 + if (info1[k].Pressure > 0) { - string UIFile = System.IO.Directory.GetCurrentDirectory() + "\\adv_ui.jpg"; - int ret1 = FiveInchDll.ComSendAdvantageImage(UIFile); - Console.WriteLine("ComSendAdvantageImage:" + ret1); - FiveInchDll.ComSetBackGroundAdv(); + if (info1[k].Pressure > 0 && info1[k].Pressure < 500) + { + pressurevl = 1; + pen.Width = 1; + } + else if (info1[k].Pressure >= 500 && info1[k].Pressure < 1000) + { + pressurevl = 2; + pen.Width = 2; + } + else if (info1[k].Pressure >= 1000 && info1[k].Pressure < 1500) + { + pressurevl = 3; + pen.Width = 3; + } + else if (info1[k].Pressure >= 1500 && info1[k].Pressure < 2048) + { + pressurevl = 4; + pen.Width = 4; + } + else + { + pressurevl = 0; + pen.Width = 1; + } + } + else + { + //抬笔 + pressurevl = 0; + + lastpointx[0] = -1; + lastpointy[0] = -1; + lastpointx[1] = -1; + lastpointy[1] = -1; + lastpointx[2] = -1; + lastpointy[2] = -1; + continue; } - if (true) + if (info1[k].Pressure > 10) //有画线宽度 { - string UIFile = System.IO.Directory.GetCurrentDirectory() + "\\sign_ui.jpg"; - int ret1 = FiveInchDll.ComSetSignBackgroundImage(UIFile); - Console.WriteLine("ComSetSignBackgroundImage:" + ret1); + lastpointx[2] = x; + lastpointy[2] = y; + + + if (lastpointx[2] != -1) + { + if (lastpointx[1] != -1 && lastpointx[0] != -1) + { + //float dx = Math.Abs(lastpointx[2] - beginPos.X); + //float dy = Math.Abs(endPos.Y - beginPos.Y); + + dx = Math.Abs(lastpointx[2] - lastpointx[1]); + dy = Math.Abs(lastpointy[2] - lastpointy[1]); + if ((dx != 0) && (dy != 0)) + { + + if (lastpointy[1] != -1 && lastpointy[2] != -1) //y轴相同的点不画,直接跳过 + { + if (lastpointx[1] != -1 && lastpointx[2] != -1) //第3个点和第二个点比较是否x坐标在同一个位置,不是就执行画第一个点到第二个点的线 + { + //painter->drawLine(frontPos, beginPos); //画线 + //g.DrawLine(pen, lastpointx[0], lastpointy[0], lastpointx[1], lastpointy[1]); + //painter->drawPoint(beginPos); //画点 + //frontPos = beginPos; + //beginPos = endPos; + lastpointx[0] = lastpointx[1]; + lastpointy[0] = lastpointy[1]; + lastpointx[1] = lastpointx[2]; + lastpointy[1] = lastpointy[2]; + } + else + { + //是就执行画第一个点到第三个点的线 + //painter->drawLine(frontPos, endPos); + //g.DrawLine(pen, lastpointx[0], lastpointy[0], lastpointx[2], lastpointy[2]); + //frontPos = endPos; //第三个点赋值第一个点 + //beginPos = QPointF(0, 0); //第二个点置空 + //beginPos.X = -1; + //beginPos.Y = -1; + lastpointx[0] = lastpointx[2]; + lastpointy[0] = lastpointy[2]; + lastpointx[1] = -1; + lastpointy[1] = -1; + } + } + } + }// + else + { + if (lastpointx[1] != -1) //不为空在赋值,防止丢弃点时赋空值 + { + lastpointx[0] = lastpointx[1]; + lastpointy[0] = lastpointy[1]; + } + lastpointx[1] = lastpointx[2]; + lastpointy[1] = lastpointy[2]; + } + } + + } + else + { + dx = dy = 0; + lastpointx[0] = -1; + lastpointy[0] = -1; + lastpointx[1] = -1; + lastpointy[1] = -1; + lastpointx[2] = -1; + lastpointy[2] = -1; + + } + } + } + + //打开签字版 + public static int OpenComDevice() + { + try + { + status = FiveInchDll.OpenComDevice(GetTouchNumber); + Console.WriteLine("OpenComDevice:" + status); + if (status == 0) + { + ////2022-08-16 加实时报点 + int ret = FiveInchDll.ComSendPoint(0, null); + //Console.WriteLine("ComSendPoint:" + ret); + if (true) + { + string UIFile = System.IO.Directory.GetCurrentDirectory() + "\\adv_ui.jpg"; + int ret1 = FiveInchDll.ComSendAdvantageImage(UIFile); + Console.WriteLine("ComSendAdvantageImage:" + ret1); + FiveInchDll.ComSetBackGroundAdv(); + } + + if (true) + { + string UIFile = System.IO.Directory.GetCurrentDirectory() + "\\sign_ui.jpg"; + int ret1 = FiveInchDll.ComSetSignBackgroundImage(UIFile); + Console.WriteLine("ComSetSignBackgroundImage:" + ret1); + } } + + //if (status == -1) + //{ + // status = FiveInchDll.OpenComDevice(GetTouchNumber); + // Console.WriteLine("OpenComDevice:" + status); + // if (status == 0) + // { + // ////2022-08-16 加实时报点 + // //int ret = FiveInchDll.ComSendPoint(1, GetTouchPoint); + // //Console.WriteLine("ComSendPoint:" + ret); + // if (true) + // { + // string UIFile = System.IO.Directory.GetCurrentDirectory() + "\\adv_ui.jpg"; + // int ret1 = FiveInchDll.ComSendAdvantageImage(UIFile); + // Console.WriteLine("ComSendAdvantageImage:" + ret1); + // FiveInchDll.ComSetBackGroundAdv(); + // } + + // if (true) + // { + // string UIFile = System.IO.Directory.GetCurrentDirectory() + "\\sign_ui.jpg"; + // int ret1 = FiveInchDll.ComSetSignBackgroundImage(UIFile); + // Console.WriteLine("ComSetSignBackgroundImage:" + ret1); + // } + // } + //} + //else if (status == 0) + //{ + // ////2022-08-16 加实时报点 + // //int ret = FiveInchDll.ComSendPoint(1, GetTouchPoint); + // //Console.WriteLine("ComSendPoint:" + ret); + // if (true) + // { + // string UIFile = System.IO.Directory.GetCurrentDirectory() + "\\adv_ui.jpg"; + // int ret1 = FiveInchDll.ComSendAdvantageImage(UIFile); + // Console.WriteLine("ComSendAdvantageImage:" + ret1); + // FiveInchDll.ComSetBackGroundAdv(); + // } + + // if (true) + // { + // string UIFile = System.IO.Directory.GetCurrentDirectory() + "\\sign_ui.jpg"; + // int ret1 = FiveInchDll.ComSetSignBackgroundImage(UIFile); + // Console.WriteLine("ComSetSignBackgroundImage:" + ret1); + // } + //} } + catch(Exception ex) { Log.Info(ex.Message); } + finally { } return status; } @@ -124,14 +308,19 @@ namespace DevicesService.Devices public static int CloseComDevice() { int ret = -1; - if (status == 0) + try { - ret = FiveInchDll.CloseComDevice(); - if (ret == 0) + if (status == 0) { - status = -1; + ret = FiveInchDll.CloseComDevice(); + if (ret == 0) + { + status = -1; + } } } + catch { } + finally { } return ret; } diff --git a/DevicesService/DevicesService.csproj b/DevicesService/DevicesService.csproj index 6360e4a..ce6b1d4 100644 --- a/DevicesService/DevicesService.csproj +++ b/DevicesService/DevicesService.csproj @@ -22,6 +22,14 @@ AnyCPU + + True + + + + True + + diff --git a/DevicesService/Program.cs b/DevicesService/Program.cs index b1dd2c9..1332bb2 100644 --- a/DevicesService/Program.cs +++ b/DevicesService/Program.cs @@ -46,7 +46,8 @@ namespace DevicesService //启动Tcp服务 Task.Run(() => { - TcpServer.Start(); + TcpServer tcp = new TcpServer(); + tcp.Start(); }); while (true) { diff --git a/DevicesService/Properties/launchSettings.json b/DevicesService/Properties/launchSettings.json new file mode 100644 index 0000000..50adbe1 --- /dev/null +++ b/DevicesService/Properties/launchSettings.json @@ -0,0 +1,12 @@ +{ + "profiles": { + "WSL": { + "commandName": "WSL2", + "distributionName": "" + }, + "DevicesService": { + "commandName": "Executable", + "executablePath": "D:\\Coder\\DevicesServiceIntern\\DevicesService\\bin\\x86\\Debug\\net6.0\\DevicesService.exe" + } + } +} \ No newline at end of file