使用了aircv中的find_template方法从大图片中搜索小图片的坐标。用来点击按钮,查看是否满足状态等。
import aircv as ac
import os
class Device:
def __init__(self, id):
self.id = id
# 搜索返回图片位置
def search_location(self, imsrc, imsch, threshold = 0.7):
imsrc = ac.imread(imsrc)
imsch = ac.imread(imsch)
result = ac.find_template(imsrc, imsch, threshold=threshold)
if not result:
return result
return result
# 返回
def back(self):
os.system('adb -s ' + self.id + ' shell input keyevent 4')
# 滑动
def swipe(self):
os.system('adb -s ' + self.id + ' shell input swipe 350 1580 350 680 200')
# 截图
def screen_cap(self):
print(self)
os.system('adb -s ' + self.id + ' shell screencap -p /sdcard/' + self.id + '.png')
os.system('adb -s ' + self.id + ' pull /sdcard/' + self.id + '.png')
def pic_in_screen(self, search_img, donotclick=False):
if not donotclick:
self.screen_cap()
result = self.search_location(self.id + '.png', search_img)
if not result:
return False
return result['result']
# 点击
def click(self, pos):
os.system('adb -s ' + self.id + ' shell input tap ' + str(pos[0]) + ' ' + str(pos[1]))
# 点击图片
def click_pic(self, path, donotclick=False):
if not donotclick:
self.screen_cap()
result = self.search_location(self.id + '.png', path)
if not result:
return False
self.click(result['result'])
return True
# 返回当前连接的设备列表
def get_drive_list():
devices = os.popen('adb devices')
str_list = devices.read().split('\n')
drive_list = []
for i in str_list:
if '\t' in i:
drive_list.append(i.split('\t')[0])
return drive_list