Take the following steps to start a project and to add some code that will load an image and read its barcodes in a Windows Store application using LEADTOOLS WinRT libraries.
In the "Solution Explorer" window, right-click on the "References" folder for the project and select "Add Reference..." from the context menu. Browse to the <LEADTOOLS_INSTALLDIR>\Bin\WinRT8_1\ folder (depending on your target platform), and select the following .WINMD files:
Click the Add button and then click OK to add the above references.
Add the LEADTOOLS Advantage OCR Engine runtime files to the project by doing the following:
Right-click the new folder and select Add-->Existing Item from the context menu. Browse to the <LEADTOOLS_INSTALLDIR>\Bin\Common\OcrAdvantageRuntime folder, and select the following files:
For each of the files, set the Package Action setting to Content and the Copy To Output Directory setting to Copy if newer.
Open the default.html file and copy the below html code into the editor:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>OCRImage</title>
<!-- WinJS references -->
<link href="//Microsoft.WinJS.1.0/css/ui-dark.css" rel="stylesheet" />
<script src="//Microsoft.WinJS.1.0/js/base.js"></script>
<script src="//Microsoft.WinJS.1.0/js/ui.js" ></script>
<!-- OCRImage references -->
<link href="/css/default.css" rel="stylesheet" />
<script src="/js/default.js" ></script>
</head>
<body>
<input id="btnLoadAndRecognize" type="button" value="Load And Recognize Text" />
<p id="statusText" ></p>
<textarea id="resultsTextArea" readonly="readonly" style="width: 500px; height: 500px" ></textarea>
</body>
</html>
Change to default.js in the Solution Explorer and copy the following code into the file (overwrite all existing code):
// For an introduction to the Blank template, see the following documentation:
// http://msdn.microsoft.com/en-us/library/windows/apps/hh758331.aspx
(function () {
"use strict";
WinJS.Binding.optimizeBindingReferences = true;
var app = WinJS.Application;
var activation = Windows.ApplicationModel.Activation;
app.onactivated = function (args) {
if (args.detail.kind === activation.ActivationKind.launch) {
if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
// TODO: This application has been newly launched. Initialize
// your application here. LoadAndRecognize()
document.getElementById("btnLoadAndRecognize").addEventListener("click" , LoadAndRecognize, false );
// replace this with RasterSupport.SetLicense when you have a valid runtime license.
Leadtools.RasterSupport.initialize();
//Create and start the OCR engine
var engineDirectory = Windows.ApplicationModel.Package.current.installedLocation.path + "\\OCRRuntime";
_ocrEngine = Leadtools.Forms.Ocr.OcrEngineManager.createEngine(Leadtools.Forms.Ocr.OcrEngineType.advantage, false);
var _ocrCodecs = new Leadtools.Codecs.RasterCodecs();
_ocrEngine.startup(_ocrCodecs, null , "", engineDirectory);
_document = _ocrEngine.documentManager.createDocument();
} else {
// TODO: This application has been reactivated from suspension.
// Restore application state here.
}
args.setPromise(WinJS.UI.processAll());
}
};
app.oncheckpoint = function (args) {
// TODO: This application is about to be suspended. Save any state
// that needs to persist across suspensions here. You might use the
// WinJS.Application.sessionState object, which is automatically
// saved and restored across suspension. If you need to complete an
// asynchronous operation before your application is suspended, call
// args.setPromise().
};
app.start();
var _ocrEngine = null ;
var _document = null ;
function LoadAndRecognize() {
// Create a file picker object
var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
openPicker.viewMode = Windows.Storage.Pickers.PickerViewMode.thumbnail;
openPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.picturesLibrary;
openPicker.ViewMode = Windows.Storage.Pickers.PickerViewMode.List;
//Add a few supported file types
openPicker.fileTypeFilter.append(".jpg");
openPicker.fileTypeFilter.append(".jpeg");
openPicker.fileTypeFilter.append(".jxr");
openPicker.fileTypeFilter.append(".png");
openPicker.fileTypeFilter.append(".bmp");
openPicker.fileTypeFilter.append(".pcx");
openPicker.fileTypeFilter.append(".tif");
openPicker.fileTypeFilter.append(".tiff");
openPicker.fileTypeFilter.append(".j2k");
openPicker.fileTypeFilter.append(".jbg" );
openPicker.fileTypeFilter.append(".gif");
openPicker.fileTypeFilter.append(".jls");
openPicker.fileTypeFilter.append(".jb2");
openPicker.fileTypeFilter.append(".psd");
openPicker.fileTypeFilter.append(".dcm");
openPicker.fileTypeFilter.append(".dic");
openPicker.fileTypeFilter.append(".pdf");
// Open the picker for the user to pick a file
openPicker.pickSingleFileAsync().then(function (file) {
if (!file) {
// The picker was dismissed with no selected file
return;
}
document.getElementById('resultsTextArea').value = "";
// Create a RasterCodecs object to load the file
var codecs = new Leadtools.Codecs.RasterCodecs();
document.getElementById('statusText').innerText = "Loading image...";
// Create a LEAD stream for the file
var leadStream = Leadtools.LeadStreamFactory.create(file);
// Load it
codecs.options.load.initAlpha = true ;
codecs.loadAsync(leadStream, 32, Leadtools.Codecs.CodecsLoadByteOrder.bgr, 1, 1 ).then(function (rasterImage) {
_document.pages.clear();
//Add the page to the OCR document
_document.pages.addPage(rasterImage, null);
document.getElementById('statusText').innerText = "Recognizing Text...";
//Recognize the text
var results = _document.pages[0].recognizeText(null);
if (results.length == 0) {
document.getElementById('statusText').innerText = "No text recognized";
}
else {
//Show the results
document.getElementById('resultsTextArea').value = results;
document.getElementById('statusText').innerText = "Complete";
}
}, function (e) {
var msg = new Windows.UI.Popups.MessageDialog("Error loading image");
msg.showAsync();
document.getElementById('statusText').innerText = "Error loading image";
});
});
}
})();
Build, and Run the program to test it.
Products |
Support |
Feedback: OCR and Image in WinRT (Windows Store) using LEADTOOLS WinRT Libraries |
Introduction |
Help Version 19.0.2017.6.21
|