using OpenCvSharp; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Windows.Forms; namespace laserPTZ.AI { public class ScoketClient { public static ResponseData responseData = new ResponseData(); static Socket clientSocket; public static bool isConnect = false; public static void start() { try { if (isConnect) return; String ipStr= ConfigHelper.GetAppConfig("scoketIp"); String portStr = ConfigHelper.GetAppConfig("scoketPort"); clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress ip = IPAddress.Parse(ipStr); IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(portStr)); //获得要连接的远程服务器应用程序的IP地址和端口号 clientSocket.Connect(point); isConnect = true; //开启一个新的线程不停的接收服务端发来的消息 Thread th1 = new Thread(ReciveMessage); th1.IsBackground = true; th1.Start(); } catch (Exception ex) { isConnect = false; } } public static void sendMsg(String msg) { if (!isConnect) return; byte[] buffer = System.Text.Encoding.UTF8.GetBytes(msg); clientSocket.Send(buffer); } static StringBuilder sb = new StringBuilder(); static string terminateString = "\r\n"; static void ReciveMessage() { while (isConnect) { try { byte[] buffer = new byte[1024 * 1024 * 3]; int receivedSize = clientSocket.Receive(buffer); string rawMsg = Encoding.Default.GetString(buffer, 0, receivedSize); int rnFixLength = terminateString.Length; //这个是指消息结束符的长度,此处为\r\n for (int i = 0; i < rawMsg.Length;) //遍历接收到的整个buffer文本 { if (i <= rawMsg.Length - rnFixLength) { if (rawMsg.Substring(i, rnFixLength) != terminateString)//非消息结束符,则加入sb { sb.Append(rawMsg[i]); i++; } else { responseData = ResponseData.convertToObject(sb.ToString()); sb.Clear(); i += rnFixLength; } } else { sb.Append(rawMsg[i]); i++; } } } catch (Exception ex) { //MessageBox.Show(ex.Message, "ReciveMessage"); } } } public static void close() { if (isConnect) { clientSocket.Close(); isConnect = false; } } public static Scalar getColor() { //Random ro = new Random(10); long tick = DateTime.Now.Ticks; Random ran = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32)); int R = ran.Next(255); int G = ran.Next(255); int B = ran.Next(255); B = (R + G > 400) ? R + G - 400 : B; B = (B > 255) ? 255 : B; return new Scalar(R, G, B); } public static void drawRange(Mat mat,double sourceWidth,double sourceHeight) { try { if (responseData.Msg == null) return; Msg[] msgArray = responseData.Msg; foreach (Msg msg in msgArray) { int x1 = (int)(sourceWidth * msg.Left_top[0]); int y1 = (int)(sourceHeight * msg.Left_top[1]); int x2 = (int)(sourceWidth * msg.Right_bottom[0]); int y2 = (int)(sourceHeight * msg.Right_bottom[1]); string label = Enum.Parse(typeof(EnumClassType), msg.Class_id) + ":" + msg.Confidence; Scalar scalar = getColor(); Cv2.Rectangle(mat, new Point(x1, y1), new Point(x2, y2), scalar, 2); Cv2.PutText(mat, label, new Point(x1, y1 - 2), 0, 1, scalar, 2); } } catch (Exception ex) { MessageBox.Show(ex.Message, "drawRange"); } } public static void drawLabel(Mat image, String _label) { String label = "CH4:" + _label + "ppm·m"; Cv2.PutText(image, label, new Point(20, 60), HersheyFonts.HersheyPlain, 1, Scalar.Black, 2); } } }