How can I do zonal OCR in VB6? -
as can see down there made programme scans document , optionally page info , material & size infos , date info.

when use ocr scanning this:
dim mdoc modi.document dim mlay modi.layout dim fso scripting.filesystemobject dim logfile object public function scanman(byval name string, byval path string) string set mdoc = new modi.document 'set mdoc = createobject("modi.document") set fso = new scripting.filesystemobject doevents '''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''' create ocrlog file ''''''''''''''''''' ocrpath = app.path & "\ocr results log\" ocrname = str(datetime.date) & " ocrresults" if fso.folderexists(ocrpath) = false fso.createfolder (ocrpath) end if if fso.fileexists(ocrpath & ocrname & ".txt") = false fso.createtextfile ocrpath & ocrname & ".txt" end if set logfile = fso.opentextfile(ocrpath & ocrname & ".txt", forappending) '''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''''''''''' on error goto ocrerr doevents mdoc.create path & "\" & name mdoc.images(0).ocr milang_english, true, true logfile.write mdoc.images(0).layout.text scanman = mlay.text mdoc.close false set mlay = nothing set mdoc = nothing exit function ocrerr: logfile.writeline "ocr given (" & err.number & ") numbered (" & err.description & ") error." logfile.close end function this gets whole page want 3 spesific area scanned how can achive that? there function that? scans x,y coordinates?
a vb6 snippet
sub testtextselection() dim mitextsel modi.imiselectableitem dim miselectrects modi.miselectrects dim miselectrect modi.miselectrect dim strtextselinfo string set mitextsel = midocview1.textselection set miselectrects = mitextsel.getselectrects strtextselinfo = _ "bounding rectangle page & coordinates: " & vbcrlf each miselectrect in miselectrects miselectrect strtextselinfo = strtextselinfo & _ .pagenumber & ", " & .top & ", " & _ .left & ", " & .bottom & ", " & _ .right & vbcrlf end next msgbox strtextselinfo, vbinformation + vbokonly, _ "text selection info" set miselectrect = nothing set miselectrects = nothing set mitextsel = nothing end sub though question tagged vb6 answer vb.net 2010. hope vb.net converted vb6, matters few more time.
the basic idea create xml file image , run query on xml file fetch text of required block surrounded (x1,y1) , (x2,y2).
the core class
imports system imports system.io imports system.xml imports system.linq imports modi public class clscore public sub new() 'blah blah blah end sub public function gettextfromcoordinates(byval ipath$, byval x1&, byval y1&, byval x2&, byval y2&) string try dim xdoc xelement = me.convertimage2xml(ipath) if isnothing(xdoc) = false dim result new xelement(<text/>) dim query = xdoc...<wd>.where(function(c) val(cstr(c.@left)) >= x1 , val(cstr(c.@right)) <= x2 , val(cstr(c.@top)) >= y1 , val(cstr(c.@bottom)) <= y2) each ele xelement in query result.add(cstr(ele.value) & " ") next ele return trim(result.value) else return "" end if catch ex exception console.writeline(ex.tostring) return ex.tostring end try end function private function convertimage2xml(byval ipath$) xelement try if file.exists(ipath) = true dim midoc new modi.document dim result new xelement(<image path=<%= ipath %>/>) midoc.create(ipath) each miimg modi.image in midoc.images dim page new xelement(<page id=<%= result...<page>.count + 1 %>/>) miimg.ocr() each miword modi.word in miimg.layout.words dim wd new xelement(<wd block=<%= miword.regionid.tostring %>><%= miword.text %></wd>) each mirect modi.mirect in miword.rects wd.add(new xattribute("left", mirect.left)) wd.add(new xattribute("top", mirect.top)) wd.add(new xattribute("right", mirect.right)) wd.add(new xattribute("bottom", mirect.bottom)) next mirect page.add(wd) next miword result.add(page) next miimg return result else return nothing end if catch ex exception console.writeline(ex.tostring) return nothing end try end function end class the main module
imports system imports system.io imports system.text.regularexpressions module modmain sub main() dim ipath$ = "", ipos$ = "150,825,1400,1200" console.writeline("enter path file:") ipath = console.readline() console.writeline("") console.writeline("enter co-ordinates(i.e., x1,y1,x2,y2 or 150,825,1400,1200):") ipos = console.readline() dim tmp string() = regex.split(ipos, "\d+") dim outtext$ = new clscore().gettextfromcoordinates(ipath, tmp(0), tmp(1), tmp(2), tmp(3)) console.writeline("") console.writeline(string.format("{0}[({1},{2})-({3},{4})]:{5}{5}{6}", dir(ipath), tmp(0), tmp(1), tmp(2), tmp(3), vbcrlf, outtext)) console.readline() end sub end module update
the following example reports page number , coordinates of bounding rectangle around user's image selection in viewer control. , can used later within picturebox.
sub testimageselection() dim miimagesel modi.imiselectableimage dim lngpageno long dim lngleft long, lngtop long dim lngright long, lngbottom long dim strimageselinfo string set miimagesel = midocview1.imageselection miimagesel.getboundingrect lngpageno, _ lngleft, lngtop, lngright, lngbottom strimageselinfo = _ "page number: " & lngpageno & vbcrlf & _ "bounding rectangle coordinates: " & vbcrlf & _ lngleft & ", " & lngtop & ", " & _ lngright & ", " & lngbottom msgbox strimageselinfo, vbinformation + vbokonly, _ "image selection info" set miimagesel = nothing end sub hope helps.
Comments
Post a Comment