一:准备工具
- adb 驱动 下载链接:http://adbshell.com/upload/adb.zip
- 安卓手机
- 打开手机调试模式
- usb线接好手机和电脑
PyCharm:专门用于Python开发的IDE工具
实现原理
- 获取手机的实时的截图
- 点击起始位置和落地位置
- 计算两个点的距离
- 计算按压时间
- 发送按压指令
- 重新刷新手机截图
1.安装此次程序所用到的模块(根据自己安装的Windows版本和python版本进行下载)。
numpy-1.14.0rc1-cp36-none-win_amd64.whl
Pillow-5.0.0-cp36-cp36m-win_amd64.whl
matplotlib-2.1.1-cp36-cp36m-win_amd64.whl
下载链接:https://www.lfd.uci.edu/~gohlke/pythonlibs/使用cmd进入下载好的文件的目录,安装指令:(opencv是最后安装的)
pip3.6 install C:\python36\Scripts\numpy-1.14.0rc1-cp36-none-win_amd64.whl
pip3.6 install C:\python36\Scripts\Pillow-5.0.0-cp36-cp36m-win_amd64.whl
pip3.6 install C:\python36\Scripts\matplotlib-2.1.1-cp36-cp36m-win_amd64.whl
- 2.结果
- 3.代码部分
# !/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'wechat'
import os
import PIL,numpy
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import time
need_update = True
def get_screen_image():
os.system('adb shell screencap -p /sdcard/screen.png')#获取当前界面的手机截图
os.system('adb pull /sdcard/screen.png')#下载当前这个截图到当前电脑当前文件夹下
return numpy.array(PIL.Image.open('screen.png'))
def jump_to_next(point1, point2):#计算炫的长度
x1, y1 = point1; x2, y2 = point2
distance = ((x2-x1)**2 + (y2-y1)**2)**0.5
os.system('adb shell input swipe 320 410 320 410 {}'.format(int(distance*1.35)))
def on_calck(event, coor=[]):#绑定的鼠标单击事件
global need_update
coor.append((event.xdata, event.ydata))
if len(coor) == 2:
jump_to_next(coor.pop(), coor.pop())
need_update = True
def update_screen(frame):#更新图片 /从画图片
global need_update
if need_update:
time.sleep(1)
axes_image.set_array(get_screen_image())
need_update = False
return axes_image,
figure = plt.figure()#创建一个空白的图片对象/创建一张图片
axes_image = plt.imshow(get_screen_image(), animated=True)#把获取的图片话在坐标轴上面
figure.canvas.mpl_connect('button_press_event', on_calck)
ani = FuncAnimation(figure, update_screen, interval=50, blit=True)
plt.show()