脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python编写adb截图工具的实现源码

python编写adb截图工具的实现源码

2021-12-23 00:04mengyuelby Python

adb截图工具可用于Android手机及Android终端,Android端或者Android终端的远程截图至本地电脑中,今天通过本文给大家介绍python编写adb截图工具的实现源码,感兴趣的朋友一起看看吧

一、 功能

android端或者android终端的远程截图至本地电脑中

python编写adb截图工具的实现源码

二、使用说明

1.adb截图工具可用于android手机及android终端
2.使用数据线连接前提:电脑与android终端/手机已连接
android终端/手机已打开开发者模式
3.生成的图片格式为png

三、实现

1.初始源码

?
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import time
import os,sys
 
#截图
def screencap_cmd(filepath,devices=none):
    if filepath==none:
        filepath='d:/png/'
    if not os.path.exists(filepath):
        os.makedirs(filepath)
    filename=time.strftime("%y%m%d%h%m%s",time.localtime())+".png"
    if devices==none:
        cmd_screencap='adb shell screencap -p sdcard/'+filename
        cmd_pull='adb pull sdcard/'+filename+' '+filepath+filename
        cmd_delete='adb shell rm sdcard/'+filename
    else:
        cmd_screencap='adb -s '+devices+' shell screencap -p sdcard/'+filename
        cmd_pull='adb -s '+devices+' pull sdcard/'+filename+' '+filepath+filename
        cmd_delete='adb -s '+devices+' shell rm sdcard/'+filename
    os.system(cmd_screencap)
    os.system(cmd_pull)
    os.system(cmd_delete)
#保存地址判断及设备信息分类调用
def screencap_info(devices=none,filepath=none):
    if filepath==none:
        filepath='d:/png/'
    if not os.path.exists(filepath):
        os.makedirs(filepath)
    if devices==none:
        screencap_cmd(filepath)
        adb_device_none(filepath)
    else:
        screencap_cmd(filepath,devices)
        adb_info(devices,filepath)
#更换设备
def change_devices():
    r=os.popen("adb devices")
    lines=r.readlines()
    lines=lines[1:-1]
    n=len(lines)
    for i in lines:
        print(i.split("\t")[0])
    devices=input("请输入指定设备:\n")
    for i in lines:
        if devices in i:
            return devices
    print("未找到改设备请重新输入!")
    change_devices()
#截图命令判断,电脑连接多个设备或使用ip截图时
def adb_info(lines,filepath=none):   
    s=input("是否截图(t/f/n)?\n")
    if s=='t' or s=='t':
        if filepath==none:
            screencap_info(lines)
        else:
            screencap_info(lines,filepath)
    elif s=='n' or s=='n' or s=='exit':
        sys.exit()
    elif s=='f' or s=='f':
        devices=change_devices()
        if filepath==none:
            adb_info(devices)
        else:
            adb_info(devices,filepath)   
 #截图命令判断,电脑连接仅连接一个设备时 
def adb_device_none(filepath=none):
    s=input("是否截图(t/f/n)?\n")
    if s=='t' or s=='t':
        if filepath==none:
            screencap_info()
        else:
            screencap_info(none,filepath)
    elif s=='n' or s=='n' or s=='exit':
        sys.exit()
    elif s=='f' or s=='f':
        devices=change_devices()
        if filepath==none:
            adb_info(devices)
        else:
            adb_info(devices,filepath)
#使用设备判断
def adb_devices(n,lines,filepath=none):
    if n==0:
        print("请检查是否已接连或启动调试模式或安装adb环境")
        s=input("再次启动(t/f)?\n")
        if s=='t' or s=='t':
            r=os.popen("adb devices")
            lines=r.readlines()
            lines=lines[1:-1]
            n=len(lines)
            adb_devices(n,r.readlines())
        else:
            sys.exit()
    elif ":5555" in lines:
        print("t:截图  f:更换设备 exit|n:退出 ")
        if filepath==none:
            adb_info(lines)
        else:
            adb_info(lines,filepath)  
    elif n==1:
        print("t:截图  f:更换设备  exit|n:退出")
        if filepath==none:
            adb_device_none()
        else:
            adb_device_none(filepath)   
    elif n>1:
        for i in lines:
            print(i.split("\t")[0])
        devices=input("请输入指定设备:\n")
        for i in lines:
            if devices in i:
                print("t:截图  f:更换设备  exit|n:退出")
                if filepath==none:
                    adb_info(devices)
                else:
                    adb_info(devices,filepath) 
        print("未找到改设备请重新输入!")
        if filepath==none:
            adb_devices(n,lines)
        else
            adb_devices(n,lines,filepath)
 
#输入ip
def ip_info():
    ippath=input("请输入ip:(不输入enter跳过默认使用数据线连接)\n")
    if len(ippath)>0:
        try:
            cnd='adb connect '+ippath
            os.system(cnd)
            return ippath
        except:
            print("adb调试模式为usb,需要切换到无线调试模式\n")
            print("切换方法:\n第一步:android设备开启usb调试,并且通过usb线连接到电脑\n第二步:在终端执行以下命令”adb tcpip 5555“\n第三步:在终端执行以下命令”adb connect ip“。此时拔出usb线,应该就可以adb通过wifi调试设备\n")
            return ip_info()
if  __name__ == '__main__':
    ippath=ip_info()
    filepath=input("请输入保存地址:(不输入enter跳过,默认保存至d:\png\)\n")
    #查询当前连接的设备
    r=os.popen("adb devices")
    lines=r.readlines()
    lines=lines[1:-1]
    n=len(lines)
    ip_add=0
    if ippath!=none:
        for i in lines:
            if ippath in i:
                ip_add=i.split("\t")[0]
    if len(filepath)>0:
        if ":\\" in filepath:
            if ip_add!=0:
                adb_devices(n,ip_add,filepath)
            else:
                adb_devices(n,lines,filepath)
        else:
            print("地址输入非法,使用默认地址\n")
            if ip_add!=0:
                adb_devices(n,ip_add)
            else:
                adb_devices(n,lines)
    else:
        if ip_add!=0:
            adb_devices(n,ip_add)
        else:
            adb_devices(n,lines)

2.优化:增加ip连接断开重连处理

在输入ip函数ip_info()增加ip连接判断,将os.system替换成os.popen函数执行cmd命令:

?
1
2
3
4
5
6
7
8
r=os.popen(cnd)
#读取执行结果
lines=r.readlines()[0]
if 'unable' in lines:
    print("连接失败\n")
    return ip_info()
else:
    return ippath
  1. os.system函数无法获取执行结果,只能获取执行成功或失败的结果,成功返回0,失败返回1,且会执行的结果打印在终端上,相当于调起dos执行命令
  2. os.popen函数可获取执行的结果内容,但获取的结果要进行readlines读取,执行的结果不会打印在终端上

在输入ip函数ip_info()增加ip连接判断,将os.system替换成os.popen函数执行cmd命令:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
a=os.system(cmd_screencap)
if a==0:
   os.system(cmd_pull)
   os.system(cmd_delete)
else:
   if ":5555" in devices:
            print("连接断开,请重新连接\n")
            ippath=ip_info()
            if ippath==none:
                ippath=ip_info()
            else:
                device=ippath+":5555"
                adb_info(device,filepath)
        else:
            print("设备连接断开,请更换设备\n")
            device=change_devices()
            adb_info(device,filepath)

3.最终源码

?
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import time
import os,sys
 
#输入ip
def ip_info():
    ippath=input("请输入ip:(不输入enter跳过默认使用数据线连接)\n")
    if len(ippath)>0:
        try:
            cnd='adb connect '+ippath
            r=os.popen(cnd)
            lines=r.readlines()[0]
            if 'unable' in lines:
                print("连接失败\n")
                return ip_info()
            else:
                return ippath
 
        except:
            print("adb调试模式为usb,需要切换到无线调试模式\n")
            print("切换方法:\n第一步:android设备开启usb调试,并且通过usb线连接到电脑\n第二步:在终端执行以下命令”adb tcpip 5555“\n第三步:在终端执行以下命令”adb connect ip“。此时拔出usb线,应该就可以adb通过wifi调试设备\n")
            return ip_info()
#保存地址判断及设备信息分类调用
def screencap_info(devices=none,filepath=none):
    if filepath==none:
        filepath='d:/png/'
    if not os.path.exists(filepath):
        os.makedirs(filepath)
    if devices==none:
        screencap_cmd(filepath)
        adb_device_none(filepath)
    else:
        screencap_cmd(filepath,devices)
        adb_info(devices,filepath)
#更换设备
def change_devices():
    r=os.popen("adb devices")
    lines=r.readlines()
    lines=lines[1:-1]
    n=len(lines)
    for i in lines:
        print(i.split("\t")[0])
    devices=input("请输入指定设备:\n")
    for i in lines:
        if devices in i:
            return devices
    print("未找到改设备请重新输入!\n")
    change_devices()
#截图命令判断,电脑连接多个设备或使用ip截图时
def adb_info(lines,filepath=none):   
    s=input("是否截图(t/f/n)?\n")
    if s=='t' or s=='t':
        if filepath==none:
            screencap_info(lines)
        else:
            screencap_info(lines,filepath)
    elif s=='n' or s=='n' or s=='exit':
        sys.exit()
    elif s=='f' or s=='f':
        devices=change_devices()
        if filepath==none:
            adb_info(devices)
        else:
            adb_info(devices,filepath)   
#截图命令判断,电脑连接仅连接一个设备时 
def adb_device_none(filepath=none):
    s=input("是否截图(t/f/n)?\n")
    if s=='t' or s=='t':
        if filepath==none:
            screencap_info()
        else:
            screencap_info(none,filepath)
    elif s=='n' or s=='n' or s=='exit':
        sys.exit()
    elif s=='f' or s=='f':
        devices=change_devices()
        if filepath==none:
            adb_info(devices)
        else:
            adb_info(devices,filepath)
#使用设备判断
def adb_devices(n,lines,filepath=none):
    if n==0:
        print("请检查是否已接连或启动调试模式或安装adb环境\n")
        s=input("再次启动(t/f)?\n")
        if s=='t' or s=='t':
            r=os.popen("adb devices")
            lines=r.readlines()
            lines=lines[1:-1]
            n=len(lines)
            adb_devices(n,r.readlines())
        else:
            sys.exit()
    elif ":5555" in lines:
        print("t:截图  f:更换设备 exit|n:退出\n")
        if filepath==none:
            adb_info(lines)
        else:
            adb_info(lines,filepath)  
    elif n==1:
        print("t:截图  f:更换设备  exit|n:退出\n")
        if filepath==none:
            adb_device_none()
        else:
            adb_device_none(filepath)   
    elif n>1:
        for i in lines:
            print(i.split("\t")[0])
        devices=input("请输入指定设备:\n")
        for i in lines:
            if devices in i:
                print("t:截图  f:更换设备  exit|n:退出")
                if filepath==none:
                    adb_info(devices)
                else:
                    adb_info(devices,filepath) 
        print("未找到改设备请重新输入!")
        if filepath==none:
            adb_devices(n,lines)
        else
            adb_devices(n,lines,filepath)
#截图
def screencap_cmd(filepath,devices=none):
    if filepath==none:
        filepath='d:/png/'
    if not os.path.exists(filepath):
        os.makedirs(filepath)
    filename=time.strftime("%y%m%d%h%m%s",time.localtime())+".png"
    if devices==none:
        cmd_screencap='adb shell screencap -p sdcard/'+filename
        cmd_pull='adb pull sdcard/'+filename+' '+filepath+filename
        cmd_delete='adb shell rm sdcard/'+filename
    else:
        cmd_screencap='adb -s '+devices+' shell screencap -p sdcard/'+filename
        cmd_pull='adb -s '+devices+' pull sdcard/'+filename+' '+filepath+filename
        cmd_delete='adb -s '+devices+' shell rm sdcard/'+filename
    a=os.system(cmd_screencap)
    if a==0:
        os.system(cmd_pull)
        os.system(cmd_delete)
    else:
        if ":5555" in devices:
            print("连接断开,请重新连接\n")
            ippath=ip_info()
            if ippath==none:
                ippath=ip_info()
            else:
                device=ippath+":5555"
                adb_info(device,filepath)
        else:
            print("设备连接断开,请更换设备\n")
            device=change_devices()
            adb_info(device,filepath)
if  __name__ == '__main__':
    ippath=ip_info()
    filepath=input("请输入保存地址:(不输入enter跳过,默认保存至d:\png\)\n")
    #查询当前连接的设备
    r=os.popen("adb devices")
    lines=r.readlines()
    lines=lines[1:-1]
    n=len(lines)
    ip_add=0
    if ippath!=none:
        for i in lines:
            if ippath in i:
                ip_add=i.split("\t")[0]
    if len(filepath)>0:
        if ":\\" in filepath:
            if ip_add!=0:
                adb_devices(n,ip_add,filepath)
            else:
                adb_devices(n,lines,filepath)
        else:
            print("地址输入非法,使用默认地址\n")
            if ip_add!=0:
                adb_devices(n,ip_add)
            else:
                adb_devices(n,lines)
    else:
        if ip_add!=0:
            adb_devices(n,ip_add)
        else:
            adb_devices(n,lines)

到此这篇关于python编写adb截图工具的文章就介绍到这了,更多相关python adb截图工具内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/mengyuelby/article/details/119780371

延伸 · 阅读

精彩推荐