using Newtonsoft.Json; using OpenCvSharp; using System; using System.Diagnostics; using System.Drawing; using System.Net.Http; using System.Net.NetworkInformation; using System.Threading.Tasks; using System.Windows.Forms; namespace AIParkingApplication { public static class Util { private static HttpClient httpClientEngine = new HttpClient { BaseAddress = new Uri("http://localhost:8080/"), Timeout = TimeSpan.FromMilliseconds(5000) }; public static bool IsPingable(string destinationIPAddress, out long pingTime, int timeOut = UtilConstant.PING_TIMEOUT_MS) { var pingTask = Task.Factory.StartNew(async () => { Ping pinger = new Ping(); try { PingReply reply = await pinger.SendPingAsync(destinationIPAddress, timeOut); return reply; } catch (Exception) { return null; } finally { pinger.Dispose(); } }); var pingResult = pingTask.Result.Result; if (pingResult != null && pingResult.Status == IPStatus.Success) { pingTime = pingResult.RoundtripTime; return true; } else { pingTime = -1; return false; } } public static string GetCurrentMethodName() { var strackTrace = new StackTrace(); var sf = strackTrace.GetFrame(1); return sf.GetMethod().Name; } public static async Task SendEngineRequestAsync(Mat plateImage, PlateType plateType) { string plateImageBase64 = Convert.ToBase64String(plateImage.ToBytes()); try { var request = new PlateRequestEngineModel { Img64 = plateImageBase64, Mode = plateType == PlateType.Square ? "square" : "long", Display = "full" }; HttpResponseMessage response = await httpClientEngine.PostAsJsonAsync("/get-from-frame", request); response.EnsureSuccessStatusCode(); var ocrResult = await response.Content.ReadAsAsync(); return ocrResult; } catch (Exception ex) { Console.WriteLine($"SendEngineRequest : {ex.Message}"); return new OcrResult(); } } public static void UpdateImage(this PictureBox pictureBox, Bitmap image) { pictureBox.Invoke(new Action(() => { pictureBox.Image?.Dispose(); pictureBox.Image = image; })); } public static void UpdateLabel(this Label label, string text, Color color) { label.Invoke(new Action(() => { label.BackColor = color; label.Text = text; })); } public static void UpdateLabel(this Label label, string text) { label.Invoke(new Action(() => { label.Text = text; })); } } public class PlateRequestEngineModel { [JsonProperty("img64")] public string Img64 { get; set; } [JsonProperty("mode")] public string Mode { get; set; } [JsonProperty("display")] public string Display { get; set; } } public class OcrResult { [JsonProperty("ocr")] public string PlateString { get; set; } [JsonProperty("plate")] public string PlateImageBase64 { get; set; } } public static class UtilConstant { public const int PING_TIMEOUT_MS = 500; public const string HTTP_POST_METHOD = "POST"; public const string HTTP_POST_CONTENTTYPE = "application/json"; } }