Home > Net >  Windows.Media.Ocr won't scan anything if width and/or height are too small
Windows.Media.Ocr won't scan anything if width and/or height are too small

Time:01-14

I'm using Windows.Media.Ocr that will recognize text through a transparent picturebox. It works with the follow code:


Imports Windows.Media.Ocr
Imports System.IO
Imports System.Runtime.InteropServices.WindowsRuntime
Public Class Form1

   Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
       Dim softwareBmp As Windows.Graphics.Imaging.SoftwareBitmap
       Using bmp As Bitmap = New Bitmap(PictureBox1.Width, PictureBox1.Height)
           Using g As Graphics = Graphics.FromImage(bmp)
               Dim pt As Point = Me.PointToScreen(New Point(PictureBox1.Left, PictureBox1.Top))
               g.CopyFromScreen(pt.X, pt.Y, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy)
               Using memStream = New Windows.Storage.Streams.InMemoryRandomAccessStream()
                   bmp.Save(memStream.AsStream(), System.Drawing.Imaging.ImageFormat.Bmp)
                   Dim decoder As Windows.Graphics.Imaging.BitmapDecoder = Await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(memStream)
                   softwareBmp = Await decoder.GetSoftwareBitmapAsync()
               End Using
           End Using
       End Using

       Dim ocrEng = OcrEngine.TryCreateFromLanguage(New Windows.Globalization.Language("en-US"))

       Dim languages As IReadOnlyList(Of Windows.Globalization.Language) = ocrEng.AvailableRecognizerLanguages
       For Each language In languages
           Console.WriteLine(language.LanguageTag)
       Next
       Dim r = ocrEng.RecognizerLanguage
       Dim n = ocrEng.MaxImageDimension
       Dim ocrResult = Await ocrEng.RecognizeAsync(softwareBmp)
       RichTextBox1.Text = ocrResult.Text
   End Sub
End Class 

but it seems not to scan anything if the width and or height are a bit small, like width:89 and height:27 eventhough the text is really visible on it. . It seems to start to scan when height is atleast 50 and width at least 40. Is this a limitation of the ocr engine? Thanks

CodePudding user response:

I tested by resizing the final SoftwareBitmap and it seems to work with a small PictureBox height (tested with 32, which did not work with original SoftwareBitmap )

Add: Dim scaledSoftwareBitmap As Windows.Graphics.Imaging.SoftwareBitmap

and after the line softwareBmp = Await decoder.GetSoftwareBitmapAsync()add

 Dim transform = New Windows.Graphics.Imaging.BitmapTransform() With {
                         .ScaledWidth = CUInt(PictureBox1.Width * 2),
                         .ScaledHeight = CUInt(PictureBox1.Height * 2),
                         .InterpolationMode = Windows.Graphics.Imaging.BitmapInterpolationMode.NearestNeighbor
                     }
 Dim pixelData As Windows.Graphics.Imaging.PixelDataProvider = Await decoder.GetPixelDataAsync(Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8, Windows.Graphics.Imaging.BitmapAlphaMode.Premultiplied, transform, Windows.Graphics.Imaging.ExifOrientationMode.RespectExifOrientation, Windows.Graphics.Imaging.ColorManagementMode.ColorManageToSRgb)
 scaledSoftwareBitmap = New Windows.Graphics.Imaging.SoftwareBitmap(Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8, CInt(transform.ScaledWidth), CInt(transform.ScaledHeight))
 Dim pixels As Byte() = pixelData.DetachPixelData()
 Dim buffer As Windows.Storage.Streams.IBuffer = pixels.AsBuffer()
 scaledSoftwareBitmap.CopyFromBuffer(buffer)

and replace

 Dim ocrResult = Await ocrEng.RecognizeAsync(softwareBmp)

with

     Dim ocrResult = Await ocrEng.RecognizeAsync(scaledSoftwareBitmap)

Now it should works

  •  Tags:  
  • Related