using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Drawing;
 

namespace WebManager
{
    public class ImageProcess : IImageProcessByWeb
    {
        #region
        /// <summary>
        /// 数据定义
        /// </summary>
        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[9];

        #endregion

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="ws_ip">WebServer的IP地址</param>
        /// <param name="ws_port">WebServer的端口号</param>
        public ImageProcess(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();
        }

        /// <summary>
        /// 更新Web网址
        /// </summary>
        private void Update_Url()
        {
            this.webServer_Path = "/" + this.webServer_Path;
            this.Url = "http://" + this.webServer_IP + ":" + this.webServer_Port + this.webServer_Path;
        }

        /// <summary>
        /// WebServer的IP地址
        /// </summary>
        public String WebServer_IP
        {
            //get { return this.webServer_IP; }
            set
            {
                this.webServer_IP = value;
            }
        }

        /// <summary>
        /// WebServer的端口号
        /// </summary>
        public String WebServer_Port
        {
            //get { return this.webServer_IP; }
            set
            {
                this.webServer_Port = value;
            }
        }

        /// <summary>
        /// 向WebServer请求数据及返回结果
        /// </summary>
        /// <param name="postString">请求数据的JSON</param>
        /// <returns>返回结果的JSON</returns>
        private String RequestString(String postString)
        {
            try
            {
                String res = HttpRequestHelper.DoRequest(this.Url, Post, Content_Type, postString);
                return res;
            }
            catch
            {
                return "";
            }
        }

        /// <summary>
        /// 计算原始图像偏移角度及方向
        /// </summary>
        /// <param name="imagePath">图片路径</param>
        /// <param name="imageType">孔类型</param>
        /// <param name="firm">厂家类型</param>
        /// <param name="degree">返回角度</param>
        /// <param name="direction">返回方向</param>
        /// <param name="state">图像接口返回状态</param>
        /// <returns>json字符串</returns>
        public void Img_OffsetAngle_Direction(String imagePath, int imageType, String firm, out float 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(imagePath) != null && jret.Property(imagePath).ToString() != "")
            {
                JObject jname = JObject.Parse(jret[imagePath].ToString());
                state = Convert.ToInt32(jname["state"].ToString());
                if (state == 1)
                {
                    degree = Convert.ToSingle(jname["degree"].ToString());
                    direction = Convert.ToInt32(jname["direction"].ToString());
                }
                else
                {
                    degree = 0;
                    direction = 0;
                }
            }
            else
            {
                state = 0;
                direction = 0;
                degree = 0;
            }
        }

        /// <summary>
        /// 计算切割点位置
        /// </summary>
        /// <param name="imagePath">图片路径</param>
        /// <param name="imageType">样品类型</param>
        /// <param name="firm">厂家类型</param>
        /// <param name="offsetx1">切割点相对于原点坐标左上角x值</param>
        /// <param name="offsetx2">切割点相对于原点坐标左上角y值</param>
        /// <param name="offsety1">切割点相对于原点坐标右上角x值</param>
        /// <param name="offsety2">切割点相对于原点坐标右上角y值</param>
        /// <param name="state">返回状态</param>
        /// <returns>json字符串</returns>
        public void Img_Cut_Position(String imagePath, int 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(imagePath) != null && jret.Property(imagePath).ToString() != "")
            {
                JObject jname = JObject.Parse(jret[imagePath].ToString());
                JArray jlocation = JArray.Parse(jname["location"].ToString());
                
                state = Convert.ToInt32(jname["state"].ToString());
                if (state == 1)
                {
                    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());
                }
                else
                {
                    offsetx1 = 0;
                    offsety1 = 0;
                    offsetx2 = 0;
                    offsety2 = 0;
                }
            }
            else
            {
                state = 0;
                offsetx1 = 0;
                offsety1 = 0;
                offsetx2 = 0;
                offsety2 = 0;
            }
        }

        /// <summary>
        /// 是否切割成功验证
        /// </summary>
        /// <param name="imageBefore">切割前图像路径</param>
        /// <param name="imageAfter">切割后图像路径</param>
        /// <param name="state">图像接口返回状态</param>
        /// <returns>json字符串</returns>
        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;
            }
        }

        /// <summary>
        /// 计算切割后图像梯形区域上边中心点坐标
        /// </summary>
        /// <param name="imagePath">图片路径</param>
        /// <param name="offsetx">梯形中心点X轴坐标</param>
        /// <param name="offsety">梯形中心点Y轴坐标</param>
        /// <param name="state">图像接口返回状态</param>
        /// <returns>json字符串</returns>
        public void Img_Trapezoid_Top_Center_Position(String imagePath, int imageType, String firm, out float offsetx, out float offsety, out int state)//2020-12-15 修改添加  imageType,  firm 两个参数
        {
            try
            {
                this.webServer_Path = this._url[3];
            }
            catch
            {
                this.webServer_Path = "test4";
            }
            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(imagePath) != null && jret.Property(imagePath).ToString() != "")
            {
                JObject jname = JObject.Parse(jret[imagePath].ToString());
                JArray jlocation = JArray.Parse(jname["top_center"].ToString());
                
                state = Convert.ToInt32(jname["state"].ToString());
                if(state==1)
                {
                    offsetx = Convert.ToSingle(jlocation[0].ToString());
                    offsety = Convert.ToSingle(jlocation[1].ToString());
                }
                else
                {
                    offsetx = 0;
                    offsety = 0;
                }
            }
            else
            {
                state = 0;
                offsetx = 0;
                offsety = 0;
            }
        }

        /// <summary>
        /// 自动对焦
        /// </summary>
        /// <param name="imagePath">一组图片路径</param>
        /// <returns>json字符串</returns>
        public String Img_Auto_Focus(List<String> imagePath)
        {
            try
            {
                this.webServer_Path = this._url[4];
            }
            catch
            {
                this.webServer_Path = "test5";
            }
            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 "";
            }
        }

        /// <summary>
        /// 自动消像散
        /// </summary>
        /// <param name="imagePath">一组图片路径</param>
        /// <returns>json字符串</returns>
        public String Img_Auto_Stigmatic(List<String> 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 "";
            }
        }

        /// <summary>
        /// 计算切割面区域偏移角度及方向
        /// </summary>
        /// <param name="imagePath">图片路径</param>
        /// <param name="degree">图像偏移角度</param>
        /// <param name="direction">旋转方向</param>
        /// <param name="state">图像接口返回状态</param>
        /// <returns>json字符串</returns>
        public void Img_Center_Position_OffsetAngle_Direction(String imagePath, int imageType, String firm, 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(imagePath) != null && jret.Property(imagePath).ToString() != "")
            {
                JObject jname = JObject.Parse(jret[imagePath].ToString());
                state = Convert.ToInt32(jname["state"].ToString());
                if (state == 1)
                {
                    degree = Convert.ToSingle(jname["degree"].ToString());
                    direction = Convert.ToInt32(jname["direction"].ToString());
                }
                else
                {
                    degree = 0;
                    direction = 0;
                }
            }
            else
            {
                state = 0;
                direction = 0;
                degree = 0;
                //offsetx = 0;
                //offsety = 0;
            }
        }

        /// <summary>
        /// 计算两个测量区域坐标
        /// </summary>
        /// <param name="imagePath">图片路径</param>
        /// <param name="imageType">样品类型</param>
        /// <param name="firm">厂商</param>
        /// <param name="center1">观测点1位置</param>
        /// <param name="mag1">观测点1放大倍数</param>
        /// <param name="center2">观测点2位置</param>
        /// <param name="mag2">观测点2放大倍数</param>
        /// <param name="state">返回结果状态</param>
        /// <returns>json字符串</returns>
        public void Img_Two_Region_Position(String imagePath, int imageType, String firm, out Point center1, out float mag1, out Point center2, out float mag2, out Point center3, 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("img_type", imageType);
            json.Add("firm", firm);
            String res = RequestString(JsonConvert.SerializeObject(json));
            JObject jret = (JObject)JsonConvert.DeserializeObject(res);
            if (jret != null && jret.Property(imagePath) != null && jret.Property(imagePath).ToString() != "")
            {
                JObject jname = JObject.Parse(jret[imagePath].ToString());
                state = Convert.ToInt32(jname["state"].ToString());
                if (state == 1)
                {
                    JArray jcenter1 = JArray.Parse(jname["center1"].ToString());
                    String ptx = jcenter1[0].ToString();
                    String pty = jcenter1[1].ToString();
                    center1 = new Point(Convert.ToInt32(ptx),Convert.ToInt32(pty));
                    String jmag1 = jname["magnification1"].ToString();
                    mag1 = Convert.ToSingle(jmag1.ToString());
                    JArray jcenter2 = JArray.Parse(jname["center2"].ToString());
                    ptx = jcenter2[0].ToString();
                    pty = jcenter2[1].ToString();
                    center2 = new Point(Convert.ToInt32(ptx), Convert.ToInt32(pty));
                    String jmag2 = jname["magnification2"].ToString();
                    mag2 = Convert.ToSingle(jmag2.ToString());
                    JArray jcenter3 = JArray.Parse(jname["center3"].ToString());
                    ptx = jcenter3[0].ToString();
                    pty = jcenter3[1].ToString();
                    center3 = new Point(Convert.ToInt32(ptx), Convert.ToInt32(pty));
                }
                else
                {
                    center1 = new Point(0, 0);
                    center2 = new Point(0, 0);
                    center3 = new Point(0, 0);
                    mag1 = 0;
                    mag2 = 0;
                    state = 0;
                }
            }
            else
            {
                center1 = new Point(0, 0);
                center2 = new Point(0, 0);
                center3 = new Point(0, 0);
                mag1 = 0;
                mag2 = 0;
                state = 0;
            }
        }

        /// <summary>
        /// 测量尺寸
        /// </summary>
        /// <param name="imagePath1">图片1路径</param>
        /// <param name="imagePath2">图片2路径</param>
        /// <param name="size1">图片1的像素大小</param>
        /// <param name="size2">图片2的像素大小</param>
        /// <param name="resultPath">测量结果的生成目录</param>
        /// <param name="imageType">样品类型</param>
        /// <param name="firm">厂类</param>
        /// <param name="state">测量结果返回</param>
        /// <returns>json字符串</returns>
        public void Img_Measure_Size(String imagePath1, String imagePath2, float size1, float size2, String resultPath, int imageType, String firm, out int state)
        {
            try
            {
                this.webServer_Path = this._url[8];
            }
            catch
            {
                this.webServer_Path = "test9";
            }
            Update_Url();
            JObject json = new JObject();
            json.Add("img_path1", imagePath1);
            json.Add("img_path2", imagePath2);
            json.Add("size1", size1);
            json.Add("size2", size2);
            json.Add("result_path", resultPath);
            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("state") != null && jret.Property("state").ToString() != "")
            {
                state = Convert.ToInt32(jret["state"].ToString());
            }
            else
            {
                state = 0;
            }
        }

        /// <summary>
        /// DDIC计算原始图像偏移角度及方向
        /// </summary>
        /// <param name="imagePath">图片路径</param>
        /// <param name="degree">返回角度</param>
        /// <param name="direction">返回方向</param>
        /// <param name="state">图像接口返回状态</param>
        /// <returns>json字符串</returns>
        public void Img_DDIC_OffsetAngle_Direction(String imagePath, out float degree, out int direction, out int state)
        {
            try
            {
                this.webServer_Path = this._url[9];
            }
            catch
            {
                this.webServer_Path = "DDIC_degree_recognize";
            }
            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(imagePath) != null && jret.Property(imagePath).ToString() != "")
            {
                JObject jname = JObject.Parse(jret[imagePath].ToString());
                state = Convert.ToInt32(jname["state"].ToString());
                if (state == 1)
                {
                    degree = Convert.ToSingle(jname["degree"].ToString());
                    direction = Convert.ToInt32(jname["direction"].ToString());
                }
                else
                {
                    degree = 0;
                    direction = 0;
                }
            }
            else
            {
                state = 0;
                direction = 0;
                degree = 0;
            }
        }

        /// <summary>
        /// DDIC计算切割点位置
        /// </summary>
        /// <param name="imagePath">图片路径</param>
        /// <param name="offsetx">切割点相对于原点坐标左上角x值</param>
        /// <param name="offsety">切割点相对于原点坐标左上角y值</param>
        /// <param name="state">返回状态</param>
        /// <returns>json字符串</returns>
        public void Img_DDIC_Cut_Position(String imagePath, int imageType, String firm, out float offsetx, out float offsety, out int state)
        {
            try
            {
                this.webServer_Path = this._url[10];
            }
            catch
            {
                this.webServer_Path = "DDIC_cal_cut_location";
            }
            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(imagePath) != null && jret.Property(imagePath).ToString() != "")
            {
                JObject jname = JObject.Parse(jret[imagePath].ToString());
                state = Convert.ToInt32(jname["state"].ToString());
                if (state == 1)
                {
                    JArray jlocation = JArray.Parse(jname["location"].ToString());
                    offsetx = Convert.ToSingle(jlocation[0][0].ToString());
                    offsety = Convert.ToSingle(jlocation[0][1].ToString());
                }
                else
                {
                    offsetx = 0;
                    offsety = 0;
                }
            }
            else
            {
                state = 0;
                offsetx = 0;
                offsety = 0;
            }
        }

        /// <summary>
        /// DDIC梯形位置识别
        /// </summary>
        /// <param name="imagePath">图片路径</param>
        /// <param name="offsetx">梯形中心点X轴坐标</param>
        /// <param name="offsety">梯形中心点Y轴坐标</param>
        /// <param name="state">图像接口返回状态</param>
        /// <returns>json字符串</returns>
        public void Img_DDIC_Trapezoid_Top_Center_Position(String imagePath, out float offsetx, out float offsety, out int state)
        {
            try
            {
                this.webServer_Path = this._url[11];
            }
            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(imagePath) != null && jret.Property(imagePath).ToString() != "")
            {
                JObject jname = JObject.Parse(jret[imagePath].ToString());
                JArray jlocation = JArray.Parse(jname["top_center"].ToString());

                state = Convert.ToInt32(jname["state"].ToString());
                if (state == 1)
                {
                    offsetx = Convert.ToSingle(jlocation[0].ToString());
                    offsety = Convert.ToSingle(jlocation[1].ToString());
                }
                else
                {
                    offsetx = 0;
                    offsety = 0;
                }
            }
            else
            {
                state = 0;
                offsetx = 0;
                offsety = 0;
            }
        }

        /// <summary>
        /// DDIC测量点计算
        /// </summary>
        /// <param name="imagePath">图片路径</param>
        /// <param name="state">返回结果状态</param>
        /// <returns>json字符串</returns>
        public void Img_DDIC_Measure_Location(String imagePath, out List<Point> measure_points, out int state)
        {
            try
            {
                this.webServer_Path = this._url[12];
            }
            catch
            {
                this.webServer_Path = "DDIC_measure_location";
            }
            Update_Url();
            measure_points = new List<Point>();
            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(imagePath) != null && jret.Property(imagePath).ToString() != "")
            {
                JObject jname = JObject.Parse(jret[imagePath].ToString());
                state = Convert.ToInt32(jname["state"].ToString());
                if (state == 1)
                {
                    JArray jcenter1 = JArray.Parse(jname["location1"].ToString());
                    String ptx = jcenter1[0].ToString();
                    String pty = jcenter1[1].ToString();
                    measure_points.Add(new Point(Convert.ToInt32(ptx), Convert.ToInt32(pty)));
                    jcenter1 = JArray.Parse(jname["location2"].ToString());
                    ptx = jcenter1[0].ToString();
                    pty = jcenter1[1].ToString();
                    measure_points.Add(new Point(Convert.ToInt32(ptx), Convert.ToInt32(pty)));
                    jcenter1 = JArray.Parse(jname["location3"].ToString());
                    ptx = jcenter1[0].ToString();
                    pty = jcenter1[1].ToString();
                    measure_points.Add(new Point(Convert.ToInt32(ptx), Convert.ToInt32(pty)));
                    jcenter1 = JArray.Parse(jname["location4"].ToString());
                    ptx = jcenter1[0].ToString();
                    pty = jcenter1[1].ToString();
                    measure_points.Add(new Point(Convert.ToInt32(ptx), Convert.ToInt32(pty)));
                    jcenter1 = JArray.Parse(jname["location5"].ToString());
                    ptx = jcenter1[0].ToString();
                    pty = jcenter1[1].ToString();
                    measure_points.Add(new Point(Convert.ToInt32(ptx), Convert.ToInt32(pty)));
                }
                else
                {
                    state = 0;
                }
            }
            else
            {
                state = 0;
            }
        }

        /// <summary>
        /// DDIC测量尺寸
        /// </summary>
        /// <param name="imagePath1">图片1路径</param>
        /// <param name="imagePath2">图片2路径</param>
        /// <param name="imagePath3">图片3路径</param>
        /// <param name="imagePath4">图片4路径</param>
        /// <param name="imagePath5">图片5路径</param>
        /// <param name="resultPath">测量结果的生成目录</param>
        /// <param name="state">测量结果返回</param>
        /// <returns>json字符串</returns>
        public void Img_DDIC_Measure_Size(String imagePath1, String imagePath2, String imagePath3, String imagePath4, String imagePath5, String resultPath, out int state)
        {
            try
            {
                this.webServer_Path = this._url[13];
            }
            catch
            {
                this.webServer_Path = "DDIC_measure_parameter";
            }
            Update_Url();
            JObject json = new JObject();
            json.Add("img_path1", imagePath1);
            json.Add("img_path2", imagePath2);
            json.Add("img_path3", imagePath3);
            json.Add("img_path4", imagePath4);
            json.Add("img_path5", imagePath5);
            json.Add("result_path", resultPath);
            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;
            }
        }

        /// <summary>
        /// EDS返回点信息
        /// </summary>
        /// <param name="imagePath">图片路径</param>
        /// <param name="num">第几张图片</param>
        /// <param name="imageType">图片类型</param>
        /// <param name="firm">厂商</param>
        /// <param name="listPoints">返回点数据</param>
        /// <param name="state">返回状态</param>
        public void EDS_Param_Points(String imagePath, int num, int imageType, String firm, out List<Point> listPoints, out int state)
        {
            try
            {
                this.webServer_Path = this._url[9];
            }
            catch
            {
                this.webServer_Path = "test9";
            }
            listPoints = new List<Point>();
            state = 0;
            Update_Url();
            JObject json = new JObject();
            json.Add("img_path", imagePath);
            json.Add("num", num);
            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(imagePath)!=null && jret.Property(imagePath).ToString()!="")
            {
                JObject jname = JObject.Parse(jret[imagePath].ToString());
                state = Convert.ToInt32(jname["state"].ToString());
                if(state==1)
                {
                    JArray xpoints = JArray.Parse(jname["Xlists"].ToString());
                    JArray ypoints = JArray.Parse(jname["Ylists"].ToString());
                    if(xpoints.Count==ypoints.Count)
                    {
                        List<int> lt_x = new List<int>();
                        for(int i=0;i<xpoints.Count;i++)
                        {
                            lt_x.Add(Convert.ToInt32(xpoints[i].ToString()));
                        }
                        for(int i=0;i<ypoints.Count;i++)
                        {
                            for (int j = 0; j < ypoints[i].Count(); j++)
                            {
                                Point pt = new Point();
                                pt.X = lt_x[i];
                                pt.Y = Convert.ToInt32(ypoints[i][j][0].ToString());
                                listPoints.Add(pt);
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// EDS返回线信息
        /// </summary>
        /// <param name="imagePath">图片路径</param>
        /// <param name="num">第几张图片</param>
        /// <param name="imageType">图片类型</param>
        /// <param name="firm">厂商</param>
        /// <param name="listPoints">返回点数据</param>
        /// <param name="state">返回状态</param>
        public void EDS_Param_Lines(String imagePath, int num, int imageType, String firm, out List<Point> list_pt, out List<int> height, out int state)
        {
            try
            {
                this.webServer_Path = this._url[10];
            }
            catch
            {
                this.webServer_Path = "test10";
            }
            list_pt = new List<Point>();
            height = new List<int>();
            state = 0;
            Update_Url();
            JObject json = new JObject();
            json.Add("img_path", imagePath);
            json.Add("num", num);
            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(imagePath) != null && jret.Property(imagePath).ToString() != "")
            {
                JObject jname = JObject.Parse(jret[imagePath].ToString());
                state = Convert.ToInt32(jname["state"].ToString());
                if (state == 1)
                {
                    JArray lines = JArray.Parse(jname["lines1"].ToString());
                    int x = Convert.ToInt32(lines[0][0].ToString());
                    int y1 = Convert.ToInt32(lines[0][1].ToString());
                    int y2 = Convert.ToInt32(lines[1][1].ToString());
                    Point pt = new Point();
                    pt.X = x;
                    pt.Y = y1;
                    list_pt.Add(pt);
                    height.Add(Math.Abs(y2 - y1));
                    
                    lines = JArray.Parse(jname["lines2"].ToString());
                    x = Convert.ToInt32(lines[0][0].ToString());
                    y1 = Convert.ToInt32(lines[0][1].ToString());
                    y2 = Convert.ToInt32(lines[1][1].ToString());
                    pt = new Point();
                    pt.X = x;
                    pt.Y = y1;
                    list_pt.Add(pt);
                    height.Add(Math.Abs(y2 - y1));

                    lines = JArray.Parse(jname["lines3"].ToString());
                    x = Convert.ToInt32(lines[0][0].ToString());
                    y1 = Convert.ToInt32(lines[0][1].ToString());
                    y2 = Convert.ToInt32(lines[1][1].ToString());
                    pt = new Point();
                    pt.X = x;
                    pt.Y = y1;
                    list_pt.Add(pt);
                    height.Add(Math.Abs(y2 - y1));
                }
            }
        }

        /// <summary>
        /// EDS返回点信息
        /// </summary>
        /// <param name="imagePath">图片路径</param>
        /// <param name="num">第几张图片</param>
        /// <param name="imageType">图片类型</param>
        /// <param name="firm">厂商</param>
        /// <param name="listPoints">返回点数据</param>
        /// <param name="state">返回状态</param>
        public void EDS_Param_Areas(String imagePath, int num, int imageType, String firm, out Point pt_left_top, out int width, out int height, out int state)
        {
            try
            {
                this.webServer_Path = this._url[11];
            }
            catch
            {
                this.webServer_Path = "test11";
            }
            pt_left_top = new Point();
            width = 0;
            height = 0;
            state = 0;
            Update_Url();
            JObject json = new JObject();
            json.Add("img_path", imagePath);
            json.Add("num", num);
            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(imagePath) != null && jret.Property(imagePath).ToString() != "")
            {
                JObject jname = JObject.Parse(jret[imagePath].ToString());
                state = Convert.ToInt32(jname["state"].ToString());
                if (state == 1)
                {
                    width = Convert.ToInt32(jname["length"].ToString());
                    JArray lines = JArray.Parse(jname["areas"].ToString());
                    pt_left_top.X = Convert.ToInt32(lines[0][0].ToString());
                    int y1 = Convert.ToInt32(lines[0][1].ToString());
                    pt_left_top.Y = y1;
                    int y2 = Convert.ToInt32(lines[1][1].ToString());
                    height = Math.Abs(y2 - y1);
                }
            }
        }

        /// <summary>
        /// 计算测量区域坐标
        /// </summary>
        /// <param name="imagePath">图片路径</param>
        /// <param name="imageType">样品类型</param>
        /// <param name="firm">厂商</param>
        /// <param name="center">观测点位置列表</param>
        /// <param name="mag1">观测点放大倍数列表</param>
        /// <param name="center0">对焦点位置</param>
        /// <param name="state">返回结果状态</param>
        /// <returns>json字符串</returns>
        public void Img_Measure_Region_Position(String imagePath, int imageType, String firm, out List<Point> center, out List<float> mag, out Point center0, 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("img_type", imageType);
            json.Add("firm", firm);
            center = new List<Point>();
            mag = new List<float>();
            String res = RequestString(JsonConvert.SerializeObject(json));
            JObject jret = (JObject)JsonConvert.DeserializeObject(res);
            if (jret != null && jret.Property(imagePath) != null && jret.Property(imagePath).ToString() != "")
            {
                JObject jname = JObject.Parse(jret[imagePath].ToString());
                state = Convert.ToInt32(jname["state"].ToString());
                if (state == 1)
                {
                    JArray jcenter = JArray.Parse(jname["center"].ToString());
                    String ptx = "";
                    String pty = "";
                    for (int i = 0; i < jcenter.Count; i++)
                    {
                        ptx = jcenter[i][0].ToString();
                        pty = jcenter[i][1].ToString();
                        Point pt = new Point(Convert.ToInt32(ptx), Convert.ToInt32(pty));
                        center.Add(pt);
                    }
                    JArray jmag = JArray.Parse(jname["magnification"].ToString());
                    for (int i = 0; i < jmag.Count; i++)
                    {
                        mag.Add(Convert.ToSingle(jmag[i].ToString()));
                    }
                    JArray jcenter0 = JArray.Parse(jname["center0"].ToString());
                    ptx = jcenter0[0].ToString();
                    pty = jcenter0[1].ToString();
                    center0 = new Point(Convert.ToInt32(ptx), Convert.ToInt32(pty));
                }
                else
                {
                    center0 = new Point(0, 0);
                    state = 0;
                }
            }
            else
            {
                center0 = new Point(0, 0);
                state = 0;
            }
        }

        /// <summary>
        /// 测量尺寸
        /// </summary>
        /// <param name="imagePath1">图片1路径</param>
        /// <param name="imagePath2">图片2路径</param>
        /// <param name="size1">图片1的像素大小</param>
        /// <param name="size2">图片2的像素大小</param>
        /// <param name="resultPath">测量结果的生成目录</param>
        /// <param name="imageType">样品类型</param>
        /// <param name="firm">厂类</param>
        /// <param name="state">测量结果返回</param>
        /// <returns>json字符串</returns>
        public void Img_Measure_Height(List<String> imagePath, List<float> size, String resultPath, int imageType, String firm, out int state)
        {
            try
            {
                this.webServer_Path = this._url[8];
            }
            catch
            {
                this.webServer_Path = "test9";
            }
            Update_Url();
            JObject json = new JObject();
            JArray jpath = new JArray();
            JArray jsize = new JArray();
            for (int i = 0; i < imagePath.Count; i++)
            {
                jpath.Add(imagePath[i]);
                jsize.Add(size[i]);
            }
            json.Add("img_path", jpath);
            json.Add("result_path", resultPath);
            json.Add("size", jsize);
            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("state") != null && jret.Property("state").ToString() != "")
            {
                state = Convert.ToInt32(jret["state"].ToString());
            }
            else
            {
                state = 0;
            }
        }

    }
}
