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);