原創(chuàng)|使用教程|編輯:郝浩|2013-09-24 09:18:11.000|閱讀 715 次
概述:本文主要介紹如何利用LEADTOOLS WinRT SDK中的OCR功能將圖像轉(zhuǎn)換為文本,并提供源代碼。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
LEADTOOLS WinRT SDK提供了開發(fā)人員創(chuàng)建Windows Store應(yīng)用程序所需的所有最先進(jìn)的成像技術(shù),比如觸屏查看器,格式,壓縮,圖像處理,注釋和標(biāo)記,OCR,條形碼,PACS等等。借助于LEADTOOLS先進(jìn)的文檔和醫(yī)療成像技術(shù),開發(fā)人員可以輕松創(chuàng)建Windows Store應(yīng)用程序。
LEADTOOLS OCR SDK提供了原生WinRT庫,可在桌面,平板電腦或移動(dòng)設(shè)備上運(yùn)行。不管是掃描并將文檔轉(zhuǎn)換為可搜索的PDF文檔,還是拍攝名片并將其添加到聯(lián)系人中,LEADTOOLS 都可幫你實(shí)現(xiàn)。
下面的示例展示了OCR應(yīng)用程序的基本功能:轉(zhuǎn)換成可搜索的文本格式(如PDF,PDF / A,DOCX,TXT等),整頁文字識(shí)別,緯向文本識(shí)別。
首先,我們初始化LEADTOOLS OCR引擎,并準(zhǔn)備一份文檔:
// 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);
將掃描圖像轉(zhuǎn)換成可搜索文本(PDF,PDF / A,Word,XML和TXT)非常容易。識(shí)別功能處理文檔并將識(shí)別數(shù)據(jù)存儲(chǔ)為EMF。
// Auto-zone the page _ocrPage.AutoZone(null); // Recognize the page _ocrPage.Recognize(null);
識(shí)別完成后,OCR引擎利用DocumentWriter類將OCR結(jié)果轉(zhuǎn)換為任意格式。
// 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功能將圖像轉(zhuǎn)換為原始文本也非常的容易。
// Auto-zone the page _ocrPage.AutoZone(null); // Recognize the page and get the results as text TextResults.Text = _ocrPage.RecognizeText(null);
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn