public void Read() { ////do ocr foreach (string file in Directory.GetFiles(iDir)) { if (file.EndsWith(".tif", true, null) || file.EndsWith(".tiff", true, null))// || file.EndsWith(".pdf", true, null)) { string fileName = file.Substring(file.LastIndexOf("\\")); fileName = fileName.Substring(0, fileName.LastIndexOf(".")); Bitmap image = new Bitmap(file); tessnet2.Tesseract ocr = new tessnet2.Tesseract(); ocr.Init(null, "ENG", false); List <tessnet2.Word> result = ocr.DoOCR(image, Rectangle.Empty); image = ocr.GetThresholdedImage(image, Rectangle.Empty); //// fileName += ".bmp"; //// image.Save(fileName); //// for (int i = 0; i < oDir.Length; i++) { if (oType[i] == "PDF") { fileName += ".pdf"; ////create pdf } else if (oType[i] == "DOC") { fileName += ".doc"; ////create doc } if (oEmail[i]) { try { SendMail(Properties.Settings.Default.SMTPServer, Properties.Settings.Default.SMTPPort, Properties.Settings.Default.SSL, Properties.Settings.Default.Username, Properties.Settings.Default.Password, Properties.Settings.Default.FromAddress, Properties.Settings.Default.FromName, Properties.Settings.Default.Subject, Properties.Settings.Default.Message, Properties.Settings.Default.HTML, new string[] { oDir[i] }, new string[] { fileName }); File.Delete(fileName); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.ToString(), "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); } } else { File.Move(fileName, oDir[i] + fileName); } } } } }
static void Main(string[] args) { // Code usage sample Ocr ocr = new Ocr(); using (Bitmap bmp = new Bitmap(@"D:\temp\ocr\b1.bmp")) { tessnet2.Tesseract tessocr = new tessnet2.Tesseract(); tessocr.Init(null, "eng", false); tessocr.GetThresholdedImage(bmp, Rectangle.Empty).Save("c:\\temp\\" + Guid.NewGuid().ToString() + ".bmp"); // Tessdata directory must be in the directory than this exe Console.WriteLine("Multithread version"); ocr.DoOCRMultiThred(bmp, "eng"); Console.WriteLine("Normal version"); ocr.DoOCRNormal(bmp, "eng"); } }
static void Main(string[] args) { // Code usage sample Ocr ocr = new Ocr(); using (Bitmap bmp = new Bitmap(@"C:\temp\test.bmp")) { tessnet2.Tesseract tessocr = new tessnet2.Tesseract(); tessocr.Init(@"C:\Users\mkmak\Documents\Visual Studio 2013\Projects\ConsoleApplication1\tessdata", "eng", false); tessocr.GetThresholdedImage(bmp, Rectangle.Empty).Save("c:\\temp\\" + Guid.NewGuid().ToString() + ".bmp"); // Tessdata directory must be in the directory than this exe //Console.WriteLine("Multithread version"); //ocr.DoOCRMultiThred(bmp, "eng"); Console.WriteLine("Normal version"); ocr.DoOCRNormal(bmp, "eng"); } Thread.Sleep(100000); }
private void solveCaptcha() { Ocr ocr = new Ocr(); FirefoxWebElement image = (FirefoxWebElement)fox.FindElementById("imagemCaptcha"); Byte[] ba = ((ITakesScreenshot)fox).GetScreenshot().AsByteArray; var ss = new Bitmap(new MemoryStream(ba)); var crop = new Rectangle(image.Location.X, image.Location.Y, image.Size.Width, image.Size.Height); //create a new image by cropping the original screenshot Bitmap image2 = ss.Clone(crop, ss.PixelFormat); using (image2) { tessnet2.Tesseract tessocr = new tessnet2.Tesseract(); tessocr.Init(null, "eng", false); tessocr.GetThresholdedImage(image2, Rectangle.Empty).Save("c:\\temp\\" + Guid.NewGuid().ToString() + ".jpg"); // Tessdata directory must be in the directory than this exe Console.WriteLine("Multithread version"); ocr.DoOCRMultiThred(image2, "eng"); Console.WriteLine("Normal version"); ocr.DoOCRNormal(image2, "eng"); } }
private void buttonOpen_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); if (ofd.ShowDialog() == DialogResult.OK) { Bitmap bmp = new Bitmap(ofd.FileName); pictureBoxFilled.Image = bmp; pictureBoxInput.Image = (Bitmap)bmp.Clone(); List <Color> lst = new List <Color>(); Color c; for (int x = 2; x < bmp.Width; x++) { c = bmp.GetPixel(x, 2); if (!lst.Contains(c)) { lst.Add(c); } c = bmp.GetPixel(x, bmp.Height - 3); if (!lst.Contains(c)) { lst.Add(c); } } for (int y = 0; y < bmp.Height; y++) { c = bmp.GetPixel(2, y); if (!lst.Contains(c)) { lst.Add(c); } c = bmp.GetPixel(31, y); if (!lst.Contains(c)) { lst.Add(c); } c = bmp.GetPixel(90, y); if (!lst.Contains(c)) { lst.Add(c); } c = bmp.GetPixel(bmp.Width - 3, y); if (!lst.Contains(c)) { lst.Add(c); } } for (int x = 0; x < bmp.Width; x++) { for (int y = 0; y < bmp.Height; y++) { c = bmp.GetPixel(x, y); if (lst.Contains(c)) { if (c != Color.White) { FloodFill(bmp, x, y, Color.White); } } } } Ocr ocr = new Ocr(); //Input tessnet2.Tesseract tessocr = new tessnet2.Tesseract(); tessocr.SetVariable("tessedit_char_whitelist", "0123456789*+-="); tessocr.Init(null, "eng", false); pictureBoxInputLib.Image = tessocr.GetThresholdedImage((Bitmap)pictureBoxInput.Image, Rectangle.Empty); List <tessnet2.Word> text = ocr.DoOCRNormal((Bitmap)pictureBoxInput.Image, "eng"); textBoxInput.Text = text[0].Text; //Filled tessocr = new tessnet2.Tesseract(); tessocr.SetVariable("tessedit_char_whitelist", "0123456789*+-="); tessocr.Init(null, "eng", false); pictureBoxFilledLib.Image = tessocr.GetThresholdedImage((Bitmap)pictureBoxFilled.Image, Rectangle.Empty); text = ocr.DoOCRNormal((Bitmap)pictureBoxFilled.Image, "eng"); textBoxFilled.Text = text[0].Text; } }
public int readScore(Awesomium.Windows.Forms.WebControl clientBrowser, int gameID) { int score = 0; int[] size = new int[2]; int fontColor = 255; switch (gameID) { case 1: //Fireworks break; case 2: //Jungle Cruise size[0] = 94; size[1] = 36; fontColor = 239; break; default: //Something went wrong return(0); } //Take screenshot of region, store to temporary bitmap Bitmap scoreShot = new Bitmap(size[0], size[1]); Graphics gfx = Graphics.FromImage((Image)scoreShot); Point ctrlp = clientBrowser.PointToScreen(Point.Empty); try { gfx.CopyFromScreen(ctrlp.X, ctrlp.Y, 0, 0, new Size(size[0], size[1])); } catch { return(-1); } //Replace everything except characters with black for (int x = 0; x < scoreShot.Width; x++) { for (int y = 0; y < scoreShot.Height; y++) { Color gotColor = scoreShot.GetPixel(x, y); if (gotColor != Color.FromArgb(fontColor, fontColor, fontColor)) { scoreShot.SetPixel(x, y, Color.Black); } } } //Use tessnet2 to extract score via OCR Ocr ocr = new Ocr(); List <tessnet2.Word> res = new List <tessnet2.Word>(); using (Bitmap bmp = scoreShot) { tessnet2.Tesseract tessocr = new tessnet2.Tesseract(); tessocr.Init(null, "eng", false); tessocr.GetThresholdedImage(bmp, Rectangle.Empty).Save("c:\\x\\" + Guid.NewGuid().ToString() + ".bmp"); // Tessdata directory must be in the same directory as this exe res = ocr.DoOCRNormal(bmp, "eng"); } foreach (tessnet2.Word word in res) { bool isNumeric = int.TryParse(word.Text, out score); if (!isNumeric) { score = 0; } } //Return the score value return(score); }