OCR in Windows 10

Believe it or not, Windows 10 comes with OCR capabilities out of the box. It’s actually very easy to use as well; admittedly, it’s not the most sensitive in the world – but it does basically work. Here’s how you would scan in a bitmap image and recognise characters:

        public async string RecogniseOCR()
        {                        
            var ocrEngine = Windows.Media.Ocr.OcrEngine.TryCreateFromLanguage(new Windows.Globalization.Language("en"));
            
            var file = await Package.Current.InstalledLocation.GetFileAsync(@"Assets\test.bmp");            
            using (var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
            {
                // Create image decoder.
                var decoder = await BitmapDecoder.CreateAsync(stream);

                // Load bitmap.
                var bitmap = await decoder.GetSoftwareBitmapAsync();

                // Extract text from image.
                OcrResult result = await ocrEngine.RecognizeAsync(bitmap);

                // Return recognized text.
                return result.Text;
            }
        }

I scanned this image:

OCRScan

And it found this:

OCRScan2

Like I said – not the most sensitive recognition in the world, but still, it’s there in Windows 10! What’s remarkable is that I only found it by accident – another example of Microsoft marketing missing a huge opportunity.

11 thoughts on “OCR in Windows 10

    1. pcmichaels Post author

      You want to add an image to the above example? Or you mean you want to load an image and have it OCR the loaded image?

      Reply
      1. Farhan

        Actually i have created a upload button it uploads an image to image control so i want to call that image in ocr code

        Reply
        1. Farhan

          i creatd upload button using this to upload image in image control
          Windows.Storage.Pickers.FileOpenPicker openPicker =
          new Windows.Storage.Pickers.FileOpenPicker();
          openPicker.SuggestedStartLocation =
          Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
          openPicker.ViewMode =
          Windows.Storage.Pickers.PickerViewMode.Thumbnail;

          // Filter to include a sample subset of file types.
          openPicker.FileTypeFilter.Clear();
          openPicker.FileTypeFilter.Add(“.bmp”);
          openPicker.FileTypeFilter.Add(“.png”);
          openPicker.FileTypeFilter.Add(“.jpeg”);
          openPicker.FileTypeFilter.Add(“.jpg”);

          // Open the file picker.
          Windows.Storage.StorageFile file =
          await openPicker.PickSingleFileAsync();

          // ‘file’ is null if user cancels the file picker.
          if (file != null)
          {
          using (Windows.Storage.Streams.IRandomAccessStream fileStream =
          await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
          {

          Windows.UI.Xaml.Media.Imaging.BitmapImage bitmapImage =
          new Windows.UI.Xaml.Media.Imaging.BitmapImage();

          bitmapImage.SetSource(fileStream);
          image.Source = bitmapImage;
          }
          than i use another button to extract text from uploaded image
          await LoadImage(file);

          OcrEngine ocrEngine = null;
          ocrEngine = OcrEngine.TryCreateFromUserProfileLanguages();

          if (ocrEngine != null)
          {
          var ocrResult = await ocrEngine.RecognizeAsync(bitmap);
          textBlock.Text = ocrResult.Text;

          }

          }

          private SoftwareBitmap bitmap;

          private async Task LoadImage(StorageFile file)
          {
          using (var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
          {

          var decoder = await BitmapDecoder.CreateAsync(stream);

          bitmap = await decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
          var imgsource = new WriteableBitmap(bitmap.PixelWidth, bitmap.PixelHeight);
          bitmap.CopyToBuffer(imgsource.PixelBuffer);
          }

          want to aske what path should i enter in code of text extraction ??

          Reply
          1. pcmichaels Post author

            I’m not sure I understand your question – your code works fine for me.

    1. Farhan

      yes i visited it actually i want to know how to extract text from image at runtime i don’t want to use assets folder like you did in you example

      Reply
      1. pcmichaels Post author

        Your code works with an image at runtime. I tried it earlier. If it’s not working for you then what error/problem are you getting?

        Reply
        1. Farhan

          it say exception handling and image is not found and stops at first line where i have to give path but i didn’t

          Reply
  1. Farhan

    i use this line var file = await Package.Current.InstalledLocation.GetFileAsync(@”Assets\test.bmp”);
    as
    var file = await Package.Current.InstalledLocation.GetFileAsync(” “);

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *