For MBS Xojo Plugins 23.1 we add new classes to use the built-in OCR in Windows 10. Similar to the Vision framework on macOS, these classes provide an easy way to use the text recognition in Windows. We got these classes:
- WindowsOCREngineMBS is the central class to recognize text.
- WindowsOCRExceptionMBS is the exception class to report errors.
- WindowsOCRLanguageMBS defines which language to use.
- WindowsOCRResultMBS provides the result of the recognition process.
- WindowsOCRLineMBS represents a line of text within the result.
- WindowsOCRWordMBS represents a word within the line.
You can query the available languages on your computer with the AvailableRecognizerLanguages method. This usually only includes the installed language packs for Windows like in Germany the German one. But you can go to system settings and install another language if you need it.
Here is a sample method to query languages, put the display name and language tag in an array of string and shows it with a MessageBox:
Sub ShowLanguages()
Dim languages() As WindowsOCRLanguageMBS = WindowsOCREngineMBS.AvailableRecognizerLanguages
Dim lines() As String
For Each n As WindowsOCRLanguageMBS In languages
lines.append n.DisplayName+", "+n.LanguageTag
Next
MessageBox String.FromArray(lines, EndOfLine)
Exception error As WindowsOCRExceptionMBS
MessageBox Introspection.GetType(error).name+": "+error.Message
End Sub
In our sample project, it looks like this when you recognize some text:
If you like to recognize a file (or picture), you can use either the synchronous or the asynchronous recognize methods in our WindowsOCREngineMBS class. You can initialize such an object with a specific language or just use the one for the current user. The recognizeSync functions return the result right away or raise an exception. Here is a bit of sample code:
Sub Recognize(language as string, file as FolderItem)
Dim Recognizer As WindowsOCREngineMBS
If language = "" Then
// default language
Recognizer = New WindowsOCREngineMBS
Else
Dim lang As New WindowsOCRLanguageMBS(language)
Recognizer = New WindowsOCREngineMBS(lang)
End If
Dim result As WindowsOCRResultMBS = Recognizer.RecognizeSync(file)
OCRText.Text = Result.Text
Exception error As WindowsOCRExceptionMBS
MessageBox Introspection.GetType(error).name+": "+error.Message
End Sub
Please try the new classes in 23.1pr2 and let us know if you have questions or find an issue.