using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json.Linq; using Newtonsoft.Json; namespace WebManager { public class WebResult { #region /// /// 数据定义 /// private String webServer_IP = ""; private String webServer_Port = ""; private String webServer_Path = ""; private String Url = ""; private const String Post = "Post"; private const String Content_Type= "Content-Type:application/json"; private String[] _url = new string[8]; #endregion /// /// 构造函数 /// /// WebServer的IP地址 /// WebServer的端口号 public WebResult(String ws_ip,String ws_port,String ws_url) { this.webServer_IP = ws_ip; this.webServer_Port = ws_port; this._url = ws_url.Split(','); //更新Web网址 Update_Url(); } /// /// 更新Web网址 /// private void Update_Url() { this.webServer_Path = "/" + this.webServer_Path; this.Url = "http://" + this.webServer_IP + ":" + this.webServer_Port + this.webServer_Path; } /// /// WebServer的IP地址 /// public String WebServer_IP { //get { return this.webServer_IP; } set { this.webServer_IP = value; } } /// /// WebServer的端口号 /// public String WebServer_Port { //get { return this.webServer_IP; } set { this.webServer_Port = value; } } /// /// 向WebServer请求数据及返回结果 /// /// 请求数据的JSON /// 返回结果的JSON private String RequestString(String postString) { try { String res = HttpRequestHelper.DoRequest(this.Url, Post, Content_Type, postString); return res; } catch { return ""; } } /// /// 计算原始图像偏移角度及方向 /// /// 图片路径 /// 孔类型 /// 厂家类型 /// json字符串 public void Img_OffsetAngle_Direction(String imagePath, int imageType, String firm, out double degree, out int direction, out int state) { try { this.webServer_Path = this._url[0]; } catch { this.webServer_Path = "FIB_degree_recognize"; } Update_Url(); JObject json = new JObject(); json.Add("img_path", imagePath); json.Add("img_type", imageType); json.Add("firm", firm); String res = RequestString(JsonConvert.SerializeObject(json)); JObject jret= (JObject)JsonConvert.DeserializeObject(res); if (jret != null && jret.Property("img_name") != null && jret.Property("img_name").ToString() != "") { JObject jname = JObject.Parse(jret["img_name"].ToString()); degree = Convert.ToDouble(jname["degree"].ToString()); direction = Convert.ToInt32(jname["direction"].ToString()); state = Convert.ToInt32(jname["state"].ToString()); } else { state = 0; direction = 0; degree = 0; } } /// /// 计算切割点位置 /// /// 图片路径 /// 孔类型 /// 厂家类型 /// 切割点相对于原点坐标左上角x值 /// 切割点相对于原点坐标左上角y值 /// 切割点相对于原点坐标右上角x值 /// 切割点相对于原点坐标右上角y值 /// 返回状态 /// json字符串 public void Img_Cut_Position(String imagePath, String imageType, String firm, out float offsetx1, out float offsety1, out float offsetx2, out float offsety2, out int state) { try { this.webServer_Path = this._url[1]; } catch { this.webServer_Path = "test2"; } Update_Url(); JObject json = new JObject(); json.Add("img_path", imagePath); json.Add("img_type", imageType); json.Add("firm", firm); String res = RequestString(JsonConvert.SerializeObject(json)); JObject jret = (JObject)JsonConvert.DeserializeObject(res); if (jret != null && jret.Property("img_name") != null && jret.Property("img_name").ToString() != "") { JObject jname = JObject.Parse(jret["img_name"].ToString()); JArray jlocation = JArray.Parse(jname["location"].ToString()); offsetx1 = Convert.ToSingle(jlocation[0][0].ToString()); offsety1 = Convert.ToSingle(jlocation[0][1].ToString()); offsetx2 = Convert.ToSingle(jlocation[1][0].ToString()); offsety2 = Convert.ToSingle(jlocation[1][1].ToString()); state = Convert.ToInt32(jname["state"].ToString()); } else { state = 0; offsetx1 = 0; offsety1 = 0; offsetx2 = 0; offsety2 = 0; } } /// /// 是否切割成功验证 /// /// 切割前图像路径 /// 切割后图像路径 /// json字符串 public void Img_Cut_Success(String imageBefore, String imageAfter, out int state) { try { this.webServer_Path = this._url[2]; } catch { this.webServer_Path = "FIB_verify"; } Update_Url(); JObject json = new JObject(); json.Add("img_before_path", imageBefore); json.Add("img_after_path", imageAfter); String res = RequestString(JsonConvert.SerializeObject(json)); JObject jret = (JObject)JsonConvert.DeserializeObject(res); if (jret != null && jret.Property("state") != null && jret.Property("state").ToString() != "") { state = Convert.ToInt32(jret["state"].ToString()); } else { state = 0; } } /// /// 计算切割后图像梯形区域上边中心点坐标 /// /// 图片路径 /// json字符串 public void Img_Trapezoid_Top_Center_Position(String imagePath, out double offsetx, out double offsety, out int state) { try { this.webServer_Path = this._url[3]; } catch { this.webServer_Path = "test4"; } Update_Url(); JObject json = new JObject(); json.Add("img_path", imagePath); String res = RequestString(JsonConvert.SerializeObject(json)); JObject jret = (JObject)JsonConvert.DeserializeObject(res); if (jret != null && jret.Property("img_name") != null && jret.Property("img_name").ToString() != "") { JObject jname = JObject.Parse(jret["img_name"].ToString()); JArray jlocation = JArray.Parse(jname["top_center"].ToString()); offsetx = Convert.ToDouble(jlocation[0].ToString()); offsety = Convert.ToDouble(jlocation[1].ToString()); state = Convert.ToInt32(jname["state"].ToString()); } else { state = 0; offsetx = 0; offsety = 0; } } /// /// 自动对焦 /// /// 一组图片路径 /// json字符串 public String Img_Auto_Focus(List imagePath) { try { this.webServer_Path = this._url[4]; } catch { this.webServer_Path = "test5"; } Update_Url(); JObject json = new JObject(); for(int i=0;i /// 自动消像散 /// /// 一组图片路径 /// json字符串 public String Img_Auto_Stigmatic(List imagePath) { try { this.webServer_Path = this._url[5]; } catch { this.webServer_Path = "test6"; } Update_Url(); JObject json = new JObject(); for (int i = 0; i < imagePath.Count; i++) { json.Add("img_path" + i.ToString(), imagePath[i]); } String res = RequestString(JsonConvert.SerializeObject(json)); JObject jret = (JObject)JsonConvert.DeserializeObject(res); if (jret != null && jret.Property("img_path") != null && jret.Property("img_path").ToString() != "") { return jret["img_path"].ToString(); } else { return ""; } } /// /// 计算切割面区域中心坐标,以及偏移角度及方向 /// /// 图片路径 /// json字符串 public void Img_Center_Position_OffsetAngle_Direction(String imagePath, String imageType, String firm, out float offsetx, out float offsety, out float degree, out int direction, out int state) { try { this.webServer_Path = this._url[6]; } catch { this.webServer_Path = "test7"; } Update_Url(); JObject json = new JObject(); json.Add("img_path", imagePath); json.Add("img_type", imageType); json.Add("firm", firm); String res = RequestString(JsonConvert.SerializeObject(json)); JObject jret = (JObject)JsonConvert.DeserializeObject(res); if (jret != null && jret.Property("img_name") != null && jret.Property("img_name").ToString() != "") { JObject jname = JObject.Parse(jret["img_name"].ToString()); JArray jlocation = JArray.Parse(jname["center"].ToString()); offsetx = Convert.ToSingle(jlocation[0].ToString()); offsety = Convert.ToSingle(jlocation[1].ToString()); state = Convert.ToInt32(jname["state"].ToString()); degree = Convert.ToSingle(jname["degree"].ToString()); direction = Convert.ToInt32(jname["direction"].ToString()); } else { state = 0; direction = 0; degree = 0; offsetx = 0; offsety = 0; } } /// /// 测量尺寸 /// /// 图片路径 /// 放大倍数 /// 一像素代表**mm/um /// json字符串 public void Img_Measure_Size(String imagePath, String ratio, String size, out int state) { try { this.webServer_Path = this._url[7]; } catch { this.webServer_Path = "test8"; } Update_Url(); JObject json = new JObject(); json.Add("img_path", imagePath); json.Add("ratio", ratio); json.Add("size", size); String res = RequestString(JsonConvert.SerializeObject(json)); JObject jret = (JObject)JsonConvert.DeserializeObject(res); if (jret != null && jret.Property("img_name") != null && jret.Property("img_name").ToString() != "") { JObject jstate = JObject.Parse(jret["img_name"].ToString()); state = Convert.ToInt32(jstate["state"].ToString()); } else { state = 0; } } } }