一、 功能
android端或者android終端的遠程截圖至本地電腦中
二、使用說明
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 |
- os.system函數無法獲取執行結果,只能獲取執行成功或失敗的結果,成功返回0,失敗返回1,且會執行的結果打印在終端上,相當于調起dos執行命令
- 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