I love this utility - thank you for figuring this all out.
My needs are not as complicated and I do not need all the functionality provided here. All I really ever want to do is to read the text displayed on the active window contained in some rectangle. I actually find if you limit the OCR to an area of the screen with text (and not with images (icons/dropbox controls/etc.) then the OCR works a lot better.
You still need to #include <Tesseract.au3> and use _TesseractTempPathSet()
Please Note: Unlike PixelSearch when specifying an area to OCR you must specify the pixel area to search from upper left to lower right.
Some examples:
; convert the entire active window screen image to text
$resultText = _SimpleTesseractScreenCapture()
; convert the text in the pixel area (200,200) to (400,600)
$resultText = _SimpleTesseractScreenCapture(200,200,400,600)
; when the text is small you may find accuracy is better if you raise the scale factor. Here the pixel area is blown up 5 times before running it into the OCR.
$resultText = _SimpleTesseractScreenCapture(200, 200, 400, 600, 5)
#include <Tesseract.au3>
_TesseractTempPathSet("c:\Users\Odlid Tubpu\Tesseract\Temp")
ConsoleWrite("------------------------"&@CRLF)
; OCR all the text on the active window
$result = _SimpleTesseractScreenCapture()
ConsoleWrite($result&@CRLF)
ConsoleWrite("------------------------"&@CRLF)
; OCR the text on the active window in the area 200 pixels to the left of the window left edge, 200 pixels down from the window top edge to 400 pixels from the left and 600 pixels down
$result = _SimpleTesseractScreenCapture(200,200,400,600)
ConsoleWrite($result&@CRLF)
ConsoleWrite("------------------------"&@CRLF)
; if OCR accuracy is low you can sacrifice performance for more accuracy by magnifying the screen before OCRing (although experiment...sometimes lower magnification works better)
$result = _SimpleTesseractScreenCapture(200,200,400,600,5)
ConsoleWrite($result&@CRLF)
ConsoleWrite("------------------------"&@CRLF)
Func _SimpleTesseractScreenCapture($left=0,$top=0,$right=@DesktopWidth,$bottom=@DesktopHeight,$scale = 2)
Local $hGUI
Local $giTIFColorDepth = 24
Local $giTIFCompression = $GDIP_EVTCOMPRESSIONNONE
Local $capture_filename, $ocr_filename, $ocr_filename_and_ext, $hBitmap2, $hImage1, $hImage2, $hWnd, $hDC, $hBMP, $hGraphic, $CLSID, $tParams, $tData, $pParams, $final_ocr
; create a temporary file for the screen capture and the OCR text results file
$capture_filename = _TempFile($tesseract_temp_path, "~", ".tif")
$ocr_filename = StringLeft($capture_filename, StringLen($capture_filename) - 4)
$ocr_filename_and_ext = $ocr_filename & ".txt"
; Capture window
$hBitmap2 =_ScreenCapture_Capture("",$left,$top,$right,$bottom,False)
_GDIPlus_Startup ()
; Convert the image to a bitmap
$hImage2 = _GDIPlus_BitmapCreateFromHBITMAP ($hBitmap2)
$hWnd = _WinAPI_GetDesktopWindow()
$hDC = _WinAPI_GetDC($hWnd)
$hBMP = _WinAPI_CreateCompatibleBitmap($hDC, ($right-$left) * $scale, ($bottom-$top) * $scale)
_WinAPI_ReleaseDC($hWnd, $hDC)
$hImage1 = _GDIPlus_BitmapCreateFromHBITMAP ($hBMP)
$hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage1)
_GDIPLus_GraphicsDrawImageRect($hGraphic, $hImage2, 0, 0, ($right-$left) * $scale, ($bottom-$top)*$scale)
$CLSID = _GDIPlus_EncodersGetCLSID("TIF")
; Set TIFF parameters
$tParams = _GDIPlus_ParamInit(2)
$tData = DllStructCreate("int ColorDepth;int Compression")
DllStructSetData($tData, "ColorDepth", $giTIFColorDepth)
DllStructSetData($tData, "Compression", $giTIFCompression)
_GDIPlus_ParamAdd($tParams, $GDIP_EPGCOLORDEPTH, 1, $GDIP_EPTLONG, DllStructGetPtr($tData, "ColorDepth"))
_GDIPlus_ParamAdd($tParams, $GDIP_EPGCOMPRESSION, 1, $GDIP_EPTLONG, DllStructGetPtr($tData, "Compression"))
If IsDllStruct($tParams) Then $pParams = DllStructGetPtr($tParams)
; Save TIFF and cleanup
_GDIPlus_ImageSaveToFileEx($hImage1, $capture_filename, $CLSID, $pParams)
_GDIPlus_ImageDispose($hImage1)
_GDIPlus_ImageDispose($hImage2)
_GDIPlus_GraphicsDispose ($hGraphic)
_WinAPI_DeleteObject($hBMP)
_GDIPlus_Shutdown()
; use an OS shell to command line run tesseract.exe and do the actual OCR
ShellExecuteWait(@ProgramFilesDir & "\tesseract\tesseract.exe", $capture_filename & " " & $ocr_filename)
$final_ocr = FileRead($ocr_filename_and_ext)
; clean up the files
FileDelete($ocr_filename & ".*")
Return $final_ocr
EndFunc