原創|使用教程|編輯:郝浩|2013-09-24 09:18:11.000|閱讀 715 次
概述:本文主要介紹如何利用LEADTOOLS WinRT SDK中的OCR功能將圖像轉換為文本,并提供源代碼。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
LEADTOOLS WinRT SDK提供了開發人員創建Windows Store應用程序所需的所有最先進的成像技術,比如觸屏查看器,格式,壓縮,圖像處理,注釋和標記,OCR,條形碼,PACS等等。借助于LEADTOOLS先進的文檔和醫療成像技術,開發人員可以輕松創建Windows Store應用程序。
LEADTOOLS OCR SDK提供了原生WinRT庫,可在桌面,平板電腦或移動設備上運行。不管是掃描并將文檔轉換為可搜索的PDF文檔,還是拍攝名片并將其添加到聯系人中,LEADTOOLS 都可幫你實現。
下面的示例展示了OCR應用程序的基本功能:轉換成可搜索的文本格式(如PDF,PDF / A,DOCX,TXT等),整頁文字識別,緯向文本識別。
首先,我們初始化LEADTOOLS OCR引擎,并準備一份文檔:
// Create an instance of the engine string strEngineDirectory = Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, @"OCR"); _ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, false); _ocrEngine.Startup(null, null, string.Empty, strEngineDirectory); // Create the OCR document _ocrDocument = _ocrEngine.DocumentManager.CreateDocument();
接下來,加載圖像,并把圖像添加到文檔中。
// Show the file picker var picker = new FileOpenPicker(); picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; picker.ViewMode = PickerViewMode.List; foreach (var imageFormat in _imageFormats) picker.FileTypeFilter.Add(imageFormat.Extension); var file = await picker.PickSingleFileAsync(); if (file == null) return; // Create a LEADTOOLS stream from the file ILeadStream leadStream = LeadStreamFactory.Create(file); // Get the RasterCodecs object to load the image from the OCR engine RasterCodecs codecs = _ocrEngine.RasterCodecsInstance; // Load the image (first page only) RasterImage rasterImage = await codecs.LoadAsync(leadStream, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1); // Add it to the OCR engine // Check if we have previous pages, remove them _ocrDocument.Pages.Clear(); _ocrPage = _ocrDocument.Pages.AddPage(rasterImage, null);
將掃描圖像轉換成可搜索文本(PDF,PDF / A,Word,XML和TXT)非常容易。識別功能處理文檔并將識別數據存儲為EMF。
// Auto-zone the page _ocrPage.AutoZone(null); // Recognize the page _ocrPage.Recognize(null);
識別完成后,OCR引擎利用DocumentWriter類將OCR結果轉換為任意格式。
// Create a LEADTOOLS stream from the file ILeadStream leadStream = LeadStreamFactory.Create(file); // Set PDF output options, use PDF/A PdfDocumentOptions options = _ocrEngine.DocumentWriterInstance.GetOptions(DocumentFormat.Pdf) as PdfDocumentOptions; options.DocumentType = PdfDocumentType.PdfA; _ocrEngine.DocumentWriterInstance.SetOptions(DocumentFormat.Pdf, options); // Save the OCR'd document as searchable PDF await _ocrDocument.SaveAsync(leadStream, DocumentFormat.Pdf, null);
利用RecognizeText功能將圖像轉換為原始文本也非常的容易。
// Auto-zone the page _ocrPage.AutoZone(null); // Recognize the page and get the results as text TextResults.Text = _ocrPage.RecognizeText(null);
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn