Code : Tout sélectionner
; #FUNCTION# ;===============================================================================
;
; Name...........: _TesseractScreenCapture()
; Description ...: Captures text from the screen.
; 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)
; Parameters ....: $get_last_capture - Retrieve the text of the last capture, rather than
; performing another capture. Useful if the text in
; the window or control hasn't changed since the last capture.
; 0 = do not retrieve the last capture (default)
; 1 = retrieve the last capture
; $delimiter - Optional: The string that delimits elements in the text.
; A string of text will be returned if this isn't provided.
; An array of delimited text will be returned if this is provided.
; Eg. Use @CRLF to return the items of a listbox as an array.
; $cleanup - Optional: Remove invalid text recognised
; 0 = do not remove invalid text
; 1 = remove invalid text (default)
; $scale - Optional: The scaling factor of the screenshot prior to text recognition.
; Increase this number to improve accuracy.
; The default is 2.
; $iLeft - x-Left coordinate
; $iTop - y-Top coordinate
; $iRight - x-Right coordinate
; $iBottom - y-Bottom coordinate
; $show_capture - Display screenshot and text captures
; (for debugging purposes).
; 0 = do not display the screenshot taken (default)
; 1 = display the screenshot taken and exit
; Return values .: On Success - Returns an array of text that was captured.
; On Failure - Returns an empty array.
; Author ........: seangriffin
; Modified.......:
; Remarks .......: Use the default values for first time use. If the text recognition accuracy is low,
; I suggest setting $show_capture to 1 and rerunning. If the screenshot of the
; window or control includes borders or erroneous pixels that may interfere with
; the text recognition process, then use $left_indent, $top_indent, $right_indent and
; $bottom_indent to adjust the portion of the screen being captured, to
; exclude these non-textural elements.
; If text accuracy is still low, increase the $scale parameter. In general, the higher
; the scale the clearer the font and the more accurate the text recognition.
; Related .......:
; Link ..........:
; Example .......: No
;
; ;==========================================================================================
func _TesseractScreenCapture($get_last_capture = 0, $delimiter = "", $cleanup = 1, $scale = 2, $iLeft = 0, $iTop = 0, $iRight = 1, $iBottom = 1, $show_capture = 0)
Local $tInfo
dim $aArray, $final_ocr[1], $xyPos_old = -1, $capture_scale = 3
Local $tSCROLLINFO = DllStructCreate($tagSCROLLINFO)
DllStructSetData($tSCROLLINFO, "cbSize", DllStructGetSize($tSCROLLINFO))
DllStructSetData($tSCROLLINFO, "fMask", $SIF_ALL)
if $last_capture = "" Then
$last_capture = ObjCreate("Scripting.Dictionary")
EndIf
; if last capture is requested, and one exists.
if $get_last_capture = 1 and $last_capture.item(0) <> "" Then
return $last_capture.item(0)
EndIf
$capture_filename = _TempFile($tesseract_temp_path, "~", ".tif")
$ocr_filename = StringLeft($capture_filename, StringLen($capture_filename) - 4)
$ocr_filename_and_ext = $ocr_filename & ".txt"
CaptureToTIFF("", "", "", $capture_filename, $scale, $iLeft , $iTop , $iRight , $iBottom )
ShellExecuteWait(@ProgramFilesDir & "\tesseract\tesseract.exe", $capture_filename & " " & $ocr_filename)
; If no delimter specified, then return a string
if StringCompare($delimiter, "") = 0 Then
$final_ocr = FileRead($ocr_filename_and_ext)
Else
_FileReadToArray($ocr_filename_and_ext, $aArray)
_ArrayDelete($aArray, 0)
; Append the recognised text to a final array
_ArrayConcatenate($final_ocr, $aArray)
EndIf
; If the captures are to be displayed
if $show_capture = 1 Then
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
GUISetBkColor(0xE0FFFF)
$Obj1 = ObjCreate("Preview.Preview.1")
$Obj1_ctrl = GUICtrlCreateObj($Obj1, 0, 0, 640, 480)
$Obj1.ShowFile ($capture_filename, 1)
GUISetState()
if IsArray($final_ocr) Then
_ArrayDisplay($aArray, "Tesseract Text Capture")
Else
MsgBox(0, "Tesseract Text Capture", $final_ocr)
EndIf
GUIDelete()
EndIf
FileDelete($ocr_filename & ".*")
; Cleanup
if IsArray($final_ocr) And $cleanup = 1 Then
; Cleanup the items
for $final_ocr_num = 1 to (UBound($final_ocr)-1)
; Remove erroneous characters
$final_ocr[$final_ocr_num] = StringReplace($final_ocr[$final_ocr_num], ".", "")
$final_ocr[$final_ocr_num] = StringReplace($final_ocr[$final_ocr_num], "'", "")
$final_ocr[$final_ocr_num] = StringReplace($final_ocr[$final_ocr_num], ",", "")
$final_ocr[$final_ocr_num] = StringStripWS($final_ocr[$final_ocr_num], 3)
Next
; Remove duplicate and blank items
for $each in $final_ocr
$found_item = _ArrayFindAll($final_ocr, $each)
; Remove blank items
if IsArray($found_item) Then
if StringCompare($final_ocr[$found_item[0]], "") = 0 Then
_ArrayDelete($final_ocr, $found_item[0])
EndIf
EndIf
; Remove duplicate items
for $found_item_num = 2 to UBound($found_item)
_ArrayDelete($final_ocr, $found_item[$found_item_num-1])
Next
Next
EndIf
; Store a copy of the capture
if $last_capture.item(0) = "" Then
$last_capture.item(0) = $final_ocr
EndIf
Return $final_ocr
EndFunc