Microsoft OCR Library for Windows Runtime

The new Optical Character Recognition (OCR) library from Microsoft is a performant nuget package to recognize text from images and with an API that is easy to use. The library is a Windows Runtime Component and can be used from .Net, WinJs and C++. The nuget package works for both Windows and Windows Phone and it’s easy to use it from the Shared “project” in Universal projects. The library runs completely on the client and no server calls are involved. Note that the library doesn’t work for “Any CPU”!

An example that uses basically the whole API:

var imageData = await ImageData.CreateFromStream(stream);
var ocrLanguage = OcrLanguage.English;
var ocrEngine = new OcrEngine(ocrLanguage);
var result = await ocrEngine.RecognizeAsync(imageData.OrientedPixelHeight, imageData.OrientedPixelWidth, imageData.Pixels);
if (result.Lines != null)
    foreach (var ocrLine in result.Lines)
        foreach (var ocrWord in ocrLine.Words)
            DoSomethingWithText(ocrWord.Text, ocrWord.Left, ocrWord.Top, ocrWord.Width, ocrWord.Height, result.TextAngle);

The library has currently support for 21 language but can probably recognize much more well enough. Even when I selected English it could recognize Swedish words just fine. If you want to tell the algorithms to actively search for other than English words you have to generate a new OCR resource file and add it to your project. More about that process here.

The choice to only support a pixel array as input forces you to write more code but makes the API much cleaner and flexible. You are not limited to a specific image format or source. The size limit for images is 2600 x 2600 pixels so you have to scale down pictures from e.g. the camera. The limit feels reasonable set to be big enough for most scenarios to work but still give good performance.

Don’t let the WindowsPreview namespace fool you. This is a stable version of the library and it can be used in apps published on the Store.
I can’t wait to see all new apps that is made possible because of this lib!

Some links with more info: MSDN, DevRadio, MS Sample
Here you can download sample code I written:

Over and out

This Post Has One Comment

  1. Sahil Bhavsar

    Hi Andreas Ekberg,
    can I use this library with Java code.

Leave a Reply