wechat_jump.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import matplotlib.animation as animation
  4. import math
  5. import time
  6. import os
  7. import cv2
  8. import datetime
  9. scale = 0.25
  10. template = cv2.imread('character.png')
  11. template = cv2.resize(template, (0, 0), fx=scale, fy=scale)
  12. template_size = template.shape[:2]
  13. def search(img):
  14. result = cv2.matchTemplate(img, template, cv2.TM_SQDIFF)
  15. min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
  16. cv2.rectangle(img, (min_loc[0], min_loc[1]), (min_loc[0] + template_size[1], min_loc[1] + template_size[0]), (255, 0, 0), 4)
  17. return img, min_loc[0] + template_size[1] / 2, min_loc[1] + template_size[0]
  18. def pull_screenshot():
  19. filename = datetime.datetime.now().strftime("%H%M%S") + '.png'
  20. os.system('mv 1.png {}'.format(filename))
  21. os.system('adb shell screencap -p /sdcard/1.png')
  22. os.system('adb pull /sdcard/1.png .')
  23. def jump(distance):
  24. press_time = distance * 1.35
  25. press_time = int(press_time)
  26. cmd = 'adb shell input swipe 320 410 320 410 ' + str(press_time)
  27. print cmd
  28. os.system(cmd)
  29. def update_data():
  30. global src_x, src_y
  31. img = cv2.imread('1.png')
  32. img = cv2.resize(img, (0, 0), fx=scale, fy=scale)
  33. img, src_x, src_y = search(img)
  34. return img
  35. fig = plt.figure()
  36. index = 0
  37. # pull_screenshot()
  38. img = update_data()
  39. update = True
  40. im = plt.imshow(img, animated=True)
  41. def updatefig(*args):
  42. global update
  43. if update:
  44. time.sleep(1)
  45. pull_screenshot()
  46. im.set_array(update_data())
  47. update = False
  48. return im,
  49. def onClick(event):
  50. global update
  51. global src_x, src_y
  52. dst_x, dst_y = event.xdata, event.ydata
  53. distance = (dst_x - src_x)**2 + (dst_y - src_y)**2
  54. distance = (distance ** 0.5) / scale
  55. print 'distance = ', distance
  56. jump(distance)
  57. update = True
  58. fig.canvas.mpl_connect('button_press_event', onClick)
  59. ani = animation.FuncAnimation(fig, updatefig, interval=5, blit=True)
  60. plt.show()