private void btnRecognizeSelected_Click(object sender, EventArgs e) { if (m_bActiveRecognition == false) { int nPageCount = SmartOcrSdkExports.OCR_GetPageCount(); //m_nSelectedPageIndex = lstLoadedImages.Items.Count > 0 ? lstLoadedImages.SelectedIndex : -1; m_nSelectedPageIndex = lstLoadedImages.Items.Count > 0 ? 0 : -1; if (m_nSelectedPageIndex >= 0) { btnLoad.Enabled = false; btnRecognizeSelected.Enabled = false; btnChooseFile.Enabled = false; lstLoadedImages.Enabled = false; btnRemoveRegion.Enabled = false; panel1.Enabled = false; panel2.Enabled = false; panel3.Enabled = false; m_bCancelRequested = false; m_bActiveRecognition = true; prgRecognition.Value = 0; Thread threadRecognition = new Thread(new ThreadStart(RecognizeSelectedThreadProc)); threadRecognition.Start(); } } }
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()); }
int OcrScanCallback(int nEvent, IntPtr hBitmap, IntPtr pCallbackData) { if (nEvent == SmartOcrSdkExports.OCR_SE_IMAGE_SCANNED && hBitmap != IntPtr.Zero) { SmartOcrSdkExports.OCR_AddPageFromHBITMAP(hBitmap, -1, -1); } return(1); }
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; } }
public DemoTemplateForm() { InitializeComponent(); btnAdd.Click += btnAdd_Click; btnUpdateTemplate.Click += btnUpdateTemplate_Click; pbPreview.MouseUp += OnMouseUp; pbPreview.MouseDown += pbPreview_MouseDown; pbPreview.MouseMove += pbPreview_MouseMove; pbPreview.Paint += pbPreview_Paint; lstLoadedImages.SelectedIndexChanged += lstLoadedImages_SelectedIndexChanged; dataGridView1.RowHeaderMouseClick += dataGridView1_RowHeaderMouseClick; dataGridView2.CellContentClick += dataGridView2_CellContentClick; dataGridView2.RowHeaderMouseClick += dataGridView2_RowHeaderMouseClick; //Add Columns AddItemListView(); // dataGridView(); btnChooseFile.Click += btnChooseFile_Click; this.WindowState = FormWindowState.Maximized; this.FormBorderStyle = FormBorderStyle.FixedDialog; this.MaximizeBox = false; this.StartPosition = FormStartPosition.CenterScreen; int h = Screen.PrimaryScreen.WorkingArea.Height; int w = Screen.PrimaryScreen.WorkingArea.Width; this.Height = h; this.Width = w; panel3.Height = h; panel1.Height = h; panel2.Height = h; panel3.Width = w - (panel1.Width + panel2.Width); pbPreview.Width = panel3.Width; pbPreview.Height = h; //pbPreview.Height = h; m_bCancelRequested = false; m_bActiveRecognition = false; m_UpdateProgress = new UpdateProgressDelegate(UpdateProgressSafe); m_nSelectedPageIndex = -1; // Lấy số page hiện tại m_Page = SmartOcrSdkExports.OCR_GetPageCount(); if (!string.IsNullOrEmpty(FileTemplateOCR_DTO.m_Path)) { AddFromPageOrc(FileTemplateOCR_DTO.m_Path); FileTemplateOCR_DTO.m_Path = string.Empty; } }
void UpdateProgressSafe(int value) { if (value == 0) { btnLoad.Enabled = true; btnRecognizeSelected.Enabled = true; btnChooseFile.Enabled = true; lstLoadedImages.Enabled = true; panel1.Enabled = true; panel2.Enabled = true; panel3.Enabled = true; IntPtr hBmp = IntPtr.Zero; int res = SmartOcrSdkExports.OCR_GetHBITMAP_FromPageImage(m_nSelectedPageIndex, ref hBmp); if (res != SmartOcrSdkExports.RES_ERROR) { pbPreview.Image = Bitmap.FromHbitmap(hBmp); } if (!m_Dictionary_Page.ContainsKey(m_nSelectedPageIndex)) { m_Dictionary_Page.Add(m_nSelectedPageIndex, null); } var Template_ID = CheckExitsTemplate(); if (Template_ID.HasValue) { LoadDynamicInput(Template_ID.Value); LoadDynamicRectangle(Template_ID.Value); } else { MessageBox.Show("Template Incorrect"); if (MessageBox.Show("Bạn có muốn tạo template cho file?", "", MessageBoxButtons.YesNo) == DialogResult.Yes) { var m_Path = lstLoadedImages.SelectedItem.ToString(); string m_Extention = Path.GetExtension(m_Path); string[] m_Str = m_Extention.Split('_'); if (m_Str.Length > 1) { m_Path = m_Path.Replace("_" + m_Str[1], ""); } FileTemplateOCR_DTO.m_Path = m_Path; var frmTemplate = new DemoTemplateForm(); frmTemplate.MaximizeBox = false; frmTemplate.Show(this); } } } prgRecognition.Value = value; }
private void DemoForm_FormClosing(object sender, FormClosingEventArgs e) { // Release the OCR engine. // Use try-catch block in order to guarantee application exit when // SmartOCR_SDK.dll is not in the same folder with the application try { // Release the OCR engine SmartOcrSdkExports.OCR_ReleaseAll(); } catch (System.Exception) { } }
public void AddFromPageOrc(string m_FileName) { string ext = Path.GetExtension(m_FileName); m_strPath = m_FileName; int pageSelectIndex = 0; m_PageOld = SmartOcrSdkExports.OCR_GetPageCount(); if (m_PageOld > m_Page) { for (int i = m_Page; i < m_PageOld; i++) { var nPageIndex = m_Page; SmartOcrSdkExports.OCR_RemovePage(nPageIndex); } lstLoadedImages.Items.Clear(); } if (ext == ".pdf") { SmartOcrSdkExports.OCR_AddPagesFromPDF(m_FileName, null, IntPtr.Zero); } else { SmartOcrSdkExports.OCR_AddPageFromImage(m_FileName); } int nPageCount = SmartOcrSdkExports.OCR_GetPageCount() - m_Page; int index = lstLoadedImages.Items.Count; if (nPageCount > 1) { for (int i = 0; i < nPageCount; i++) { pageSelectIndex++; string filePath = ""; filePath = m_FileName + "_page" + pageSelectIndex; lstLoadedImages.Items.Add(filePath); } } else { lstLoadedImages.Items.Add(m_FileName); } // Automatically select the first item if (lstLoadedImages.SelectedItem == null) { lstLoadedImages.SelectedIndex = 0; } }
private void TemplateForm_FormClosing(object sender, FormClosingEventArgs e) { try { // Release the OCR engine int nPageCount = SmartOcrSdkExports.OCR_GetPageCount();// -m_PageOld; for (int i = m_Page; i < nPageCount; i++) { var nPageIndex = m_Page; int res = SmartOcrSdkExports.OCR_RemovePage(nPageIndex); } } catch (System.Exception) { } }
void btnChooseFile_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); if (openFileDialog.ShowDialog() == DialogResult.OK) { string ext = Path.GetExtension(openFileDialog.FileName); string fileName = openFileDialog.FileName; int pageSelectIndex = 0; int nPageCount_1 = SmartOcrSdkExports.OCR_GetPageCount(); if (ext == ".pdf") { SmartOcrSdkExports.OCR_AddPagesFromPDF(fileName, null, IntPtr.Zero); } else { SmartOcrSdkExports.OCR_AddPageFromImage(fileName); } int nPageCount = SmartOcrSdkExports.OCR_GetPageCount() - nPageCount_1; int index = lstLoadedImages.Items.Count; if (nPageCount > 1) { for (int i = 0; i < nPageCount; i++) { pageSelectIndex++; string filePath = ""; filePath = fileName + "_page" + pageSelectIndex; lstLoadedImages.Items.Add(filePath); } } else { lstLoadedImages.Items.Add(fileName); } // Automatically select the first item if (lstLoadedImages.SelectedItem == null) { lstLoadedImages.SelectedIndex = 0; } else { lstLoadedImages.SelectedIndex = lstLoadedImages.Items.Count - nPageCount; } } }
private void btnSave_Click(object sender, EventArgs e) { if (m_bActiveRecognition) { return; } int res = SmartOcrSdkExports.OCR_ExportDocument(m_strModulePath + "\\ExportedFile", cbOutputFormats.SelectedIndex + 1, 0, 0, 1); if (res != SmartOcrSdkExports.RES_ERROR) { MessageBox.Show("The file is successfully saved to: \n\"" + m_strModulePath + "\\\""); } else { MessageBox.Show("Failed to save." + " Please make sure that the application is started with Administrator privileges" + " and there is at least one recognized page."); } }
void UpdateProgressSafe(int value) { if (value == 0) { btnChooseFile.Enabled = true; lstLoadedImages.Enabled = true; panel1.Enabled = true; panel2.Enabled = true; //panel3.Enabled = true; IntPtr hBmp = IntPtr.Zero; int res = SmartOcrSdkExports.OCR_GetHBITMAP_FromPageImage(m_nSelectedPageIndex, ref hBmp); if (res != SmartOcrSdkExports.RES_ERROR) { pbPreview.Image = Bitmap.FromHbitmap(hBmp); } } prgRecognition.Value = value; }
void lstLoadedImages_SelectedIndexChanged(object sender, EventArgs e) { if (lstLoadedImages.SelectedItem != null) { if (pbPreview.Image != null) { pbPreview.Image.Dispose(); } Remove(); if (m_bActiveRecognition == false) { m_nSelectedPageIndex = lstLoadedImages.Items.Count > 0 ? (m_Page + lstLoadedImages.SelectedIndex) : -1; if (m_nSelectedPageIndex >= 0) { IntPtr hBmp = IntPtr.Zero; int res = SmartOcrSdkExports.OCR_GetHBITMAP_FromPageImage(m_nSelectedPageIndex, ref hBmp); if (res != SmartOcrSdkExports.RES_ERROR) { pbPreview.Image = Bitmap.FromHbitmap(hBmp); } var Template_ID = Convert.ToInt32(txtTemplate_ID.Text); if (!m_Dictionary_Page.ContainsKey(m_nSelectedPageIndex)) { m_Dictionary_Page.Add(m_nSelectedPageIndex, Template_ID); btnChooseFile.Enabled = false; lstLoadedImages.Enabled = false; panel1.Enabled = false; panel2.Enabled = false; m_bCancelRequested = false; m_bActiveRecognition = true; prgRecognition.Value = 0; Thread threadRecognition = new Thread(new ThreadStart(RecognizeSelectedThreadProc)); threadRecognition.Start(); } } } } }
public void OCR_Init(string strUserName = null, string strLicenseKey = null, string strIntsallPath = null, string strTempDir = null) { int res = SmartOcrSdkExports.RES_ERROR; try { if (Directory.Exists(strIntsallPath) == false) { strIntsallPath = ""; } var watchOCR_Init = System.Diagnostics.Stopwatch.StartNew(); res = SmartOcrSdkExports.OCR_Init(strUserName, strLicenseKey, strIntsallPath, strTempDir); watchOCR_Init.Stop(); LOGGER.InfoFormat("OCR_Init --> {0} seconds", (long)watchOCR_Init.Elapsed.TotalSeconds); SmartOcrSdkExports.OCR_SetRecognitionMode(SmartOcrSdkExports.OCR_RM_FORM); } catch (System.DllNotFoundException ex) { LOGGER.Error(ex); MessageBox.Show("System.DllNotFoundException occurred.\n\n" + "Please make sure that SmartOCR_SDK.dll is in the same folder with your executable.\n\n" + "Exception description: \n" + ex.Message, "SmartOCR_SDK.dll not found"); } catch (System.BadImageFormatException ex) { LOGGER.Error(ex); MessageBox.Show("System.BadImageFormatException occurred.\n\n" + "Please make sure to build your project with platform target \'x86\' instead of \'Any CPU\'.\n\n" + "Exception description: \n" + ex.Message, "System.BadImageFormatException exception"); } if (res == SmartOcrSdkExports.RES_ERROR) { MessageBox.Show("Could not initialize SmartOCR SDK.\n" + "Please make sure to place redistributables in the folder of your executable.", "Smart OCR init error"); } }
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(); } }
private void lstLoadedImages_SelectedIndexChanged(object sender, EventArgs e) { if (lstLoadedImages.SelectedItem != null) { if (pbPreview.Image != null) { pbPreview.Image.Dispose(); } #region note //int nPageCount = SmartOcrSdkExports.OCR_GetPageCount(); //if (nPageCount >= 0) //{ // SmartOcrSdkExports.OCR_RemoveAllPages(); //} //string ext = Path.GetExtension(lstLoadedImages.SelectedItem.ToString()); //string filePath = lstLoadedImages.SelectedItem.ToString(); //string[] page = ext.Split('_'); //int pageIndex = 0, index = 0; //if (page != null) //{ // ext = page[0].ToString(); // if(page.Length > 1) // { // string strPage = page[1]; // index = strPage.Length; // pageIndex = (Convert.ToInt16(strPage.Substring(4))) - 1; // filePath = filePath.Replace("_" + strPage, ""); // } //} //if (ext == ".pdf") //{ // SmartOcrSdkExports.OCR_AddPagesFromPDF(filePath, null, IntPtr.Zero); // #region Note // //PdfDocument doc = new PdfDocument(); // //doc.LoadFromFile(filePath); // //Image emf = doc.SaveAsImage(0, Spire.Pdf.Graphics.PdfImageType.Bitmap); // //pbPreview.Image = emf; // #endregion // ExtentionFile = 0; //} //else //{ // SmartOcrSdkExports.OCR_AddPageFromImage(filePath); // // pbPreview.Image = Bitmap.FromFile(filePath); // ExtentionFile = 0; //} #endregion Remove(); if (m_bActiveRecognition == false) { m_nSelectedPageIndex = lstLoadedImages.Items.Count > 0 ? lstLoadedImages.SelectedIndex : -1; if (m_nSelectedPageIndex >= 0) { LoadDynamicInput(); IntPtr hBmp = IntPtr.Zero; int res = SmartOcrSdkExports.OCR_GetHBITMAP_FromPageImage(m_nSelectedPageIndex, ref hBmp); if (res != SmartOcrSdkExports.RES_ERROR) { pbPreview.Image = Bitmap.FromHbitmap(hBmp); #region "Resize image" //Image MyImage = Bitmap.FromHbitmap(hBmp); //Bitmap MyBitMap = new Bitmap(MyImage, panel1.Width + 200, panel1.Height + 200); //Graphics Graphic = Graphics.FromImage(MyBitMap); //Graphic.InterpolationMode = InterpolationMode.High; //pbPreview.Image = MyBitMap; #endregion } if (!m_Dictionary_Page.ContainsKey(m_nSelectedPageIndex)) { btnLoad.Enabled = false; btnRecognizeSelected.Enabled = false; btnChooseFile.Enabled = false; lstLoadedImages.Enabled = false; btnRemoveRegion.Enabled = false; panel1.Enabled = false; panel2.Enabled = false; panel3.Enabled = false; m_bCancelRequested = false; m_bActiveRecognition = true; prgRecognition.Value = 0; //@Tuan: Nen xem xet cai nay Thread threadRecognition = new Thread(new ThreadStart(RecognizeSelectedThreadProc)); threadRecognition.Start(); } else { var Template_ID = CheckExitsTemplate(); if (Template_ID.HasValue) { LoadDynamicInput(Template_ID.Value); LoadDynamicRectangle(Template_ID.Value); } else { MessageBox.Show("Template Incorrect"); if (MessageBox.Show("Bạn có muốn tạo template cho file?", "", MessageBoxButtons.YesNo) == DialogResult.Yes) { var m_Path = lstLoadedImages.SelectedItem.ToString(); string m_Extention = Path.GetExtension(m_Path); string[] m_Str = m_Extention.Split('_'); if (m_Str.Length > 1) { m_Path = m_Path.Replace("_" + m_Str[1], ""); } FileTemplateOCR_DTO.m_Path = m_Path; var frmTemplate = new DemoTemplateForm(); frmTemplate.MaximizeBox = false; frmTemplate.Show(this); } } } } } } }
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(); } }
void RecognizeAllThreadProc() { SmartOcrSdkExports.OCR_RecognizeAllPages(new OcrProgressCallbackDelegate(OcrProgressCallback), IntPtr.Zero); prgRecognition.Invoke(m_UpdateProgress, new object[] { 0 }); m_bActiveRecognition = 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; }
private void btnLoad_Click(object sender, EventArgs e) { const string strFilters = "*.png;*.bmp;*.tif;*.tiff;*.jpg;*.jpeg;*.gif"; string[] arrFilters = strFilters.Split(';'); foreach (string strFilter in arrFilters) { // Get files for each filter string[] arrImgFiles = Directory.GetFiles(m_strModulePath + "\\Test_Images", strFilter); // Load files foreach (string strImg in arrImgFiles) { #region Tuan // OCR_AddPageFromImage_W // DeleteAllRegionsInPage ( int nPageIndex ) // OCR_DeleteRegionInPage ( int nPageIndex, int nRegionIndex ) : Xóa 1 vùng cho 1 trang cụ thể //ExternalGetRecognizedText //OCR_LoadRegionTemplate_A //OCR_RecognizeRegions // OCR_SaveRegionTemplate_W #endregion int res = SmartOcrSdkExports.OCR_AddPageFromImage(strImg); // Add to the list only if we successfully loaded the image if (res != SmartOcrSdkExports.RES_ERROR) { lstLoadedImages.Items.Add(strImg); // Automatically select the first item if (lstLoadedImages.SelectedItem == null) { lstLoadedImages.SelectedIndex = 0; } } } } if (lstLoadedImages.SelectedItem == null) { MessageBox.Show("Could not find the test images.\n" + "The demo application searches for test images in \"Test_Images\" folder.\n" + "This folder must be located in the same directory with the application."); } #region // Demo: Scan // - Init scan, should be called only once // SmartOcrSdkExports.OCR_InitScan(); // /* Additional scan tune if required * int len = SmartOcrSdkExports.OCR_GetScanners(null, 0); * if (len > 0) * { * StringBuilder sb = new StringBuilder(len); * SmartOcrSdkExports.OCR_GetScanners(sb, len); * string[] arrScanners = sb.ToString().Replace("\r", "").Split('\n'); * if(arrScanners.Length > 0) * SmartOcrSdkExports.OCR_SetActiveScanner(arrScanners[0]); * } */ // - Perform scan (async) // SmartOcrSdkExports.OCR_PerformScan(new OcrScanCallbackDelegate(OcrScanCallback), 0, IntPtr.Zero); #endregion }