Platforms to show: All Mac Windows Linux Cross-Platform
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /WinFrameworks/Windows OCR
This example is the version from Fri, 2nd Feb 2023.
Project "Windows OCR.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class MainWindow Inherits Window
Control OpenButton Inherits PushButton
ControlInstance OpenButton Inherits PushButton
EventHandler Sub Action()
Dim f As FolderItem = GetOpenFolderItem(FileTypeGroup1.All)
If f = Nil Then Return
result = nil
pic = Picture.Open(f)
output.Invalidate
If pic = Nil Then
MessageBox "Failed to open picture!"
end if
If CheckLoad.Value Then
// use file
If CheckAsync.Value Then
// let recognizer load file
Recognizer.RecognizeAsync(f)
Else
result = Recognizer.RecognizeSync(f)
OCRText.Text = Result.Text
output.Invalidate
End If
Else
// use picture
If CheckAsync.Value Then
// or we have a picture
Recognizer.RecognizeAsync(pic)
Else
result = Recognizer.RecognizeSync(pic)
OCRText.Text = Result.Text
output.Invalidate
End If
End If
End EventHandler
End Control
Control OCRText Inherits TextArea
ControlInstance OCRText Inherits TextArea
End Control
Control Output Inherits Canvas
ControlInstance Output Inherits Canvas
EventHandler Sub Paint(g As Graphics, areas() As REALbasic.Rect)
#Pragma Unused areas
if pic <> nil then
dim faktor as Double = min( g.Height / Pic.Height, g.Width / Pic.Width)
dim w as Integer = Pic.Width * faktor
dim h as Integer = Pic.Height * faktor
g.DrawPicture pic, 0, 0, w, h, 0, 0, pic.Width, pic.Height
if result <> nil then
g.FontSize = 12
g.ForeColor = &cFF0000
// check result
dim lines() as WindowsOCRLineMBS = result.Lines
for each line as WindowsOCRLineMBS in lines
dim words() as WindowsOCRWordMBS = line.Words
for each word as WindowsOCRWordMBS in words
g.DrawText word.Text, word.x * faktor, word.y * faktor
next
next
end if
end if
End EventHandler
End Control
Control CheckAsync Inherits CheckBox
ControlInstance CheckAsync Inherits CheckBox
End Control
Control CheckLoad Inherits CheckBox
ControlInstance CheckLoad Inherits CheckBox
End Control
Control OpenButton1 Inherits PushButton
ControlInstance OpenButton1 Inherits PushButton
EventHandler Sub Action()
Result = Nil
pic = Nil
Output.Invalidate
OCRText.Text = ""
End EventHandler
End Control
EventHandler Sub Open()
If WindowsOCREngineMBS.Available Then
Dim lines() As String
lines.append "Available languages: "
Dim languages() As WindowsOCRLanguageMBS = WindowsOCREngineMBS.AvailableRecognizerLanguages
For Each n As WindowsOCRLanguageMBS In languages
lines.append n.DisplayName+", "+n.LanguageTag
Next
lines.append ""
Recognizer = New WindowsOCREngine
lines.append "max image size: "+Recognizer.MaxImageDimension.ToString
lines.append "current language: "+Recognizer.RecognizerLanguage.DisplayName
lines.append ""
OCRText.Text = join(lines, EndOfLine)
Else
MessageBox "Only for Windows 10 and newer."
End If
End EventHandler
Sub GotResult(Result as WindowsOCRResultMBS)
Self.Result = result
OCRText.text = Result.Text
output.Invalidate
End Sub
Property Recognizer As WindowsOCREngine
Property Result As WindowsOCRResultMBS
Property pic As Picture
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
MenuItem HelpMenu = "&Help"
End MenuBar
Class WindowsOCREngine Inherits WindowsOCREngineMBS
EventHandler Sub RecognizeCompleted(ErrorMessage as String, ErrorCode as Integer, Result as WindowsOCRResultMBS)
System.DebugLog CurrentMethodName
if ErrorMessage <> "" then
System.DebugLog "Error " + ErrorCode.ToString + ": " + ErrorMessage
MessageBox "OCR Failed with error " + ErrorCode.ToString + EndOfLine + EndOfLine + ErrorMessage
end if
If Result <> Nil Then
MainWindow.GotResult result
End If
End EventHandler
EventHandler Sub Stopped(Error as Integer)
System.DebugLog CurrentMethodName+": "+str(Error)
Break
End EventHandler
End Class
FileTypeGroup1
Filetype image/jpeg
Filetype image/png
Filetype image/tiff
End FileTypeGroup1
End Project
See also:
The items on this page are in the following plugins: MBS WinFrameworks Plugin.