Sign in to follow this  
Followers 0
chancity

Dynamic Screen Capture

9 posts in this topic

#1 ·  Posted (edited)

Hello,

I'm creating an automation tool that captures views from a tool called Cascade Pilot (Network Analysis Software) I currently have everything work as need but would like some advice of how I could clean up the code to help it perform better I.E. error handling.

The views can consist of 0 - 4 views and the views can be resized, I currently have the script doing a "ImageGridSearch"  quad.png (its a point that is always at the top right corner of each view) that will always be between filtertop.png (always at the top left corner that contains views) and filterbottom.png (always at the bottom right corner that contains the views). It works well but I don't like how the function "ScreenCapture" is currently setup because it is static and is a pain in the ass to add support for lower resolutions.

Thanks in advanced!

Edit - Also I've tried to use AutoInfo to get control names but it seems that all of the controls are embedded and can't be manipulated with AutoIt. I can see the visible text and hidden text within the window though.

Func TakeCapture()
    $x_ratio = @DesktopWidth / 1920
    $y_ratio = @DesktopHeight / 1080


    ;Misc..
    Local $tsource[3] = ["IN", "OUT", "AGG"]
    Local $getview = StringSplit(WinGetText("Cascade Pilot Personal Edition", ""), @CRLF, 2)
    Local $viewname = StringRegExp($getview[0], "[A-Z]", 3)
    Local $viewtab = _ArrayFindAll($getview, "DUID", 0, 0, 0, 1)
    Local $viewfoldername = StringReplace(_ArrayToString($viewname), "|", "")
    Local $newfilename = ""

    ;Screen Capture and Data Point save directory
    Local $reportdirectory = XMLParseSimple(FileRead($ConfigFile), "reporting", "baseFolder")
    Local $source = $reportdirectory[0] & "\"
    Local $filename = GUICtrlRead($nExtInput) & "_" & GUICtrlRead($LocInput) & "_" & $tsource[$trafficsource] & "_" & $viewfoldername
    Local $destination = $reportdirectory[0] & "\" & GUICtrlRead($nExtInput) & "\" & GUICtrlRead($LocInput) & "\" & $tsource[$trafficsource] & "\" & $viewfoldername & "\"

    ;Screen Capture Locations
    Local $filtertop = _FindImageCenter(_ImageSearch($pngLocation & "filtertop.png", 0 * $x_ratio, 0 * $y_ratio, 1920 * $x_ratio, 300 * $y_ratio, 90))
    Local $filterbot = _FindImageCenter(_ImageSearch($pngLocation & "filterbot.png", 0 * $x_ratio, 950 * $y_ratio, 1920 * $x_ratio, 1080 * $y_ratio, 90))

    If Not IsArray($filtertop) Or Not IsArray($filterbot) Then
        GUICtrlSetData($lblScriptStatus, "Status: Capture Failed" & @CRLF & @CRLF & @CRLF & @CRLF & "Press  ""HOME""  to try again")
    Else

        DirCreate($destination)
        $newfilename = DupFile($filename & ".png", $destination)

        ScreenCapture($filtertop, $filterbot, $destination, $newfilename, $getview, $viewtab)

        ControlSend("Cascade Pilot Personal Edition", "", "", "{ALT}")
        Sleep(50)
        ControlSend("Cascade Pilot Personal Edition", "", "", "{C}")
        Sleep(50)

        If  ExportFile($source, $destination, $newfilename) Then
            $timer = TimerDiff($timer)/1000
            Local $completetime = "Completed in " & StringLeft($timer, StringInStr($timer, ".") +3) & " second(s)"
            GUICtrlSetData($lblScriptStatus, "Status: Exported " & $newfilename & @CRLF & @CRLF & $completetime & @CRLF & @CRLF  & @CRLF  & "Press  ""HOME""  to continue")
        Else
            GUICtrlSetData($lblScriptStatus, "Status: Failed to export Excel document " & $newfilename & ".xls" & @CRLF & @CRLF & @CRLF & @CRLF & @CRLF  & "Press  ""HOME""  to try again")
        EndIf

        ToolTip("")
    EndIf
EndFunc   ;==>TakeCapture

Func ScreenCapture($filtertop, $filterbot, $destination, $newfilename, $getview, $viewtab)
    If @DesktopWidth = 1920 And @DesktopHeight = 1080 Then
        Local $quad = _FindImageCenter(_ImageGridSearch($pngLocation & "quad.png", $filtertop[0][0] - 12 * $x_ratio, $filtertop[0][1] - 8 * $y_ratio, $filterbot[0][0] + 5 * $x_ratio, $filterbot[0][1] + 8 * $y_ratio, 250 * $x_ratio, 250 * $y_ratio, 20))
        If UBound($quad) > 1 Then
            _ScreenCapture_Capture($destination & $newfilename & ".png", _
                    Round($filtertop[0][0] - 11 * $x_ratio), _
                    Round($filtertop[0][1] + 12 * $y_ratio), _
                    Round($filterbot[0][0] + 5 * $x_ratio), _
                    Round($filterbot[0][1] - 10 * $y_ratio))
        EndIf

        _ScreenCapture_Capture($destination & $newfilename & "_FILTER.png", _
                Round($filtertop[0][0] - 12 * $x_ratio), _
                Round($filtertop[0][1] - 8 * $y_ratio), _
                Round($filterbot[0][0] + 5 * $x_ratio), _
                Round($filterbot[0][1] + 8 * $y_ratio))

        Select
            Case UBound($quad) <= 1
                _ScreenCapture_Capture($destination & $newfilename & ".png", _
                        Round($filtertop[0][0] - 11 * $x_ratio), _
                        Round($filtertop[0][1] + 12 * $y_ratio), _
                        Round($filterbot[0][0] + 5 * $x_ratio), _
                        Round($filterbot[0][1] - 10 * $y_ratio))

            Case UBound($quad) = 2
                _ScreenCapture_Capture($destination & $newfilename & StringUpper(StringReplace($getview[$viewtab[0]], "DUID", "")) & ".png", _
                        Round($filtertop[0][0] - 11 * $x_ratio), _
                        Round($filtertop[0][1] + 12 * $y_ratio), _
                        Round($filterbot[0][0] + 5 * $x_ratio), _
                        Round($quad[1][1] - 19 * $y_ratio))

                _ScreenCapture_Capture($destination & $newfilename & StringUpper(StringReplace($getview[$viewtab[1]], "DUID", "")) & ".png", _
                        Round($filtertop[0][0] - 11 * $x_ratio), _
                        Round($quad[1][1] - 10 * $y_ratio), _
                        Round($filterbot[0][0] + 5 * $x_ratio), _
                        Round($filterbot[0][1] - 10 * $y_ratio))

            Case UBound($quad) = 3
                _ScreenCapture_Capture($destination & $newfilename & StringUpper(StringReplace($getview[$viewtab[0]], "DUID", "")) & ".png", _
                        Round($filtertop[0][0] - 11 * $x_ratio), _
                        Round($filtertop[0][1] + 12 * $y_ratio), _
                        Round($filterbot[0][0] + 5 * $x_ratio), _
                        Round($quad[2][1] - 19 * $y_ratio))

                _ScreenCapture_Capture($destination & $newfilename & StringUpper(StringReplace($getview[$viewtab[1]], "DUID", "")) & ".png", _
                        Round($filtertop[0][0] - 11 * $x_ratio), _
                        Round($quad[1][1] - 10 * $y_ratio), _
                        Round($quad[1][0] + 15 * $x_ratio), _
                        Round($filterbot[0][1] - 10 * $y_ratio))

                _ScreenCapture_Capture($destination & $newfilename & StringUpper(StringReplace($getview[$viewtab[2]], "DUID", "")) & ".png", _
                        Round($quad[1][0] + 25 * $x_ratio), _
                        Round($quad[1][1] - 10 * $y_ratio), _
                        Round($filterbot[0][0] + 5 * $x_ratio), _
                        Round($filterbot[0][1] - 10 * $y_ratio))

            Case UBound($quad) = 4
                _ScreenCapture_Capture($destination & $newfilename & StringUpper(StringReplace($getview[$viewtab[0]], "DUID", "")) & ".png", _
                        Round($filtertop[0][0] - 11 * $x_ratio), _
                        Round($filtertop[0][1] + 12 * $y_ratio), _
                        Round($quad[0][0] + 15 * $x_ratio), _
                        Round($quad[2][1] - 22 * $y_ratio))

                _ScreenCapture_Capture($destination & $newfilename & StringUpper(StringReplace($getview[$viewtab[1]], "DUID", "")) & ".png", _
                        Round($quad[0][0] + 24 * $x_ratio), _
                        Round($filtertop[0][1] + 12 * $y_ratio), _
                        Round($filterbot[0][0] + 5 * $x_ratio), _
                        Round($quad[2][1] - 22 * $y_ratio))

                _ScreenCapture_Capture($destination & $newfilename & StringUpper(StringReplace($getview[$viewtab[2]], "DUID", "")) & ".png", _
                        Round($filtertop[0][0] - 11 * $x_ratio), _
                        Round($quad[2][1] - 10 * $y_ratio), _
                        Round($quad[2][0] + 15 * $x_ratio), _
                        Round($filterbot[0][1] - 10 * $y_ratio))

                _ScreenCapture_Capture($destination & $newfilename & StringUpper(StringReplace($getview[$viewtab[3]], "DUID", "")) & ".png", _
                        Round($quad[2][0] + 24 * $x_ratio), _
                        Round($quad[2][1] - 10 * $y_ratio), _
                        Round($filterbot[0][0] + 5 * $x_ratio), _
                        Round($filterbot[0][1] - 10 * $y_ratio))
        EndSelect
    ElseIf @DesktopWidth = 1366 And @DesktopHeight = 768 Then
        Local $quad = _FindImageCenter(_ImageGridSearch($pngLocation & "quad.png", $filtertop[0][0] - 16 * $x_ratio, $filtertop[0][1] - 12 * $y_ratio, $filterbot[0][0] + 10 * $x_ratio, $filterbot[0][1] + 12 * $y_ratio, 250 * $x_ratio, 250 * $y_ratio, 20))
        If UBound($quad) > 1 Then
            _ScreenCapture_Capture($destination & $newfilename & ".png", _
                    Round($filtertop[0][0] - 12 * $x_ratio), _
                    Round($filtertop[0][1] + 20 * $y_ratio), _
                    Round($filterbot[0][0] + 5 * $x_ratio), _
                    Round($filterbot[0][1] - 15 * $y_ratio))
        EndIf

        _ScreenCapture_Capture($destination & $newfilename & "_FILTER.png", _
                Round($filtertop[0][0] - 16 * $x_ratio), _
                Round($filtertop[0][1] - 12 * $y_ratio), _
                Round($filterbot[0][0] + 10 * $x_ratio), _
                Round($filterbot[0][1] + 12 * $y_ratio))

        Select
            Case UBound($quad) <= 1
                _ScreenCapture_Capture($destination & $newfilename & ".png", _
                        Round($filtertop[0][0] - 10 * $x_ratio), _
                        Round($filtertop[0][1] + 19 * $y_ratio), _
                        Round($filterbot[0][0] + 5 * $x_ratio), _
                        Round($filterbot[0][1] - 15 * $y_ratio))

            Case UBound($quad) = 2
                _ScreenCapture_Capture($destination & $newfilename & StringUpper(StringReplace($getview[$viewtab[0]], "DUID", "")) & ".png", _
                        Round($filtertop[0][0] - 12 * $x_ratio), _
                        Round($filtertop[0][1] + 20 * $y_ratio), _
                        Round($filterbot[0][0] + 5 * $x_ratio), _
                        Round($quad[1][1] - 30 * $y_ratio))

                _ScreenCapture_Capture($destination & $newfilename & StringUpper(StringReplace($getview[$viewtab[1]], "DUID", "")) & ".png", _
                        Round($filtertop[0][0] - 12 * $x_ratio), _
                        Round($quad[1][1] - 12 * $y_ratio), _
                        Round($filterbot[0][0] + 5 * $x_ratio), _
                        Round($filterbot[0][1] - 15 * $y_ratio))

            Case UBound($quad) = 3
                _ScreenCapture_Capture($destination & $newfilename & StringUpper(StringReplace($getview[$viewtab[0]], "DUID", "")) & ".png", _
                        Round($filtertop[0][0] - 12 * $x_ratio), _
                        Round($filtertop[0][1] + 20 * $y_ratio), _
                        Round($filterbot[0][0] + 5 * $x_ratio), _
                        Round($quad[2][1] - 30 * $y_ratio))

                _ScreenCapture_Capture($destination & $newfilename & StringUpper(StringReplace($getview[$viewtab[1]], "DUID", "")) & ".png", _
                        Round($filtertop[0][0] - 12 * $x_ratio), _
                        Round($quad[1][1] - 12 * $y_ratio), _
                        Round($quad[1][0] + 25 * $x_ratio), _
                        Round($filterbot[0][1] - 15 * $y_ratio))

                _ScreenCapture_Capture($destination & $newfilename & StringUpper(StringReplace($getview[$viewtab[2]], "DUID", "")) & ".png", _
                        Round($quad[1][0] + 35 * $x_ratio), _
                        Round($quad[1][1] - 12 * $y_ratio), _
                        Round($filterbot[0][0] + 5 * $x_ratio), _
                        Round($filterbot[0][1] - 15 * $y_ratio))

            Case UBound($quad) = 4
                _ScreenCapture_Capture($destination & $newfilename & StringUpper(StringReplace($getview[$viewtab[0]], "DUID", "")) & ".png", _
                        Round($filtertop[0][0] - 12 * $x_ratio), _
                        Round($filtertop[0][1] + 20 * $y_ratio), _
                        Round($quad[0][0] + 25 * $x_ratio), _
                        Round($quad[2][1] - 30 * $y_ratio))

                _ScreenCapture_Capture($destination & $newfilename & StringUpper(StringReplace($getview[$viewtab[1]], "DUID", "")) & ".png", _
                        Round($quad[0][0] + 35 * $x_ratio), _
                        Round($filtertop[0][1] + 20 * $y_ratio), _
                        Round($filterbot[0][0] + 5 * $x_ratio), _
                        Round($quad[2][1] - 30 * $y_ratio))

                _ScreenCapture_Capture($destination & $newfilename & StringUpper(StringReplace($getview[$viewtab[2]], "DUID", "")) & ".png", _
                        Round($filtertop[0][0] - 12 * $x_ratio), _
                        Round($quad[2][1] - 10 * $y_ratio), _
                        Round($quad[2][0] + 25 * $x_ratio), _
                        Round($filterbot[0][1] - 15 * $y_ratio))

                _ScreenCapture_Capture($destination & $newfilename & StringUpper(StringReplace($getview[$viewtab[3]], "DUID", "")) & ".png", _
                        Round($quad[2][0] + 35 * $x_ratio), _
                        Round($quad[2][1] - 10 * $y_ratio), _
                        Round($filterbot[0][0] + 5 * $x_ratio), _
                        Round($filterbot[0][1] - 15 * $y_ratio))

        EndSelect
    Else
        Return 0
    EndIf
EndFunc   ;==>ScreenCapture

Func _ImageSearch($png, $left, $top, $Xright, $Ybottom, $tolerance)
    Local $errmessage = "Invalid path to: " & @CRLF & @CRLF & "'" & $png & "'"
    If Not FileExists($png) Then Return ToolTip($errmessage, (@DesktopWidth / 2) - (StringLen($errmessage) * 2), 0 * $y_ratio)

    Local $findImage = $png
    Local $result

    If $tolerance > 0 Then $findImage = "*" & $tolerance & " " & $findImage
    $findImage = "*TRANSBLACK " & $findImage

    If @AutoItX64 Then
        $result = DllCall("ImageSearchDLL_x64.dll", "str", "ImageSearch", "int", $left, "int", $top, "int", $Xright, "int", $Ybottom, "str", $findImage)
    Else
        $result = DllCall("ImageSearchDLL.dll", "str", "ImageSearch", "int", $left, "int", $top, "int", $Xright, "int", $Ybottom, "str", $findImage)
    EndIf

    If $result[0] = "0" Then
        $errmessage = "Unable to locate: " & @CRLF & "'" & $png & "'" & @CRLF & "LeftX=" & $left & ", TopY=" & $top & ", RightX=" & $Xright & ", BottomY=" & $Ybottom
        ToolTip($errmessage, (@DesktopWidth / 2) - (StringLen($errmessage) * 2), 0 * $y_ratio)
        Return 0
    EndIf

    Return $result
EndFunc   ;==>_ImageSearch

Func _FindAllImages($png, $left, $top, $Xright, $Ybottom, $tolerance)
    Local $colImages[1]
    Local $colImage
    Local $result
    Local $i = 1

    Do
        $result = _ImageSearch($png, $left, $top, $Xright, $Ybottom, $tolerance)
        If Not IsArray($result) And $i = 1 Then
            Return 0
        ElseIf Not IsArray($result) Then
            Return $colImages
        Else
            ReDim $colImages[$i]

            $colImages[$i - 1] = $result[0]
            $colImage = StringSplit($colImages[$i - 1], "|")

            If (UBound($colImage) >= 4) Then
                $top = $colImage[3] + $colImage[5] + 2 * $y_ratio
            EndIf
        EndIf

        $i += 1
    Until Not IsArray($result)
EndFunc   ;==>_FindAllImages

Func _FindImageCenter($colImagesPos)
    If Not IsArray($colImagesPos) Then Return 0

    Local $i
    Local $colImagePos
    Local $pos[1][2]

    For $i = 1 To UBound($colImagesPos)
        $colImagePos = StringSplit($colImagesPos[$i - 1], "|")

        If (UBound($colImagePos) >= 4) Then
            ReDim $pos[$i][2]

            $x = Int(Number($colImagePos[2]))
            $y = Int(Number($colImagePos[3]))
            $pos[$i - 1][0] = $x + Int(Number($colImagePos[4]) / 2)
            $pos[$i - 1][1] = $y + Int(Number($colImagePos[5]) / 2)
        EndIf
    Next
    Return $pos
EndFunc   ;==>_FindImageCenter

Func _ImageGridSearch($png, $xStart, $yStart, $xEnd, $yEnd, $Xright, $Ybottom, $tolerance)
    Local $nXleft, $nYtop, $nXright, $nYbottom
    Local $colImages[1]
    Local $result
    Local $w = 0

    For $j = 0 To ($yEnd - $yStart) / $Ybottom
        $nYtop = $yStart + $j * $Ybottom
        $nYbottom = $yStart + $Ybottom + $j * $Ybottom
        If $yStart + $Ybottom + $j * $Ybottom > $yEnd Then $nYbottom = $yEnd

        For $i = 0 To ($xEnd - $xStart) / $Xright
            $nXleft = $xStart + $i * $Xright
            $nXright = $xStart + $Xright + $i * $Xright
            If $xStart + $Xright + $i * $Xright > $xEnd Then $nXright = $xEnd

            $result = _ImageSearch($png, $nXleft, $nYtop, $nXright, $nYbottom, $tolerance)

            ;DrawArea($nXleft&","& $nYtop&","& $nXright&","& $nYbottom, $i&","&$j)
            If IsArray($result) Then
                $w = $w + 1
                ReDim $colImages[$w]
                $colImages[$w - 1] = $result[0]
            EndIf

        Next
    Next

    If $w >= 2 Then
        Return $colImages
    Else
        Return 0
    EndIf
EndFunc   ;==>_ImageGridSearch
Edited by chancity

Share this post


Link to post
Share on other sites



I tried seeing if there was a common variable so that I wouldn't need to do that, I couldn't really find one. There is a possible chance that there is going to 0-4 views on the screen. I've attached a screen shot with 4 views. Those views boxes can be shifted around...

 

post-74151-0-06231800-1399311213_thumb.p

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

After playing with AutoIt info alittle more I was able to see the Control information for the views, as in get the size and position of each one and its Advanced (Class) (only thing that seems to be unique). I'm just not sure how to gather the information or just use _ScreenCapture_CaptureWnd to take the screen shot. Any guidance?

Not sure why I didn't see this before, I spent a lot of time toying with _ImageSearch...

Edited by chancity

Share this post


Link to post
Share on other sites

I uploaded a photo with some of the information that I'm able to retrieve....How can I capture that specific control?

What is your issue with _ScreenCapture_CaptureWnd?

 

post-74151-0-32603600-1399319977_thumb.p

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

You can even use WinGetPos on a control, and it will give you exactly what you need.

#include <Array.au3>
Run("notepad.exe")
$h = WinWaitActive("[CLASS:Notepad]")
$a = WinGetPos($h)
$s = _ArrayToString($a)
ConsoleWrite($s & @CRLF)

$h2 = ControlGetHandle($h,"","Edit1")
$a = WinGetPos($h2)
$s = _ArrayToString($a)
ConsoleWrite($s & @CRLF)
Edited by jdelaney
1 person likes this

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

Thanks guys, I'll post my results :)

Share this post


Link to post
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
Sign in to follow this  
Followers 0