Platforms to show: All Mac Windows Linux Cross-Platform

/OCR/Tesseract3/OCR with text styles


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 text styles

This example is the version from Thu, 14th Nov 2018.

Project "OCR with text styles.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 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 end if g.ForeColor=&cFF0000 // check result dim r as TesseractResultIteratorMBS = o.ResultIterator if r<>Nil 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 f = findfile("phototest.tif") if f = nil then MsgBox "Please put in test file." return end if dim p as Picture = f.OpenAsPicture if o.SetImage(p) then call o.Recognize CopyTextWithStyles(o, EditField1) pic=p end if Exception ex as TesseractErrorExceptionMBS MsgBox "Exception: "+ex.message End EventHandler
Sub CopyTextWithStyles(o as TesseractMBS, dest as TextArea) // check result dim r as TesseractResultIteratorMBS = o.ResultIterator if r<>Nil then dim st as StyledText = dest.StyledText do dim bold as Boolean dim italic as Boolean dim underline as Boolean dim monospace as Boolean dim serif as Boolean dim smallcaps as Boolean dim pointsize as integer dim fontid as integer dim fontName as string = r.WordFontAttributes(bold, italic, underline, monospace, serif, smallcaps, pointsize, fontid) dim sr as new StyleRun sr.Text = r.Text(r.kLevelWord) sr.Underline = underline sr.Bold = bold sr.Italic = italic sr.Size = pointsize * 4 sr.Font = fontName st.AppendStyleRun sr if r.IsAtFinalElement(r.kLevelParagraph, r.kLevelWord) then // last word in Paragraph? // add new line sr = new StyleRun sr.Text = EndOfLine st.AppendStyleRun sr else // add space sr = new StyleRun sr.Text = " " st.AppendStyleRun sr end if // and go to next word loop until r.NextItem(r.kLevelWord) = false end if End Sub
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.


The biggest plugin in space...