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