AoiProject.cs 4.9 KB
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;

namespace AOI
{
    public class AoiProject
    {

        public AoiProject(Image theImage)
        {
            this.standardImage = theImage;
        }
        /// <summary>
        /// 标准的Image
        /// </summary>
        public Image standardImage { get; set; }

        /// <summary>
        /// 所有的AOI方法
        /// </summary>
        public Dictionary<string, AoiMethod> methodMap = new Dictionary<string, AoiMethod>();

        public List<ResultBean> CheckAll(Image scr, out Image resultImg)
        {
            Image image = (Image)scr.Clone();
            //如果设置了校准方法,先校准图片
            var markMethodMap = methodMap.Where(kv=>kv.Value is AoiMarkMethod);
            foreach (var item in markMethodMap)
            {
                ResultBean resultBean = item.Value.Check(standardImage, image);
                if (resultBean.result)
                {
                    //校准成功
                    image = resultBean.currentRoiImage;
                }
            }
            List<ResultBean> resultBeans = new List<ResultBean>();
            foreach(var item in methodMap)
            {
                AoiMethod method = item.Value;
                if(method is AoiMarkMethod)
                {
                    //校准图片的,忽略
                }
                else
                {
                    ResultBean resultBean = method.Check(standardImage, image);
                    resultBean.roiPath = method.RoiPath;
                    resultBean.labelKey = item.Key;
                    resultBeans.Add(resultBean);
                }
            }
            using (Graphics g = Graphics.FromImage(image))
            {
                foreach(ResultBean resultBean in resultBeans)
                {
                    Pen pen = new Pen(Color.Lime,5);
                    if (!resultBean.result)
                    {
                        pen = new Pen(Color.Red,5);
                    }
                    g.DrawPath(pen,resultBean.roiPath);
                }
            }
            resultImg = image;
            return resultBeans;
        }
        /// <summary>
        /// 保存项目
        /// </summary>
        /// <param name="filePath"></param>
        public void Save(string filePath)
        {
            Dictionary<string, string> projectMap = new Dictionary<string, string>();
            string base64ImgStr = Base64Util.ToBase64(this.standardImage);
            projectMap.Add("base64Img", base64ImgStr);
            var mapForJson = new Dictionary<string, string>();
            foreach(var item in this.methodMap)
            {
                JObject obj = JObject.FromObject(item.Value);
                obj.Add("FullTypeName",item.Value.GetType().FullName);
                var roiPathData = item.Value.GetRoiPathData();
                string roiPathDataStr = JsonUtil.SerializeObject(roiPathData);
                obj.Add("PathDataStr", roiPathDataStr);
                string jsonStr = JsonUtil.SerializeObject(obj);
                mapForJson.Add(item.Key, jsonStr);
            }
            string methodMapJson = JsonUtil.SerializeObject(mapForJson);
            projectMap.Add("methodMap", methodMapJson);
            JsonUtil.SerializeObjectToFile(projectMap,filePath,false);
        }
        /// <summary>
        /// 加载项目
        /// </summary>
        /// <param name="filePath"></param>
        public void Load(string filePath)
        {
            Dictionary<string, string> projectMap = JsonUtil.DeserializeJsonToObjectFromFile<Dictionary<string, string>>(filePath);
            string base64Img = projectMap["base64Img"];
            this.standardImage = Base64Util.ToImage(base64Img);
            string methodMapJson = projectMap["methodMap"];
            var jsonMap = JsonUtil.DeserializeJsonToObject<Dictionary<string, string>>(methodMapJson);
            foreach(var item in jsonMap)
            {
                JObject obj = JObject.Parse(item.Value);
                string fullTypeName = obj.Value<string>("FullTypeName");
                Type t = Type.GetType(fullTypeName);
                JsonSerializer serializer = new JsonSerializer();
                StringReader sr = new StringReader(item.Value);
                object o = serializer.Deserialize(new JsonTextReader(sr), t);
                AoiMethod aoiMethod = (AoiMethod)o;
                string PathDataStr = obj.Value<string>("PathDataStr");
                PathData pathData = JsonUtil.DeserializeJsonToObject<PathData>(PathDataStr);
                aoiMethod.RoiPath = new GraphicsPath(pathData.Points, pathData.Types);
                methodMap.Add(item.Key, aoiMethod);
            }
        }
        

       
    }
}