Python寫一個Windows下的android設備截圖工具


界面版

利用python的wx庫寫個ui界面,用來把android設備的截圖輸出到電腦屏幕,前提需要安裝adb,涉及到的python庫也要安裝。代碼如下:

import wx,subprocess,os,platform

class AutyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'Android Auty', size=(350, 300))
        self.panel = wx.Panel(self, -1)
        #Android devices combox.
        combox_list = []
        r = execute_shell("adb devices")
        for i in range(1,len(r)-1):
            if r[i].startswith("*") and r[i].endswith("*"):
                pass
            else:
                combox_list.append(r[i].split("\t")[0])
        wx.StaticText(self.panel, -1, "Select devices:", (15, 15))
        self.devices_combobox = wx.ComboBox(self.panel, -1, r[1].split("\t")[0], (15, 35), wx.DefaultSize, combox_list, wx.CB_DROPDOWN)
        #Capture button.
        self.capture_button = wx.Button(self.panel, -1, "capture", pos=(188, 35), size=(66,25))
        self.reload_button = wx.Button(self.panel, -1, "reload", pos=(258, 35), size=(66,25))
        self.Bind(wx.EVT_BUTTON, self.captureClick, self.capture_button)
        self.Bind(wx.EVT_BUTTON, self.reloadClick, self.reload_button) 
        self.capture_button.SetDefault()
        self.reload_button.SetDefault()
    def captureClick(self, event):
        capture_android(self.devices_combobox.GetValue())
        if("Windows" in platform.platform()):
            os.startfile("d:\\screenshot.png")
    def reloadClick(self, event):
        self.devices_combobox.Clear()
        k = execute_shell("adb devices")
        for i in range(1,len(k)-1):
            self.devices_combobox.Append(k[i].split("\t")[0])
        self.devices_combobox.SetValue(k[1].split("\t")[0])

def execute_shell(shell):
    p = subprocess.Popen(shell,shell=True,stdout=subprocess.PIPE)
    out = p.stdout.readlines()
    return out

def capture_android(device_id):
    sh1 = "adb -s "+device_id+" shell /system/bin/screencap -p /sdcard/screenshot.png"
    sh2 = "adb -s "+device_id+" pull /sdcard/screenshot.png d:/screenshot.png"
    execute_shell(sh1)
    execute_shell(sh2)

if __name__ == '__main__':
    app = wx.PySimpleApp()
    AutyFrame().Show()
    app.MainLoop()

運行截圖:

優點:

1. 比uiautomatorviewer運行速度快,比monitor更快;

2. 可以針對多個設備,選擇性進行截屏;

3. 截屏以后截圖(保存在D盤根目錄下“screenshot.png”文件)會自動打開;

4. 插拔設備后可以reload重新加載設備列表。

命令行版

如果不想安裝wx庫,提供一個命令行版的安卓截屏python腳本(capture_android.py):

import sys,os,platform
from execute_shell import execute_shell

def capture_android(device_id):
    sh1 = "adb -s "+device_id+" shell /system/bin/screencap -p /sdcard/screenshot.png"
    sh2 = "adb -s "+device_id+" pull /sdcard/screenshot.png d:/screenshot.png"
    execute_shell(sh1)
    execute_shell(sh2)

if __name__ == '__main__':
    if len(sys.argv) == 2:
        device_id = sys.argv[1]
        capture_android(sys.argv[1])
        if("Windows" in platform.platform()):
            os.startfile("d:\\screenshot.png")

引用的execute_shell.py內容如下(把引用的文件放在同級目錄下就行):

import subprocess

def execute_shell(shell):
    p = subprocess.Popen(shell,shell=True,stdout=subprocess.PIPE)
    out = p.stdout.readlines()
    return out

使用方法(python 腳本路徑 device_id參數):

截屏后圖片會自動打開。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM