一、 功能
android端或者android終端的遠(yuǎn)程截圖至本地電腦中
二、使用說(shuō)明
1.adb截圖工具可用于android手機(jī)及android終端
2.使用數(shù)據(jù)線連接前提:電腦與android終端/手機(jī)已連接
android終端/手機(jī)已打開開發(fā)者模式
3.生成的圖片格式為png
三、實(shí)現(xiàn)
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) #保存地址判斷及設(shè)備信息分類調(diào)用 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) #更換設(shè)備 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 ( "請(qǐng)輸入指定設(shè)備:\n" ) for i in lines: if devices in i: return devices print ( "未找到改設(shè)備請(qǐng)重新輸入!" ) change_devices() #截圖命令判斷,電腦連接多個(gè)設(shè)備或使用ip截圖時(shí) 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) #截圖命令判斷,電腦連接僅連接一個(gè)設(shè)備時(shí) 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) #使用設(shè)備判斷 def adb_devices(n,lines,filepath = none): if n = = 0 : print ( "請(qǐng)檢查是否已接連或啟動(dòng)調(diào)試模式或安裝adb環(huán)境" ) s = input ( "再次啟動(dòng)(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:更換設(shè)備 exit|n:退出 " ) if filepath = = none: adb_info(lines) else : adb_info(lines,filepath) elif n = = 1 : print ( "t:截圖 f:更換設(shè)備 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 ( "請(qǐng)輸入指定設(shè)備:\n" ) for i in lines: if devices in i: print ( "t:截圖 f:更換設(shè)備 exit|n:退出" ) if filepath = = none: adb_info(devices) else : adb_info(devices,filepath) print ( "未找到改設(shè)備請(qǐng)重新輸入!" ) if filepath = = none: adb_devices(n,lines) else : adb_devices(n,lines,filepath) #輸入ip def ip_info(): ippath = input ( "請(qǐng)輸入ip:(不輸入enter跳過默認(rèn)使用數(shù)據(jù)線連接)\n" ) if len (ippath)> 0 : try : cnd = 'adb connect ' + ippath os.system(cnd) return ippath except : print ( "adb調(diào)試模式為usb,需要切換到無(wú)線調(diào)試模式\n" ) print ( "切換方法:\n第一步:android設(shè)備開啟usb調(diào)試,并且通過usb線連接到電腦\n第二步:在終端執(zhí)行以下命令”adb tcpip 5555“\n第三步:在終端執(zhí)行以下命令”adb connect ip“。此時(shí)拔出usb線,應(yīng)該就可以adb通過wifi調(diào)試設(shè)備\n" ) return ip_info() if __name__ = = '__main__' : ippath = ip_info() filepath = input ( "請(qǐng)輸入保存地址:(不輸入enter跳過,默認(rèn)保存至d:\png\)\n" ) #查詢當(dāng)前連接的設(shè)備 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 ( "地址輸入非法,使用默認(rèn)地址\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.優(yōu)化:增加ip連接斷開重連處理
在輸入ip函數(shù)ip_info()增加ip連接判斷,將os.system替換成os.popen函數(shù)執(zhí)行cmd命令:
1
2
3
4
5
6
7
8
|
r = os.popen(cnd) #讀取執(zhí)行結(jié)果 lines = r.readlines()[ 0 ] if 'unable' in lines: print ( "連接失敗\n" ) return ip_info() else : return ippath |
- os.system函數(shù)無(wú)法獲取執(zhí)行結(jié)果,只能獲取執(zhí)行成功或失敗的結(jié)果,成功返回0,失敗返回1,且會(huì)執(zhí)行的結(jié)果打印在終端上,相當(dāng)于調(diào)起dos執(zhí)行命令
- os.popen函數(shù)可獲取執(zhí)行的結(jié)果內(nèi)容,但獲取的結(jié)果要進(jìn)行readlines讀取,執(zhí)行的結(jié)果不會(huì)打印在終端上
在輸入ip函數(shù)ip_info()增加ip連接判斷,將os.system替換成os.popen函數(shù)執(zhí)行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 ( "連接斷開,請(qǐng)重新連接\n" ) ippath = ip_info() if ippath = = none: ippath = ip_info() else : device = ippath + ":5555" adb_info(device,filepath) else : print ( "設(shè)備連接斷開,請(qǐng)更換設(shè)備\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 ( "請(qǐng)輸入ip:(不輸入enter跳過默認(rèn)使用數(shù)據(jù)線連接)\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調(diào)試模式為usb,需要切換到無(wú)線調(diào)試模式\n" ) print ( "切換方法:\n第一步:android設(shè)備開啟usb調(diào)試,并且通過usb線連接到電腦\n第二步:在終端執(zhí)行以下命令”adb tcpip 5555“\n第三步:在終端執(zhí)行以下命令”adb connect ip“。此時(shí)拔出usb線,應(yīng)該就可以adb通過wifi調(diào)試設(shè)備\n" ) return ip_info() #保存地址判斷及設(shè)備信息分類調(diào)用 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) #更換設(shè)備 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 ( "請(qǐng)輸入指定設(shè)備:\n" ) for i in lines: if devices in i: return devices print ( "未找到改設(shè)備請(qǐng)重新輸入!\n" ) change_devices() #截圖命令判斷,電腦連接多個(gè)設(shè)備或使用ip截圖時(shí) 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) #截圖命令判斷,電腦連接僅連接一個(gè)設(shè)備時(shí) 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) #使用設(shè)備判斷 def adb_devices(n,lines,filepath = none): if n = = 0 : print ( "請(qǐng)檢查是否已接連或啟動(dòng)調(diào)試模式或安裝adb環(huán)境\n" ) s = input ( "再次啟動(dòng)(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:更換設(shè)備 exit|n:退出\n" ) if filepath = = none: adb_info(lines) else : adb_info(lines,filepath) elif n = = 1 : print ( "t:截圖 f:更換設(shè)備 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 ( "請(qǐng)輸入指定設(shè)備:\n" ) for i in lines: if devices in i: print ( "t:截圖 f:更換設(shè)備 exit|n:退出" ) if filepath = = none: adb_info(devices) else : adb_info(devices,filepath) print ( "未找到改設(shè)備請(qǐng)重新輸入!" ) 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 ( "連接斷開,請(qǐng)重新連接\n" ) ippath = ip_info() if ippath = = none: ippath = ip_info() else : device = ippath + ":5555" adb_info(device,filepath) else : print ( "設(shè)備連接斷開,請(qǐng)更換設(shè)備\n" ) device = change_devices() adb_info(device,filepath) if __name__ = = '__main__' : ippath = ip_info() filepath = input ( "請(qǐng)輸入保存地址:(不輸入enter跳過,默認(rèn)保存至d:\png\)\n" ) #查詢當(dāng)前連接的設(shè)備 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 ( "地址輸入非法,使用默認(rèn)地址\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) |
到此這篇關(guān)于python編寫adb截圖工具的文章就介紹到這了,更多相關(guān)python adb截圖工具內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/mengyuelby/article/details/119780371