AutoitMike Posted October 17, 2022 Share Posted October 17, 2022 (edited) I saw a forum question that asked how to OCR non selectable text from a screen and get its coordinates. This intrigued me, as I would like to have this ability in my scripts. I found the following VB code and I am having difficulty "Converting" it to Autoit code. It provides a pixel count to a rectangle around a specified word relative to the window it is in. Sub TestRects() Dim miSelectRectDoc As MODI.Document Dim miSelectRectWord As MODI.Word Dim miSelectRectRects As MODI.MiRects Dim miSelectRectRect As MODI.MiRect Dim strRectInfo As String ' Load an existing TIFF file. Set miSelectRectDoc = New MODI.Document miSelectRectDoc.Create "C:\document1.tif" ' Perform OCR. miSelectRectDoc.Images(0).OCR ' Retrieve and display bounding rectangle information. Set miSelectRectRects = miSelectRectDoc.Images(0).Layout.Words(2).Rects strRectInfo = "Word falls within " & miSelectRectRects.Count & _ " bounding rectangle(s)." & vbCrLf For Each miSelectRectRect In miSelectRectRects strRectInfo = strRectInfo & _ " Rectangle coordinates: " & vbCrLf & _ " - Left: " & miSelectRectRect.Left & vbCrLf & _ " - Right: " & miSelectRectRect.Right & vbCrLf & _ " - Top: " & miSelectRectRect.Top & vbCrLf & _ " - Bottom: " & miSelectRectRect.Bottom Next MsgBox strRectInfo, vbInformation + vbOKOnly, _ "Rectangle Information" Set miSelectRectRect = Nothing Set miSelectRectRects = Nothing Set miSelectRectWord = Nothing Set miSelectRectDoc = Nothing End Sub I already have an OCR function that closely resembles this code. I would like to insert just the MiRect function into it. I get an error when attempting to create the MiRect and MiRects objects: expandcollapse popupFunc OCR_Region($x,$y,$width,$height) ;note $height is NOT the height, it is the Y coordinate bottom right corner, width is bottom right corner X. Dim $miDoc, $Doc Dim $str Dim $oWord Dim $sArray[200] Dim $oMyError Dim $HexNumber Dim $msg Dim $i ;Const $miLANG_CZECH = 5 ;Const $miLANG_DANISH = 6 ;Const $miLANG_DUTCH = 19 Const $miLANG_ENGLISH = 9 ;Const $miLANG_FINNISH = 11 ;Const $miLANG_FRENCH = 12 ;Const $miLANG_GERMAN = 7 ;Const $miLANG_GREEK = 8 ;Const $miLANG_HUNGARIAN = 14 ;Const $miLANG_ITALIAN = 16 ;Const $miLANG_JAPANESE = 17 ;Const $miLANG_KOREAN = 18 ;Const $miLANG_NORWEGIAN = 20 ;Const $miLANG_POLISH = 21 ;Const $miLANG_PORTUGUESE = 22 ;Const $miLANG_RUSSIAN = 25 ;Const $miLANG_SPANISH = 10 ;Const $miLANG_SWEDISH = 29 ;Const $miLANG_TURKISH = 31 ;Const $miLANG_SYSDEFAULT = 2048 ;Const $miLANG_CHINESE_SIMPLIFIED = 2052 ;Const $miLANG_CHINESE_TRADITIONAL = 1028 Local $hBitmap1,$hImage1,$temp $temp=@ScriptDir & "\temp.bmp" _GDIPlus_Startup () ; Capture screen region $hBitmap1 = _ScreenCapture_Capture ("",$x, $y, $width, $height, False) $hImage1 = _GDIPlus_BitmapCreateFromHBITMAP ($hBitmap1) _GDIPlus_ImageSaveToFile ($hImage1, $temp) _GDIPlus_ImageDispose ($hImage1) _WinAPI_DeleteObject ($hBitmap1) $miDoc = ObjCreate("MODI.Document") ;=============================================================================== ;These two statements added for the MiRect function, they both produce an error $miSelectRectRects = ObjCreate("MODI.MiRects") $miSelectRectRect = ObjCreate("MODI.MiRect") ;============================================================================== $miDoc.Create($temp) ;MsgBox(0,"",$temp) ;this is the bitmap image which is "temp.bmp" $miDoc.Ocr($miLANG_ENGLISH, True, False) ;=============================================================================== ;These two statements added for the MiRect function, neither produce an error $miSelectRectRects = $miDoc.Images(0).Layout.Words(1).Rects $Left=$miSelectRectRects.count ;I don need ".Count", but it does something ;============================================================================== $i = 0 For $oWord in $miDoc.Images(0).Layout.Words $str = $str & $oWord.text & @CrLf ;ConsoleWrite($oWord.text & @CRLF) $sArray [$i] = $oWord.text $i += 1 Next FileDelete($temp) Return $sArray EndFunc Thanks for your help Edited October 17, 2022 by AutoitMike Link to comment Share on other sites More sharing options...
Solution Nine Posted October 18, 2022 Solution Share Posted October 18, 2022 You may mean something like this : Local $miRects = $miDoc.Images(0).Layout.Words(1).Rects Local $strRectInfo = "Word falls within " & $miRects.Count & _ " bounding rectangle(s)." & @CRLF For $miRect In $miRects $strRectInfo &= _ " Rectangle coordinates: " & @CRLF & _ " - Left: " & $miRect.Left & @CRLF & _ " - Right: " & $miRect.Right & @CRLF & _ " - Top: " & $miRect.Top & @CRLF & _ " - Bottom: " & $miRect.Bottom Next MsgBox($MB_SYSTEMMODAL, "", $strRectInfo) “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
AutoitMike Posted October 18, 2022 Author Share Posted October 18, 2022 Thanks 9 I will try this out. Link to comment Share on other sites More sharing options...
AutoitMike Posted October 19, 2022 Author Share Posted October 19, 2022 9, Thanks ever so much, It works. I had a little trouble figuring out where to insert the code, which is right after: $miDoc.Ocr($miLANG_ENGLISH, True, False) Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now