Platforms to show: All Mac Windows Linux Cross-Platform
/OCR/Tesseract3/OCR with picture
Required plugins for this example: MBS OCR Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /OCR/Tesseract3/OCR with picture
This example is the version from Fri, 8th Dec 2022.
Project "OCR with picture.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Löschen"
Const kFileQuit = "Beenden"
Const kFileQuitShortcut = ""
EventHandler Function UnhandledException(error As RuntimeException) As Boolean
MsgBox error.Message+EndOfLine+EndOfLine+Join(error.Stack,EndOfLine)
End EventHandler
End Class
Class Window1 Inherits Window
Control EditField1 Inherits TextArea
ControlInstance EditField1 Inherits TextArea
End Control
Control CheckBox1 Inherits CheckBox
ControlInstance CheckBox1 Inherits CheckBox
EventHandler Sub Action()
Refresh
End EventHandler
End Control
Control CheckBox2 Inherits CheckBox
ControlInstance CheckBox2 Inherits CheckBox
EventHandler Sub Action()
Refresh
End EventHandler
End Control
Control Out Inherits Canvas
ControlInstance Out Inherits Canvas
EventHandler Sub Paint(g As Graphics, areas() As REALbasic.Rect)
if pic<>Nil Then
g.DrawPicture pic, 0, 0, pic.Width, pic.Height, 0, 0, pic.Width, pic.Height
end if
g.ForeColor=&cFF0000
// check result
dim r as TesseractResultIteratorMBS = o.ResultIterator
if r<>Nil and CheckBox2.Value then
do
// query values like confidence
g.ForeColor=&c000000
if CheckBox1.Value then
dim Confidence as Double = r.Confidence(r.kLevelWord)
g.ForeColor=Rgb((100-Confidence)*2.55, Confidence*2.55, 0)
end if
dim rleft, rtop, rright, rbottom as integer
// query bounding box
if r.BoundingBox(r.kLevelWord, rleft, rtop, rright, rbottom) then
dim rwidth as integer = rright - rleft
dim rheight as integer = rbottom - rtop
g.DrawRect rleft, rtop, rwidth, rHeight
end if
// and go to next word
loop until r.NextItem(r.kLevelWord) = false
end if
End EventHandler
End Control
EventHandler Sub Open()
// search tessdata folder
dim fo as FolderItem = findfile("tessdata")
if TargetWin32 then
// on Windows the parent folder
fo = fo.Parent
else
// path must be correct to have it find the files and point to folder where tessdata folder is inside
fo = GetFolderItem("")
end if
o = new TesseractMBS(fo, "eng")
dim n as integer = o.NumDawgs
if n = 0 then
MsgBox "No lang data found!"
quit
end if
dim f as FolderItem
// pick a test file
if rnd<0.5 then
f = findfile("eurotext.tif")
else
f = findfile("phototest.tif")
end if
dim p as Picture = f.OpenAsPicture
if o.SetImage(p) then
EditField1.text = o.getText
pic=p
end if
Exception ex as TesseractErrorExceptionMBS
MsgBox "Exception: "+ex.message
End EventHandler
Function FindFile(name as string) As FolderItem
// Look for file in parent folders from executable on
dim parent as FolderItem = app.ExecutableFile.Parent
while parent<>Nil
dim file as FolderItem = parent.Child(name)
if file<>Nil and file.Exists then
Return file
end if
parent = parent.Parent
wend
End Function
Property o As TesseractMBS
Property pic As Picture
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&Ablage"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Bearbeiten"
MenuItem EditUndo = "&Rückgängig"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "&Ausschneiden"
MenuItem EditCopy = "&Kopieren"
MenuItem EditPaste = "&Einfügen"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "&Alles auswählen"
End MenuBar
End Project
See also:
The items on this page are in the following plugins: MBS OCR Plugin.