Example #1
0
        /// <summary>
        /// 添加新字符(由于会做同字符数据比较,所以会有一些慢。)
        /// </summary>
        /// <param name="charInfo">字符数据</param>
        /// <returns>返回false表示已经存在或charInfo参数为null。</returns>
        public bool AddChar(CharInfo charInfo)
        {
            if (charInfo == null)
            {
                return(false);
            }
            CharInfo original = _chars.Find(p => {
                if (p == charInfo)//同一个
                {
                    return(true);
                }
                //不同字符,或不同点数,就肯定不一样。
                if (p.Value != charInfo.Value || p.Points.Count != charInfo.Points.Count)
                {
                    return(false);
                }
                for (int i = 0; i < p.Points.Count; i++)
                {
                    CharPoint p2 = p.Points[i];
                    CharPoint p3 = charInfo.Points[i];
                    //坐标不同,颜色不同都可以认为是不同
                    if (p2.X != p3.X || p2.Y != p3.Y || p2.R != p3.R)
                    {
                        return(false);
                    }
                }
                //既然数据一样,就是同一个
                return(true);
            });

            if (original == null)
            {
                _chars.Add(charInfo);
                return(true);
            }
            return(false);
        }
Example #2
0
        static void LoadValues(Symbol.IO.Packing.TreePackage data, CharLibrary result)
        {
            result._data             = data;
            result._name             = data["name"] as string;
            result._version          = data["version"] as string;
            result._description      = data["description"] as string;
            result._width            = TypeExtensions.Convert <int>(data["width"], 0);
            result._height           = TypeExtensions.Convert <int>(data["height"], 0);
            result._charWidth        = TypeExtensions.Convert <int>(data["charWidth"], 0);
            result._charHeight       = TypeExtensions.Convert <int>(data["charHeight"], 0);
            result._charMaxWidth     = TypeExtensions.Convert <int>(data["charMaxWidth"], 0);
            result._minPoints        = TypeExtensions.Convert <int>(data["minPoints"], 0);
            result._needCenterMiddle = TypeExtensions.Convert <bool>(data["needCenterMiddle"], false);
            result._emptyColorR      = TypeExtensions.Convert <byte>(data["emptyColorR"], 0);
            result._zool             = TypeExtensions.Convert <float>(data["zool"], 1.0F);
            if (result._zool <= 0F)
            {
                result._zool = 1.0F;
            }
            result._pageUrl   = data["pageUrl"] as string;
            result._codeUrl   = data["codeUrl"] as string;
            result._extension = data["extension"] as string;

            int preCount = TypeExtensions.Convert <int>(data["preHandlers"], 0);

            result._preHandlers.Clear();
            if (preCount > 0)
            {
                for (int i = 0; i < preCount; i++)
                {
                    string keyBefore = "pre_" + i + "_";
                    string key       = LinqHelper.FirstOrDefault(data.Keys, p => p.StartsWith(keyBefore));
                    if (string.IsNullOrEmpty(key))
                    {
                        continue;
                    }
                    IPreHandler handler = PreHandlerHelper.CreateInstance(key.Substring(keyBefore.Length));
                    if (handler == null)
                    {
                        continue;
                    }
                    handler.Load((Symbol.IO.Packing.TreePackage)data[key]);
                    result._preHandlers.Add(handler);
                }
            }

            //int takeCount = TypeExtensions.Convert<int>(data["takeHandlers"], 0);
            //result._takeHandlers.Clear();
            //if (takeCount > 0) {
            //    for (int i = 0; i < takeCount; i++) {
            //        string keyBefore = "take_" + i + "_";
            //        string key = LinqHelper.FirstOrDefault(data.Keys, p => p.StartsWith(keyBefore));
            //        if (string.IsNullOrEmpty(key))
            //            continue;
            //        ITakeHandler handler = TakeHandlerHelper.CreateInstance(key.Substring(keyBefore.Length));
            //        if (handler == null)
            //            continue;
            //        handler.Load((Symbol.IO.Packing.TreePackage)data[key]);
            //        result._takeHandlers.Add(handler);
            //    }
            //}
            //if (result._takeHandlers.Count == 0) {
            //    result._takeHandlers.Add(TakeHandlerHelper.CreateInstance("YDotMatrix"));
            //}

            int charCount = TypeExtensions.Convert <int>(data["chars"], 0);

            result._chars.Clear();
            if (charCount > 0)
            {
                for (int i = 0; i < charCount; i++)
                {
                    string keyBefore = "char_" + i + "_";
                    string key       = LinqHelper.FirstOrDefault(data.Keys, p => p.StartsWith(keyBefore));
                    if (string.IsNullOrEmpty(key) || key.Length == keyBefore.Length)
                    {
                        continue;
                    }
                    Symbol.IO.Packing.TreePackage charData = (Symbol.IO.Packing.TreePackage)data[key];
                    if (charData == null)
                    {
                        continue;
                    }

                    CharInfo charInfo = new CharInfo()
                    {
                        Value = key.Substring(keyBefore.Length)[0]
                    };
                    int pointCount = TypeExtensions.Convert <int>(charData["count"], 0);
                    if (pointCount > 0)
                    {
                        for (int j = 0; j < pointCount; j++)
                        {
                            string    keyBefore2 = j + "_";
                            CharPoint charPoint  = new CharPoint()
                            {
                                OriginalX = TypeExtensions.Convert <int>(charData[keyBefore2 + "OriginalX"], 0),
                                OriginalY = TypeExtensions.Convert <int>(charData[keyBefore2 + "OriginalY"], 0),
                                X         = TypeExtensions.Convert <int>(charData[keyBefore2 + "X"], 0),
                                Y         = TypeExtensions.Convert <int>(charData[keyBefore2 + "Y"], 0),
                                R         = TypeExtensions.Convert <byte>(charData[keyBefore2 + "R"], 0),
                                G         = TypeExtensions.Convert <byte>(charData[keyBefore2 + "G"], 0),
                                B         = TypeExtensions.Convert <byte>(charData[keyBefore2 + "B"], 0),
                            };
                            charInfo.Points.Add(charPoint);
                        }
                    }
                    result._chars.Add(charInfo);
                }
            }
            string takeHandlerKeyBefore = "takeHandler_";
            string takeHandlerKey       = LinqHelper.FirstOrDefault(data.Keys, p => p.StartsWith(takeHandlerKeyBefore));

            if (!string.IsNullOrEmpty(takeHandlerKey) && takeHandlerKey.Length != takeHandlerKeyBefore.Length)
            {
                result._takeHandler = TakeHandlerHelper.CreateInstance(takeHandlerKey.Substring(takeHandlerKeyBefore.Length));
                if (result._takeHandler != null)
                {
                    Symbol.IO.Packing.TreePackage takeHandlerData = (Symbol.IO.Packing.TreePackage)data[takeHandlerKey];
                    result._takeHandler.Load(takeHandlerData);
                }
            }
            if (result._takeHandler == null)
            {
                result._takeHandler = TakeHandlerHelper.CreateInstance("YDotMatrix");
            }

            string charRecognizerKeyBefore = "charRecognizer_";
            string charRecognizerKey       = LinqHelper.FirstOrDefault(data.Keys, p => p.StartsWith(charRecognizerKeyBefore));

            if (!string.IsNullOrEmpty(charRecognizerKey) && charRecognizerKey.Length != charRecognizerKeyBefore.Length)
            {
                result._charRecognizer = CharRecognizerHelper.CreateInstance(charRecognizerKey.Substring(charRecognizerKeyBefore.Length));
                if (result._charRecognizer != null)
                {
                    Symbol.IO.Packing.TreePackage charRecognizerData = (Symbol.IO.Packing.TreePackage)data[charRecognizerKey];
                    result._charRecognizer.Load(charRecognizerData);
                }
            }
            if (result._charRecognizer == null)
            {
                result._charRecognizer = CharRecognizerHelper.CreateInstance("Weight3x3");
            }
        }