思路
1.截取安卓手机当前屏幕图片并复制到Mac。 知识点:ADB工具--截屏命令
2.测量截图中两方块之间的距离。知识点:Matplotlib制图工具
3.根据距离判断手机所需按下的时间。
4.通过鼠标控制Android触摸屏,完成跳跃。知识点:ADB工具--滑屏命令
5.跳跃完成后,载入下一张手机截图,重复1~4步骤。
知识点:
1. ADB Android Bridge
安装方法:https://www.jianshu.com/p/1b3fb1f27b67 。安装页面下方的命令行工具即可。
截屏命令:https://blog.csdn.net/u012283902/article/details/79186354
模拟按键与输入:https://www.cnblogs.com/lybolg/p/7308745.html
2. Matplotlib制图工具:
思路实现:
1.截取安卓手机当前屏幕图片并复制到电脑。
#通过ADB命令截图
os.system('adb shell screencap -p /sdcard/screen.png')
#通过ADB命令复制图片到本地
os.system('adb pull /sdcard/screen.png /Users/songfei/Downloads')
#通过PIL.Image ,并通过numpy把数据存储到矩阵中。
p = PIL.Image.open('/Users/XXXXXX/Downloads/screen.png')
n = numpay.array(p)
2.测量截图中两方块之间的距离。
1)Matplotlib中,用鼠标点击一下图片,就是一个鼠标事件event。这个event记录了此时鼠标的操作类型(单击)、位置等信息。其中event.x 就是x坐标,event.y 就是y坐标。
2)连续传入2个(x,y)到函数onclick()中,就可以在onclick中计算两坐标之间的距离。
3)传入方式:fig.canvas.mpl_connect('button_press_event',onclick)
即,把'buttom_press_event'这个鼠标按下事件event作为参数传入onclick()函数。这里的onclick后面没有括号。
def onclick(event):
print('点击坐标:(%d,%d)'%(event.x, event.y))
#通过勾股定理计算距离
cid = fig.canvas.mpl_connect('button_press_event', onclick)
3.根据距离判断手机所需按下的时间
距离越长,所需蓄力时间越大。经多次测算,距离乘以6.5相对准确。
press_time = int(l*6.5)#时间单位是毫秒,所以不能再有小数点了,int取整。
4.通过鼠标控制Android触摸屏,完成跳跃动作。
似乎adb中只有swipe滑动命令有时间参数,可以用来模拟触摸时间。5个参数,前4个是x1,y1 x2,y2,最后一个是时间参数,单位是毫秒ms。
def press_screen(l):
press_time = int(l*6.5)
cmd = 'adb shell input swipe %s %s %s %s %s' % (x1,y1,x2,y2,press_time)
os.system(cmd)
5.跳跃完成后,载入下一张手机截图。
这里其实可以通过matplotlib中的animation功能实现载入下一张图片,不过这里用了笨办法,重新载入一张新图片。详见完整代码。
完整代码:
from PIL import Image as im
import numpy as np
import matplotlib.pyplot as plt
import os
import time
#定于坐标列表
coor = []
x1=0
x2=0
y1=0
y2=0
n=None
def get_screen_image():
global n #此函数不返回值,更新全局变量n。其实完全没有必要,当时不知道为什么这么写了。
os.system('adb shell screencap -p /sdcard/screen.png')# 手机截图
os.system('adb pull /sdcard/screen.png /Users/songfei/Downloads')#图片复制到本地
p = im.open('/Users/songfei/Downloads/screen.png') #用PIL.Image读图
n = np.array(p) #图片转换为数组n,用于matplotlib绘图。
def onclick(event):
global coor
print('点击坐标:(%d,%d)'%(event.x, event.y))
#列表coor的元素少于2个时,录入坐标
if len(coor)<2:
coor.append((event.x, event.y))
#列表中有两个坐标元素时:
if len(coor)==2:
l = distance(coor) #第二步,计算两个坐标间的距离
print ('两点间距离:',l)
press_screen(l) #第三、四步,根据距离,在屏幕上滑动相应时间,完成跳跃。
coor = [] #清空列表coor中的坐标元素。
refresn_screen_image() #第五步,调用refresh_screen_image(),重新载入图片,在refresh函数中又会重新调用本条函数。开始重复二三四五步骤。
def distance(coor):
global x1,x2,y1,y2
x1 = coor[0][0]
y1 = coor[0][1]
x2 = coor[1][0]
y2 = coor[1][1]
l = (abs((x2-x1)**2 + (y2-y1)**2))**0.5
return l
def press_screen(l):
press_time = int(l*6.5)
cmd = 'adb shell input swipe %s %s %s %s %s' % (x1,y1,x2,y2,press_time)
print ('蓄力时间:%sms'%press_time)
os.system(cmd)
def refresn_screen_image():
time.sleep(1)#等待1秒,以便完成跳跃动画。
plt.close()#关闭前一张图片
get_screen_image()#调用函数,手机截图
fig = plt.figure()#重新画图
image = plt.imshow(n,animated=True)
cid = fig.canvas.mpl_connect('button_press_event', onclick) #记录鼠标动作,回到onclick函数,重复二三四五步骤。
plt.show()
#第一步,载入图片并画图
get_screen_image()
fig = plt.figure()
plt.imshow(n,animated=True)
cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()