python编写adb截图工具的实现源码
作者:mengyuelby 发布时间:2021-03-24 08:50:15
标签:python,adb,截图工具
一、 功能
Android端或者Android终端的远程截图至本地电脑中
二、使用说明
1.adb截图工具可用于Android手机及Android终端
2.使用数据线连接前提:电脑与Android终端/手机已连接
Android终端/手机已打开开发者模式
3.生成的图片格式为png
三、实现
1.初始源码
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命令:
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命令:
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.最终源码
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)
来源:https://blog.csdn.net/mengyuelby/article/details/119780371
0
投稿
猜你喜欢
- 这章是结合之前学习的Tensforboard与Transforms的一个练习。直接上代码:from PIL import Imagefrom
- 最近 全栈数据工程师养成攻略 的微信群已经将近500人,开了二群之后为了打通不同微信群之间的消息,花了点时间做了个消息同步机器人,在任意群收
- 首先 你要有个女朋友效果:需要安装几个包pip install wxpypip install wechat_senderpip insta
- 前言:我目前使用的服务器为centos6.x 系统自带的python的版本为2.6.x,但是目前无论是学习还是使用python,python
- 通过学习借鉴朋友的实现方法进行整理,实现了PHP版的微信公共平台消息主动推送,分享给大家供大家参考,具体内容如下此方法是通过模拟登录微信公共
- 如果你能很好的理解我下面的一句话,那这些都不是问题了。asp是服务器端语言,它的作用是动态生成客户端浏览器所能识别的html css jav
- 问:如何在SQL Enterprise Manager version 6.5下操作SQL Server 6.0的服务器?答:在使用SQL
- 装饰器的价值不言而喻,可以用来增强函数功能、简化代码、减少代码冗余。它的使用场景同样很多,比较简单的场景包含打印日志、统计运行时间,这类例子
- Python request获取网页中文乱码问题r = requests.get(“http://www.baidu.com“)
- 代码如下:DECLARE @T varchar(255), @C varchar(255) DECLARE Table_Cursor CUR
- 当代码已经写得差不多,发现某个变量名需要修改,但代码中很多地方都有该变量,一一修改太麻烦了,在不同的情景下,可以采取更加简便的方法,如下介绍
- 上一篇介绍了 HTML5 中 Canvas 的基本概念,这篇将要介绍一下 Canvas&n
- MySQL安全性指南(3) 作 者: 晏子2.4 不用GRANT设置用户如果你有一个早于3.22.11的MySQL版本,你不能使用GRANT
- 如果当前绝对定位的元素需要透明(没啥内容、且不设置背景),背景元素有内容透出来的时候,IE6/IE7响应的不是期望的当前元素,而是背景元素。
- # 从X和Y中取出相应步长对应的数组并保存至x_data和y_data中x_data = []y_data = []for i in ran
- 在这个abc.php文件中写入如下代码。<?php phpinfo(); ?>你将会看到一个网页,网页内容通常,如下图所示:用中
- 在处理css的机制上,IE总是有很多让人吐血的举动,但对于他们现在的改进力度还是值得高兴的。就拿对伪类:hover的支持来说,IE7+终于添
- ckptfrom tensorflow.python import pywrap_tensorflow checkpoint_path =
- 本文实例讲述了PHP实现的杨辉三角求解算法。分享给大家供大家参考,具体如下:♥ 前言对于 杨辉三角 是什么的问题,请参考百度百科的详细解释:
- 因为正则不够完善,所以代码中不能直接出现 <? 和 ?>如果是字符串,可以拆开写 "<" + &quo