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


猜你喜欢
- 本地路径的创建在做下载操作时,我们一般先把文件下载到本地指定的路径下,然后再做其他使用。为了防止程序出现异常,我们通常需要先判断本地是否存在
- 如何一行输入多个数,并存入列表在python里,如果你仅使用input()的话是输入一行的内容并将该行的内容以字符串的形式存到变量中,但如果
- Linux Centos 下使用yum 命令安装mysql实现步骤1. 查看服务器中有没有安装过Mysql1. 查看有没有安装包: &nbs
- 想通过编写Python代码来打开本地的.mp4格式文件,使用os模块来操作文件。我的电脑默认的是QQ影音播放器,执行Python代码打开默认
- 网页给人最直观的感受就是它的页面框架与构造,就像一座大楼的主体框架与形态,你可能记不起东方明珠塔和艾菲尔铁塔是用什么颜色或什么材料涂的外墙,
- MySQL5.7版本开始支持JSON格式,在创建表时,可以指定列表的数据类型为JSON,但是如何在JSON格式上创建索引呢??本人做了一个简
- 最近想学习一些python数据分析的内容,就弄了个爬虫爬取了一些数据,并打算用Anaconda一套的工具(pandas, numpy, sc
- 问题描述今天在使用Numpy中的矩阵做相减操作时,出现了一些本应为负值的位置自动转换为了正值,观察发现转换后的正值为原本的负值加上256得到
- 前言本文介绍如何使用Python制作一个简单的猜数字游戏。游戏规则玩家将猜测一个数字。如果猜测是正确的,玩家赢。如果不正确,程序会提示玩家所
- 本文实例为大家分享了python定时发送邮件的具体代码,供大家参考,具体内容如下全部代码如下:import timefrom datetim
- 在Windows平台上安装mysql模块用于Python开发用python连接mysql的时候,需要用的安装版本,源码版本容易有错误提示。下
- 简介我知道有很多文章和指南介绍在互联网上实现主-从复制。在主-从复制中,主机影响从机。但从数据库中的任何更改不会影响主数据库,这篇文章将帮助
- Python的turtle模块画国旗主要用到两个函数:draw_rentangle和draw_star。至于函数的调用就和我们学的C,C++
- 效果图代码<template> <div class="outWrap"> &nbs
- Python是一门简单易学的编程语言,语法简洁而清晰,并且拥有丰富和强大的类库。与其它大多数程序设计语言使用大括号不一样 ,它使用缩进来定义
- 在前人的基础上,我对比较优秀的sql语句进行了重新的编辑和整理,力求精短易学。希望大家可以举一反三,更好学习sql语句,如果有问题,还请翻阅
- 首先这是VGG的结构图,VGG11则是红色框里的结构,共分五个block,如红框中的VGG11第一个block就是一个conv3-64卷积层
- 什么要学习PyTorch?有的人总是选择,选择的人最多的框架,来作为自己的初学框架,比如Tensorflow,但是大多论文的实现都是基于Py
- 我就废话不多说了,大家还是直接看代码吧~package main import ("fmt""time&quo
- 1、XML 是什么?XML仅仅是一种数据存放格式,这种格式是一种文本(虽然XML规范中也提供了存放二进制数据的解决方案)。事实上有很多文本格