Advertisement
Guest User

Tesseract.au3

a guest
Jul 13th, 2017
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 41.60 KB | None | 0 0
  1. #include-once
  2. #Include <Array.au3>
  3. #Include <File.au3>
  4. #include <GDIPlus.au3>
  5. #include <ScreenCapture.au3>
  6. #include <WinAPI.au3>
  7. #include <ScrollBarConstants.au3>
  8. #include <WindowsConstants.au3>
  9. #Include <GuiComboBox.au3>
  10. #Include <GuiListBox.au3>
  11. #Region Header
  12. #cs
  13.     Title:          Tesseract UDF Library for AutoIt3
  14.     Filename:       Tesseract.au3
  15.     Description:    A collection of functions for capturing text in applications.
  16.     Author:         seangriffin
  17.     Version:        V0.6
  18.     Last Update:    17/03/09
  19.     Requirements:   AutoIt3 3.2 or higher,
  20.                     Tesseract 2.01.
  21.     Changelog:      ---------15/02/09---------- v0.1
  22.                     Initial release.
  23.  
  24.                     ---------15/02/09---------- v0.2
  25.                     Changed path to tesseract.exe to @ProgramFilesDir.
  26.                     Added scaling as input to _TesseractCapture.
  27.                     Fixed indentation.
  28.                     Changed CaptureHWNDToTIFF to input window and control IDs.
  29.  
  30.                     ---------16/02/09---------- v0.3
  31.                     Added the parameter $get_last_capture to _TesseractCapture.
  32.                     Added the parameter $show_capture to _TesseractCapture.
  33.  
  34.                     ---------16/02/09---------- v0.4
  35.                     Added the function _TesseractFind.
  36.  
  37.                     ---------21/02/09---------- v0.5
  38.                     Updated _TesseractCapture to remove a listbox selection entirely,
  39.                         and return it after the text capture is done.
  40.  
  41.                     ---------17/03/09---------- v0.6
  42.                     Split the function "_TesseractCapture" into 3 functions:
  43.                         _TesseractScreenCapture
  44.                         _TesseractWinCapture
  45.                         _TesseractControlCapture
  46.                     Split the function "_TesseractFind" into 3 functions:
  47.                         _TesseractScreenFind
  48.                         _TesseractWinFind
  49.                         _TesseractControlFind
  50.                     Renamed the function "CaptureHWNDToTIFF" to "CaptureToTIFF",
  51.                         and modified it to allow for handling of the screen, windows
  52.                         and controls.
  53.                     Added the function "_TesseractTempPathSet".
  54. #ce
  55. #EndRegion Header
  56. #Region Global Variables and Constants
  57. Global $last_capture
  58. Global $tesseract_temp_path = "C:\"
  59. #EndRegion Global Variables and Constants
  60. #Region Core functions
  61. ; #FUNCTION# ;===============================================================================
  62. ;
  63. ; Name...........:  _TesseractTempPathSet()
  64. ; Description ...:  Sets the location where Tesseract functions temporary store their files.
  65. ;                       You must have read and write access to this location.
  66. ;                       The default location is "C:\".
  67. ; Syntax.........:  _TesseractTempPathSet($temp_path)
  68. ; Parameters ....:  $temp_path  - The path to use for temporary file storage.
  69. ;                                   This path must not contain any spaces (see "Remarks" below).
  70. ; Return values .:  On Success  - Returns 1.
  71. ;                   On Failure  - Returns 0.
  72. ; Author ........:  seangriffin
  73. ; Modified.......:
  74. ; Remarks .......:  The current version of Tesseract doesn't support paths with spaces.
  75. ; Related .......:
  76. ; Link ..........:
  77. ; Example .......:  No
  78. ;
  79. ; ;==========================================================================================
  80. func _TesseractTempPathSet($temp_path)
  81.  
  82.     $tesseract_temp_path = $temp_path
  83.  
  84.     Return 1
  85. EndFunc
  86.  
  87. ; #FUNCTION# ;===============================================================================
  88. ;
  89. ; Name...........:  _TesseractScreenCapture()
  90. ; Description ...:  Captures text from the screen.
  91. ; Syntax.........:  _TesseractScreenCapture($get_last_capture = 0, $delimiter = "", $cleanup = 1, $scale = 2, $left_indent = 0, $top_indent = 0, $right_indent = 0, $bottom_indent = 0, $show_capture = 0)
  92. ; Parameters ....:  $get_last_capture   - Retrieve the text of the last capture, rather than
  93. ;                                           performing another capture.  Useful if the text in
  94. ;                                           the window or control hasn't changed since the last capture.
  95. ;                                           0 = do not retrieve the last capture (default)
  96. ;                                           1 = retrieve the last capture
  97. ;                   $delimiter          - Optional: The string that delimits elements in the text.
  98. ;                                           A string of text will be returned if this isn't provided.
  99. ;                                           An array of delimited text will be returned if this is provided.
  100. ;                                           Eg. Use @CRLF to return the items of a listbox as an array.
  101. ;                   $cleanup            - Optional: Remove invalid text recognised
  102. ;                                           0 = do not remove invalid text
  103. ;                                           1 = remove invalid text (default)
  104. ;                   $scale              - Optional: The scaling factor of the screenshot prior to text recognition.
  105. ;                                           Increase this number to improve accuracy.
  106. ;                                           The default is 2.
  107. ;                   $left_indent        - A number of pixels to indent the capture from the
  108. ;                                           left of the screen.
  109. ;                   $top_indent         - A number of pixels to indent the capture from the
  110. ;                                           top of the screen.
  111. ;                   $right_indent       - A number of pixels to indent the capture from the
  112. ;                                           right of the screen.
  113. ;                   $bottom_indent      - A number of pixels to indent the capture from the
  114. ;                                           bottom of the screen.
  115. ;                   $show_capture       - Display screenshot and text captures
  116. ;                                           (for debugging purposes).
  117. ;                                           0 = do not display the screenshot taken (default)
  118. ;                                           1 = display the screenshot taken and exit
  119. ; Return values .:  On Success  - Returns an array of text that was captured.
  120. ;                   On Failure  - Returns an empty array.
  121. ; Author ........:  seangriffin
  122. ; Modified.......:
  123. ; Remarks .......:  Use the default values for first time use.  If the text recognition accuracy is low,
  124. ;                   I suggest setting $show_capture to 1 and rerunning.  If the screenshot of the
  125. ;                   window or control includes borders or erroneous pixels that may interfere with
  126. ;                   the text recognition process, then use $left_indent, $top_indent, $right_indent and
  127. ;                   $bottom_indent to adjust the portion of the screen being captured, to
  128. ;                   exclude these non-textural elements.
  129. ;                   If text accuracy is still low, increase the $scale parameter.  In general, the higher
  130. ;                   the scale the clearer the font and the more accurate the text recognition.
  131. ; Related .......:
  132. ; Link ..........:
  133. ; Example .......:  No
  134. ;
  135. ; ;==========================================================================================
  136. func _TesseractScreenCapture($get_last_capture = 0, $delimiter = "", $cleanup = 1, $scale = 2, $left_indent = 0, $top_indent = 0, $right_indent = 0, $bottom_indent = 0, $show_capture = 0)
  137.  
  138.     Local $tInfo
  139.     dim $aArray, $final_ocr[1], $xyPos_old = -1, $capture_scale = 3
  140.     Local $tSCROLLINFO = DllStructCreate($tagSCROLLINFO)
  141.     DllStructSetData($tSCROLLINFO, "cbSize", DllStructGetSize($tSCROLLINFO))
  142.     DllStructSetData($tSCROLLINFO, "fMask", $SIF_ALL)
  143.  
  144.     if $last_capture = "" Then
  145.  
  146.         $last_capture = ObjCreate("Scripting.Dictionary")
  147.     EndIf
  148.  
  149.     ; if last capture is requested, and one exists.
  150.     if $get_last_capture = 1 and $last_capture.item(0) <> "" Then
  151.  
  152.         return $last_capture.item(0)
  153.     EndIf
  154.  
  155.     $capture_filename = _TempFile($tesseract_temp_path, "~", ".tif")
  156.     $ocr_filename = StringLeft($capture_filename, StringLen($capture_filename) - 4)
  157.     $ocr_filename_and_ext = $ocr_filename & ".txt"
  158.  
  159.     CaptureToTIFF("", "", "", $capture_filename, $scale, $left_indent, $top_indent, $right_indent, $bottom_indent)
  160.  
  161.     ;ShellExecuteWait(@ProgramFilesDir & "\Tesseract-OCR\tesseract.exe", $capture_filename & " " & $ocr_filename)
  162.     ShellExecuteWait("C:Program Files (x86)\Tesseract-OCR\tesseract.exe", $capture_filename & " " & $ocr_filename, "", "", @SW_HIDE)
  163.  
  164.  
  165.     ; If no delimter specified, then return a string
  166.     if StringCompare($delimiter, "") = 0 Then
  167.  
  168.         $final_ocr = FileRead($ocr_filename_and_ext)
  169.     Else
  170.  
  171.         _FileReadToArray($ocr_filename_and_ext, $aArray)
  172.         _ArrayDelete($aArray, 0)
  173.  
  174.         ; Append the recognised text to a final array
  175.         _ArrayConcatenate($final_ocr, $aArray)
  176.     EndIf
  177.  
  178.     ; If the captures are to be displayed
  179.     if $show_capture = 1 Then
  180.  
  181.         GUICreate("Tesseract Screen Capture.  Note: image displayed is not to scale", 640, 480, 0, 0, $WS_SIZEBOX + $WS_SYSMENU)  ; will create a dialog box that when displayed is centered
  182.  
  183.         GUISetBkColor(0xE0FFFF)
  184.  
  185.         $Obj1 = ObjCreate("Preview.Preview.1")
  186.         $Obj1_ctrl = GUICtrlCreateObj($Obj1, 0, 0, 640, 480)
  187.         $Obj1.ShowFile ($capture_filename, 1)
  188.  
  189.         GUISetState()
  190.  
  191.         if IsArray($final_ocr) Then
  192.  
  193.             _ArrayDisplay($aArray, "Tesseract Text Capture")
  194.         Else
  195.  
  196.             MsgBox(0, "Tesseract Text Capture", $final_ocr)
  197.         EndIf
  198.  
  199.         GUIDelete()
  200.     EndIf
  201.  
  202.     FileDelete($ocr_filename & ".*")
  203.  
  204.     ; Cleanup
  205.     if IsArray($final_ocr) And $cleanup = 1 Then
  206.  
  207.         ; Cleanup the items
  208.         for $final_ocr_num = 1 to (UBound($final_ocr)-1)
  209.  
  210.             ; Remove erroneous characters
  211.             $final_ocr[$final_ocr_num] = StringReplace($final_ocr[$final_ocr_num], ".", "")
  212.             $final_ocr[$final_ocr_num] = StringReplace($final_ocr[$final_ocr_num], "'", "")
  213.             $final_ocr[$final_ocr_num] = StringReplace($final_ocr[$final_ocr_num], ",", "")
  214.             $final_ocr[$final_ocr_num] = StringStripWS($final_ocr[$final_ocr_num], 3)
  215.         Next
  216.  
  217.         ; Remove duplicate and blank items
  218.         for $each in $final_ocr
  219.  
  220.             $found_item = _ArrayFindAll($final_ocr, $each)
  221.  
  222.             ; Remove blank items
  223.             if IsArray($found_item) Then
  224.                 if StringCompare($final_ocr[$found_item[0]], "") = 0 Then
  225.  
  226.                     _ArrayDelete($final_ocr, $found_item[0])
  227.                 EndIf
  228.             EndIf
  229.  
  230.             ; Remove duplicate items
  231.             for $found_item_num = 2 to UBound($found_item)
  232.  
  233.                 _ArrayDelete($final_ocr, $found_item[$found_item_num-1])
  234.             Next
  235.         Next
  236.     EndIf
  237.  
  238.     ; Store a copy of the capture
  239.     if $last_capture.item(0) = "" Then
  240.  
  241.         $last_capture.item(0) = $final_ocr
  242.     EndIf
  243.  
  244.     Return $final_ocr
  245. EndFunc
  246.  
  247. ; #FUNCTION# ;===============================================================================
  248. ;
  249. ; Name...........:  _TesseractWinCapture()
  250. ; Description ...:  Captures text from a window.
  251. ; Syntax.........:  _TesseractWinCapture($win_title, $win_text = "", $get_last_capture = 0, $delimiter = "", $cleanup = 1, $scale = 2, $left_indent = 0, $top_indent = 0, $right_indent = 0, $bottom_indent = 0, $show_capture = 0)
  252. ; Parameters ....:  $win_title          - The title of the window to capture text from.
  253. ;                   $win_text           - Optional: The text of the window to capture text from.
  254. ;                   $get_last_capture   - Retrieve the text of the last capture, rather than
  255. ;                                           performing another capture.  Useful if the text in
  256. ;                                           the window or control hasn't changed since the last capture.
  257. ;                                           0 = do not retrieve the last capture (default)
  258. ;                                           1 = retrieve the last capture
  259. ;                   $delimiter          - Optional: The string that delimits elements in the text.
  260. ;                                           A string of text will be returned if this isn't provided.
  261. ;                                           An array of delimited text will be returned if this is provided.
  262. ;                                           Eg. Use @CRLF to return the items of a listbox as an array.
  263. ;                   $cleanup            - Optional: Remove invalid text recognised
  264. ;                                           0 = do not remove invalid text
  265. ;                                           1 = remove invalid text (default)
  266. ;                   $scale              - Optional: The scaling factor of the screenshot prior to text recognition.
  267. ;                                           Increase this number to improve accuracy.
  268. ;                                           The default is 2.
  269. ;                   $left_indent        - A number of pixels to indent the capture from the
  270. ;                                           left of the window.
  271. ;                   $top_indent         - A number of pixels to indent the capture from the
  272. ;                                           top of the window.
  273. ;                   $right_indent       - A number of pixels to indent the capture from the
  274. ;                                           right of the window.
  275. ;                   $bottom_indent      - A number of pixels to indent the capture from the
  276. ;                                           bottom of the window.
  277. ;                   $show_capture       - Display screenshot and text captures
  278. ;                                           (for debugging purposes).
  279. ;                                           0 = do not display the screenshot taken (default)
  280. ;                                           1 = display the screenshot taken and exit
  281. ; Return values .:  On Success  - Returns an array of text that was captured.
  282. ;                   On Failure  - Returns an empty array.
  283. ; Author ........:  seangriffin
  284. ; Modified.......:
  285. ; Remarks .......:  Use the default values for first time use.  If the text recognition accuracy is low,
  286. ;                   I suggest setting $show_capture to 1 and rerunning.  If the screenshot of the
  287. ;                   window or control includes borders or erroneous pixels that may interfere with
  288. ;                   the text recognition process, then use $left_indent, $top_indent, $right_indent and
  289. ;                   $bottom_indent to adjust the portion of the window being captured, to
  290. ;                   exclude these non-textural elements.
  291. ;                   If text accuracy is still low, increase the $scale parameter.  In general, The higher
  292. ;                   the scale the clearer the font and the more accurate the text recognition.
  293. ; Related .......:
  294. ; Link ..........:
  295. ; Example .......:  No
  296. ;
  297. ; ;==========================================================================================
  298. func _TesseractWinCapture($win_title, $win_text = "", $get_last_capture = 0, $delimiter = "", $cleanup = 1, $scale = 2, $left_indent = 0, $top_indent = 0, $right_indent = 0, $bottom_indent = 0, $show_capture = 0)
  299.  
  300.     Local $tInfo
  301.     dim $aArray, $final_ocr[1], $xyPos_old = -1, $capture_scale = 3
  302.     Local $tSCROLLINFO = DllStructCreate($tagSCROLLINFO)
  303.     DllStructSetData($tSCROLLINFO, "cbSize", DllStructGetSize($tSCROLLINFO))
  304.     DllStructSetData($tSCROLLINFO, "fMask", $SIF_ALL)
  305.  
  306.     if $last_capture = "" Then
  307.  
  308.         $last_capture = ObjCreate("Scripting.Dictionary")
  309.     EndIf
  310.  
  311.     $hwnd = WinGetHandle($win_title, $win_text)
  312.  
  313.     ; if last capture is requested, and one exists.
  314.     if $get_last_capture = 1 and $last_capture.item(Number($hwnd)) <> "" Then
  315.  
  316.         return $last_capture.item(Number($hwnd))
  317.     EndIf
  318.  
  319.     ; Perform the text recognition
  320.  
  321.     $capture_filename = _TempFile($tesseract_temp_path, "~", ".tif")
  322.     $ocr_filename = StringLeft($capture_filename, StringLen($capture_filename) - 4)
  323.     $ocr_filename_and_ext = $ocr_filename & ".txt"
  324.  
  325.     CaptureToTIFF($win_title, $win_text, "", $capture_filename, $scale, $left_indent, $top_indent, $right_indent, $bottom_indent)
  326.  
  327.     ; ShellExecuteWait(@ProgramFilesDir & "\Tesseract-OCR\tesseract.exe", $capture_filename & " " & $ocr_filename)
  328.     ShellExecuteWait("C:Program Files (x86)\Tesseract-OCR\tesseract.exe", $capture_filename & " " & $ocr_filename, "", "", @SW_HIDE)
  329.     ; If no delimter specified, then return a string
  330.     if StringCompare($delimiter, "") = 0 Then
  331.  
  332.         $final_ocr = FileRead($ocr_filename_and_ext)
  333.     Else
  334.  
  335.         _FileReadToArray($ocr_filename_and_ext, $aArray)
  336.         _ArrayDelete($aArray, 0)
  337.  
  338.         ; Append the recognised text to a final array
  339.         _ArrayConcatenate($final_ocr, $aArray)
  340.     EndIf
  341.  
  342.     ; If the captures are to be displayed
  343.     if $show_capture = 1 Then
  344.  
  345.         GUICreate("Tesseract Screen Capture.  Note: image displayed is not to scale", 640, 480, 0, 0, $WS_SIZEBOX + $WS_SYSMENU)  ; will create a dialog box that when displayed is centered
  346.  
  347.         GUISetBkColor(0xE0FFFF)
  348.  
  349.         $Obj1 = ObjCreate("Preview.Preview.1")
  350.         $Obj1_ctrl = GUICtrlCreateObj($Obj1, 0, 0, 640, 480)
  351.         $Obj1.ShowFile ($capture_filename, 1)
  352.  
  353.         GUISetState()
  354.  
  355.         if IsArray($final_ocr) Then
  356.  
  357.             _ArrayDisplay($aArray, "Tesseract Text Capture")
  358.         Else
  359.  
  360.             MsgBox(0, "Tesseract Text Capture", $final_ocr)
  361.         EndIf
  362.  
  363.         GUIDelete()
  364.     EndIf
  365.  
  366.     FileDelete($ocr_filename & ".*")
  367.  
  368.     ; Cleanup
  369.     if IsArray($final_ocr) And $cleanup = 1 Then
  370.  
  371.         ; Cleanup the items
  372.         for $final_ocr_num = 1 to (UBound($final_ocr)-1)
  373.  
  374.             ; Remove erroneous characters
  375.             $final_ocr[$final_ocr_num] = StringReplace($final_ocr[$final_ocr_num], ".", "")
  376.             $final_ocr[$final_ocr_num] = StringReplace($final_ocr[$final_ocr_num], "'", "")
  377.             $final_ocr[$final_ocr_num] = StringReplace($final_ocr[$final_ocr_num], ",", "")
  378.             $final_ocr[$final_ocr_num] = StringStripWS($final_ocr[$final_ocr_num], 3)
  379.         Next
  380.  
  381.         ; Remove duplicate and blank items
  382.         for $each in $final_ocr
  383.  
  384.             $found_item = _ArrayFindAll($final_ocr, $each)
  385.  
  386.             ; Remove blank items
  387.             if IsArray($found_item) Then
  388.                 if StringCompare($final_ocr[$found_item[0]], "") = 0 Then
  389.  
  390.                     _ArrayDelete($final_ocr, $found_item[0])
  391.                 EndIf
  392.             EndIf
  393.  
  394.             ; Remove duplicate items
  395.             for $found_item_num = 2 to UBound($found_item)
  396.  
  397.                 _ArrayDelete($final_ocr, $found_item[$found_item_num-1])
  398.             Next
  399.         Next
  400.     EndIf
  401.  
  402.     ; Store a copy of the capture
  403.     if $last_capture.item(Number($hwnd)) = "" Then
  404.  
  405.         $last_capture.item(Number($hwnd)) = $final_ocr
  406.     EndIf
  407.  
  408.     Return $final_ocr
  409. EndFunc
  410.  
  411. ; #FUNCTION# ;===============================================================================
  412. ;
  413. ; Name...........:  _TesseractControlCapture()
  414. ; Description ...:  Captures text from a control.
  415. ; Syntax.........:  _TesseractControlCapture($win_title, $win_text = "", $ctrl_id = "", $get_last_capture = 0, $delimiter = "", $expand = 1, $scrolling = 1, $cleanup = 1, $max_scroll_times = 5, $scale = 2, $left_indent = 0, $top_indent = 0, $right_indent = 0, $bottom_indent = 0, $show_capture = 0)
  416. ; Parameters ....:  $win_title          - The title of the window to capture text from.
  417. ;                   $win_text           - Optional: The text of the window to capture text from.
  418. ;                   $ctrl_id            - Optional: The ID of the control to capture text from.
  419. ;                                           The text of the window will be returned if one isn't provided.
  420. ;                   $get_last_capture   - Retrieve the text of the last capture, rather than
  421. ;                                           performing another capture.  Useful if the text in
  422. ;                                           the window or control hasn't changed since the last capture.
  423. ;                                           0 = do not retrieve the last capture (default)
  424. ;                                           1 = retrieve the last capture
  425. ;                   $delimiter          - Optional: The string that delimits elements in the text.
  426. ;                                           A string of text will be returned if this isn't provided.
  427. ;                                           An array of delimited text will be returned if this is provided.
  428. ;                                           Eg. Use @CRLF to return the items of a listbox as an array.
  429. ;                   $expand             - Optional: Expand the control before capturing text from it?
  430. ;                                           0 = do not expand the control
  431. ;                                           1 = expand the control (default)
  432. ;                   $scrolling          - Optional: Scroll the control to capture all it's text?
  433. ;                                           0 = do not scroll the control
  434. ;                                           1 = scroll the control (default)
  435. ;                   $cleanup            - Optional: Remove invalid text recognised
  436. ;                                           0 = do not remove invalid text
  437. ;                                           1 = remove invalid text (default)
  438. ;                   $max_scroll_times   - The maximum number of scrolls to capture in a control
  439. ;                                           If a control has a very long scroll bar, the text recognition
  440. ;                                           process will take too long.  Use this value to restrict
  441. ;                                           the amount of text to recognise in a long control.
  442. ;                   $scale              - Optional: The scaling factor of the screenshot prior to text recognition.
  443. ;                                           Increase this number to improve accuracy.
  444. ;                                           The default is 2.
  445. ;                   $left_indent        - A number of pixels to indent the capture from the
  446. ;                                           left of the control.
  447. ;                   $top_indent         - A number of pixels to indent the capture from the
  448. ;                                           top of the control.
  449. ;                   $right_indent       - A number of pixels to indent the capture from the
  450. ;                                           right of the control.
  451. ;                   $bottom_indent      - A number of pixels to indent the capture from the
  452. ;                                           bottom of the control.
  453. ;                   $show_capture       - Display screenshot and text captures
  454. ;                                           (for debugging purposes).
  455. ;                                           0 = do not display the screenshot taken (default)
  456. ;                                           1 = display the screenshot taken and exit
  457. ; Return values .:  On Success  - Returns an array of text that was captured.
  458. ;                   On Failure  - Returns an empty array.
  459. ; Author ........:  seangriffin
  460. ; Modified.......:
  461. ; Remarks .......:  Use the default values for first time use.  If the text recognition accuracy is low,
  462. ;                   I suggest setting $show_capture to 1 and rerunning.  If the screenshot of the
  463. ;                   window or control includes borders or erroneous pixels that may interfere with
  464. ;                   the text recognition process, then use $left_indent, $top_indent, $right_indent and
  465. ;                   $bottom_indent to adjust the portion of the control being captured, to
  466. ;                   exclude these non-textural elements.
  467. ;                   If text accuracy is still low, increase the $scale parameter.  In general, The higher
  468. ;                   the scale the clearer the font and the more accurate the text recognition.
  469. ; Related .......:
  470. ; Link ..........:
  471. ; Example .......:  Yes
  472. ;
  473. ; ;==========================================================================================
  474. func _TesseractControlCapture($win_title, $win_text = "", $ctrl_id = "", $get_last_capture = 0, $delimiter = "", $expand = 1, $scrolling = 1, $cleanup = 1, $max_scroll_times = 5, $scale = 2, $left_indent = 0, $top_indent = 0, $right_indent = 0, $bottom_indent = 0, $show_capture = 0)
  475.  
  476.     Local $tInfo
  477.     dim $aArray, $final_ocr[1], $xyPos_old = -1, $capture_scale = 3
  478.     Local $tSCROLLINFO = DllStructCreate($tagSCROLLINFO)
  479.     DllStructSetData($tSCROLLINFO, "cbSize", DllStructGetSize($tSCROLLINFO))
  480.     DllStructSetData($tSCROLLINFO, "fMask", $SIF_ALL)
  481.  
  482.     if $last_capture = "" Then
  483.  
  484.         $last_capture = ObjCreate("Scripting.Dictionary")
  485.     EndIf
  486.  
  487.     ; if a control ID is specified, then get it's HWND
  488.     if StringCompare($ctrl_id, "") <> 0 Then
  489.  
  490.         $hwnd = ControlGetHandle($win_title, $win_text, $ctrl_id)
  491.  
  492.         ; if expansion of the control is required.
  493.         if $expand = 1 and StringCompare($delimiter, "") <> 0 Then
  494.  
  495.             $hwnd2 = $hwnd
  496.  
  497.             If _GUICtrlComboBox_GetComboBoxInfo($hwnd, $tInfo) Then
  498.  
  499.                 $hwnd = DllStructGetData($tInfo, "hList")
  500.             EndIf
  501.  
  502.             ; Expand the control.
  503.             _GUICtrlComboBox_ShowDropDown($hwnd2, True)
  504.         EndIf
  505.     EndIf
  506.  
  507.     ; if last capture is requested, and one exists.
  508.     if $get_last_capture = 1 and $last_capture.item(Number($hwnd)) <> "" Then
  509.  
  510.         return $last_capture.item(Number($hwnd))
  511.     EndIf
  512.  
  513.     ; Text recognition improves alot if the current selection and focus rectangle is removed.
  514.     ;   The following code will remove the selection.
  515.     ;   After text recognition the selection is returned.
  516.     $sel_index = _GUICtrlListBox_GetCurSel($hwnd)
  517.  
  518.     ; The following two lines should remove the current selection and focus rectangle
  519.     ;   in all cases.
  520.     _GUICtrlListBox_SetCurSel($hWnd, -1)
  521.     _GUICtrlListBox_SetCaretIndex($hWnd, -1)
  522.  
  523.     ; Scroll to the top
  524.     DllCall("user32.dll", "int", "SendMessage", "hwnd", $hwnd, "int", $WM_VSCROLL, "int", $SB_TOP, "int", 0)
  525.  
  526.     for $i = 1 to $max_scroll_times
  527.  
  528.         if $i > 1 Then
  529.  
  530.             ; Scroll the list down one page
  531.             DllCall("user32.dll", "int", "SendMessage", "hwnd", $hwnd, "int", $WM_VSCROLL, "int", $SB_PAGEDOWN, "int", 0)
  532.  
  533.         EndIf
  534.  
  535.         ; Get the position of the scroll bar
  536.         DllCall("user32.dll", "int", "GetScrollInfo", "hwnd", $hwnd, "int", $SB_VERT, "ptr", DllStructGetPtr($tSCROLLINFO))
  537.         $xyPos = DllStructGetData($tSCROLLINFO, "nPos")
  538.  
  539.         ; If the scroll bar hasn't moved, we have finished scrolling
  540.         if $xyPos_old = $xyPos then ExitLoop
  541.  
  542.         $xyPos_old = $xyPos
  543.  
  544.         ; Perform the text recognition
  545.         WinActivate($win_title)
  546.  
  547.         $capture_filename = _TempFile($tesseract_temp_path, "~", ".tif")
  548.         $ocr_filename = StringLeft($capture_filename, StringLen($capture_filename) - 4)
  549.         $ocr_filename_and_ext = $ocr_filename & ".txt"
  550.  
  551.         CaptureToTIFF($win_title, $win_text, $hwnd, $capture_filename, $scale, $left_indent, $top_indent, $right_indent, $bottom_indent)
  552.  
  553.         ;ShellExecuteWait(@ProgramFilesDir & "\Tesseract-OCR\tesseract.exe", $capture_filename & " " & $ocr_filename)
  554.         ShellExecuteWait("C:Program Files (x86)\Tesseract-OCR\tesseract.exe", $capture_filename & " " & $ocr_filename, "", "", @SW_HIDE)
  555.         ; Return the current selection (if one existed)
  556.         if $sel_index > -1 Then
  557.  
  558.             _GUICtrlListBox_SetCurSel($hwnd, $sel_index)
  559.         EndIf
  560.  
  561.         ; If no delimter specified, then return a string
  562.         if StringCompare($delimiter, "") = 0 Then
  563.  
  564.             $final_ocr = FileRead($ocr_filename_and_ext)
  565.             $i = $max_scroll_times
  566.         Else
  567.  
  568.             _FileReadToArray($ocr_filename_and_ext, $aArray)
  569.             _ArrayDelete($aArray, 0)
  570.  
  571.             ; Append the recognised text to a final array
  572.             _ArrayConcatenate($final_ocr, $aArray)
  573.         EndIf
  574.  
  575.         ; If the captures are to be displayed
  576.         if $show_capture = 1 Then
  577.  
  578.             GUICreate("Tesseract Screen Capture.  Note: image displayed is not to scale", 640, 480, 0, 0, $WS_SIZEBOX + $WS_SYSMENU)  ; will create a dialog box that when displayed is centered
  579.  
  580.             GUISetBkColor(0xE0FFFF)
  581.  
  582.             $Obj1 = ObjCreate("Preview.Preview.1")
  583.             $Obj1_ctrl = GUICtrlCreateObj($Obj1, 0, 0, 640, 480)
  584.             $Obj1.ShowFile ($capture_filename, 1)
  585.  
  586.             GUISetState()
  587.  
  588.             if IsArray($final_ocr) Then
  589.  
  590.                 _ArrayDisplay($aArray, "Tesseract Text Capture")
  591.             Else
  592.  
  593.                 MsgBox(0, "Tesseract Text Capture", $final_ocr)
  594.             EndIf
  595.  
  596.             GUIDelete()
  597.         EndIf
  598.  
  599.         FileDelete($ocr_filename & ".*")
  600.     Next
  601.  
  602.     ; Cleanup
  603.     if IsArray($final_ocr) And $cleanup = 1 Then
  604.  
  605.         ; Cleanup the items
  606.         for $final_ocr_num = 1 to (UBound($final_ocr)-1)
  607.  
  608.             ; Remove erroneous characters
  609.             $final_ocr[$final_ocr_num] = StringReplace($final_ocr[$final_ocr_num], ".", "")
  610.             $final_ocr[$final_ocr_num] = StringReplace($final_ocr[$final_ocr_num], "'", "")
  611.             $final_ocr[$final_ocr_num] = StringReplace($final_ocr[$final_ocr_num], ",", "")
  612.             $final_ocr[$final_ocr_num] = StringStripWS($final_ocr[$final_ocr_num], 3)
  613.         Next
  614.  
  615.         ; Remove duplicate and blank items
  616.         for $each in $final_ocr
  617.  
  618.             $found_item = _ArrayFindAll($final_ocr, $each)
  619.  
  620.             ; Remove blank items
  621.             if IsArray($found_item) Then
  622.                 if StringCompare($final_ocr[$found_item[0]], "") = 0 Then
  623.  
  624.                     _ArrayDelete($final_ocr, $found_item[0])
  625.                 EndIf
  626.             EndIf
  627.  
  628.             ; Remove duplicate items
  629.             for $found_item_num = 2 to UBound($found_item)
  630.  
  631.                 _ArrayDelete($final_ocr, $found_item[$found_item_num-1])
  632.             Next
  633.         Next
  634.     EndIf
  635.  
  636.     ; Store a copy of the capture
  637.     if $last_capture.item(Number($hwnd)) = "" Then
  638.  
  639.         $last_capture.item(Number($hwnd)) = $final_ocr
  640.     EndIf
  641.  
  642.     Return $final_ocr
  643. EndFunc
  644.  
  645. ; #FUNCTION# ;===============================================================================
  646. ;
  647. ; Name...........:  _TesseractScreenFind()
  648. ; Description ...:  Finds the location of a string within text captured from the screen.
  649. ; Syntax.........:  _TesseractScreenFind($find_str = "", $partial = 1, $get_last_capture = 0, $delimiter = "", $cleanup = 1, $scale = 2, $left_indent = 0, $top_indent = 0, $right_indent = 0, $bottom_indent = 0, $show_capture = 0)
  650. ; Parameters ....:  $find_str           - The text (string) to find.
  651. ;                   $partial            - Optional: Find the text using a partial match?
  652. ;                                           0 = use a full text match
  653. ;                                           1 = use a partial text match (default)
  654. ;                   $get_last_capture   - Search within the text of the last capture, rather than
  655. ;                                           performing another capture.  Useful if the text in
  656. ;                                           the window or control hasn't changed since the last capture.
  657. ;                                           0 = do not use the last capture (default)
  658. ;                                           1 = use the last capture
  659. ;                   $delimiter          - Optional: The string that delimits elements in the text.
  660. ;                                           A string of text will be searched if this isn't provided.
  661. ;                                           The index of the item found will be returned if this is provided.
  662. ;                                           Eg. Use @CRLF to find an item in a listbox.
  663. ;                   $cleanup            - Optional: Remove invalid text recognised
  664. ;                                           0 = do not remove invalid text
  665. ;                                           1 = remove invalid text (default)
  666. ;                   $scale              - Optional: The scaling factor of the screenshot prior to text recognition.
  667. ;                                           Increase this number to improve accuracy.
  668. ;                                           The default is 2.
  669. ;                   $left_indent        - A number of pixels to indent the capture from the
  670. ;                                           left of the screen.
  671. ;                   $top_indent         - A number of pixels to indent the capture from the
  672. ;                                           top of the screen.
  673. ;                   $right_indent       - A number of pixels to indent the capture from the
  674. ;                                           right of the screen.
  675. ;                   $bottom_indent      - A number of pixels to indent the capture from the
  676. ;                                           bottom of the screen.
  677. ;                   $show_capture       - Display screenshot and text captures
  678. ;                                           (for debugging purposes).
  679. ;                                           0 = do not display the screenshot taken (default)
  680. ;                                           1 = display the screenshot taken and exit
  681. ; Return values .:  On Success          - Returns the location of the text that was found.
  682. ;                                           If $delimiter is "", then the character position of the text found
  683. ;                                               is returned.
  684. ;                                           If $delimiter is not "", then the element of the array where the
  685. ;                                               text was found is returned.
  686. ;                   On Failure          - Returns an empty array.
  687. ; Author ........:  seangriffin
  688. ; Modified.......:
  689. ; Remarks .......:
  690. ; Related .......:
  691. ; Link ..........:
  692. ; Example .......:  No
  693. ;
  694. ; ;==========================================================================================
  695. func _TesseractScreenFind($find_str = "", $partial = 1, $get_last_capture = 0, $delimiter = "", $cleanup = 1, $scale = 2, $left_indent = 0, $top_indent = 0, $right_indent = 0, $bottom_indent = 0, $show_capture = 0)
  696.  
  697.     ; Get all the text from the screen
  698.     $recognised_text = _TesseractScreenCapture($get_last_capture, $delimiter, $cleanup, $scale, $left_indent, $top_indent, $right_indent, $bottom_indent, $show_capture)
  699.  
  700.     if IsArray($recognised_text) Then
  701.  
  702.         $index_found = _ArraySearch($recognised_text, $find_str, 0, 0, 0, $partial)
  703.     Else
  704.  
  705.         if $partial = 1 Then
  706.  
  707.             $index_found = StringInStr($recognised_text, $find_str)
  708.         Else
  709.  
  710.             if StringCompare($recognised_text, $find_str) = 0 Then
  711.  
  712.                 $index_found = 1
  713.             Else
  714.  
  715.                 $index_found = 0
  716.             EndIf
  717.         EndIf
  718.     EndIf
  719.  
  720.     Return $index_found
  721. EndFunc
  722.  
  723. ; #FUNCTION# ;===============================================================================
  724. ;
  725. ; Name...........:  _TesseractWinFind()
  726. ; Description ...:  Finds the location of a string within text captured from a window.
  727. ; Syntax.........:  _TesseractWinFind($win_title, $win_text = "", $find_str = "", $partial = 1, $get_last_capture = 0, $delimiter = "", $cleanup = 1, $scale = 2, $left_indent = 0, $top_indent = 0, $right_indent = 0, $bottom_indent = 0, $show_capture = 0)
  728. ; Parameters ....:  $win_title          - The title of the window to find text in.
  729. ;                   $win_text           - Optional: The text of the window to find text in.
  730. ;                   $find_str           - The text (string) to find.
  731. ;                   $partial            - Optional: Find the text using a partial match?
  732. ;                                           0 = use a full text match
  733. ;                                           1 = use a partial text match (default)
  734. ;                   $get_last_capture   - Search within the text of the last capture, rather than
  735. ;                                           performing another capture.  Useful if the text in
  736. ;                                           the window or control hasn't changed since the last capture.
  737. ;                                           0 = do not use the last capture (default)
  738. ;                                           1 = use the last capture
  739. ;                   $delimiter          - Optional: The string that delimits elements in the text.
  740. ;                                           A string of text will be searched if this isn't provided.
  741. ;                                           The index of the item found will be returned if this is provided.
  742. ;                                           Eg. Use @CRLF to find an item in a listbox.
  743. ;                   $cleanup            - Optional: Remove invalid text recognised
  744. ;                                           0 = do not remove invalid text
  745. ;                                           1 = remove invalid text (default)
  746. ;                   $scale              - Optional: The scaling factor of the screenshot prior to text recognition.
  747. ;                                           Increase this number to improve accuracy.
  748. ;                                           The default is 2.
  749. ;                   $left_indent        - A number of pixels to indent the capture from the
  750. ;                                           left of the window.
  751. ;                   $top_indent         - A number of pixels to indent the capture from the
  752. ;                                           top of the window.
  753. ;                   $right_indent       - A number of pixels to indent the capture from the
  754. ;                                           right of the window.
  755. ;                   $bottom_indent      - A number of pixels to indent the capture from the
  756. ;                                           bottom of the window.
  757. ;                   $show_capture       - Display screenshot and text captures
  758. ;                                           (for debugging purposes).
  759. ;                                           0 = do not display the screenshot taken (default)
  760. ;                                           1 = display the screenshot taken and exit
  761. ; Return values .:  On Success          - Returns the location of the text that was found.
  762. ;                                           If $delimiter is "", then the character position of the text found
  763. ;                                               is returned.
  764. ;                                           If $delimiter is not "", then the element of the array where the
  765. ;                                               text was found is returned.
  766. ;                   On Failure          - Returns an empty array.
  767. ; Author ........:  seangriffin
  768. ; Modified.......:
  769. ; Remarks .......:
  770. ; Related .......:
  771. ; Link ..........:
  772. ; Example .......:  No
  773. ;
  774. ; ;==========================================================================================
  775. func _TesseractWinFind($win_title, $win_text = "", $find_str = "", $partial = 1, $get_last_capture = 0, $delimiter = "", $cleanup = 1, $scale = 2, $left_indent = 0, $top_indent = 0, $right_indent = 0, $bottom_indent = 0, $show_capture = 0)
  776.  
  777.     ; Get all the text from the window
  778.     $recognised_text = _TesseractWinCapture($win_title, $win_text, $get_last_capture, $delimiter, $cleanup, $scale, $left_indent, $top_indent, $right_indent, $bottom_indent, $show_capture)
  779.  
  780.     if IsArray($recognised_text) Then
  781.  
  782.         $index_found = _ArraySearch($recognised_text, $find_str, 0, 0, 0, $partial)
  783.     Else
  784.  
  785.         if $partial = 1 Then
  786.  
  787.             $index_found = StringInStr($recognised_text, $find_str)
  788.         Else
  789.  
  790.             if StringCompare($recognised_text, $find_str) = 0 Then
  791.  
  792.                 $index_found = 1
  793.             Else
  794.  
  795.                 $index_found = 0
  796.             EndIf
  797.         EndIf
  798.     EndIf
  799.  
  800.     Return $index_found
  801. EndFunc
  802.  
  803. ; #FUNCTION# ;===============================================================================
  804. ;
  805. ; Name...........:  _TesseractControlFind()
  806. ; Description ...:  Finds the location of a string within text captured from a control.
  807. ; Syntax.........:  _TesseractControlFind($win_title, $win_text = "", $ctrl_id = "", $find_str = "", $partial = 1, $get_last_capture = 0, $delimiter = "", $expand = 1, $scrolling = 1, $cleanup = 1, $max_scroll_times = 5, $scale = 2, $left_indent = 0, $top_indent = 0, $right_indent = 0, $bottom_indent = 0, $show_capture = 0)
  808. ; Parameters ....:  $win_title          - The title of the window to find text in.
  809. ;                   $win_text           - Optional: The text of the window to find text in.
  810. ;                   $ctrl_id            - Optional: The ID of the control to find text in.
  811. ;                                           The text of the window will be usee if one isn't provided.
  812. ;                   $find_str           - The text (string) to find.
  813. ;                   $partial            - Optional: Find the text using a partial match?
  814. ;                                           0 = use a full text match
  815. ;                                           1 = use a partial text match (default)
  816. ;                   $get_last_capture   - Search within the text of the last capture, rather than
  817. ;                                           performing another capture.  Useful if the text in
  818. ;                                           the window or control hasn't changed since the last capture.
  819. ;                                           0 = do not use the last capture (default)
  820. ;                                           1 = use the last capture
  821. ;                   $delimiter          - Optional: The string that delimits elements in the text.
  822. ;                                           A string of text will be searched if this isn't provided.
  823. ;                                           The index of the item found will be returned if this is provided.
  824. ;                                           Eg. Use @CRLF to find an item in a listbox.
  825. ;                   $expand             - Optional: Expand the control before searching it?
  826. ;                                           0 = do not expand the control
  827. ;                                           1 = expand the control (default)
  828. ;                   $scrolling          - Optional: Scroll the control to search all it's text?
  829. ;                                           0 = do not scroll the control
  830. ;                                           1 = scroll the control (default)
  831. ;                   $cleanup            - Optional: Remove invalid text recognised
  832. ;                                           0 = do not remove invalid text
  833. ;                                           1 = remove invalid text (default)
  834. ;                   $max_scroll_times   - The maximum number of scrolls to capture in a control
  835. ;                                           If a control has a very long scroll bar, the text recognition
  836. ;                                           process will take too long.  Use this value to restrict
  837. ;                                           the amount of text to recognise in a long control.
  838. ;                   $scale              - Optional: The scaling factor of the screenshot prior to text recognition.
  839. ;                                           Increase this number to improve accuracy.
  840. ;                                           The default is 2.
  841. ;                   $left_indent        - A number of pixels to indent the capture from the
  842. ;                                           left of the control.
  843. ;                   $top_indent         - A number of pixels to indent the capture from the
  844. ;                                           top of the control.
  845. ;                   $right_indent       - A number of pixels to indent the capture from the
  846. ;                                           right of the control.
  847. ;                   $bottom_indent      - A number of pixels to indent the capture from the
  848. ;                                           bottom of the control.
  849. ;                   $show_capture       - Display screenshot and text captures
  850. ;                                           (for debugging purposes).
  851. ;                                           0 = do not display the screenshot taken (default)
  852. ;                                           1 = display the screenshot taken and exit
  853. ; Return values .:  On Success          - Returns the location of the text that was found.
  854. ;                                           If $delimiter is "", then the character position of the text found
  855. ;                                               is returned.
  856. ;                                           If $delimiter is not "", then the element of the array where the
  857. ;                                               text was found is returned.
  858. ;                   On Failure          - Returns an empty array.
  859. ; Author ........:  seangriffin
  860. ; Modified.......:
  861. ; Remarks .......:
  862. ; Related .......:
  863. ; Link ..........:
  864. ; Example .......:  Yes
  865. ;
  866. ; ;==========================================================================================
  867. func _TesseractControlFind($win_title, $win_text = "", $ctrl_id = "", $find_str = "", $partial = 1, $get_last_capture = 0, $delimiter = "", $expand = 1, $scrolling = 1, $cleanup = 1, $max_scroll_times = 5, $scale = 2, $left_indent = 0, $top_indent = 0, $right_indent = 0, $bottom_indent = 0, $show_capture = 0)
  868.  
  869.     ; Get all the text from the control
  870.     $recognised_text = _TesseractControlCapture($win_title, $win_text, $ctrl_id, $get_last_capture, $delimiter, $expand, $scrolling, $cleanup, $max_scroll_times, $scale, $left_indent, $top_indent, $right_indent, $bottom_indent, $show_capture)
  871.  
  872.     if IsArray($recognised_text) Then
  873.  
  874.         $index_found = _ArraySearch($recognised_text, $find_str, 0, 0, 0, $partial)
  875.     Else
  876.  
  877.         if $partial = 1 Then
  878.  
  879.             $index_found = StringInStr($recognised_text, $find_str)
  880.         Else
  881.  
  882.             if StringCompare($recognised_text, $find_str) = 0 Then
  883.  
  884.                 $index_found = 1
  885.             Else
  886.  
  887.                 $index_found = 0
  888.             EndIf
  889.         EndIf
  890.     EndIf
  891.  
  892.     Return $index_found
  893. EndFunc
  894.  
  895. ; #FUNCTION# ;===============================================================================
  896. ;
  897. ; Name...........:  CaptureToTIFF()
  898. ; Description ...:  Captures an image of the screen, a window or a control, and saves it to a TIFF file.
  899. ; Syntax.........:  CaptureToTIFF($win_title = "", $win_text = "", $ctrl_id = "", $sOutImage = "", $scale = 1, $left_indent = 0, $top_indent = 0, $right_indent = 0, $bottom_indent = 0)
  900. ; Parameters ....:  $win_title      - The title of the window to capture an image of.
  901. ;                   $win_text       - Optional: The text of the window to capture an image of.
  902. ;                   $ctrl_id        - Optional: The ID of the control to capture an image of.
  903. ;                                       An image of the window will be returned if one isn't provided.
  904. ;                   $sOutImage      - The filename to store the image in.
  905. ;                   $scale          - Optional: The scaling factor of the capture.
  906. ;                   $left_indent    - A number of pixels to indent the screen capture from the
  907. ;                                       left of the window or control.
  908. ;                   $top_indent     - A number of pixels to indent the screen capture from the
  909. ;                                       top of the window or control.
  910. ;                   $right_indent   - A number of pixels to indent the screen capture from the
  911. ;                                       right of the window or control.
  912. ;                   $bottom_indent  - A number of pixels to indent the screen capture from the
  913. ;                                       bottom of the window or control.
  914. ; Return values .:  None
  915. ; Author ........:  seangriffin
  916. ; Modified.......:
  917. ; Remarks .......:
  918. ; Related .......:
  919. ; Link ..........:
  920. ; Example .......:  No
  921. ;
  922. ; ;==========================================================================================
  923. Func CaptureToTIFF($win_title = "", $win_text = "", $ctrl_id = "", $sOutImage = "", $scale = 1, $left_indent = 0, $top_indent = 0, $right_indent = 0, $bottom_indent = 0)
  924.  
  925.     Local $hWnd, $hwnd2, $hDC, $hBMP, $hImage1, $hGraphic, $CLSID, $tParams, $pParams, $tData, $i = 0, $hImage2, $pos[4]
  926.     Local $Ext = StringUpper(StringMid($sOutImage, StringInStr($sOutImage, ".", 0, -1) + 1))
  927.     Local $giTIFColorDepth = 24
  928.     Local $giTIFCompression = $GDIP_EVTCOMPRESSIONNONE
  929.  
  930.     ; If capturing a control
  931.     if StringCompare($ctrl_id, "") <> 0 Then
  932.  
  933.         $hwnd2 = ControlGetHandle($win_title, $win_text, $ctrl_id)
  934.         $pos = ControlGetPos($win_title, $win_text, $ctrl_id)
  935.     Else
  936.  
  937.         ; If capturing a window
  938.         if StringCompare($win_title, "") <> 0 Then
  939.  
  940.             $hwnd2 = WinGetHandle($win_title, $win_text)
  941.             $pos = WinGetPos($win_title, $win_text)
  942.         Else
  943.  
  944.             ; If capturing the desktop
  945.             $hwnd2 = ""
  946.             $pos[0] = 0
  947.             $pos[1] = 0
  948.             $pos[2] = @DesktopWidth
  949.             $pos[3] = @DesktopHeight
  950.         EndIf
  951.     EndIf
  952.  
  953.     ; Capture an image of the window / control
  954.     if IsHWnd($hwnd2) Then
  955.  
  956.         WinActivate($win_title, $win_text)
  957.         $hBitmap2 = _ScreenCapture_CaptureWnd("", $hwnd2, 0, 0, -1, -1, False)
  958.     Else
  959.  
  960.         $hBitmap2 = _ScreenCapture_Capture("", 0, 0, -1, -1, False)
  961.     EndIf
  962.  
  963.     _GDIPlus_Startup ()
  964.  
  965.     ; Convert the image to a bitmap
  966.     $hImage2 = _GDIPlus_BitmapCreateFromHBITMAP ($hBitmap2)
  967.  
  968.     $hWnd = _WinAPI_GetDesktopWindow()
  969.     $hDC = _WinAPI_GetDC($hWnd)
  970.     $hBMP = _WinAPI_CreateCompatibleBitmap($hDC, ($pos[2] * $scale) - ($right_indent * $scale), ($pos[3] * $scale) - ($bottom_indent * $scale))
  971.  
  972.     _WinAPI_ReleaseDC($hWnd, $hDC)
  973.     $hImage1 = _GDIPlus_BitmapCreateFromHBITMAP ($hBMP)
  974.     $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage1)
  975.     _GDIPLus_GraphicsDrawImageRect($hGraphic, $hImage2, 0 - ($left_indent * $scale), 0 - ($top_indent * $scale), ($pos[2] * $scale) + $left_indent, ($pos[3] * $scale) + $top_indent)
  976.     $CLSID = _GDIPlus_EncodersGetCLSID($Ext)
  977.  
  978.     ; Set TIFF parameters
  979.     $tParams = _GDIPlus_ParamInit(2)
  980.     $tData = DllStructCreate("int ColorDepth;int Compression")
  981.     DllStructSetData($tData, "ColorDepth", $giTIFColorDepth)
  982.     DllStructSetData($tData, "Compression", $giTIFCompression)
  983.     _GDIPlus_ParamAdd($tParams, $GDIP_EPGCOLORDEPTH, 1, $GDIP_EPTLONG, DllStructGetPtr($tData, "ColorDepth"))
  984.     _GDIPlus_ParamAdd($tParams, $GDIP_EPGCOMPRESSION, 1, $GDIP_EPTLONG, DllStructGetPtr($tData, "Compression"))
  985.     If IsDllStruct($tParams) Then $pParams = DllStructGetPtr($tParams)
  986.  
  987.     ; Save TIFF and cleanup
  988.     _GDIPlus_ImageSaveToFileEx($hImage1, $sOutImage, $CLSID, $pParams)
  989.     _GDIPlus_ImageDispose($hImage1)
  990.     _GDIPlus_ImageDispose($hImage2)
  991.     _GDIPlus_GraphicsDispose ($hGraphic)
  992.     _WinAPI_DeleteObject($hBMP)
  993.     _GDIPlus_Shutdown()
  994. EndFunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement