static void Main(string[] args) { var imagePath = args.FirstOrDefault(); if (String.IsNullOrWhiteSpace(imagePath) || !File.Exists(args[0])) { Console.WriteLine("Usage: {0} [Path to image file]", Path.GetFileName(Assembly.GetAssembly(typeof(Program)).CodeBase)); return; } Console.WriteLine("Running OCR for file " + imagePath); try { using (var ocrEngine = new OnenoteOcrEngine()) using (var image = Image.FromFile(imagePath)) { var text = ocrEngine.Recognize(image); if (text == null) Console.WriteLine("nothing recognized"); else Console.WriteLine("Recognized: " + text); } } catch (OcrException ex) { Console.WriteLine("OcrException:\n" + ex); } catch (Exception ex) { Console.WriteLine("General Exception:\n" + ex); } Console.ReadLine(); }
public static string ExtractTextFromImage(string imagePath) { using (var ocrEngine = new OnenoteOcrEngine()) using (var image = Image.FromFile(imagePath)) { var text = ocrEngine.Recognize(image); if (text == null) return ""; else return text; } }
static void Main(string[] args) { var imagePath = args.FirstOrDefault(); if (String.IsNullOrWhiteSpace(imagePath) || !File.Exists(args[0])) { Console.WriteLine("Passo: {0} [Caminho do Arquivo de Imagem]", Path.GetFileName(Assembly.GetAssembly(typeof(Program)).CodeBase)); return; } Console.WriteLine("Execução do OCR para Arquivo " + imagePath); try { using (var ocrEngine = new OnenoteOcrEngine()) using (var image = Image.FromFile(imagePath)) { var text = ocrEngine.Recognize(image); if (text == null) { Console.WriteLine("Não reconhecido"); } else { Console.WriteLine("Reconhecido: " + text); } } } catch (OcrException ex) { Console.WriteLine("Excessão de OCR:\n" + ex); } catch (Exception ex) { Console.WriteLine("Excessão Geral:\n" + ex); } Console.ReadLine(); }
static void Main(string[] args) { var imagePath = args.FirstOrDefault(); if (String.IsNullOrWhiteSpace(imagePath) || !File.Exists(args[0])) { Console.WriteLine("Usage: {0} [Path to image file]", Path.GetFileName(Assembly.GetAssembly(typeof(Program)).CodeBase)); return; } Console.WriteLine("Running OCR for file " + imagePath); try { using (var ocrEngine = new OnenoteOcrEngine()) using (var image = Image.FromFile(imagePath)) { var text = ocrEngine.Recognize(image); if (text == null) { Console.WriteLine("nothing recognized"); } else { Console.WriteLine("Recognized: " + text); } } } catch (OcrException ex) { Console.WriteLine("OcrException:\n" + ex); } catch (Exception ex) { Console.WriteLine("General Exception:\n" + ex); } Console.ReadLine(); }
//Method that runs on a single email static void ReadSingleMail(dynamic item) { timer = Stopwatch.StartNew(); //Reinitialize data array back to empty for (int index = 0; index <= 8; index++) { data[index] = ""; } skipTextReader = false; // Reinitialize variable back to false //Set up OCR engine to prepare for OCR OnenoteOcrEngine ocr = new OnenoteOcrEngine(); string bodyText; // Email body string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); //Path to My Documents StringBuilder sb = new StringBuilder(); if (ReadImageFromEmail(item) == 1) // If email has an attachment, read the attachment- Assuming this is E-business card { Image imageToParse = Image.FromFile(@"C:\TestFileSave\" + item.Attachments[1].FileName); //Get text from OCR, and normalize bodyText = ocr.Recognize(imageToParse); bodyText = bodyText.Replace(" ", ";"); bodyText = bodyText.Replace(";;", ";"); bodyText = bodyText.Replace(",", ""); bodyText = bodyText.Replace("|", ""); skipTextReader = true; //Skip parsing email body if email has image signature data = parseOCRSignature(bodyText); // Parse body into fields } else //If no attachment, extract email body { if (item != null) { bodyText = item.Body; } else { return; // If no email body, exit function. } sb.Append(bodyText); } if (skipTextReader == false) //If no image signature, parse email body { File.AppendAllText(mydocpath + @"\MailFile.txt", sb.ToString()); sb.Remove(0, bodyText.Length); string signature = extractSignature().Replace(";;", ";"); //Extract signature block //IF EMBEDDED IMAGE SIGNATURE FOUND if (((signature.ToLower()).Contains(".png") || (signature.ToLower()).Contains(".gif") || (signature.ToLower()).Contains(".jpg")) && signature.Length < 20) { //MessageBox.Show(signature); Image imageToParse = Image.FromFile(@"C:\TestFileSave\" + signature); bodyText = ocr.Recognize(imageToParse); //MessageBox.Show(bodyText); if (bodyText != null) { bodyText = bodyText.Replace(" ", ";"); bodyText = bodyText.Replace(";;", ";"); bodyText = bodyText.Replace(",", ""); bodyText = bodyText.Replace("|", ";"); skipTextReader = true; //Bool variable if email has image signature data = parseOCRSignature(bodyText); } } else { data = parseImageSignature(signature); //Parse signature block } } //Send data to database string strEmailSenderEmailId = Convert.ToString(item.SenderEmailAddress); SqlConnection connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Prashanth\Desktop\CAPSTONE JPMORGAN\OutlookAddIn1\OutlookContacts.mdf;Integrated Security=True;User Instance=True"); SqlCommand command = new SqlCommand(); SqlDataReader dataReader; command.Connection = connection; connection.Open(); command.CommandText = "select * from New_Contacts where Email_ID = '" + strEmailSenderEmailId + "'"; dataReader = command.ExecuteReader(); //Check if contact exists already if (dataReader.HasRows == false) { command.CommandText = "insert into New_Contacts VALUES('" + data[0] + "' , '" + data[1] + "' , '" + data[2] + "' , '" + data[3] + "' , '" + data[4] + "' , '" + data[5] + "' , '" + data[6] + "' , '" + data[7] + "' , '" + data[8] + "' , '" + strEmailSenderEmailId + "' , '" + data[9] + "')"; dataReader.Close(); command.ExecuteNonQuery(); } //If it does, update and overwrite selected data. This method prevents overwriting past data with empty fields else { UpdateSelectFields(data[2], data[4], data[7], data[5], data[6], data[8], data[9], strEmailSenderEmailId); } // Delete the file, stop the timer System.IO.File.Delete(mydocpath + @"\MailFile.txt"); timer.Stop(); time_elapsed = time_elapsed + timer.Elapsed.TotalSeconds; ocr.Dispose(); MessageBox.Show("This email took " + (time_elapsed).ToString() + " seconds!"); }
//Big Brother of ReadSingleMail() - Deals with email batches - Runs only once when Outlook is first launched static void ReadMail() { //Set up OCR OnenoteOcrEngine ocr = new OnenoteOcrEngine(); string bodyText; string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); //Get unread emails from Inbox Microsoft.Office.Interop.Outlook.Application app = null; Microsoft.Office.Interop.Outlook._NameSpace ns = null; Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null; app = new Microsoft.Office.Interop.Outlook.Application(); ns = app.GetNamespace("MAPI"); inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox); Outlook.Items unreadItems = inboxFolder.Items.Restrict("[Unread]=true"); int max_runs; //Go through each Unread email if (unreadItems.Count > 10) { max_runs = 10; } else max_runs = unreadItems.Count; for (int counter = 1; counter <= max_runs; counter++) { //Reinitialize Data array for (int index = 0; index <= 8; index++) { data[index] = ""; } skipTextReader = false; dynamic item = unreadItems[counter]; StringBuilder sb = new StringBuilder(); if (ReadImageFromEmail(item) == 1) // If email has an attachment, read the attachment { Image imageToParse = Image.FromFile(@"C:\TestFileSave\" + item.Attachments[1].FileName); bodyText = ocr.Recognize(imageToParse); //MessageBox.Show(bodyText); if (bodyText != null) { bodyText = bodyText.Replace(" ", ";"); bodyText = bodyText.Replace(";;", ";"); bodyText = bodyText.Replace(",", ""); bodyText = bodyText.Replace("|", ";"); skipTextReader = true; //Bool variable if email has image signature data = parseOCRSignature(bodyText); } else { break; } } else //If no attachment, extract email body { if (item != null) { bodyText = item.Body; } else { continue; } sb.Append(bodyText); } if (skipTextReader == false) //If no image signature, parse email body { File.AppendAllText(mydocpath + @"\MailFile.txt", sb.ToString()); sb.Remove(0, bodyText.Length); string signature = extractSignature().Replace(";;", ";"); //Extract signature block //IF EMBEDDED IMAGE SIGNATURE FOUND if (((signature.ToLower()).Contains(".png") || (signature.ToLower()).Contains(".gif") || (signature.ToLower()).Contains(".jpg")) && signature.Length<20) { //MessageBox.Show(signature); Image imageToParse = Image.FromFile(@"C:\TestFileSave\" + signature); bodyText = ocr.Recognize(imageToParse); //MessageBox.Show(bodyText); if (bodyText != null) { bodyText = bodyText.Replace(" ", ";"); bodyText = bodyText.Replace(";;", ";"); bodyText = bodyText.Replace(",", ""); bodyText = bodyText.Replace("|", ";"); skipTextReader = true; //Bool variable if email has image signature data = parseOCRSignature(bodyText); } } else { data = parseImageSignature(signature); //Parse signature block } } //Send data to database //Get Sender Email Address string strEmailSenderEmailId = Convert.ToString(item.SenderEmailAddress); //Set up DB connections SqlConnection connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Prashanth\Desktop\CAPSTONE JPMORGAN\OutlookAddIn1\OutlookContacts.mdf;Integrated Security=True;User Instance=True"); SqlCommand command = new SqlCommand(); SqlDataReader dataReader; command.Connection = connection; connection.Open(); command.CommandText = "select * from New_Contacts where Email_ID = '" + strEmailSenderEmailId + "'"; dataReader = command.ExecuteReader(); //Check if contact exists already if (dataReader.HasRows == false) { command.CommandText = "insert into New_Contacts VALUES('" + data[0] + "' , '" + data[1] + "' , '" + data[2] + "' , '" + data[3] + "' , '" + data[4] + "' , '" + data[5] + "' , '" + data[6] + "' , '" + data[7] + "' , '" + data[8] + "' , '" + strEmailSenderEmailId + "' , '" + data[9] + "')"; dataReader.Close(); command.ExecuteNonQuery(); } //If it does, update and overwrite past data else { UpdateSelectFields(data[2], data[4], data[7], data[5], data[6], data[8] , data[9], strEmailSenderEmailId); } } System.IO.File.Delete(mydocpath + @"\MailFile.txt"); timer.Stop(); time_elapsed = time_elapsed + timer.Elapsed.TotalSeconds; ocr.Dispose(); MessageBox.Show("SigX took " + (time_elapsed).ToString() + " seconds for " + unreadItems.Count + "emails!"); //connection.Close(); }