private string GetOCR(Bitmap big, Imaging.ImageRange range, bool flag = true) { Point p = new Point(); int width = 0; int height = 0; p.X = range.loc.X; p.Y = range.loc.Y; width = range.width; height = range.height; big = Imaging.CropImage(big, p, width, height); big = Imaging.ConvertFormat(big, System.Drawing.Imaging.PixelFormat.Format32bppPArgb); if (!flag) { big = Imaging.MakeGrayscale3(big); } //; else { big = grayscale(big); } //tessnet2.Tesseract api = new tessnet2.Tesseract(); //api.Init() OcrApi.PathToEngine = Environment.CurrentDirectory + @"\tesseract.dll"; OcrApi api = OcrApi.Create(); Languages[] lang = { Languages.English }; api.Init(lang, null, OcrEngineMode.OEM_TESSERACT_CUBE_COMBINED); api.SetVariable("tessedit_char_whitelist", "0123456789"); string plainText = api.GetTextFromImage(big); ////Console.WriteLine(plainText); ////this.Invoke(new MethodInvoker(delegate() { txtLog.AppendText(plainText + Environment.NewLine); })); api.Dispose(); api = null; System.GC.Collect(0, GCCollectionMode.Forced); System.GC.WaitForPendingFinalizers(); return(plainText); //return ""; }
private string GetOCR(Bitmap big, Imaging.ImageRange range, bool flag = true) { Point p = new Point(); int width = 0; int height = 0; p.X = range.loc.X; p.Y = range.loc.Y; width = range.width; height = range.height; big = Imaging.CropImage(big, p, width, height); big = Imaging.ConvertFormat(big, System.Drawing.Imaging.PixelFormat.Format32bppArgb); //if (!flag) //big = Imaging.MakeGrayscale3(big); ////; //else //big = grayscale(big); //big.Save(Environment.CurrentDirectory + "\\test.png"); ////tessnet2.Tesseract api = new tessnet2.Tesseract(); ////api.Init() //OcrApi.PathToEngine = Environment.CurrentDirectory + @"\tesseract.dll"; //OcrApi api = OcrApi.Create(); //Languages[] lang = { Languages.English }; //api.Init(lang, null, OcrEngineMode.OEM_TESSERACT_CUBE_COMBINED); //api.SetVariable("tessedit_char_whitelist", "0123456789"); //string plainText = api.GetTextFromImage(big); //////Console.WriteLine(plainText); //////this.Invoke(new MethodInvoker(delegate() { txtLog.AppendText(plainText + Environment.NewLine); })); //api.Dispose(); //api = null; //System.GC.Collect(0, GCCollectionMode.Forced); //System.GC.WaitForPendingFinalizers(); //return plainText; var tmpFile = Path.GetTempFileName(); string text = ""; var bmp = new Bitmap(Math.Max(big.Width, 1024), Math.Max(big.Height, 768)); var gfxResize = Graphics.FromImage(bmp); gfxResize.DrawImage(big, new Rectangle(0, 0, big.Width, big.Height)); bmp.Save(tmpFile + ".png", ImageFormat.Png); //EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 100L); // 0(최저화질 고압축) ~ 100L(최고화질 저압축) //myEncoderParameters.Param[0] = myEncoderParameter; //bmp.Save(tmpFile + ".jpg", jpgEncoder, myEncoderParameters); var doc = new MODI.Document(); doc.Create(tmpFile + ".png"); //Regex reg = new Regex(@"[^\d]+"); try { doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true); var img = (MODI.Image)doc.Images[0]; var layout = img.Layout; text = layout.Text; File.Delete(tmpFile); File.Delete(tmpFile + ".png"); } catch (Exception e) { try { doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, false, false); var img = (MODI.Image)doc.Images[0]; var layout = img.Layout; text = layout.Text; } catch (Exception) { } finally { File.Delete(tmpFile); File.Delete(tmpFile + ".png"); } } finally { File.Delete(tmpFile); File.Delete(tmpFile + ".png"); } Int32 tryCheck = 0; text = Regex.Replace(text.Replace(" ", "").Replace(",", "").Replace(".", "").Trim(), @"[^\d]", ""); Int32.TryParse(text, out tryCheck); if (tryCheck == 0 || tryCheck < 1000) { try { EncoderParameters myEncoderParameters = new EncoderParameters(1); System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality; ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg); tmpFile = Path.GetTempFileName(); text = ""; bmp = new Bitmap(Math.Max(big.Width, 1024), Math.Max(big.Height, 768)); gfxResize = Graphics.FromImage(bmp); gfxResize.DrawImage(big, new Rectangle(0, 0, big.Width, big.Height)); EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 100L); // 0(최저화질 고압축) ~ 100L(최고화질 저압축) myEncoderParameters.Param[0] = myEncoderParameter; bmp.Save(tmpFile + ".jpg", jpgEncoder, myEncoderParameters); doc = new MODI.Document(); doc.Create(tmpFile + ".jpg"); doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, true, true); var img = (MODI.Image)doc.Images[0]; var layout = img.Layout; text = layout.Text; } catch (Exception) { doc.OCR(MODI.MiLANGUAGES.miLANG_ENGLISH, false, false); var img = (MODI.Image)doc.Images[0]; var layout = img.Layout; text = layout.Text; } finally { File.Delete(tmpFile); File.Delete(tmpFile + ".jpg"); } } text = Regex.Replace(text.Replace(" ", "").Replace(",", "").Replace(".", "").Trim(), @"[^\d]", ""); doc.Close(); doc = null; bmp.Dispose(); bmp = null; System.GC.Collect(0, GCCollectionMode.Forced); System.GC.WaitForPendingFinalizers(); return(text); //return ""; }