string ExtractDataOCR(SmartOcrSdkExports.TOCR_RECT tocr_Rect) { int res = SmartOcrSdkExports.OCR_DeleteAllRegionsInPage(m_nSelectedPageIndex); res = SmartOcrSdkExports.OCR_AddCustomRegionInPage(m_nSelectedPageIndex, SmartOcrSdkExports.OCR_RT_TEXT, tocr_Rect); res = SmartOcrSdkExports.OCR_RecognizeRegion(m_nSelectedPageIndex, 0, SmartOcrSdkExports.OCR_RT_TEXT, null, IntPtr.Zero); int nRegionType_1 = SmartOcrSdkExports.OCR_RT_UNDEFINED; // SmartOcrSdkExports.TOCR_RECT rectRegion = new SmartOcrSdkExports.TOCR_RECT(); SmartOcrSdkExports.OCR_GetRegionInfo(m_nSelectedPageIndex, 0, ref nRegionType_1, ref tocr_Rect); int regionCount = SmartOcrSdkExports.OCR_GetRegionCountInPage(m_nSelectedPageIndex); StringBuilder sbText = new StringBuilder(); for (int i = 0; i < regionCount; i++) { // First calculate how much space do we need int length = SmartOcrSdkExports.OCR_GetRecognizedTextFromPageRegion(m_nSelectedPageIndex, i, null, 0); if (length != SmartOcrSdkExports.RES_ERROR) { StringBuilder sbBuffer = new StringBuilder(length); res = SmartOcrSdkExports.OCR_GetRecognizedTextFromPageRegion(m_nSelectedPageIndex, i, sbBuffer, length); if (res != SmartOcrSdkExports.RES_ERROR) { sbText.Append(sbBuffer.ToString()); sbText.Append(System.Environment.NewLine); sbText.Append(System.Environment.NewLine); } } } return(sbText.ToString()); }
private void ExtractData(object sender, EventArgs e) { var fileName = "KBLAAOCR_" + String.Format("{0:ddMMyyyyHHmmss}", DateTime.Now) + ".txt"; string strRedistPath = m_strModulePath + "\\ExportedFile\\" + fileName; FileInfo saveFile = new FileInfo(strRedistPath); StreamWriter writer = null; bool flag = false; if (myRectangles.Count > 0) { writer = saveFile.CreateText(); flag = true; } foreach (var rect in myRectangles) { SmartOcrSdkExports.TOCR_RECT tocr_Rect; tocr_Rect.m_nBottom = rect.Bottom; tocr_Rect.m_nLeft = rect.Left; tocr_Rect.m_nRight = rect.Right; tocr_Rect.m_nTop = rect.Top; int res = SmartOcrSdkExports.OCR_DeleteAllRegionsInPage(m_nSelectedPageIndex); res = SmartOcrSdkExports.OCR_AddCustomRegionInPage(m_nSelectedPageIndex, SmartOcrSdkExports.OCR_RT_TEXT, tocr_Rect); res = SmartOcrSdkExports.OCR_RecognizeRegion(m_nSelectedPageIndex, 0, SmartOcrSdkExports.OCR_RT_TEXT, null, IntPtr.Zero); int nRegionType_1 = SmartOcrSdkExports.OCR_RT_UNDEFINED; SmartOcrSdkExports.TOCR_RECT rectRegion = new SmartOcrSdkExports.TOCR_RECT(); SmartOcrSdkExports.OCR_GetRegionInfo(m_nSelectedPageIndex, 0, ref nRegionType_1, ref rectRegion); int regionCount = SmartOcrSdkExports.OCR_GetRegionCountInPage(m_nSelectedPageIndex); StringBuilder sbText = new StringBuilder(); for (int i = 0; i < regionCount; i++) { // First calculate how much space do we need int length = SmartOcrSdkExports.OCR_GetRecognizedTextFromPageRegion(m_nSelectedPageIndex, i, null, 0); if (length != SmartOcrSdkExports.RES_ERROR) { StringBuilder sbBuffer = new StringBuilder(length); res = SmartOcrSdkExports.OCR_GetRecognizedTextFromPageRegion(m_nSelectedPageIndex, i, sbBuffer, length); if (res != SmartOcrSdkExports.RES_ERROR) { sbText.Append(sbBuffer.ToString()); sbText.Append(System.Environment.NewLine); sbText.Append(System.Environment.NewLine); writer.Write(sbText.ToString()); } } } } if (flag) { writer.Close(); string msg = "Data was extracted and imported to file which put , " + strRedistPath + ", Please browse for view !"; MessageBox.Show(msg); btnExtract.Enabled = false; } }
private void button1_Click(object sender, EventArgs e) { SmartOcrSdkExports.OCR_SetRecognitionMode(SmartOcrSdkExports.OCR_RM_FORM); // int ress = SmartOcrSdkExports.CAP_AddSampleFieldData(SmartOcrSdkExports.CAP_FF_TEXT, "TOTAL"); // ress = SmartOcrSdkExports.CAP_AddSampleFieldDataByName("TOTAL", "1000"); //// ress = SmartOcrSdkExports.CAP_RegisterCustomField("TOTAL", "tbInvoice", SmartOcrSdkExports.CAP_FF_TEXT, null, null, null); // int length_v2 = SmartOcrSdkExports.OCR_GetRecognizedTextFromPageRegion(m_nSelectedPageIndex, 0, null, 0); // StringBuilder sbBuffer_v2 = new StringBuilder(length_v2); // // res = SmartOcrSdkExports.OCR_GetRecognizedTextFromPageRegion(m_nSelectedPageIndex, i, sbBuffer, length); // ress = SmartOcrSdkExports.CAP_FilterTextAs("TOTAL", SmartOcrSdkExports.CAP_FF_TEXT, sbBuffer_v2, length_v2, null, null); // MessageBox.Show(ress + ""); DataTable dbTable = new DataTable(); try { //Adding columns to datatable dbTable.Columns.Add("UNIT PRICE", typeof(string)); dbTable.Columns.Add("TOTAL", typeof(string)); // Init the OCR SDK //if (!Init()) return; // Add pages //SmartOcrSdkExports.OCR_AddPagesFromPDF("C:\\Temp\\W-2.pdf", null, IntPtr.Zero); // SmartOcrSdkExports.OCR_AddPageFromImage("C:\\Temp\\test.png"); //set region mode:OCR_RM_FORM SmartOcrSdkExports.OCR_SetRecognitionMode(SmartOcrSdkExports.OCR_RM_FORM); SmartOcrSdkExports.TOCR_RECT rectRegion = new SmartOcrSdkExports.TOCR_RECT(); //rectRegion.m_nLeft = 1778; //rectRegion.m_nTop = 1387; //rectRegion.m_nRight = 2375; //rectRegion.m_nBottom = 2036; rectRegion.m_nLeft = 507; rectRegion.m_nTop = 1130; rectRegion.m_nRight = 1216; rectRegion.m_nBottom = 1643; int res = SmartOcrSdkExports.OCR_DeleteAllRegionsInPage(m_nSelectedPageIndex); res = SmartOcrSdkExports.OCR_AddCustomRegionInPage(m_nSelectedPageIndex, SmartOcrSdkExports.OCR_RT_TEXT, rectRegion); res = SmartOcrSdkExports.OCR_RecognizeRegion(m_nSelectedPageIndex, 0, SmartOcrSdkExports.OCR_RT_TEXT, null, IntPtr.Zero); int nRegionType_1 = SmartOcrSdkExports.OCR_RT_UNDEFINED; //SmartOcrSdkExports.TOCR_RECT rectRegion = new SmartOcrSdkExports.TOCR_RECT(); SmartOcrSdkExports.OCR_GetRegionInfo(m_nSelectedPageIndex, 0, ref nRegionType_1, ref rectRegion); int regionCount = SmartOcrSdkExports.OCR_GetRegionCountInPage(m_nSelectedPageIndex); StringBuilder sbText = new StringBuilder(); for (int i = 0; i < regionCount; i++) { // First calculate how much space do we need int length = SmartOcrSdkExports.OCR_GetRecognizedTextFromPageRegion(m_nSelectedPageIndex, i, null, 0); if (length != SmartOcrSdkExports.RES_ERROR) { StringBuilder sbBuffer = new StringBuilder(length); res = SmartOcrSdkExports.OCR_GetRecognizedTextFromPageRegion(m_nSelectedPageIndex, i, sbBuffer, length); if (res != SmartOcrSdkExports.RES_ERROR) { sbText.Append(sbBuffer.ToString()); sbText.Append(System.Environment.NewLine); sbText.Append(System.Environment.NewLine); } } } MessageBox.Show(sbText.ToString()); //DataColumnCollection columns = dbTable.Columns; //sbText.Replace(columns.ToString(), ""); foreach (DataColumn dc in dbTable.Columns) { if (sbText.ToString().Contains(dc.ColumnName)) { sbText.Replace(dc.ColumnName, ""); } } List <String> items = new List <String>(sbText.ToString().Split(new Char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)); for (var i = 0; i < items.Count; i++) { List <String> items_2 = new List <String>(items[i].ToString().Split(new Char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)); if (items_2.Count > 1) { DataRow row; row = dbTable.NewRow(); row["UNIT PRICE"] = items_2[0]; row["TOTAL"] = items_2[1]; dbTable.Rows.Add(row); } } } catch (Exception ex) { Console.WriteLine(ex); } finally { // Release the OCR - can be called when your application is closing SmartOcrSdkExports.OCR_ReleaseAll(); } dataGridView1.DataSource = dbTable; }
void OnMouseUp(object sender, MouseEventArgs e) { if (drawing) { drawing = false; var rect = getRectangle(); if (rect.Width > 0 && rect.Height > 0) { myRectangles.Add(rect); btnExtract.Enabled = true; btnRemoveRegion.Enabled = true; } SmartOcrSdkExports.TOCR_RECT tocr_Rect; tocr_Rect.m_nBottom = rect.Bottom; tocr_Rect.m_nLeft = rect.Left; tocr_Rect.m_nRight = rect.Right; tocr_Rect.m_nTop = rect.Top; // MessageBox.Show(tocr_Rect.m_nBottom + " " + tocr_Rect.m_nLeft + " " + tocr_Rect.m_nRight + " " + tocr_Rect.m_nTop); int res = SmartOcrSdkExports.OCR_DeleteAllRegionsInPage(m_nSelectedPageIndex); res = SmartOcrSdkExports.OCR_AddCustomRegionInPage(m_nSelectedPageIndex, SmartOcrSdkExports.OCR_RT_TEXT, tocr_Rect); res = SmartOcrSdkExports.OCR_RecognizeRegion(m_nSelectedPageIndex, 0, SmartOcrSdkExports.OCR_RT_TEXT, null, IntPtr.Zero); int nRegionType_1 = SmartOcrSdkExports.OCR_RT_UNDEFINED; SmartOcrSdkExports.TOCR_RECT rectRegion = new SmartOcrSdkExports.TOCR_RECT(); SmartOcrSdkExports.OCR_GetRegionInfo(m_nSelectedPageIndex, 0, ref nRegionType_1, ref rectRegion); int regionCount = SmartOcrSdkExports.OCR_GetRegionCountInPage(m_nSelectedPageIndex); StringBuilder sbText = new StringBuilder(); for (int i = 0; i < regionCount; i++) { // First calculate how much space do we need int length = SmartOcrSdkExports.OCR_GetRecognizedTextFromPageRegion(m_nSelectedPageIndex, i, null, 0); if (length != SmartOcrSdkExports.RES_ERROR) { StringBuilder sbBuffer = new StringBuilder(length); res = SmartOcrSdkExports.OCR_GetRecognizedTextFromPageRegion(m_nSelectedPageIndex, i, sbBuffer, length); if (res != SmartOcrSdkExports.RES_ERROR) { sbText.Append(sbBuffer.ToString()); sbText.Append(System.Environment.NewLine); sbText.Append(System.Environment.NewLine); } foreach (Control c in panel3.Controls) { if (c is TextBox) { TextBox TextBoxControl = (TextBox)c; if (TextBoxControl.Focused) { TextBoxControl.Text = sbText.ToString(); string txt = TextBoxControl.Name; Int64 Region_ID = Convert.ToInt64(txt.Split('_')[1]); } if (string.IsNullOrEmpty(TextBoxControl.Text)) { TextBoxControl.Focus(); break; } } } } } pbPreview.Invalidate(); } }
void OnMouseUp(object sender, MouseEventArgs e) { if (drawing) { drawing = false; var rect = getRectangle(); if (rect.Width > 0 && rect.Height > 0) { myRectangles.Add(rect); } SmartOcrSdkExports.TOCR_RECT tocr_Rect; tocr_Rect.m_nBottom = rect.Bottom; tocr_Rect.m_nLeft = rect.Left; tocr_Rect.m_nRight = rect.Right; tocr_Rect.m_nTop = rect.Top; int res = SmartOcrSdkExports.OCR_DeleteAllRegionsInPage(m_nSelectedPageIndex); res = SmartOcrSdkExports.OCR_AddCustomRegionInPage(m_nSelectedPageIndex, SmartOcrSdkExports.OCR_RT_TEXT, tocr_Rect); res = SmartOcrSdkExports.OCR_RecognizeRegion(m_nSelectedPageIndex, 0, SmartOcrSdkExports.OCR_RT_TEXT, null, IntPtr.Zero); int nRegionType_1 = SmartOcrSdkExports.OCR_RT_UNDEFINED; SmartOcrSdkExports.TOCR_RECT rectRegion = new SmartOcrSdkExports.TOCR_RECT(); SmartOcrSdkExports.OCR_GetRegionInfo(m_nSelectedPageIndex, 0, ref nRegionType_1, ref rectRegion); int regionCount = SmartOcrSdkExports.OCR_GetRegionCountInPage(m_nSelectedPageIndex); StringBuilder sbText = new StringBuilder(); for (int i = 0; i < regionCount; i++) { // First calculate how much space do we need int length = SmartOcrSdkExports.OCR_GetRecognizedTextFromPageRegion(m_nSelectedPageIndex, i, null, 0); if (length != SmartOcrSdkExports.RES_ERROR) { StringBuilder sbBuffer = new StringBuilder(length); res = SmartOcrSdkExports.OCR_GetRecognizedTextFromPageRegion(m_nSelectedPageIndex, i, sbBuffer, length); if (res != SmartOcrSdkExports.RES_ERROR) { sbText.Append(sbBuffer.ToString()); sbText.Append(System.Environment.NewLine); sbText.Append(System.Environment.NewLine); } if (txtMarkers.Focused) { txtMarkers.Text = sbText.ToString(); T_Rectangle = new Rectangle(); T_Rectangle = rect; } else if (txtResult.Focused) { txtResult.Text = sbText.ToString(); R_Rectangle = new Rectangle(); R_Rectangle = rect; } } } pbPreview.Invalidate(); } }