Jump to content

OCR & Paste


ronriel
 Share

Recommended Posts

Hi, This can be quite useful.

This little script lets you select any part of the screen, use OCR to read for texts,and then store them in the clipboard.

You may also select image files directly and read texts in them.

Just Press ALT+Q to start.

UPDATE:

*OCR script now tries to detect line breaks. OCR'd text will now be pasted with @CRLFS.

*Removed the cursor from the captured image.

;r-OCR
;By: ronriel

;updated oct. 20, 2008
;autoit-v3.2.12.1
;Tested  Windows XP SP2 with MS Office 2007

#Include <ScreenCapture.au3>
#include <WindowsConstants.au3>
#Include <Misc.au3>

_Singleton("script",0)
Opt("GUIOnEventMode",1)
Opt("WinTitleMatchMode", 4)
Opt("TrayOnEventMode",1)
Opt("TrayMenuMode",1)
HotKeySet("!q","ShowWin")

Local $err = ObjEvent("AutoIt.Error","Error")

$GUI = GUICreate("", 90, 40,-1,-1,0x80880000,$WS_EX_TOOLWINDOW)
WinSetTrans($GUI,'',200)
$B1 = GUICtrlCreateButton("Read Region", 0, 0, 90, 20)
GUICtrlSetBkColor(-1,0xe6e6fa)
GUICtrlSetOnEvent(-1,"Capture")
$B2 = GUICtrlCreateButton("Read File", 0, 20, 90, 20)
GUICtrlSetBkColor(-1,0xe6e6fa)
GUICtrlSetOnEvent(-1,"Capture")

$GUI2 =  GUICreate("", 0 , 0 , 0, 0,  BitOR($WS_POPUP,$WS_BORDER), BitOR($WS_EX_TOPMOST, $WS_EX_TOOLWINDOW))
GUISetBkColor(0xe6e6fa,$GUI2)
WinSetTrans($GUI2,'',60)
GUISetState(@SW_HIDE)

$GUI3 = GUICreate("", 0 , 0 , 0,0 ,  BitOR($WS_POPUP,$WS_BORDER), BitOR($WS_EX_TOPMOST, $WS_EX_TOOLWINDOW))
GUISetBkColor(0xFF0000,$GUI3)
GUISetState(@SW_HIDE)

TrayCreateItem("Info")
TrayItemSetOnEvent(-1,'Ex')
$Exit = TrayCreateItem("Exit")
TrayItemSetOnEvent(-1,'Ex')
TraySetState()

Func ShowWin()
    If BitAND(WinGetState($GUI),2) Then
        GUISetState(@SW_HIDE,$GUI)
    Else
        $Mpos = MouseGetPos()
        WinMove($GUI,'',$Mpos[0]-45,$Mpos[1]-10)
        GUISetState(@SW_SHOW,$GUI)
    EndIf
EndFunc

Func Capture()
    Local $Image = @TempDir&"\ScreenCapture.png", $v = 0

    If @GUI_CtrlId = $B1 Then
        GUISetState(@SW_HIDE,$GUI)
        GUISetState(@SW_SHOW,$GUI3)
        Do
            Sleep(10)
            ToolTip("Read Region")
            $c = MouseGetPos()
            WinMove($GUI3,"",$c[0]-2.5,$c[1]-2.5,5,5)
            If _IsPressed(01) Then
                ToolTip('')
                $C1 = MouseGetPos()
                    Do
                        Sleep(10)
                        $C2 = MouseGetPos()
                            If $C2[0] <> $C1[0] And $C2[1] <> $C1[1] Then 
                                GUISetState(@SW_SHOW,$GUI2)
                            EndIf
                            If $C1[0] < $C2[0] Then 
                                $X = $C1[0]
                                $W = $C2[0] - $C1[0]
                            Else
                                $X = $C2[0]
                                $W = $C1[0] - $C2[0]
                            EndIf
                            If $C1[1] < $C2[1] Then 
                                $Y = $C1[1]
                                $H = $C2[1] - $C1[1]
                            Else
                                $Y = $C2[1]
                                $H = $C1[1] - $C2[1]
                            EndIf
                        WinMove($GUI2,"",$X,$Y,$W,$H)
                        WinMove($GUI3,"",$c2[0]-2.5,$c2[1]-2.5,5,5)
                    Until Not _IsPressed(01)
                $v = 1
            EndIf
        Until $v = 1
        GUISetState(@SW_HIDE,$GUI2)
        GUISetState(@SW_HIDE,$GUI3)
        _ScreenCapture_Capture($Image,$x,$y,$x+$W,$y+$h,False)
    ElseIf @GUI_CtrlId = $B2 Then
        GUISetState(@SW_HIDE,$GUI)
        $Image = FileOpenDialog("Choose an Image File",@DesktopDir&"\","Images (*.jpg;*.bmp;*.gif;*.png;*.tif)")
        if @error then Return
    EndIf
    $OCR = OCR($Image)
    If @error then Return
    ClipPut($OCR)
    TrayTip("Info","Text saved to Clipboard",2,1)
EndFunc
    
Func OCR($Image)
    Local  $y = 0, $string
    
    $miDoc = ObjCreate("MODI.Document")
    $miDoc.Create($Image)
    If @error Then Return SetError(1)
    
    $miDoc.Ocr(9, True, True);language is English
    If @error Then  
        TrayTip("Info","OCR could not read any character.",2,1)
        ClipPut("Error!")
        Return SetError(1)
    EndIf

    For $Word In $miDoc.Images(0).Layout.Words
        $delim = ' '
        If $miDoc.Images(0).Layout.Words($y).Rects(0).Bottom < $miDoc.Images(0).Layout.Words($y+1).Rects(0).Top Then $delim = @CRLF
        $string &= $Word.text & $delim
        $y += 1
    Next
Return StringTrimRight($string,1)
EndFunc

Func Error()
Endfunc

Func Ex()
    If @TRAY_ID = $Exit Then 
        Exit
    Else
        MsgBox(0,"Info", "Press ALT+Q to start."&@CRLF&@CRLF&"r-OCR"&@CRLF&"By: ronriel (ronriel@yahoo.com)")
    EndIf
EndFunc

While 1
Sleep(100000)
WEnd

Good Luck!

Edited by ronriel

[font="Comic Sans MS"]-ronriel[/font][topic="48542"]r4r media player[/topic][topic="80836"]OCR & Paste[/topic]

Link to comment
Share on other sites

It's not working for me

It just gives 2 error msgboxes :)

Some Projects:[list][*]ZIP UDF using no external files[*]iPod Music Transfer [*]iTunes UDF - fully integrate iTunes with au3[*]iTunes info (taskbar player hover)[*]Instant Run - run scripts without saving them before :)[*]Get Tube - YouTube Downloader[*]Lyric Finder 2 - Find Lyrics to any of your song[*]DeskBox - A Desktop Extension Tool[/list]indifference will ruin the world, but in the end... WHO CARES :P---------------http://torels.altervista.org

Link to comment
Share on other sites

Very Nice use of MODI. Much better even than my drag and drop OCR.. This will be very useful!

Yeah. This can/will be very useful =D

@OP

Good job! I modified it though...

@All

Here is my modification. Has error checking to make sure it doesn't 'crash' on you.

Resizes the image 1.25x. This enables it to better 'read' the characters.

Removed the cursor from the capture.

Removed the ridiculously long white-space at the end of the OCR'd text.

Credits go to Ronriel. I just made it work better :)

;autoit-v3.2.12.1
;Tested  Windows XP SP2 with MS Office 2007
#Include <ScreenCapture.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <Misc.au3>
#include <Array.au3>
#include <GDIPlus.au3>
#include <WinAPI.au3>
;







Opt("GUIOnEventMode",1)
Opt("WinTitleMatchMode", 4)
Opt("TrayOnEventMode",1)
Opt("TrayMenuMode",1)
HotKeySet("!q","ShowWin")

Local $v = 0, $X,$Y,$W,$H, $Image = @TempDir&"\ScreenCapture.jpg", $Error = False

$oMyError = ObjEvent("AutoIt.Error","MyErrFunc")

$GUI = GUICreate("", 90, 40,-1,-1,0x80880000,$WS_EX_TOOLWINDOW)
WinSetTrans($GUI,'',200)
$B1 = GUICtrlCreateButton("Read Region", 0, 0, 90, 20)
GUICtrlSetBkColor(-1,0xe6e6fa)
GUICtrlSetOnEvent(-1,"Capture")
$B2 = GUICtrlCreateButton("Read File", 0, 20, 90, 20)
GUICtrlSetBkColor(-1,0xe6e6fa)
GUICtrlSetOnEvent(-1,"Capture")

$GUI2 =  GUICreate("", 0 , 0 , 0, 0,  BitOR($WS_POPUP,$WS_BORDER), BitOR($WS_EX_TOPMOST, $WS_EX_TOOLWINDOW))
GUISetBkColor(0xe6e6fa,$GUI2)
WinSetTrans($GUI2,'',60)
GUISetState(@SW_HIDE)

$GUI3 = GUICreate("", 0 , 0 , 0,0 ,  BitOR($WS_POPUP,$WS_BORDER), BitOR($WS_EX_TOPMOST, $WS_EX_TOOLWINDOW))
GUISetBkColor(0xFF0000,$GUI3)
GUISetState(@SW_HIDE)

TrayCreateItem("Info")
TrayItemSetOnEvent(-1,'Ex')
$Exit = TrayCreateItem("Exit")
TrayItemSetOnEvent(-1,'Ex')
TraySetState()

While 1
Sleep(1000)
WEnd



Func ShowWin()
    If BitAND(WinGetState($GUI),2) Then
        GUISetState(@SW_HIDE,$GUI)
    Else
        $Mpos = MouseGetPos()
        WinMove($GUI,'',$Mpos[0]-45,$Mpos[1]-10)
        GUISetState(@SW_SHOW,$GUI)
    EndIf
EndFunc

Func Capture()
    _ScreenCapture_SetJPGQuality(100)
    If @GUI_CtrlId = $B1 Then
        GUISetState(@SW_HIDE,$GUI)
        GUISetState(@SW_SHOW,$GUI3)
        Do
            Sleep(10)
            ToolTip("Read Region")
            $c = MouseGetPos()
            WinMove($GUI3,"",$c[0]-2.5,$c[1]-2.5,5,5)
            If _IsPressed(01) Then
                ToolTip('')
                $C1 = MouseGetPos()
                    Do
                        Sleep(10)
                        $C2 = MouseGetPos()
                            If $C2[0] <> $C1[0] And $C2[1] <> $C1[1] Then 
                                GUISetState(@SW_SHOW,$GUI2)
                            EndIf
                            If $C1[0] < $C2[0] Then 
                                $X = $C1[0]
                                $W = $C2[0] - $C1[0]
                            Else
                                $X = $C2[0]
                                $W = $C1[0] - $C2[0]
                            EndIf
                            If $C1[1] < $C2[1] Then 
                                $Y = $C1[1]
                                $H = $C2[1] - $C1[1]
                            Else
                                $Y = $C2[1]
                                $H = $C1[1] - $C2[1]
                            EndIf
                        WinMove($GUI2,"",$X,$Y,$W,$H)
                        WinMove($GUI3,"",$c2[0]-2.5,$c2[1]-2.5,5,5)
                    Until Not _IsPressed(01)
                $v = 1
            EndIf
        Until $v = 1
        $v = 0
        GUISetState(@SW_HIDE,$GUI2)
        GUISetState(@SW_HIDE,$GUI3)
        _ScreenCapture_Capture($Image,$x,$y,$x+$w,$y+$h, False)
        _ImageResize($Image, $Image, 600, 600)
    ElseIf @GUI_CtrlId = $B2 Then
        GUISetState(@SW_HIDE,$GUI)
        $Image = FileOpenDialog("Choose an Image File",@DesktopDir&"\","Images (*.jpg;*.bmp;*.gif;*.png;*.tif)")
        if @error then Return
    EndIf
    $OCR = OCR($Image)
EndFunc

Func OCR($Image)
    Local $miDoc, $Doc
    Local $str
    Local $oWord
    Local $sArray[500]
    
    
    $miDoc = ObjCreate("MODI.Document")
    $miDoc.Create(@TempDir & '\OCR_ImageResize.jpg')
    $miDoc.Ocr(9, True, False)
    
    If $Error = False Then
        $i = 0
        For $oWord in $miDoc.Images(0).Layout.Words

            $str = $str & $oWord.text & @CrLf
                ConsoleWrite($oWord.text & @CRLF)
            $sArray [$i] = $oWord.text
            $i += 1
            
        Next

        $as_Text = _ArrayToString($sArray," ",0)
        $as_Text = StringReplace($as_Text, "  ", "", 0)
        ClipPut($as_Text)
        TrayTip("Info","Text saved to Clipboard",2,1)
        Return 0
        
    ElseIf $Error = True Then
        
        $Error = False
        TrayTip("Info","Error. OCR could not 'read' any characters.",2,1)
        ClipPut("Error")
    EndIf
    

EndFunc




Func MyErrFunc()
  SetError(1)  ; to check for after this function returns
  $Error = True
Endfunc




Func Ex()
    If @TRAY_ID = $Exit Then 
        Exit
    Else
        MsgBox(0,"Info", "Press ALT+Q to start."&@CRLF&@CRLF&"r-OCR")
    EndIf
EndFunc





; #FUNCTION# =========================================================================================
; Name...........: _ImageResize
; Description....: Resize an image and optionally convert it to the format you want.
; Syntax.........: _ImageResize($sInImage, $sOutImage, $iW, $iH)
; Parameters ....: $sInImage  - Full path to the image to resize / convert.
;                               In types: *.bmp, *.gif, *.ico, *.jpg, *.jpeg, *.png, *.tif, *.tiff
;                  $sOutImage - Full path where to save the resized / converted image.
;                               Out types: *.bmp, *.gif, *.jpg, *.jpeg, *.png, *.tif, *.tiff
;                  $iW        - Width to resize image to.
;                  $iH        - Height to resize image to.
; Return values .: Success    - Return 1 and @error 0
;                  Failure    - Return 0 and @error 1~5
;                               @error 1 = In File does not exist
;                               @error 2 = In File format not supported
;                               @error 3 = Out File path does not exist
;                               @error 4 = Out file format not supported
;                               @error 5 = Resize Width or Height not an integer
; Author ........: smashly
; ====================================================================================================

Func _ImageResize($sInImage, $sOutImage, $iW, $iH)
    Local $sOP, $sOF, $sInExt, $Ext, $hBitmap, $hImage1, $hImage2, $hGraphic, $CLSID, $i = 0
    Local $sType = "BMP|GIF|ICO|JPG|JPEG|PNG|TIF|TIFF"
    
    If Not FileExists($sInImage) Then Return SetError(1, 0, 0)
    $sInExt = StringUpper(StringTrimLeft($sInImage, StringInStr($sInImage, ".", 0, -1)))
    If Not StringRegExp($sInExt, "\A(" & $sType & ")\z", 0) Then Return SetError(2, 0, 0)
    
    ;OutFile path, to use later on.
    $sOP = StringLeft($sOutImage, StringInStr($sOutImage, "\", 0, -1))
    If Not FileExists($sOP) Then Return SetError(3, 0, 0)
    
    ;OutFile name, to use later on.
    $sOF = StringTrimLeft($sOutImage, StringInStr($sOutImage, "\", 0, -1))

    ;OutFile extension , to use for the encoder later on.
    $Ext = StringUpper(StringTrimLeft($sOutImage, StringInStr($sOutImage, ".", 0, -1)))
    If Not StringRegExp($Ext, "\A(" & $sType & ")\z", 0) Or $Ext = "ICO" Then Return SetError(4, 0, 0)

    If Not IsInt($iW) And Not IsInt($iH) Then Return SetError(5, 0, 0)
    
    ;Start GDIPlus
    _GDIPlus_Startup()
    
    
    $hImage2 = _GDIPlus_ImageLoadFromFile($sInImage)
    $ImageHeight = (_GDIPlus_ImageGetHeight($hImage2)) * 1.25
    $ImageWidth = (_GDIPlus_ImageGetWidth($hImage2)) * 1.25
    
    ; WinAPI to create blank bitmap at the width and height to put your resized image on.
    $hBitmap = _WinAPI_CreateBitmap($ImageWidth, $ImageHeight, 1, 32)

   

    ;Get the handle of blank bitmap you created above as an image
    $hImage1 = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap)

    ;Load the image you want to resize.
    ;$hImage2 = _GDIPlus_ImageLoadFromFile($sInImage)

    ;Get the graphic context of the blank bitmap
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage1)
    
    $ImageHeight = _GDIPlus_ImageGetHeight($hImage2)
    $ImageWidth = _GDIPlus_ImageGetWidth($hImage2)
    ;Draw the loaded image onto the blank bitmap at the size you want
    _GDIPlus_GraphicsDrawImageRect($hGraphic, $hImage2, 0, 0, $ImageWidth * 1.25, $ImageHeight * 1.25)

    ;Get the encoder of to save the resized image in the format you want.
    $CLSID = _GDIPlus_EncodersGetCLSID($Ext)

    ;Generate a number for out file that doesn't already exist, so you don't overwrite an existing image.
    ;Do
    ;    $i += 1
    ;Until (Not FileExists($sOP & $i & "_" & $sOF))

    ;Prefix the number to the begining of the output filename
    $sOutImage =  @TempDir & '\OCR_ImageResize.jpg'
    FileDelete($sOutImage)
    ;Save the new resized image.
    _GDIPlus_ImageSaveToFileEx($hImage1, $sOutImage, $CLSID)

    ;Clean up and shutdown GDIPlus.
    _GDIPlus_ImageDispose($hImage1)
    _GDIPlus_ImageDispose($hImage2)
    _GDIPlus_GraphicsDispose($hGraphic)
    _WinAPI_DeleteObject($hBitmap)
    _GDIPlus_Shutdown()
    Return SetError(0, 0, 1)
EndFunc   ;==>_ImageResize
Link to comment
Share on other sites

Yeah. This can/will be very useful =D

@OP

Good job! I modified it though...

@All

Here is my modification. Has error checking to make sure it doesn't 'crash' on you.

Resizes the image 1.25x. This enables it to better 'read' the characters.

Removed the cursor from the capture.

Removed the ridiculously long white-space at the end of the OCR'd text.

Haha, @ Szhlopp Just Call you BASF, you know the commercial? We didn't make the ????, we just made it better.. LOL Good Job to BOTH

edit:Now someone needs to make the OCR'd text paste into the format it is taken from the image with spaces and @CRLFS and such...

Edited by gesller
Link to comment
Share on other sites

Does not seem to work for me. At all. I tried a bunch of different windows, including MSW 2007. It does not read any characters. At all.

[left][sub]We're trapped in the belly of this horrible machine.[/sub][sup]And the machine is bleeding to death...[/sup][sup][/sup][/left]

Link to comment
Share on other sites

Does not seem to work for me. At all. I tried a bunch of different windows, including MSW 2007. It does not read any characters. At all.

You FOR SURE have the OCR features installed?

run "Regedit"

Look under HKCR

See if you have a "MODI.Document" folder

I didn't modify this to handle the different errors. So if you don't have it installed it will still say "Error reading text".

Link to comment
Share on other sites

Hmm. I thought I did. I guess not :)

But you do have Office 03/07?

Just run the 'Change' feature under control panel on Office. You'll be able to install the OCR features from there=)

Link to comment
Share on other sites

Is it possible to at the coordinates to the Func OCR($Image)?

Sure. Just modify the script.

_ScreenCapture_Capture($Image,$x,$y,$x+$w,$y+$h, False)

This line is what grabs the image. Just change them to direct coordinates or modify the function itself to take in these parameters :)

Link to comment
Share on other sites

Sure. Just modify the script.

_ScreenCapture_Capture($Image,$x,$y,$x+$w,$y+$h, False)

This line is what grabs the image. Just change them to direct coordinates or modify the function itself to take in these parameters :)

No sorry, that was not what i mean.

I want the function:

Func OCR($Image)
    Local $Array[1], $Word
    $miDoc = ObjCreate("MODI.Document")
    $miDoc.Create($Image)
    If @error Then Return SetError(1)
    $miDoc.Ocr(9, True, True)
    If @error Then Return SetError(1)
    For $Word In $miDoc.Images(0).Layout.Words
        _ArrayAdd($Array,$Word.text)
    Next
    Return $Array
EndFunc

to also give me back the coordinates of the find letters

Link to comment
Share on other sites

  • 3 weeks later...

No sorry, that was not what i mean.

I want the function:

Func OCR($Image)
    Local $Array[1], $Word
    $miDoc = ObjCreate("MODI.Document")
    $miDoc.Create($Image)
    If @error Then Return SetError(1)
    $miDoc.Ocr(9, True, True)
    If @error Then Return SetError(1)
    For $Word In $miDoc.Images(0).Layout.Words
        _ArrayAdd($Array,$Word.text)
    Next
    Return $Array
EndFunc

to also give me back the coordinates of the find letters

seems kind of involved - I'm imagining a 2-dimensional array in which the first dimension is the character, the second is the topleft position for that word, and the third is the bottom right position, eg:

OutputArray[0][0] = word

OutputArray[0][1] = topleft coordinate in x y format

OutputArray[0][2] = bottomright coordinate in x y format

OutputArray[1][0] = next word

OutputArray[1][1] = topleft coordinate in x y format

OutputArray[1][2] = bottomright coordinate in x y format

etc...?

I'm not sure if the the objects contain that much information.

Also, Szhlopp, is there a reason you resized the image to 600 x 600? Couldn't that, in some instances be enlarging it too much? or even compressing it? I'm having trouble testing this on extremely small regions - could this be why?

Really though, this is a great program, and both of you guys have done a great job.

You should be very proud.

Link to comment
Share on other sites

seems kind of involved - I'm imagining a 2-dimensional array in which the first dimension is the character, the second is the topleft position for that word, and the third is the bottom right position, eg:

OutputArray[0][0] = word

OutputArray[0][1] = topleft coordinate in x y format

OutputArray[0][2] = bottomright coordinate in x y format

OutputArray[1][0] = next word

OutputArray[1][1] = topleft coordinate in x y format

OutputArray[1][2] = bottomright coordinate in x y format

etc...?

I'm not sure if the the objects contain that much information.

Also, Szhlopp, is there a reason you resized the image to 600 x 600? Couldn't that, in some instances be enlarging it too much? or even compressing it? I'm having trouble testing this on extremely small regions - could this be why?

Really though, this is a great program, and both of you guys have done a great job.

You should be very proud.

Thanks.

You talking about these lines?

_ScreenCapture_Capture($Image,$x,$y,$x+$w,$y+$h, False)
_ImageResize($Image, $Image, 600, 600)

I modified the "_ImageResize" function so that it takes the image width/height * 1.25 . Those parameters are useless now. I was just lazy about removing them :P

Edited by Szhlopp
Link to comment
Share on other sites

edit:Now someone needs to make the OCR'd text paste into the format it is taken from the image with spaces and @CRLFS and such...

Please check my first post. I have updated the script. :P

seems kind of involved - I'm imagining a 2-dimensional array in which the first dimension is the character, the second is the topleft position for that word, and the third is the bottom right position, eg:

OutputArray[0][0] = word

OutputArray[0][1] = topleft coordinate in x y format

OutputArray[0][2] = bottomright coordinate in x y format

OutputArray[1][0] = next word

OutputArray[1][1] = topleft coordinate in x y format

OutputArray[1][2] = bottomright coordinate in x y format

etc...?

I'm not sure if the the objects contain that much information.

Kindly check here.

It might give you some ideas.

Good luck.

[font="Comic Sans MS"]-ronriel[/font][topic="48542"]r4r media player[/topic][topic="80836"]OCR & Paste[/topic]

Link to comment
Share on other sites

This works great! I'm relatively new to AutoIT and trying to modify this to be a console application that recieves the $X, $Y, $W, & $H as input and prints out the text. I want this to end up being a function I can call from other programs that returns the text found in the region.

My problem is that with virtually the identical code minus all the GUI stuff and compiled with /console works sometimes but throws COM errors during the $miDoc.Ocr() function a lot of the times also. Are their any tips or tricks involved in turning this into a console application that prints out the text found?

The output when it works is:

GetOCRByRegion.exe 54 5 113 29

coordinates: 54, 5, 113, 29

ObjCreate - The error value is: 0

Create - The error value is: 0

Ocr - The error value is: 0

LOGON

OCR: LOGON

The output when it fails is:

GetOCRByRegion.exe 54 5 113 29

coordinates: 54, 5, 113, 29

ObjCreate - The error value is: 0

Create - The error value is: 0

The code I have is:

;autoit-v3.2.12.1
;Tested  Windows XP SP2 with MS Office 2007

#Include "..\install\Include\ScreenCapture.au3"
;#include "..\install\Include\GUIConstantsEx.au3"
;#include "..\install\Include\WindowsConstants.au3"
#Include "..\install\Include\Misc.au3"
#include "..\install\Include\Array.au3"
#include "..\install\Include\GDIPlus.au3"
;#include "..\install\Include\WinAPI.au3"


Local $v = 0, $X,$Y,$W,$H, $Image = @TempDir&"\ScreenCapture.jpg", $Error = False
Local $oMyError = ObjEvent("AutoIt.Error","MyErrFunc")

$X = $CmdLine[1]
$W = $CmdLine[3] - $CmdLine[1]
$Y = $CmdLine[2]
$H = $CmdLine[4] - $CmdLine[2]

Capture()
    

Func Capture()
    _ScreenCapture_SetJPGQuality(100)
    _ScreenCapture_Capture($Image,$X,$Y,$X+$W,$Y+$H, False)
    ConsoleWrite("coordinates: " & $X & ", " & $Y & ", " & $X+$W & ", " & $Y+$H & @CRLF)
    _ImageResize($Image, $Image, 600, 600)
    $OCR = OCR($Image)
    ConsoleWrite("OCR: " & $OCR & @CRLF)
EndFunc

Func OCR($Image)
    Local $miDoc, $Doc
    Local $str
    Local $oWord
    Local $sArray[500]
    Local $as_Text = ""
    
    
    $miDoc = ObjCreate("MODI.Document")
    ConsoleWrite("ObjCreate - The error value is: " & @error & @CRLF)
    $miDoc.Create(@TempDir & '\OCR_ImageResize.jpg')
    ConsoleWrite("Create - The error value is: " & @error & @CRLF)
    $miDoc.Ocr(9, False, False)
    ConsoleWrite("Ocr - The error value is: " & @error & @CRLF)
    
    If $Error = False Then
        $i = 0
        For $oWord in $miDoc.Images(0).Layout.Words

            $str = $str & $oWord.text & @CrLf
                ConsoleWrite($oWord.text & @CRLF)
            $sArray [$i] = $oWord.text
            $i += 1
            
        Next

        $as_Text = _ArrayToString($sArray," ",0)
        $as_Text = StringReplace($as_Text, "  ", "", 0)
        
    ElseIf $Error = True Then
        
        $Error = False
    EndIf
    
    $miDoc.Close(False)
    Return $as_Text

EndFunc


;------------------------------ This is a COM Error handler --------------------------------
Func MyErrFunc()
    ConsoleWrite("COM Error Found." & @CRLF)
    $Error = True
Endfunc


; #FUNCTION# =========================================================================================
; Name...........: _ImageResize
; Description....: Resize an image and optionally convert it to the format you want.
; Syntax.........: _ImageResize($sInImage, $sOutImage, $iW, $iH)
; Parameters ....: $sInImage  - Full path to the image to resize / convert.
;                              In types: *.bmp, *.gif, *.ico, *.jpg, *.jpeg, *.png, *.tif, *.tiff
;                 $sOutImage - Full path where to save the resized / converted image.
;                              Out types: *.bmp, *.gif, *.jpg, *.jpeg, *.png, *.tif, *.tiff
;                 $iW       - Width to resize image to.
;                 $iH       - Height to resize image to.
; Return values .: Success  - Return 1 and @error 0
;                 Failure   - Return 0 and @error 1~5
;                              @error 1 = In File does not exist
;                              @error 2 = In File format not supported
;                              @error 3 = Out File path does not exist
;                              @error 4 = Out file format not supported
;                              @error 5 = Resize Width or Height not an integer
; Author ........: smashly
; ====================================================================================================


Func _ImageResize($sInImage, $sOutImage, $iW, $iH)
    Local $sOP, $sOF, $sInExt, $Ext, $hBitmap, $hImage1, $hImage2, $hGraphic, $CLSID, $i = 0
    Local $sType = "BMP|GIF|ICO|JPG|JPEG|PNG|TIF|TIFF"
    
    If Not FileExists($sInImage) Then Return SetError(1, 0, 0)
    $sInExt = StringUpper(StringTrimLeft($sInImage, StringInStr($sInImage, ".", 0, -1)))
    If Not StringRegExp($sInExt, "\A(" & $sType & ")\z", 0) Then Return SetError(2, 0, 0)
    
   ;OutFile path, to use later on.
    $sOP = StringLeft($sOutImage, StringInStr($sOutImage, "\", 0, -1))
    If Not FileExists($sOP) Then Return SetError(3, 0, 0)
    
   ;OutFile name, to use later on.
    $sOF = StringTrimLeft($sOutImage, StringInStr($sOutImage, "\", 0, -1))

   ;OutFile extension , to use for the encoder later on.
    $Ext = StringUpper(StringTrimLeft($sOutImage, StringInStr($sOutImage, ".", 0, -1)))
    If Not StringRegExp($Ext, "\A(" & $sType & ")\z", 0) Or $Ext = "ICO" Then Return SetError(4, 0, 0)

    If Not IsInt($iW) And Not IsInt($iH) Then Return SetError(5, 0, 0)
    
   ;Start GDIPlus
    _GDIPlus_Startup()
    
    
    $hImage2 = _GDIPlus_ImageLoadFromFile($sInImage)
    $ImageHeight = (_GDIPlus_ImageGetHeight($hImage2)) * 1.25
    $ImageWidth = (_GDIPlus_ImageGetWidth($hImage2)) * 1.25
    
   ; WinAPI to create blank bitmap at the width and height to put your resized image on.
    $hBitmap = _WinAPI_CreateBitmap($ImageWidth, $ImageHeight, 1, 32)

   

   ;Get the handle of blank bitmap you created above as an image
    $hImage1 = _GDIPlus_BitmapCreateFromHBITMAP($hBitmap)

   ;Load the image you want to resize.
   ;$hImage2 = _GDIPlus_ImageLoadFromFile($sInImage)

   ;Get the graphic context of the blank bitmap
    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage1)
    
    $ImageHeight = _GDIPlus_ImageGetHeight($hImage2)
    $ImageWidth = _GDIPlus_ImageGetWidth($hImage2)
   ;Draw the loaded image onto the blank bitmap at the size you want
    _GDIPlus_GraphicsDrawImageRect($hGraphic, $hImage2, 0, 0, $ImageWidth * 1.25, $ImageHeight * 1.25)

   ;Get the encoder of to save the resized image in the format you want.
    $CLSID = _GDIPlus_EncodersGetCLSID($Ext)

   ;Generate a number for out file that doesn't already exist, so you don't overwrite an existing image.
   ;Do
   ;    $i += 1
   ;Until (Not FileExists($sOP & $i & "_" & $sOF))

   ;Prefix the number to the begining of the output filename
    $sOutImage =  @TempDir & '\OCR_ImageResize.jpg'
    FileDelete($sOutImage)
   ;Save the new resized image.
    _GDIPlus_ImageSaveToFileEx($hImage1, $sOutImage, $CLSID)

   ;Clean up and shutdown GDIPlus.
    _GDIPlus_ImageDispose($hImage1)
    _GDIPlus_ImageDispose($hImage2)
    _GDIPlus_GraphicsDispose($hGraphic)
    _WinAPI_DeleteObject($hBitmap)
    _GDIPlus_Shutdown()
    Return SetError(0, 0, 1)
EndFunc  ;==>_ImageResize

Thanks,

BotWriter

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...