Jump to content

Recommended Posts

Posted

Program ends with no message or error on Win11. 

 

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=snapshot_v5_OCR.exe
#AutoIt3Wrapper_UseX64=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=snapshot.ico
#AutoIt3Wrapper_Res_Icon_Add=snapshot_inv.ico
;#AutoIt3Wrapper_Res_HiDpi=Y



DllCall("User32.dll","bool","SetProcessDPIAware")       ;ensures correct monitor resolution on Win10


;coded by Dolik
;used part of codes from UEZ and FireFox work

#include <GDIPlus.au3>
#include <WindowsConstants.au3>
#Include <ScreenCapture.au3>
#Include <Misc.au3>
#include <GDIPlus.au3>
#include <Clipboard.au3>
;~ #include <WinAPIRes.au3>
;~ #include <Constants.au3>

Opt("TrayAutoPause", 0)
Opt("TrayOnEventMode", 1)
Opt("TrayMenuMode", 3)

;~ ; Create tray menu
;~ TrayCreateItem("Exit")

;~ TrayItemSetOnEvent(TrayCreateItem("Inverzní uložení"), "On_Inverzni")
;~ TrayCreateItem("")

TrayItemSetOnEvent(TrayCreateItem("Picture Folder"), "_Open_Picture_Folder")
TrayCreateItem("") ; Create a separator line.
$idOCR=TrayCreateItem("OCR")
TrayItemSetOnEvent($idOCR,"_OCR")
TrayCreateItem("") ; Create a separator line.
TrayItemSetOnEvent(TrayCreateItem("Exit"), "On_Exit")

Global $iX1, $iY1, $iX2, $iY2, $aPos, $sMsg, $picture_name, $picture_extension, $picture_path, $picture_counter, $inverse=0, $hTimer, $traytip=0, $stop_executing=0
Global $script = True
Global $picture_name = "File0001"
Global $picture_extension = "jpg"
Global $tesseract_temp_path = @ScriptDir


; Get total monitors resolution
$FW = DLLCall("user32.dll", "int", "GetSystemMetrics", "int", 78)[0]
$FH = DLLCall("user32.dll", "int", "GetSystemMetrics", "int", 79)[0]
ConsoleWrite($FW &"  "& $FH)


TraySetState(1)
TraySetToolTip("SnapShot - Fast screen catcher")

; Tray context menu only opened by right click
TraySetClick(8)


TraySetOnEvent(-13, "TrayEvent");mouse primarydouble
TraySetOnEvent(-14, "TrayEvent");mouse secondarydouble
;~ TraySetOnEvent(-11, "TrayEvent");mouse over tray icon


HotKeySet("^q" , "SnapShot")
TrayTip("", "Use CTRL+q to Snapshot!",2)

While 1
Sleep(500)
_Timer()
WEnd

Func _OCR()
$OCRchecked = TrayItemGetState ( $idOCR )
IF $OCRchecked = 68 then
    TrayItemSetState ( $idOCR, 65 )
ElseIf $OCRchecked = 65 Then
    TrayItemSetState ( $idOCR, 68 )
EndIf


ConsoleWrite(@crlf & $OCRchecked)




EndFunc



Func On_Exit()
    Exit
EndFunc

Func SnapShot()
    Switch Mark_Rect()

        Case -1
            Return
        Case 0
            Mark_Rect()
        Case 0 To 5
            File_Counter()

            Switch  TrayItemGetState ( $idOCR )
                Case 65
                    ConsoleWrite(@CRLF & "ORC on")
                    Local $giTIFColorDepth = 24
                    Local $giTIFCompression = $GDIP_EVTCOMPRESSIONNONE
                    Local $Ext=""
                    $picture_path = @scriptdir & "\" & $picture_name & ".tif"
                    $hBitmap2 = _ScreenCapture_Capture("",$iX1, $iY1, $iX2, $iY2, False)
                    _GDIPlus_Startup ()

                    ; Convert the image to a bitmap
                    $hImage2 = _GDIPlus_BitmapCreateFromHBITMAP ($hBitmap2)

                    $hWnd = _WinAPI_GetDesktopWindow()
                    $hDC = _WinAPI_GetDC($hWnd)
                    $hBMP = _WinAPI_CreateCompatibleBitmap($hDC, $iX2*3-$iX1*3, $iY2*3-$iY1*3)

                    _WinAPI_ReleaseDC($hWnd, $hDC)
                    $hImage1 = _GDIPlus_BitmapCreateFromHBITMAP ($hBMP)
                    $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage1)
                    _GDIPLus_GraphicsDrawImageRect($hGraphic, $hImage2, 0, 0, ($iX2-$iX1)*3, ($iY2-$iY1)*3)
                    $CLSID = _GDIPlus_EncodersGetCLSID($Ext)

                    ; 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, $picture_path, $CLSID, $pParams)
                    _GDIPlus_ImageDispose($hImage1)
                    _GDIPlus_ImageDispose($hImage2)
                    _GDIPlus_GraphicsDispose ($hGraphic)
                    _WinAPI_DeleteObject($hBMP)
                    _GDIPlus_Shutdown()




;~

;~              _WinAPI_SetSystemCursor(0,$IDC_WAIT)


                ShellExecuteWait(@scriptdir & "\tesseract-OCR\tesseract.exe", '"' & $picture_path & '"' & " -l ces " &'"' & @scriptdir & "\" & $picture_name & '"',"","open",@SW_HIDE)
;~              ShellExecuteWait("tesseract.exe", '"' & $picture_path & '"' & " -l ces " & '"' & @scriptdir & "\" & $picture_name & '"')

;~              _WinAPI_SetSystemCursor(0,$IDC_ARROW)

                ConsoleWrite(@crlf & '"' & $picture_path & '"' & " -l ces " & '"' & @scriptdir & "\" & $picture_name & '"')
                $final_ocr = FileRead(@scriptdir & "\" & $picture_name & ".txt")
                ConsoleWrite(@crlf & "OCR:" & $final_ocr)
                _ClipBoard_Open(0)
                _ClipBoard_Empty()
                _ClipBoard_SetData($final_ocr, $CF_UNICODETEXT)
                _ClipBoard_GetData()
                _ClipBoard_Close()

                Case Else
                    Switch $inverse
                    Case 0
                    _ScreenCapture_Capture($picture_path, $iX1, $iY1, $iX2, $iY2, False)
                    $hBitmap=_ScreenCapture_Capture("", $iX1, $iY1, $iX2, $iY2, False)
                    _ClipBoard_Open(0)
                    _ClipBoard_Empty()
                    _ClipBoard_SetDataEx($hBitmap, $CF_BITMAP )
                    _ClipBoard_Close()
                    _WinAPI_DeleteObject($hBitmap)
                    Case 1
                    _Invert(); save and copy to clipboard included in function
                    EndSwitch

                EndSwitch
    EndSwitch




EndFunc


Func File_Counter()


        While FileExists(@scriptdir & "\" & $picture_name & "." & $picture_extension) = 1
            $picture_counter = StringTrimLeft($picture_name,4)
            $picture_counter = Number($picture_counter)

            $picture_counter = $picture_counter + 1
              While StringLen( $picture_counter)<4
                $picture_counter = "0" & $picture_counter
              WEnd
            $picture_name = "File" & $picture_counter
            ConsoleWrite(@crlf & $picture_counter)
        WEnd

$picture_path = @scriptdir & "\" & $picture_name & "." & $picture_extension

EndFunc


Func Mark_Rect()

    Local $aMouse_Pos, $hMask, $hMaster_Mask, $iTemp,$exit_loop=0
    Local $UserDLL = DllOpen("user32.dll")
Do
    While  _IsPressed("02", $UserDLL)
        Sleep(50)
    Wend
    Sleep(200)
    ; Create transparent GUI with Cross cursor
    $hCross_GUI = GUICreate("Test", $FW, $FH, 0, 0, $WS_POPUP, $WS_EX_TOPMOST)

    WinSetTrans($hCross_GUI, "", 1)
    GUISetState(@SW_SHOW, $hCross_GUI)
    GUISetCursor(3, 1, $hCross_GUI)

    Global $hRectangle_GUI = GUICreate("", $FW, $FH, 0, 0, $WS_POPUP, $WS_EX_TOOLWINDOW + $WS_EX_TOPMOST)
    GUISetBkColor(0x000000,$hRectangle_GUI)


    ; Wait until mouse button pressed (left or right)
    While Not _IsPressed("01", $UserDLL)
        If _IsPressed("02", $UserDLL) Then
        Sleep(200); wait to not show desktop content menu on right click
         GUIDelete($hRectangle_GUI)
         GUIDelete($hCross_GUI)
         DllClose($UserDLL)
;~       Sleep(200)
;~       _SendMessage(GUICtrlCreateContextMenu (), 0x001F)
        Return -1;cancel snaphot
        EndIf
        Sleep(10)
    WEnd

    ; Get first mouse position
    $aMouse_Pos = MouseGetPos()
    $iX1 = $aMouse_Pos[0]
    $iY1 = $aMouse_Pos[1]

    ; Draw rectangle while mouse button pressed


     While _IsPressed("01", $UserDLL)
        If _IsPressed("02", $UserDLL) Then
            While _IsPressed("01", $UserDLL)
                Sleep(50)
            Wend
            Sleep(200)
             GUIDelete($hRectangle_GUI)
             GUIDelete($hCross_GUI)

        $exit_loop=0
        ExitLoop
        Else

        $aMouse_Pos = MouseGetPos()

        $hMaster_Mask = _WinAPI_CreateRectRgn(0, 0, 0, 0)
        $hMask = _WinAPI_CreateRectRgn($iX1,  $aMouse_Pos[1], $aMouse_Pos[0],  $aMouse_Pos[1] + 1) ; Bottom of rectangle
        _WinAPI_CombineRgn($hMaster_Mask, $hMask, $hMaster_Mask, 2)
        _WinAPI_DeleteObject($hMask)
        $hMask = _WinAPI_CreateRectRgn($iX1, $iY1, $iX1 + 1, $aMouse_Pos[1]) ; Left of rectangle
        _WinAPI_CombineRgn($hMaster_Mask, $hMask, $hMaster_Mask, 2)
        _WinAPI_DeleteObject($hMask)
        $hMask = _WinAPI_CreateRectRgn($iX1 + 1, $iY1 + 1, $aMouse_Pos[0], $iY1) ; Top of rectangle
        _WinAPI_CombineRgn($hMaster_Mask, $hMask, $hMaster_Mask, 2)
        _WinAPI_DeleteObject($hMask)
        $hMask = _WinAPI_CreateRectRgn($aMouse_Pos[0], $iY1, $aMouse_Pos[0] + 1,  $aMouse_Pos[1]) ; Right of rectangle
        _WinAPI_CombineRgn($hMaster_Mask, $hMask, $hMaster_Mask, 2)
        _WinAPI_DeleteObject($hMask)
        ; Set overall region
        _WinAPI_SetWindowRgn($hRectangle_GUI, $hMaster_Mask)

        _WinAPI_DeleteObject($hMaster_Mask)

        If WinGetState($hRectangle_GUI) < 15 Then GUISetState()
        GUISetBkColor(0x000000,$hRectangle_GUI)
        Sleep(10)
        GUISetBkColor(0xFFFFFF,$hRectangle_GUI)
        $exit_loop=1
        EndIf
    WEnd

    Until $exit_loop=1


    ; Get second mouse position
    $iX2 = $aMouse_Pos[0]
    $iY2 = $aMouse_Pos[1]

    ; Set in correct order if required
    If $iX2 < $iX1 Then
        $iTemp = $iX1
        $iX1 = $iX2
        $iX2 = $iTemp
    EndIf
    If $iY2 < $iY1 Then
        $iTemp = $iY1
        $iY1 = $iY2
        $iY2 = $iTemp
    EndIf

    GUIDelete($hRectangle_GUI)
    GUIDelete($hCross_GUI)
    DllClose($UserDLL)
    Return 1
EndFunc   ;==>Mark_Rect



Func _Invert()
    _GDIPlus_Startup() ;initialize GDI+
    Local $hHBmp = _ScreenCapture_Capture("", $iX1, $iY1, $iX2-1, $iY2-1) ;create a GDI bitmap by capturing an area on desktop
    Local $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hHBmp) ;convert GDI to GDI+ bitmap
    Local $hIA = _GDIPlus_ImageAttributesCreate() ;create an ImageAttribute object
    Local $tColorMatrix = _GDIPlus_ColorMatrixCreateNegative() ;create negative color matrix
    _GDIPlus_ImageAttributesSetColorMatrix($hIA, 0, True, $tColorMatrix) ;set negative color matrix

    _WinAPI_DeleteObject($hHBmp) ;release GDI bitmap resource because not needed anymore

    Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hBitmap)

    _GDIPlus_GraphicsDrawImageRectRect($hGraphics, $hBitmap, 0, 0, $iX2-$iX1, $iY2-$iY1,0, 0,$iX2-$iX1, $iY2-$iY1, $hIA) ;draw the bitmap to backbuffer  while applying the color adjustment

    _GDIPlus_ImageSaveToFile($hBitmap,$picture_path)

            _ClipBoard_Open(0)
            _ClipBoard_Empty()
            $hBitmap1=_GDIPlus_ImageCreateGDICompatibleHBITMAP($hBitmap)
            _ClipBoard_SetDataEx($hBitmap1, $CF_BITMAP )
            _ClipBoard_Close()
            _WinAPI_DeleteObject($hBitmap1)

            ;cleanup GDI+ resources

            _GDIPlus_ImageAttributesDispose($hIA)
            _GDIPlus_GraphicsDispose($hGraphics)
            _GDIPlus_BitmapDispose($hBitmap)
            _GDIPlus_Shutdown()

EndFunc   ;==>Example


Func TrayEvent()
    Switch @TRAY_ID ; Check the last tray item identifier.
        Case -13
            Sleep(100)
            Snapshot()

        Case -14
            If $inverse=0 Then
                $inverse=1
                TraySetIcon("snapshot_inv.ico")
                TrayTip("", "Inverse color mode",1)
                $hTimer = TimerInit()
                $traytip = 1

            Else
                $inverse=0
                TraySetIcon("snapshot.ico")
                TrayTip("", "Normal color mode",1)
                $hTimer = TimerInit()
                $traytip = 1
            EndIf

    EndSwitch
EndFunc   ;==>TrayEvent


Func _Timer()
If $traytip = 1 Then
If NOT $hTimer then $hTimer = TimerInit()
$TIMER = TimerDiff($hTimer)
If $TIMER > 1*1000 then
TrayTip("", "",0); kills tray tip after  one second
$traytip = 0
EndIf
EndIf

EndFunc

;=============================================================================== ;
; Function Name:   _GDIPlus_ImageCreateGDICompatibleHBITMAP
; Description::    Converts a GDIPlus-Image to GDI-combatible HBITMAP
; Parameter(s):    $hImg -> GDIplus Image object ; Requirement(s):  GDIPlus.au3
; Return Value(s): HBITMAP, compatible with ClipBoard
; Author(s):       Prog@ndy ;
;===============================================================================
 Func _GDIPlus_ImageCreateGDICompatibleHBITMAP($hImg)
    Local $hBitmap2 = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImg)
    Local $hBitmap = _WinAPI_CopyImage($hBitmap2,0,0,0,$LR_COPYDELETEORG+$LR_COPYRETURNORG)
    _WinAPI_DeleteObject($hBitmap2)
    Return $hBitmap
EndFunc


Func _Open_Picture_Folder()
    Run("explorer.exe " & @ScriptDir)
EndFunc

 

  • Developers
Posted

Moved to the appropriate AutoIt General Help and Support forum, as the Developer General Discussion forum very clearly states:

Quote

General development and scripting discussions.


Do not create AutoIt-related topics here, use the AutoIt General Help and Support or AutoIt Technical Discussion forums.

Moderation Team

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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
×
×
  • Create New...