【问题标题】:Powershell screencasting via ADB通过 ADB 进行 Powershell 截屏
【发布时间】:2019-07-24 23:21:55
【问题描述】:

我编写这个 powershell 脚本的目的是创建一个 GUI,我可以在其中看到我的 android 屏幕投射到我的计算机上。

该脚本创建一个表单,其中计时器应使用从 adb 中提取的图像更新图片框。该脚本还确保安装了 adb。

我的问题是图片框中的图像没有更新,它只是加载捕获的第一张图像。

我认为这是 adb 没有覆盖现有 png 文件的问题,但我不确定,可能是其他问题。

如果有任何帮助,我将不胜感激!

Set-ExecutionPolicy Bypass -Scope Process -Force
function startTimer() { 
   $timer.start()
   Write-Host "Timer Started"
}
function stopTimer() {
    $timer.Enabled = $false
    Write-Host "Timer Stopped"
    $btnStart.Text = "Continue"
}
function TimerTick()
{
    adb shell rm -f /sdcard/sc.png
    adb shell screencap -p /sdcard/sc.png
    adb pull /sdcard/sc.png $BpathTo
    $image = [System.Drawing.Image]::Fromfile("$BpathTo\sc.png")
    $picb.Image = $image
    $lblLog.Text = (Get-Date).ToString()
    Write-Host "Refreshed at: "(Get-Date).ToString()
    Write-Host ""
}
if (!(Get-Command adb -ErrorAction SilentlyContinue)){
    if (!(Get-Command choco -ErrorAction SilentlyContinue)){
        Write-Host "Downloading Chocolatey Installer to setup ADB (required)"
        iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
    }
    Write-Host " Installing ADB..."
    choco install adb --force -y
}
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 1000
$timer.add_tick({TimerTick})

$BpathTo = (Get-Item -Path ".\").FullName
if ([System.IO.File]::Exists("$BpathTo\sc.png")){
    Remove-Item -Path "$BpathTo\sc.png"
}
adb shell screencap -p /sdcard/sc.png
adb pull /sdcard/sc.png $BpathTo
$image = [drawing.image]::FromFile("$BpathTo\sc.png")
$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "ADB Screencast"
$objForm.Size = New-Object Drawing.Size @(($image.Width+20),($image.Height+85))
$objForm.StartPosition = "CenterScreen"
$objForm.Add_KeyDown({
    if ($_.KeyCode -eq "Escape") 
        {
            $objForm.Close()
        }
    })
$objForm.MinimizeBox=$false
$objForm.MaximizeBox=$false
$objForm.ShowIcon=$false
$objForm.ShowInTaskbar=$false
$halfH=(($image.Width)/2)
$btnStart = New-Object System.Windows.Forms.Button
$btnStart.Location = New-Object System.Drawing.Size(1,$image.Height)
$btnStart.Size = New-Object System.Drawing.Size($halfH,25)
$btnStart.Text = "Start"
$btnStart.Add_Click({StartTimer; })
$objForm.Controls.Add($btnStart)
$btnStop = New-Object System.Windows.Forms.Button
$btnStop.Location = New-Object System.Drawing.Size($halfH,$image.Height)
$btnStop.Size = New-Object System.Drawing.Size($halfH,25)
$btnStop.Text = "Stop"
$btnStop.Add_Click({StopTimer; })
$objForm.Controls.Add($btnStop)
$btnStop.Enabled  = $true
$lblLog = New-Object System.Windows.Forms.Label
$lblLog.Location = New-Object System.Drawing.Size(10,($image.Height+25)) 
$lblLog.Size = New-Object System.Drawing.Size($image.Width,20) 
$lblLog.Text = "Refresh Info:"
$objForm.Controls.Add($lblLog)
$picb = New-Object Windows.Forms.PictureBox
$picb.size = New-Object Drawing.Size @($image.Width,$image.Height)
$picb.Location = New-Object Drawing.Point 1,1
$picb.Image = $image
$objForm.Controls.Add($picb)
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

【问题讨论】:

  • 我认为你应该将你的函数向上移动一点(在 Set-ExecutionPolicy 的下方会是一个好地方),因为现在你在定义之前添加了 TimerTick 函数。此外,[System.Reflection.Assembly]::LoadWithPartialName 已弃用,请改用Add-Type -AssemblyName System.Windows.FormsAdd-Type -AssemblyName System.Drawing。完成后处理对象:$timer.Dispose()$objForm.Dispose()
  • @theo ty 用于指针,进行了相应编辑。虽然知道为什么图片没有更新吗?
  • 只是好奇..我的回答对你有用吗?
  • 它仍然没有更新,使用临时。 100张截图,然后覆盖的解决方案,但是很乱

标签: winforms powershell timer adb screencast


【解决方案1】:

除了使用带有静态图像的模型之外,我无法对此进行全面测试,因为我没有 android。
不过,这似乎对我有用:

我将这些行移到脚本的顶部:

Set-ExecutionPolicy Bypass -Scope Process -Force
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms

$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 1000

function startTimer() { 
   $timer.Start()
   Write-Host "Timer Started"
}
function stopTimer() {
    $timer.Enabled = $false
    Write-Host "Timer Stopped"
    $btnStart.Text = "Continue"
}
function TimerTick()
{
    adb shell rm -f /sdcard/sc.png
    adb shell screencap -p /sdcard/sc.png
    adb pull /sdcard/sc.png $BpathTo

    $image = [System.Drawing.Image]::Fromfile("$BpathTo\sc.png")
    $picb.Image = $image
    $lblLog.Text = (Get-Date).ToString()
    Write-Host "Refreshed at: "(Get-Date).ToString()
    Write-Host ""
}
$timer.Add_Tick({TimerTick})

并在最后添加这些:

# clean up
$timer.Stop()
$timer.Dispose()
$objForm.Dispose()

在样机中对我来说这是可行的,但我也看到了 $timer 范围为 $global:timer 的示例...

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-14
    • 1970-01-01
    • 1970-01-01
    • 2019-08-28
    相关资源
    最近更新 更多