Jump to content

Cant trap error in Custom function - (Moved)


AutoitMike
 Share

Go to solution Solved by AutoitMike,

Recommended Posts

I found this OCR code a long time ago, it really work great , When there is text to capture.

The problem is that when there is no text, or the text is too close to the defined boundary, it fails and the script just stops.

The code line that fails is

$miDoc.Ocr($miLANG_ENGLISH, True, False)

The console says:  ==> The requested action with this object has failed.:

This returns an array of captured text

#include <ScreenCapture.au3>
#include <Array.au3>

$Array=OCR_Region(TLCx,TLCy,BRCx,BRCy)          ;Top Left Corner x, Top Left Cornr y, Bottom Right Corner x, Bottom right Corner y

Func 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.
    If 1=2 Then
        Sleep(500)
    EndIf
    $OCRstart=TimerInit()
    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")
        $miDoc.Create($temp)
        ;MsgBox(0,"",$temp);this is the bitmap image which is "temp.bmp"
        $miDoc.Ocr($miLANG_ENGLISH, True, False)   ;THIS LINE FAILS

        $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

I need to trap this error, not sure how.

Thanks

Link to comment
Share on other sites

  • Developers

Moved to the appropriate AutoIt General Help and Support forum, as the Developer General Discussion forum very clearly states:

Quote

General development and scripting discussions.


Do not create AutoIt-related topics here, use the AutoIt General Help and Support or AutoIt Technical Discussion forums.

Moderation Team

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

What kind of error are you trying to trap ?  Have you tried using this :

Local $oMyError = ObjEvent("AutoIt.Error", MyErrFunc)

BTW you can screen capture directly into a file instead of going thru GDI+

Link to comment
Share on other sites

Yes this is what I was talking about.   Place the ObjEvent statement at the beginning of the script.  You should not get the error message anymore.

edit : I am using MODI successfully, so I dont know what could be the problem, unless the file is garbage...

Edited by Nine
Link to comment
Share on other sites

I did  a test script where I provided a "Box" where the screen was blank, no text. The OCR_Region function always failed.

When I provided a "Box" where text exists, it always returns an array of text, and rather quickly. The text is always correct.

There are no "1" confused with lower case L, etc.....

I'll try the trap idea you suggested.

Link to comment
Share on other sites

... you can find that info right out of the CHM help file or searching this site.
And don't declare Globals as Locals.
Bad coder, bad coder ... sit !, good coder. Have a cookie 😜 ( hmm, I may not be good at joking, it was funny in my head )

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

41 minutes ago, AutoitMike said:

I searched the help

#include <ScreenCapture.au3>
#include <Array.au3>

Global $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")

Global $Array = OCR_Region(20, 20, 200, 200)     ;Top Left Corner x, Top Left Cornr y, Bottom Right Corner x, Bottom right Corner y

Func 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.
    If 1 = 2 Then
        Sleep(500)
    EndIf
;~  Local $OCRstart = TimerInit()
    Local $miDoc, $Doc, $str, $oWord, $sArray[200]
;~  Local $oMyError
;~  Local $HexNumber
;~  Local $msg
    Local $i
    ;Const $miLANG_CZECH = 5
    ;Const $miLANG_DANISH = 6
    ;Const $miLANG_DUTCH = 19
    Local 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")
    If @error Then Return SetError(1, 0, "oops") ; if this fails then might as well quit.
    $miDoc.Create($temp)
    ;MsgBox(0,"",$temp);this is the bitmap image which is "temp.bmp"
    $miDoc.Ocr($miLANG_ENGLISH, True, False)       ;THIS LINE FAILS

    $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   ;==>OCR_Region

; User's COM error function. Will be called if COM error occurs
Func _ErrFunc($oError, $iLine = @ScriptLineNumber)
    ; Do anything here. <--- right from the help file
    ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
            @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
            @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
            @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
            @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
            @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
            @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
            @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
            @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
            @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF)
    MsgBox(262144 + 16, StringTrimRight(@ScriptName, 4), "err.scriptline is: " & $oError.scriptline & @CR & "also @ScriptLineNumber is " & $iLine, 30)
EndFunc   ;==>_ErrFunc

 

Edited by argumentum
better code

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...