Use Python to make WeChat jump jump semi-automatic plug-in + detailed comments

ideas

1. Take a screenshot of the current screen of your Android phone and copy it to your Mac. Knowledge point: ADB tool -- screenshot command

2. Measure the distance between the two squares in the screenshot. Knowledge point: Matplotlib drawing tools

3. According to the distance to judge the time required to press the phone.

4. Control the Android touch screen through the mouse to complete the jump. Knowledge point: ADB tool -- sliding screen command

5. After the jump is completed, load the next screenshot of the mobile phone and repeat steps 1-4.

Knowledge points:

1. ADB Android Bridge

Installation method: https://www.jianshu.com/p/1b3fb1f27b67  . You can install the command line tool at the bottom of the page.

Screenshot command: https://blog.csdn.net/u012283902/article/details/79186354

Simulate keys and input: https://www.cnblogs.com/lybolg/p/7308745.html

2. Matplotlib drawing tools:

Numpy Beginner Teaching Video

Matplotlib Beginner Teaching Video

Making Matplotlib plots via PIL Numpy

Matplotlib mouse events

Ideas to achieve:

1. Take a screenshot of the current screen of the Android phone and copy it to the computer.

    #通过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. Measure the distance between the two squares in the screenshot.

1) In Matplotlib, clicking a picture with the mouse is a mouse event event. This event records information such as the operation type (click) and position of the mouse at this time. where event.x is the x coordinate and event.y is the y coordinate.

2) By continuously passing two (x, y) into the function onclick(), the distance between the two coordinates can be calculated in 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()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325391828&siteId=291194637