Using the Microsoft OCR Library for WinRT from a desktop app


Recently I had a need to do some OCR processing from a desktop app. Unfortunately the OCR package from Microsoft is for WinRT apps only. However, by using this excellent NuGet package: https://blogs.msdn.microsoft.com/lucian/2015/10/23/how-to-call-uwp-apis-from-a-desktop-vbc-app/#comment-7985

I was able to get this to work from desktop code.

        private async Task<string> ExtractText(string filename)

        {

            OcrEngine ocrEngine = OcrEngine.TryCreateFromLanguage(new Windows.Globalization.Language(“EN-US”));

            SoftwareBitmap softwareBitmap = GetSoftwareBitmap(filename);

            var ocrResult = await ocrEngine.RecognizeAsync(softwareBitmap);

            string extractedText = “”;

            if (ocrResult != null)

            {

                foreach (var line in ocrResult.Lines)

                {

                    // Iterate over words in line.

                    foreach (var word in line.Words)

                    {

                        extractedText += word.Text + ” “;

                    }

 

                    extractedText += Environment.NewLine;

                }

            }

 

            return extractedText;

        }

 

        private static unsafe SoftwareBitmap GetSoftwareBitmap(string filename)

        {

            WriteableBitmap writeableBitmap = null;

            using (FileStream file = File.Open(filename, FileMode.Open))

            {

                BitmapSource srcImage = BitmapDecoder.Create(file,

                    BitmapCreateOptions.None, BitmapCacheOption.OnLoad).Frames[0];

                if (srcImage.Format != PixelFormats.Bgra32)

                    srcImage = new FormatConvertedBitmap(srcImage, PixelFormats.Bgra32, null, 0);

                writeableBitmap = new WriteableBitmap(srcImage);

            }

 

            writeableBitmap.Lock();

            var pixels = new byte[writeableBitmap.PixelWidth * writeableBitmap.PixelHeight * 4];

            byte* pBuf = (byte*)writeableBitmap.BackBuffer;

            for (int i = 0; i < pixels.Length; i++)

                pixels[i] = *pBuf++;

            var ibuffer = pixels.AsBuffer();

            writeableBitmap.Unlock();

 

            SoftwareBitmap softwareBitmap = new SoftwareBitmap(BitmapPixelFormat.Bgra8, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight);

            softwareBitmap.CopyFromBuffer(ibuffer);

            return softwareBitmap;

        }

Here is a sample app that I have prepared, you are free to use this in commercial projects. 

http://theuxblog20160710035436.azurewebsites.net/ocrdesktop.zip

Paul Tallett, UX Global Practice, Microsoft UK

Disclaimer: The information on this site is provided “AS IS” with no warranties, confers no rights, and is not supported by the authors or Microsoft Corporation. Use of included script samples are subject to the terms specified in the Terms of Use.

 

 


Comments (0)

Skip to main content