Exemplo n.º 1
0
 /// <summary>
 /// 获取识别后的结果
 /// </summary>
 /// <param name="img"></param>
 /// <returns></returns>
 public static string GetCode0(Image img)
 {
     ImageHandler ih = new ImageHandler();
     Bitmap bmp = new Bitmap(img);
     int v = ImageHelper.ComputeThresholdValue(bmp);
     //二值化
     bmp = ImageHelper.PBinary(bmp, v);
     bmp = ih.TrimBmp(bmp);
     List<Bitmap> bmpList = ih.CutImage(bmp, 8);
     string r = string.Empty;
     if (bmpList != null)
     {
         for (int j = 0; j < bmpList.Count; j++)
         {
             Bitmap _bmp = bmpList[j];
             CodeEntity ce = Realize(_bmp, StaticValues.CodeList0);
             if (ce != null)
             {
                 string charStr = ce.CharStr;
                 r = r + charStr;
             }
         }
     }
     return r;
 }
Exemplo n.º 2
0
        /// <summary>
        /// 获取图片的边界
        /// </summary>
        /// <param name="img"></param>
        /// <returns></returns>
        public static List<Boundary> GetImgBoundaryList(Image img,ref Bitmap _bmp)
        {
            List<Boundary> list = new List<Boundary>();
            Bitmap bmp = new Bitmap(img);
            int startY = 0;
            int endY = 0;
            int startX = 0;
            int endX = 0;
            try
            {
                int cnt = 1;
                for (int i = 0; i < 10; i++)
                {
                    if (cnt < 8)
                    {
                        startY = GetStartBoundaryY(img, startY);
                        if (startY >= 0)
                        {
                            endY = GetEndBoundaryY(img, startY + 1);
                            //标记
                            startX = GetStartBoundaryX(img, startY, endY);
                            int _startX = startX;
                            bool flag = false;
                            while (!flag && cnt < 4 && _startX < img.Width)
                            {
                                endX = GetEndBoundaryX(img, _startX, startY, endY);
                                flag = endX - _startX > 4;
                                _startX = _startX + 1;
                            }
                            if (endX > 0 && endX - startX > 4)
                            {
                                list.Add(new Boundary { EndY = endY, EndX = endX, StartY = startY, StartX = startX });
                                cnt++;
                            }
                        }
                        startY = endY;
                    }
                    else
                        break;
                }
                if (list.Count == 0)
                {
                    ImageHandler ih = new ImageHandler();
                    _bmp = ih.TrimBmp(img);
                    list.Add(new Boundary() { StartX = 0, EndX = _bmp.Height - 1, EndY = _bmp.Width - 1, StartY = 0 });
                }
                else
                {
                    _bmp = new Bitmap(img);
                }
                if (list.Count == 1)
                {
                    //平均切割
                    Boundary bd = list[0];
                    int _startY = bd.StartY;
                    int _endY = _startY;
                    int _startX = bd.StartX;
                    int _endX = bd.EndX;
                    int avr = (list[0].EndY - list[0].StartY) / 4;
                    list.Clear();
                    for (int i = 0; i < 4; i++)
                    {
                        _endY = _startY + avr;
                        list.Add(new Boundary { EndY = _endY, EndX = _endX, StartY = _startY, StartX = _startX });
                        _startY = _endY;

                    }
                }
                if (list.Count == 3)
                {
                    Boundary max = list[0];
                    int index = 0;
                    for (int i = 1; i < list.Count; i++)
                    {
                        Boundary _max = list[i];
                        if (max.EndY - max.StartY < _max.EndY - _max.StartY)
                        {
                            index = i;
                            max = _max;
                        }
                    }
                    //平分
                    int _startY = max.StartY;
                    int _endY = _startY;
                    int _startX = max.StartX;
                    int _endX = max.EndX;
                    int avr = (max.EndY - max.StartY) / 2;
                    list.Remove(max);
                    for (int i = 0; i < 2; i++)
                    {
                        _endY = _startY + avr;
                        list.Insert(index, new Boundary { EndY = _endY, EndX = _endX, StartY = _startY, StartX = _startX });
                        index = index + 1;
                        _startY = _endY + 1;

                    }

                }
                if (list.Count == 2)
                {
                    if (Math.Abs((list[1].EndY - list[1].StartY) - (list[0].EndY - list[0].StartY)) > 15)
                    {
                        Boundary max = (list[1].EndY - list[1].StartY) > (list[0].EndY - list[0].StartY) ? list[1] : list[0];
                        //平分
                        int index = (list[1].EndY - list[1].StartY) > (list[0].EndY - list[0].StartY) ? 1 : 0;
                        int _startY = max.StartY;
                        int _endY = _startY;
                        int _startX = max.StartX;
                        int _endX = max.EndX;
                        int avr = (max.EndY - max.StartY) / 3;
                        list.Remove(max);
                        for (int i = 0; i < 3; i++)
                        {
                            _endY = _startY + avr;
                            list.Insert(index, new Boundary { EndY = _endY, EndX = _endX, StartY = _startY, StartX = _startX });
                            index = index + 1;
                            _startY = _endY + 1;

                        }
                    }
                    else
                    {
                        List<Boundary> _temp = new List<Boundary>();
                        for (int i = 0; i < 2; i++)
                        {
                            Boundary max = list[i];
                            //平分
                            int _startY = max.StartY;
                            int _endY = _startY;
                            int _startX = max.StartX;
                            int _endX = max.EndX;
                            int avr = (max.EndY - max.StartY) / 2;
                            for (int j = 0; j < 2; j++)
                            {
                                _endY = _startY + avr;
                                _temp.Add(new Boundary { EndY = _endY, EndX = _endX, StartY = _startY, StartX = _startX });
                                _startY = _endY + 1;

                            }
                        }
                        list = _temp;
                    }
                }
                List<Boundary> tempBdList = list;
                for (int i = 0; i < tempBdList.Count; i++)
                {
                    if (!((tempBdList[i].EndY - tempBdList[i].StartY > 4) && (tempBdList[i].EndX - tempBdList[i].StartX > 4)))
                    {
                        list.Remove(tempBdList[i]);
                    }
                }
            }
            catch
            {
            }
            return list;
        }