轉帖|使用教程|編輯:龔雪|2015-05-22 09:21:13.000|閱讀 1153 次
概述:本教程主要展示如何在OCR文檔上追加/刪除和繪制識別區域。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
根據下面的步驟用戶可創建和運行一個程序,用來展示如何在OCR文檔上追加/刪除和繪制識別區域。
1. 打開Visual Studio。
2. 在菜單中選擇文件->新建->項目。
3. 在新建項目對話框中,模板選擇"Visual C#",然后選擇Windows窗體應用程序。
4. 在名稱欄輸入這個項目的名稱:"OcrTutorial1",然后選擇確定 ,當然如果需要的話可以重新指定一個目錄來存放這個項目。
5. 在"解決方案資源管理器"窗口,右鍵點擊"引用",然后在彈出菜單中選擇"添加引用"。在彈出的引用管理器對話框中,選擇"框架"然后選擇"瀏覽(B)"按鈕,定位到LEADTOOLS安裝目錄:
"<安裝目錄>\Bin\DotNet4\Win32" 然后選擇如下幾個DLL:
注意:Leadtools.Codecs.*.dll這種引用是根據支持的圖像格式命名的,請根據您的需要添加不同的格式支持。
6. 切換到Form1的代碼視圖,然后添加如下代碼到文件的最前面,如果已經有了using代碼的話請添加到已有的代碼之后:
using Leadtools; using Leadtools.Codecs; using Leadtools.Drawing; using Leadtools.Controls; using Leadtools.Forms; using Leadtools.Forms.Ocr; using Leadtools.Forms.DocumentWriters;
7. 在Form1類中添加如下的私有變量:
private ImageViewer _imageViewer; private IOcrEngine _ocrEngine; private IOcrPage _ocrPage;
8. 重寫Form1的OnLoad方法,然后添加如下代碼:
protected override void OnLoad(EventArgs e) { string licenseFilePath = "這里添加你的License文件路徑。" string developerKey = "這里添加你的DeveloperKey"; RasterSupport.SetLicense(licenseFilePath, developerKey); // 為Form添加一個ImageViewer _imageViewer = new ImageViewer(); _imageViewer.Dock = DockStyle.Fill; Controls.Add(_imageViewer); _imageViewer.BringToFront(); // 使用原始大小展示圖片 _imageViewer.UseDpi = true; // 為鼠標添加放大/縮小圖片功能,同時禁用鼠標的雙擊模式,因為我們要添加自己的事件 ImageViewerPanZoomInteractiveMode panZoomMode = new ImageViewerPanZoomInteractiveMode(); panZoomMode.DoubleTapSizeMode = ControlSizeMode.None; _imageViewer.InteractiveModes.Add(panZoomMode); // 初始化OCR引擎 _ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, false); // 啟動OCR引擎 _ocrEngine.Startup(null, null, null, @"C:\LEADTOOLS 19\Bin\Common\OcrAdvantageRuntime"); // 從一個圖片創建一個OCR頁面 string fileName = @"C:\Users\Public\Documents\LEADTOOLS Images\Ocr1.tif"; RasterImage rasterImage = _ocrEngine.RasterCodecsInstance.Load(fileName, 1); _ocrPage = _ocrEngine.CreatePage(rasterImage, OcrImageSharingMode.AutoDispose); // 自動為這個圖片添加識別區域 _ocrPage.AutoZone(null); // 追加一個額外的區域,這個是我們自己定義的 OcrZone zone = new OcrZone(); zone.Name = "Custom zone"; zone.ZoneType = OcrZoneType.Text; zone.Bounds = new LogicalRectangle(10, 10, _ocrPage.Width - 20, 100, LogicalUnit.Pixel); _ocrPage.Zones.Add(zone); // 在ImageViewer中顯示圖片 _imageViewer.Image = _ocrPage.GetRasterImage(); Text = "需要刪除區域的話請右鍵點擊任何一個即可,雙擊任何地方可以將識別結果保存為PDF文件。"; // 根據需要掛接一些事件 _imageViewer.PostRender += _imageViewer_PostRender; _imageViewer.MouseDown += _imageViewer_MouseDown; _imageViewer.MouseDoubleClick += _imageViewer_MouseDoubleClick; base.OnLoad(e); }
9. 重寫Form1的OnFormClosed方法,然后添加如下代碼:
protected override void OnFormClosed(FormClosedEventArgs e) { // 釋放識別頁面 _ocrPage.Dispose(); // 釋放識別引擎 _ocrEngine.Dispose(); base.OnFormClosed(e); }
10. 添加如下代碼實現ImageViewer的PostRender事件,來繪制我們的自定義區域:
private void _imageViewer_PostRender(object sender, ImageViewerRenderEventArgs e) { // 繪制區域 foreach (OcrZone zone in _ocrPage.Zones) { // 取得區域邊界 LogicalRectangle zoneBounds = zone.Bounds; // 該區域邊界是邏輯矩形,它可能會在單位比像素以外。轉換為像素 LeadRect bounds = zoneBounds.ToRectangle(_ocrPage.DpiX, _ocrPage.DpiY); // 將邊界轉換為ImageViewer中的單位 //需要注意的是這個Demo沒有旋轉圖片,否則你需要使用四個角點。 bounds = _imageViewer.ConvertRect(null, ImageViewerCoordinateType.Image, ImageViewerCoordinateType.Control, bounds); // 判斷是不是我們的自定義區域,如果是就將邊框畫為紅色,否則用藍色 if(zone.Name == "Custom zone") e.PaintEventArgs.Graphics.DrawRectangle(Pens.Red, bounds.X, bounds.Y, bounds.Width - 1, bounds.Height - 1); else e.PaintEventArgs.Graphics.DrawRectangle(Pens.Blue, bounds.X, bounds.Y, bounds.Width - 1, bounds.Height - 1); } }
11. 添加如下代碼來實現ImageViewer的MouseDown事件,用來刪除區域:
private void _imageViewer_MouseDown(object sender, MouseEventArgs e) { // 判斷是否是右鍵點擊 if (e.Button != MouseButtons.Right) return; // 從控件的坐標轉換為圖像坐標 LeadPoint point = new LeadPoint(e.X, e.Y); point = _imageViewer.ConvertPoint(null, ImageViewerCoordinateType.Control, ImageViewerCoordinateType.Image, point); // 使用HitTestZone方法來找到鼠標當前按下時的位置 int zoneIndex = _ocrPage.HitTestZone(new LogicalPoint(point.X, point.Y, LogicalUnit.Pixel)); if (zoneIndex != -1) { // 移除這個區域 _ocrPage.Zones.RemoveAt(zoneIndex); // 重繪顯示區域 _imageViewer.Invalidate(); _imageViewer.Update(); // 如果沒有剩下的區域,顯示一個Message if (_ocrPage.Zones.Count == 0) MessageBox.Show(this, "頁面上沒有剩余的可辨識區域,保存為PDF選項現在不可用。"); } }
12. 最后我們實現鼠標雙擊保存事件,添加如下代碼:
private void _imageViewer_MouseDoubleClick(object sender, MouseEventArgs e) { // 檢查當前頁面上是否有可辨識區域 if (_ocrPage.Zones.Count == 0) { MessageBox.Show(this, "頁面上沒有剩余的可辨識區域,保存為PDF選項現在不可用。"); return; } // 有的話開始識別 string pdfFileName = @"C:\Users\Public\Documents\LEADTOOLS Images\Ocr1.pdf"; // 如果文件存在的話試著刪除掉。也有可能被其它應用程序打開。 if (System.IO.File.Exists(pdfFileName)) { try { System.IO.File.Delete(pdfFileName); } catch { MessageBox.Show(this, "這個文件可能被其他程序占用,請關閉后重試。"); return; } } _ocrPage.Recognize(null);// 創建一個文檔 using (IOcrDocument ocrDocument = _ocrEngine.DocumentManager.CreateDocument( null, OcrCreateDocumentOptions.AutoDeleteFile)) { // 添加一個頁面 ocrDocument.Pages.Add(_ocrPage); // 保存為PDF ocrDocument.Save(pdfFileName, DocumentFormat.Pdf, null); } // 顯示這個文件 System.Diagnostics.Process.Start(pdfFileName); }
13. 保存這個項目,并運行它。
按住Ctrl可以放大縮小圖片,雙擊圖片就可以識別文字并保存為PDF,右鍵點擊任何一個藍色區域都可以刪除識別區域。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都控件網