As you know, from beginning Windows 10, a client-based OCR engine was included. This engine was a product of Microsoft Research’s project ‘Oxford,’ and this project related to Microsoft Azure’s Cognitive Service.

You can test the OCR feature, which powered by Project Oxford, in the Microsoft Azure web site.

There are plenty of options to choose what OCR engine and how to use it. In my case, I want to select and test the Desktop built-in OCR engine, which does not make any additional cost, so I focused on it solely.

Requirements

Windows 10’s OCR is available with the Universal Windows Platform (UWP) API. But most UWP API seems like available only in UWP application, not for Desktop App. Luckily, OCR API is the exception, so you can easily use the API. Before following this article, you need to install below dependencies.

Windows 10 SDK (Matching it against your current Windows 10 build)

The latest version of Visual Studio (I tested it initially on Visual Studio 2010) or LINQPAD 5

For simplicity, I’m using LINQPAD 5.

Referencing a WINMD library

To use the UWP API in C#, you should reference the WINMD file, which located in %programfiles(x86)%\Windows Kits\10\UnionMetadata. This WINMD file contains the OCR API. Interestingly, the WINMD file is compatible with .NET assembly. So you can just add the WINMD file as usual.

You can add it from Reference Add dialog in LINQPAD 5 or Visual Studio.

Also, there are some gaps between formal .NET BCL APIs and UWP APIs. You need to add two additional libraries to bridging the gaps from %windir%\Microsoft.NET\Framework64\v4.0.30319 directory. Please make the reference list below.

System.Runtime, System.Runtime.WindowsRuntime and Windows.winmd have referenced.

In my case, I want the OCR engine can handle the Korean language. In such a case, you should install an additional Korean Language OCR package component in Optional Feature.

You cannot install this feature via Add/Remove Program dialog (which lives in the classic control panel) and Microsoft Management Console snap-in.

This installation leads to download software from the internet. You should have an internet connection before doing the installation. After that, You can run the OCR engine standalone offline.

Install the OCR support for Korean language component in Optional Feature page

If you want to install this component through PowerShell, you can do that with below PowerShell code snippet. (You need to run the code with Administrator privilege, and restart might need.)

$Capability = Get-WindowsCapability -Online | Where-Object { $_.Name -Like 'Language.OCR*ko-KR*' } $Result = $Capability | Add-WindowsCapability -Online if ($Result.RestartNeeded -eq $True) { Restart-Computer -Force }

Let’s write the code!

You can write the code like below.

using Windows.Globalization;

using Windows.Graphics.Imaging;

using Windows.Media.Ocr;

using Windows.Storage;

using Windows.Storage.Streams; ... var language = new Language(“ko”); if (!OcrEngine.IsLanguageSupported(language))

{

throw new Exception($”{language.LanguageTag} is not supported in this system.”);

} var stream = File.OpenRead(“...”);

var decoder = await BitmapDecoder.CreateAsync(stream.AsRandomAccessStream());

var bitmap = await decoder.GetSoftwareBitmapAsync(); var engine = OcrEngine.TryCreateFromLanguage(language);

var ocrResult = await engine.RecognizeAsync(bitmap).AsTask(); ...

The content of the code is as follows:

Check the installation status of a specific language.

Open an image file with .NET API, then convert the Stream object into UWP’s RandomAccessStream object.

Pass the RandomAccessStream object reference to BitmapDecoder.CreateAsync API.

Invoke GetSoftwareBitmapAsync API to obtain a bitmap object.

Create the OCR engine and invoke RecognizeAsync API with the bitmap object.

You may use the UWP’s API directly. But you don’t need to learn the new API; you can just use familiar .NET BCL API and just converting it. This option makes your work neater.

Also, you can modify the image with some open source tools such as ImageMagick, OpenCV, AForge, or something else before passing the image to Windows 10’s OCR engine.

Previewing OCR result

I tested the OCR engine with a receipt photo image.

It may vary condition by condition, but with some tuning, you may have a better result.

Conclusion

Nowadays, many software giants add recognition and human interface features to their existing software stack competitively. This one is not a fully-featured OCR engine, but it may satisfy basic requirements at a low cost.

Also, I recommend you look around other UWP APIs. Recently, Microsoft has released Microsoft Windows Compatibility pack with .NET Core 3.0 release. This package contains plenty of useful APIs for your existing .NET applications.

Updates