python Windows下的android设备截图工具

目录

界面版

利用python的wx库写个ui界面,用来把android设备的截图输出到电脑屏幕,前提需要安装adb,涉及到的python库也要安装。代码如下:

#!/usr/bin/env python 
# -*- coding: utf-8 -*-
import wx, subprocess, os, platform


class MyFrame(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.capture_click, self.capture_button)
        self.Bind(wx.EVT_BUTTON, self.reload_click, self.reload_button)
        self.capture_button.SetDefault()
        self.reload_button.SetDefault()

    def capture_click(self, event):
        capture_android(self.devices_combobox.GetValue())
        if ("Windows" in platform.platform()):
            os.startfile("d:\\screenshot.png")

    def reload_click(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)


class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame()
        frame.Show(True)
        self.SetTopWindow(frame)
        return True


if __name__ == '__main__':
    app = MyApp()
    app.MainLoop()

wx库下载地址:http://wxpython.org/download.php

运行截图:

优点:
1. 比uiautomatorviewer运行速度快,比monitor更快;
2. 可以针对多个设备,选择性进行截屏;
3. 截屏以后截图(保存在D盘根目录下“screenshot.png”文件)会自动打开;
4. 插拔设备后可以reload重新加载设备列表。

命令行版

 如果不想安装wx库,提供一个命令行版的安卓截屏python脚本:

#!/usr/bin/env python 
# -*- coding: utf-8 -*-


import sys, os, platform
import subprocess


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)


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


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")

运行方式:

 


***微信扫一扫,关注“python测试开发圈”,了解更多测试教程!***
posted @ 2016-11-28 19:32  鲨鱼逛大街  阅读(758)  评论(0编辑  收藏  举报