This article will help you to integrate the Microsoft OCR APIs into Windows 8.1 Apps.
Optical Character Recognition (OCR) can be used to recognize and extract text in an image.
Adding the OCR capability to a Windows 8.1 Store app is quite easy since Microsoft has released the OCR library that it uses in its OneNote app publically for developers. The OCR APIs are in the form of a Nugget package that can be directly referenced in your project.
So without wasting more time let’s get our hands dirty and write some code.Step 1
Start a new project. We will use the Windows 8.1 Store App Blank Template.
After creating the project, go to the tools menu and navigate to the NuGet Package ManageràManage NuGet Packages for this solution as shown in the image below.
Type “Microsoft Ocr” into the search box. Select the first result in the search results and then click install. During the installation process it will ask you to accept the terms and conditions. Click I Accept.
You can also verify the installation of the package from your Solution Explorer. You will see a reference added to the “WindowsPreview.Media.Ocr” and a new folder named “OcrResources” is added to your project.
Once you have completed Step 3 it is time to add some code to the app.The XAML
Here I’ve named the image “imageToBeRead” and the textblock “textRead”.C#
After adding the using statements declare some global variables for the page just before the page constructor.
And initialize the ocrEngine in the page constructor.
Be sure when you initialize the ocrEngine that you add the languages that you want to read from the image.
In the preceding method I made a filepicker using which the user can select a file from their computer. The picked file is passed as a parameter to another method called loadImage.
The definition of loadImage is given below.
This method simply adds the selected image to the app.
The OCR processing occurs in the ReadImage button Click event as in the following:
Then in the next step, there is a foreach loop that goes through every line of the result and another foreach loop is nested that goes through the each word that is in the line.
Now we simply add the text of the word in a string.And then finally show the string using the textblock.The output is in the images below:Note: There is a small bug in the OCR API. It’s when you try to build the project with the AnyCPU configuration it will not succeed. You will need to change the build configuration to any of x64, x86 or ARM. It supports all of the CPUs but individually. I hope that in the next update Microsoft will fix this bug.
Universal Windows Platform - Complete Solution