Advertisement
Guest User

ImageSearch.au3

a guest
Jun 18th, 2012
4,169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 5.61 KB | None | 0 0
  1. #include-once
  2. ; ------------------------------------------------------------------------------
  3. ;
  4. ; AutoIt Version: 3.0
  5. ; Language: English
  6. ; Description: Functions that assist with Image Search
  7. ; Require that the ImageSearchDLL.dll be loadable
  8. ;
  9. ; ------------------------------------------------------------------------------
  10.  
  11. ;===============================================================================
  12. ;
  13. ; Description: Find the position of an image on the desktop
  14. ; Syntax: _ImageSearchArea, _ImageSearch
  15. ; Parameter(s):
  16. ; $findImage - the image to locate on the desktop
  17. ; $tolerance - 0 for no tolerance (0-255). Needed when colors of
  18. ; image differ from desktop. e.g GIF
  19. ; $resultPosition - Set where the returned x,y location of the image is.
  20. ; 1 for centre of image, 0 for top left of image
  21. ; $x $y - Return the x and y location of the image
  22. ; $transparency - TRANSBLACK, TRANSWHITE or hex value (e.g. 0xffffff) of
  23. ; the color to be used as transparency; can be omitted if
  24. ; not needed
  25. ;
  26. ; Return Value(s): On Success - Returns 1
  27. ; On Failure - Returns 0
  28. ;
  29. ; Note: Use _ImageSearch to search the entire desktop, _ImageSearchArea to specify
  30. ; a desktop region to search
  31. ;
  32. ;===============================================================================
  33. Func _ImageSearch($findImage, $resultPosition, ByRef $x, ByRef $y, $tolerance, $transparency = 0)
  34.     Return _ImageSearchArea($findImage, $resultPosition, 0, 0, @DesktopWidth, @DesktopHeight, $x, $y, $tolerance, $transparency)
  35. EndFunc   ;==>_ImageSearch
  36.  
  37. Func _ImageSearchArea($findImage, $resultPosition, $x1, $y1, $right, $bottom, ByRef $x, ByRef $y, $tolerance, $transparency = 0)
  38.     ;MsgBox(0,"asd","" & $x1 & " " & $y1 & " " & $right & " " & $bottom)
  39.     If Not ($transparency = 0) Then $findImage = "*" & $transparency & " " & $findImage
  40.     If $tolerance > 0 Then $findImage = "*" & $tolerance & " " & $findImage
  41.     $result = DllCall("ImageSearchDLL.dll", "str", "ImageSearch", "int", $x1, "int", $y1, "int", $right, "int", $bottom, "str", $findImage)
  42.  
  43.     ; If error exit
  44.     If $result[0] = "0" Then Return 0
  45.  
  46.     ; Otherwise get the x,y location of the match and the size of the image to
  47.     ; compute the centre of search
  48.     $array = StringSplit($result[0], "|")
  49.  
  50.     $x = Int(Number($array[2]))
  51.     $y = Int(Number($array[3]))
  52.     If $resultPosition = 1 Then
  53.         $x = $x + Int(Number($array[4]) / 2)
  54.         $y = $y + Int(Number($array[5]) / 2)
  55.     EndIf
  56.     Return 1
  57. EndFunc   ;==>_ImageSearchArea
  58.  
  59. ;===============================================================================
  60. ;
  61. ; Description: Wait for a specified number of seconds for an image to appear
  62. ;
  63. ; Syntax: _WaitForImageSearch, _WaitForImagesSearch
  64. ; Parameter(s):
  65. ; $waitSecs - seconds to try and find the image
  66. ; $findImage - the image to locate on the desktop
  67. ; $tolerance - 0 for no tolerance (0-255). Needed when colors of
  68. ; image differ from desktop. e.g GIF
  69. ; $resultPosition - Set where the returned x,y location of the image is.
  70. ; 1 for centre of image, 0 for top left of image
  71. ; $x $y - Return the x and y location of the image
  72. ; $transparency - TRANSBLACK, TRANSWHITE or hex value (e.g. 0xffffff) of
  73. ; the color to be used as transparency can be omitted if
  74. ; not needed
  75. ;
  76. ; Return Value(s): On Success - Returns 1
  77. ; On Failure - Returns 0
  78. ;
  79. ;
  80. ;===============================================================================
  81. Func _WaitForImageSearch($findImage, $waitSecs, $resultPosition, ByRef $x, ByRef $y, $tolerance, $transparency = 0)
  82.     $waitSecs = $waitSecs * 1000
  83.     $startTime = TimerInit()
  84.     While TimerDiff($startTime) < $waitSecs
  85.         Sleep(100)
  86.         $result = _ImageSearch($findImage, $resultPosition, $x, $y, $tolerance, $transparency)
  87.         If $result > 0 Then
  88.             Return 1
  89.         EndIf
  90.     WEnd
  91.     Return 0
  92. EndFunc   ;==>_WaitForImageSearch
  93.  
  94. ;===============================================================================
  95. ;
  96. ; Description: Wait for a specified number of seconds for any of a set of
  97. ; images to appear
  98. ;
  99. ; Syntax: _WaitForImagesSearch
  100. ; Parameter(s):
  101. ; $waitSecs - seconds to try and find the image
  102. ; $findImage - the ARRAY of images to locate on the desktop
  103. ; - ARRAY[0] is set to the number of images to loop through
  104. ; ARRAY[1] is the first image
  105. ; $tolerance - 0 for no tolerance (0-255). Needed when colors of
  106. ; image differ from desktop. e.g GIF
  107. ; $resultPosition - Set where the returned x,y location of the image is.
  108. ; 1 for centre of image, 0 for top left of image
  109. ; $x $y - Return the x and y location of the image
  110. ; $transparent - TRANSBLACK, TRANSWHITE or hex value (e.g. 0xffffff) of
  111. ; the color to be used as transparent; can be omitted if
  112. ; not needed
  113. ;
  114. ; Return Value(s): On Success - Returns the index of the successful find
  115. ; On Failure - Returns 0
  116. ;
  117. ;
  118. ;===============================================================================
  119. Func _WaitForImagesSearch($findImage, $waitSecs, $resultPosition, ByRef $x, ByRef $y, $tolerance, $transparency = 0)
  120.     $waitSecs = $waitSecs * 1000
  121.     $startTime = TimerInit()
  122.     While TimerDiff($startTime) < $waitSecs
  123.         For $i = 1 To $findImage[0]
  124.             Sleep(100)
  125.             $result = _ImageSearch($findImage[$i], $resultPosition, $x, $y, $tolerance, $transparency)
  126.             If $result > 0 Then
  127.                 Return $i
  128.             EndIf
  129.         Next
  130.     WEnd
  131.     Return 0
  132. EndFunc   ;==>_WaitForImagesSearch
  133.  
  134. ;~ ; find recycle bin if it is in the top left corner of screen
  135. ;~ ; change 2nd argument to 0 to return the top left coord instead
  136. ;~ $result = _ImageSearchArea("recycle.bmp", 1, 0, 0, 200, 200, $x1, $y1, 0, 0x000000) ;perfect black used as transparency
  137. ;~ If $result = 1 Then
  138. ;~  MouseMove($x1, $y1, 3)
  139. ;~  MsgBox(0, "Found", "Found a recycle bin with stuff in top left corner")
  140. ;~ EndIf
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement