获取安卓应用的图片和XML

方式1:adb命令

windows如果安装了Android Studio, adb.exe位置在C:\Users\UserName\AppData\Local\Android\Sdk\platform-tools

UserName 为 计算机名

截图命令:

.\adb.exe shell screencap -p /data/local/tmp/01.png

.\adb.exe pull /data/local/tmp/01.png ./screencap

获取xml命令为:

.\adb.exe shell /system/bin/uiautomator dump --compressed /data/local/tmp/02.xml

.\adb.exe pull /data/local/tmp/02.xml ./screencap

方式2:atx-agent

参考:https://github.com/openatx/atx-agent中 安装部分

https://github.com/openatx/atx-agent/releases下载以linux_armv7.tar.gz结尾的二进制包。绝大部分手机都是linux-arm架构的。

解压出atx-agent文件,然后打开控制台(需要adb命令,这里我使用的Android Studio,连接的小米手机,adb.exe 位置见方式1中所提。)

$ .\adb.exe push atx-agent /data/local/tmp

$ .\adb.exe shell chmod 755 /data/local/tmp/atx-agent

launch atx-agent in daemon mode

$ .\adb.exe shell /data/local/tmp/atx-agent server -d

stop already running atx-agent and start daemon

$ adb shell /data/local/tmp/atx-agent server -d --stop

默认监听的端口是7912。

安装文件到手机,并启动后,执行 .\adb.exe forward tcp:7912 tcp:7912

然后浏览器中打开:

http://127.0.0.1:7912/screenshot/0

http://127.0.0.1:7912/dump/hierarchy

可以分别获取当前手机截图和xml结果。

下载代码

import datetime

import requests

from PIL import Image

def getImgWithXml():

    nowtime = datetime.datetime.now()

    fname = nowtime.strftime('%Y%m%d_%H%M%S')

    pngname = fname + '.png'

    jpgname = fname + '.jpg'

    xmlname = fname + '.xml'

    screenshot_url = 'http://127.0.0.1:7912/screenshot/0'

    hierarchy_url = 'http://127.0.0.1:7912/dump/hierarchy'

    _res1 = requests.get(url=screenshot_url)

    with open(pngname, 'wb') as fimg:

        fimg.write(_res1.content)

    img = Image.open(pngname)

    img = img.convert('RGB')

    img.save(jpgname)

    img.close()

    _res2 = requests.get(url=hierarchy_url)

    r = _res2.content

    d = json.loads(r.decode('UTF-8'))

    with open(xmlname, 'w', encoding='utf-8') as fxml:

        fxml.write(d['result'])


方式3:

uiautomator2方式:

安装:

  pip install uiautomator2

获取截图和xml代码:

使用adb devices 命令获取设备代码

import datetime

import uiautomator2 as u2

class ATX(object):

    def __init__(self, data_path, decvices='ea2975fb'):

        self.d = u2.connect(decvices)

        self.data_path = data_path

    def get_img_with_xml(self):

        nowtime = datetime.datetime.now()

        fname = self.data_path + nowtime.strftime('%Y%m%d_%H%M%S')

        jpgname = fname + '.jpg'

        xmlname = fname + '.xml'

        image = self.d.screenshot()

        xml = self.d.dump_hierarchy(compressed=True, pretty=True)

        if image.height < image.width:

            image = image.rotate(90, expand=1)

        image = image.resize((720, 1280))

        image.save(jpgname, quality=10)

        with open(xmlname, 'w', encoding='utf-8') as fxml:

            fxml.write(xml)


几个问题:

1. 有些页面无法获取xml,比如抖音的短视频,上网查询后,答案是,uiautomator dump不支持动态页面,将视频暂停后可以dump,更加深入解决,可以参考 https://blog.csdn.net/itfootball/article/details/22683999

2. 截图分辨率调整

上述方法获得截图和xml的size默认为手机的分辨率,而我想要的数据的分辨率为720*1280。

个人使用的小米手机默认分辨率为1080*2160,可以使用以下命令来调整分辨率和对应的图标的倍率

.\adb.exe  shell wm size 720x1280 (这里的x是小写的英文字母X)

.\adb.exe  shell wm density 320

density    |      size

560,1440x2392

480,1080x1920

320,720x1280

240,540x960

120,270x480

经过几次测试发现:

1. 分辨率调整为720*1280,density 调整为320,结果:截图是1080*1920,应该是手机默认会将其扩充到宽或高的像素数,保持了长宽比, 但xml中还是按照720*1280来显示bound信息

2. 分辨率调整为1080*1920,density 调整为480,结果:截图是1080*1920,xml也是按照1080*1920来布局

建议采取第二种方式,未来如果需要调整为720*1280,再同步resize图片大小及xml中bound的大小

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容