Jump to content

ImageSearch Help


 Share

Recommended Posts

This is going to be my personal Help location until I complete my script and here is the first (of more than likely many) topic/question. Hopefully my coding niceties have improved somewhat since I last posted code on this forum.

Script ::

; Start of the Script Code

; Stuff that was taken from the ImageSearch.au3 file to make this thing not throw DllCall Error = 1 even when I had included ImageSearch.au3 file Not sure what it all does YET but it fixed my problem so whatever for now.
#RequireAdmin
#AutoIt3Wrapper_UseX64=y
#include <WinAPIFiles.au3> ; for _WinAPI_Wow64EnableWow64FsRedirection
#include <Misc.au3>

If Not FileExists("ImageSearchDLLx32.dll") Then FileInstall("ImageSearchDLLx32.dll", "ImageSearchDLLx32.dll", 1) ;FileInstall ( "source", "dest" [, flag = 0] )
If Not FileExists("ImageSearchDLLx64.dll") Then FileInstall("ImageSearchDLLx64.dll", "ImageSearchDLLx64.dll", 1)
If Not FileExists("msvcr110d.dll") Then FileInstall("msvcr110d.dll", "msvcr110d.dll", 1) ;Microsoft Visual C++ Redistributable dll x64
If Not FileExists("msvcr110.dll") Then FileInstall("msvcr110.dll", "msvcr110.dll", 1) ;Microsoft Visual C++ Redistributable dll x32

Local $h_ImageSearchDLL = -1
; End Copy and Pasted stuff

; Sets the location of the image files it is a nice workaround for when you do not want your main script area flooded with hundreds of pictures.
Global $imgloc = @ScriptDir & "/Search/Images/Default/"
; Sets the variables for the X and Y coords of the icon to blank for use by the Start() function
Global $IconX, $IconY
; Sets how the mouse reads coords 1 = absolute coords meaning the entire screen area
Opt("MouseCoordMode", 1)

; INI File Settings

; $Variable = Reads From INI file (At where the script is located and then add a \ then add the File Name of the INI, Sets the section name which in the INI file is surrounded by [ ], Sets the Key name in the INI file and then adds a = without spaces, Sets the DEFAULT Key Value to the variable inputed for HotKeys it must be surrounded by { } for Integers just the number will suffice For $Variables I would guess that it takes the $Variable parameter and sends that to the INI file, never tried it)

; [Hotkey Settings]
$hkplay = IniRead(@ScriptDir = "\Settings.ini", "Hotkey Settings", "Play", "{F1}")
$hkstop = IniRead(@ScriptDir = "\Settings.ini", "Hotkey Settings", "Stop", "{F2}")
$hkrefresh = IniRead(@ScriptDir = "\Settings.ini", "Hotkey Settings", "Refresh", "{F3}")

; [Speed Settings]
$mSpeed = IniRead(@ScriptDir & "\Settings.ini", "Speed Settings", "Mouse Movement Speed", "4")
$cDelay = IniRead(@ScriptDir & "\Settings.ini", "Speed Settings", "Mouse Click Delay", "100")
$mDelay = IniRead(@ScriptDir & "\Settings.ini", "Speed Settings", "Mouse Movement Delay", "100")

; [Image Settings]
$fImg2 = IniRead(@ScriptDir & "\Settings.ini", "Image Settings", "Icon", "firefox.bmp")

; End INI File Settings

; HotKeySet Settings

; Sets the Hotkey for the Start Function as defined in the INI file by a key surrounded by { } or via the defualt key set by the above " ; INI Settings
HotKeySet($hkplay, "Start")
; Sets the Hotkey for the Stop Function as defined in the INI file by a key surrounded by { } or via the defualt key set by the above " ; INI Settings
HotKeySet($hkstop, "Stop")
; Sets the Hotkey for the RefreshSettings Function as defined in the INI file by a key surrounded by { } or via the defualt key set by the above " ; INI Settings
HotKeySet($hkrefresh, "RefreshSettings")

; End HotKeySet Settings

; While the script is started sleep for 100 ms then restart the while loop too keep the script running

While 1
    Sleep(100)
WEnd

; End of keeping the code running section

; Begin main Functions

; Names the Function Start() to be called upon by anything else in the script
Func Start()
    ; Search for the image from the script directory sub-folders and returns the X and Y value of said image when found
    $1a = _ImageSearch($imgloc & $fImg2, 1, $IconX, $IconY, 25, 0)
    ; If image is found then do below code
    If $1a = 1 Then
        ; Move the mouse to the X and Y Coords of where the image of the icon was found at the speed which was set in the INI file or If it was not set at the default speed of 4
        MouseMove($IconX, $IconY, $mSpeed)
        ; Random tooltip confirming that it did complete the required code above not really needed but I like visuals to confirm things
        ToolTip("Found Icon and Moved Mouse to Location", 0, 0)
    ; If Image was not found then do the below code
    Else
        ; My nice little info box that tells me what went wrong helped me to figure out a DllCall Error = 1 when $la returned as such. Now that it does not do that I changed it to the new first line
        MsgBox(0, "Error", "IconX and/or IconY did not change when you moved the icon!" & @CRLF & "Image Location : " & $imgloc & @CRLF & "Image Name : " & $fImg2 & @CRLF & "X position : " & $IconX & @CRLF & "Y Position : " & $IconY & @CRLF & "Found Image : " & $1a)
    ; Ends the If $1a statement
    EndIf
; Ends the Start() function
EndFunc   ;==>Start


; Names the Function RefreshSettings() to be called upon by anything else in the script
Func RefreshSettings()
    ; $Variable = Reads From INI file (At where the script is located and then add a \ then add the File Name of the INI, Sets the Section name which in the INI file is surrounded by [ ], Sets the Key name in the INI file under the Section name and then adds a = without spaces on either side, Sets the Key Value without spaces on either side to the variable inputed in the INI. For HotKeys it must be surrounded by { } for Integers just the number will suffice For $Variables I would guess that it takes the $Variable parameter and sends that to the INI file, never tried it)

    ; [Hotkey Settings]
    $hkplay = IniRead(@ScriptDir = "\Settings.ini", "Hotkey Settings", "Play", "")
    $hkstop = IniRead(@ScriptDir = "\Settings.ini", "Hotkey Settings", "Stop", "")
    $hkrefresh = IniRead(@ScriptDir = "\Settings.ini", "Hotkey Settings", "Refresh", "")

    ; [Speed Settings]
    $mSpeed = IniRead(@ScriptDir & "\Settings.ini", "Speed Settings", "Mouse Movement Speed", "")
    $mDelay = IniRead(@ScriptDir & "\Settings.ini", "Speed Settings", "Mouse Movement Delay", "")
    $cDelay = IniRead(@ScriptDir & "\Settings.ini", "Speed Settings", "Mouse Click Delay", "")

    ;[Image Settings]
    $fImg2 = IniRead(@ScriptDir & "\Settings.ini", "Image Settings", "Icon", "")
; Ends the RefreshSettings() Function
EndFunc   ;==>RefreshSettings

; Names the Function Stop() to be called upon by anything else in the script
Func Stop()
    ; Tells the script its time to shutdown, If a dialog box made by the script is active and you try to close/stop the script the script will close only after the dialog box closes
    Exit
; Ends the Stop() Function
EndFunc   ;==>Stop

; End Main Functions

; Start more Copy and Pasted stuff to prevent that DllCall Error = 1

;--------------------------------------
; Image Search Stuff (no Documentation)
;--------------------------------------
Func _ImageSearch($findImage, $resultPosition, ByRef $x, ByRef $y, $tolerance, $transparency)
    Return _ImageSearchArea($findImage, $resultPosition, 0, 0, @DesktopWidth, @DesktopHeight, $x, $y, $tolerance, $transparency)
EndFunc   ;==>_ImageSearch

Func _ImageSearchArea($findImage, $resultPosition, $x1, $y1, $right, $bottom, ByRef $x, ByRef $y, $tolerance = 0, $transparency = 0) ;Credits to Sven for the Transparency addition
    If Not FileExists($findImage) Then Return "Image File not found"
    If $tolerance < 0 Or $tolerance > 255 Then $tolerance = 0
    If $h_ImageSearchDLL = -1 Then _ImageSearchStartup()

    If $transparency <> 0 Then $findImage = "*" & $transparency & " " & $findImage
    If $tolerance > 0 Then $findImage = "*" & $tolerance & " " & $findImage
    $result = DllCall($h_ImageSearchDLL, "str", "ImageSearch", "int", $x1, "int", $y1, "int", $right, "int", $bottom, "str", $findImage)
    If @error Then Return "DllCall Error=" & @error
    If $result = "0" Or Not IsArray($result) Or $result[0] = "0" Then Return False

    $array = StringSplit($result[0], "|")
    If (UBound($array) >= 4) Then
        $x = Int(Number($array[2])) ; Get the x, y location of the match
        $y = Int(Number($array[3]))
        If $resultPosition = 1 Then
            $x = $x + Int(Number($array[4]) / 2) ; Account for the size of the image to compute the centre of search
            $y = $y + Int(Number($array[5]) / 2)
        EndIf
        Return True
    EndIf
EndFunc   ;==>_ImageSearchArea

Func _WaitForImageSearch($findImage, $waitSecs, $resultPosition, ByRef $x, ByRef $y, $tolerance, $transparency = 0)
    $waitSecs = $waitSecs * 1000
    $startTime = TimerInit()
    While TimerDiff($startTime) < $waitSecs
        Sleep(100)
        If _ImageSearch($findImage, $resultPosition, $x, $y, $tolerance, $transparency) Then
            Return True
        EndIf
    WEnd
    Return False
EndFunc   ;==>_WaitForImageSearch

Func _WaitForImagesSearch($findImage, $waitSecs, $resultPosition, ByRef $x, ByRef $y, $tolerance, $transparency = 0)
    $waitSecs = $waitSecs * 1000
    $startTime = TimerInit()
    While TimerDiff($startTime) < $waitSecs
        For $i = 1 To $findImage[0]
            Sleep(100)
            If _ImageSearch($findImage[$i], $resultPosition, $x, $y, $tolerance, $transparency) Then
                Return $i
            EndIf
        Next
    WEnd
    Return False
EndFunc   ;==>_WaitForImagesSearch

Func _ImageSearchStartup()
    _WinAPI_Wow64EnableWow64FsRedirection(True)
    $sOSArch = @OSArch ;Check if running on x64 or x32 Windows ;@OSArch Returns one of the following: "X86", "IA64", "X64" - this is the architecture type of the currently running operating system.
    $sAutoItX64 = @AutoItX64 ;Check if using x64 AutoIt ;@AutoItX64 Returns 1 if the script is running under the native x64 version of AutoIt.
    If $sOSArch = "X86" Or $sAutoItX64 = 0 Then
        cr("+>" & "@OSArch=" & $sOSArch & @TAB & "@AutoItX64=" & $sAutoItX64 & @TAB & "therefore using x32 ImageSearch DLL")
        $h_ImageSearchDLL = DllOpen("ImageSearchDLLx32.dll")
        If $h_ImageSearchDLL = -1 Then Return "DllOpen failure"
    ElseIf $sOSArch = "X64" And $sAutoItX64 = 1 Then
        cr("+>" & "@OSArch=" & $sOSArch & @TAB & "@AutoItX64=" & $sAutoItX64 & @TAB & "therefore using x64 ImageSearch DLL")
        $h_ImageSearchDLL = DllOpen("ImageSearchDLLx64.dll")
        If $h_ImageSearchDLL = -1 Then Return "DllOpen failure"
    Else
        Return "Inconsistent or incompatible Script/Windows/CPU Architecture"
    EndIf
    Return True
EndFunc   ;==>_ImageSearchStartup

Func _ImageSearchShutdown()
    DllClose($h_ImageSearchDLL)
    _WinAPI_Wow64EnableWow64FsRedirection(False)
    cr(">" & "_ImageSearchShutdown() completed")
    Return True
EndFunc   ;==>_ImageSearchShutdown

Func cr($text = "", $addCR = 1, $printTime = False) ;Print to console
    Static $sToolTip
    If Not @Compiled Then
        If $printTime Then ConsoleWrite(@HOUR & ":" & @MIN & ":" & @SEC & ":" & @MSEC & " ")
        ConsoleWrite($text)
        If $addCR >= 1 Then ConsoleWrite(@CR)
        If $addCR = 2 Then ConsoleWrite(@CR)
    Else
        If $printTime Then $sToolTip &= @HOUR & ":" & @MIN & ":" & @SEC & ":" & @MSEC & " "
        $sToolTip &= $text
        If $addCR >= 1 Then $sToolTip &= @CR
        If $addCR = 2 Then $sToolTip &= @CR
        ToolTip($sToolTip)
    EndIf
    Return $text
EndFunc   ;==>cr
; End of the last of the Copy and Pasted stuff

; End of the Script

INI File ::

[Hotkey Settings]
Play={F1}
Stop={F2}
Refresh={F3}

[Speed Settings]
Mouse Movement Speed=4
Mouse Movement Delay=100
Mouse Click Delay=100

[Image Settings]
Icon=firefox.bmp

Here is the first problem of more than likely many problems yet to come. Sometimes when I manually move the icon while the script is running (for testing purposes I used firefox) my nice little message box pops up and shows me that the X and Y coords of the icon did not change after the icon was moved and the script started again by pressing F1. Is there a way to reset the X and Y coords every time I call Start() I know the $1a is supposed to do this but apparently it is not 100%, and I do not understand why.

Any help is greatly appreciated as always.  I will try any suggestions to forcibly make the script forcibly have $1a search again for new coords after finding it once already.

Edited by xxaviarxx
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...