PsAdbScreenCap.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<#
.SYNOPSIS
.EXAMPLE
    PS C:\temp> hhd-android-adb-screencap2 -Verbose
    자세한 정보 표시: ReadAllBytes ...
    자세한 정보 표시: convert ...
    자세한 정보 표시: WriteAllBytes ...
    C:\temp\screencapture-171025-001627.png
#>

function adb-screencap
{
    [CmdletBinding()]
    param
    (
    )

    

    Write-Debug "adb shell screencap -p ..."
    adb start-server
    $fileName = "screencapture-$([datetime]::Now.ToString(`"yyMMdd-HHmmss`")).png"


    [string] $permissionTestStr = (adb shell screencap -p /sdcard/test.png)
    [bool] $canScreenBufSol = $false

    if ($permissionTestStr -like "*error*")
    {
        Write-Debug "adb perm error ..."
        $canScreenBufSol = $true
    }
    else 
    {
        Write-Debug "adb solution ..."
        adb shell screencap -p "/sdcard/$fileName"
        adb pull "/sdcard/$fileName" $fileName

        if ((ls $fileName).Length -eq 0) 
        {
            $canScreenBufSol = $true
        }
    }

    if ($canScreenBufSol) 
    {
        Write-Debug "screen buffer solution ..."

        $cmd = "cmd.exe /C `"adb shell screencap -p > $fileName`""
        Invoke-Expression -Command $cmd
        
    
        Write-Verbose "ReadAllBytes ..."
        $fileBytes = [System.IO.File]::ReadAllBytes("$pwd\$fileName")
    
    
        Write-Verbose "convert ..."
        $newFileBytes = New-Object System.Collections.Generic.List[byte]
    
        for($i = 0; $i -lt $fileBytes.Length - 2; $i = $i + 1) 
        {
            if (($fileBytes[$i] -eq 0x0D) -and 
                ($fileBytes[$i + 1] -eq 0x0D) -and 
                ($fileBytes[$i + 2] -eq 0x0A))
            {
                $newFileBytes.Add(0x0A)
                $i = $i + 2
            }
            else 
            {
                $newFileBytes.Add($fileBytes[$i])
            }
        }
    
        Write-Verbose "WriteAllBytes ..."
        [System.IO.File]::WriteAllBytes("$pwd\$fileName", $newFileBytes)        
    }

    return "$pwd\$fileName"
}