Browse Source

集成识别车牌接口

weijw 5 months ago
parent
commit
394476182e
3 changed files with 114 additions and 16 deletions
  1. 52 4
      HomeModule.cs
  2. 51 12
      MainForm.cs
  3. 11 0
      api.http

+ 52 - 4
HomeModule.cs

@@ -1,4 +1,7 @@
 using Nancy;
+using Nancy.Json;
+using Nancy.ModelBinding;
+using Newtonsoft.Json;
 using System;
 using System.Collections.Generic;
 using System.Linq;
@@ -16,17 +19,62 @@ namespace xicheji
                 return "ok~";
             });
 
-            Get("/update", (req) =>
+            Get("/update-test", (req) =>
             {
-                Program.MainForm.UpdateTitle(DateTime.Now);
+                Program.MainForm.UpdateTest(DateTime.Now);
                 return "ok~";
             });
 
             Post("/update-car", (req) =>
             {
-                //TODO
-                return "ok~";
+                var requestRoot = this.Bind<RequestRoot>();
+
+                Program.MainForm.UpdateCar(requestRoot.AlarmInfoPlate.result.PlateResult);
+                
+                return JsonConvert.SerializeObject(new
+                {
+                    Response_AlarmInfoPlate = new
+                    {
+                        info = "ok",
+                        content = "...",
+                        is_pay = "true",
+                    }
+                });
             });
         }
     }
+
+
+    public class RequestRoot
+    {
+        public Alarminfoplate AlarmInfoPlate { get; set; }
+    }
+
+    public class Alarminfoplate
+    {
+        public Result result { get; set; }
+    }
+
+    public class Result
+    {
+        public Plateresult PlateResult { get; set; }
+    }
+
+    public class Plateresult
+    {
+        public string license { get; set; }//车牌号字符串,如 “京AAAAAA”
+        public string imageFile { get; set; }//识别大图片内容经过base64后的字符串,需要开启发送大图片
+        public ColorType colorType { get; set; }
+    }
+
+    public enum ColorType
+    {
+        Unknown,
+        Blue,
+        Yellow,
+        White,
+        Black,
+        Green,
+        YellowGreen,
+    }
 }

+ 51 - 12
MainForm.cs

@@ -20,6 +20,7 @@ namespace xicheji
         private byte[]? currentDataBlock;
         private Plc? plc = null;
         private HikApi? hikApi = null;
+        private string lastCarNo = string.Empty;
 
         public MainForm()
         {
@@ -197,6 +198,13 @@ namespace xicheji
 
             if (ReadPLCInt("最后一次洗车开始月") != 0)
             {
+                if (string.IsNullOrEmpty(lastCarNo))
+                {
+                    receivePLCTimer.Enabled = false;
+                    MessageBox.Show("未检测到车牌信息,请检查是否识别正常");
+                    return;
+                }
+
                 //洗车完成后才有数据的
 
                 var washStartTime = new DateTime(
@@ -218,7 +226,7 @@ namespace xicheji
                 var washRecord = new WashRecord()
                 {
                     DeviceId = macAddress,
-                    CarNum = "",//TODO
+                    CarNum = lastCarNo,
                     StartTime = washStartTime,
                     EndTime = washEndTime,
                     DryTime = ReadPLCInt("最后一次洗车风干时长"),
@@ -229,6 +237,9 @@ namespace xicheji
                 };
 
                 washRecords.Add(washRecord);
+
+                //洗车完成后重置车牌信息
+                lastCarNo = string.Empty;
             }
         }
 
@@ -577,22 +588,13 @@ namespace xicheji
                 washTime = washRecord.WashTime, //洗车时长(s)
                 widnowsDefender = washRecord.TotalWater, //用水量(t)
                 waterPressure = washRecord.WaterPressure, //压力(MPa)
-                picBase64 = ConvertImageToBase64(pbCamera.Image, ImageFormat.Jpeg), //洗车图片base64
+                picBase64 = ImageToBase64(pbCamera.Image, ImageFormat.Jpeg), //洗车图片base64
             };
             request.AddParameter("application/json", JsonConvert.SerializeObject(jsonBody), ParameterType.RequestBody);
             var response = await restClient.ExecuteAsync(request);
             if (response == null) return;
         }
 
-        private static string ConvertImageToBase64(Image image, ImageFormat format)
-        {
-            if (image == null) return string.Empty;
-
-            using var memoryStream = new MemoryStream();
-            image.Save(memoryStream, format);
-            var imageBytes = memoryStream.ToArray();
-            return Convert.ToBase64String(imageBytes);
-        }
         #endregion
 
         #region 摄像头
@@ -674,13 +676,50 @@ namespace xicheji
 
         #region 服务接口回调->更新汽车信息
 
-        public void UpdateTitle(DateTime now)
+        public void UpdateTest(DateTime now)
         {
             this.Invoke(() =>
             {
                 this.Text = now.ToString();
             });
         }
+
+        public void UpdateCar(Plateresult result)
+        {
+            this.Invoke(() =>
+            {
+                var carNo = result.license;
+                var base64String = result.imageFile;
+                var colorType = result.colorType;
+                //var image = Base64ToImage(base64String);
+
+                this.lastCarNo = carNo;
+            });
+        }
+
+        #endregion
+
+        #region Image <-> Base64
+
+        public static Image Base64ToImage(string base64String)
+        {
+            byte[] imageBytes = Convert.FromBase64String(base64String);
+            using var ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
+            ms.Write(imageBytes, 0, imageBytes.Length);
+            Image image = Image.FromStream(ms, true);
+            return image;
+        }
+
+        public static string ImageToBase64(Image image, ImageFormat format)
+        {
+            if (image == null) return string.Empty;
+
+            using var memoryStream = new MemoryStream();
+            image.Save(memoryStream, format);
+            var imageBytes = memoryStream.ToArray();
+            return Convert.ToBase64String(imageBytes);
+        }
+
         #endregion
     }
 }

File diff suppressed because it is too large
+ 11 - 0
api.http


Some files were not shown because too many files changed in this diff