Пример #1
0
        public string OCR(System.Drawing.Bitmap image)
        {
            string text  = "";
            string text2 = "";

            try
            {
                string    tessdata  = @"tessdata";
                Tesseract tesseract = new Tesseract();
                tesseract.Clear();
                tesseract.SetVariable("tessedit_char_whitelist", "123456789abcdefghijklmnpqrstuvwxyz");
                tesseract.SetVariable("tessedit_adaption_debug", true);
                tesseract.SetVariable("tessedit_ambigs_training ", true);
                tesseract.Init(tessdata, "eng", false);

                Bitmap originalImage = image;
                double needdedHeigth = Convert.ToDouble(image.Height + 10);
                double faktor        = needdedHeigth / (double)(originalImage.Height);
                int    newWidth      = Convert.ToInt32(faktor * (double)originalImage.Width);
                Bitmap ORCImage      = new Bitmap(originalImage, newWidth, Convert.ToInt32(needdedHeigth));

                System.Collections.Generic.List <Word> list  = tesseract.DoOCR(ORCImage, System.Drawing.Rectangle.Empty);
                System.Collections.Generic.List <Word> list2 = tesseract.DoOCR(image, System.Drawing.Rectangle.Empty);
                image.Dispose();

                foreach (Word current in list)
                {
                    text2 += current.Text;
                }

                foreach (Word current3 in list2)
                {
                    text += current3.Text;
                }

                if (text.Length != 4)
                {
                    text = text2;
                }
            }
            catch (System.Exception value)
            {
                System.Console.WriteLine(value);
            }
            return(text.Replace(" ", ""));
        }
Пример #2
0
 public void Dispose()
 {
     _ocr.Clear();
     _ocr.Dispose();
     _ocr = null;
 }