def get_screen_size(): adb = auto_adb() size_str = adb.get_screen() m = re.search(r'(\d+)x(\d+)', size_str) if m: return m.group(2), m.group(1) return 1920, 1080
def __init__(self): if sys.version_info.major != 3: print('请使用Python3') exit(1) try: from common.auto_adb import auto_adb except Exception as ex: print(ex) print('请将脚本放在项目根目录中运行') print('请检查项目根目录中的 common 文件夹是否存在') exit(1) self._adb = auto_adb()
def dump_device_info(): """ 显示设备信息 """ adb = auto_adb() size_str = adb.get_screen() device_str = adb.test_device_detail() phone_os_str = adb.test_device_os() density_str = adb.test_density() print("""********** Screen: {size} Density: {dpi} Device: {device} Phone OS: {phone_os} Host OS: {host_os} Python: {python} **********""".format(size=size_str.replace('\n', ''), dpi=density_str.replace('\n', ''), device=device_str.replace('\n', ''), phone_os=phone_os_str.replace('\n', ''), host_os=sys.platform, python=sys.version))
手机屏幕截图的代码 """ import subprocess import os import sys from PIL import Image from io import StringIO try: from common.auto_adb import auto_adb except Exception as ex: print(ex) print('请将脚本放在项目根目录中运行') print('请检查项目根目录中的 common 文件夹是否存在') exit(1) adb = auto_adb() # SCREENSHOT_WAY 是截图方法,经过 check_screenshot 后,会自动递减,不需手动修改 SCREENSHOT_WAY = 3 def pull_screenshot(): """ 获取屏幕截图,目前有 0 1 2 3 四种方法,未来添加新的平台监测方法时, 可根据效率及适用性由高到低排序 """ global SCREENSHOT_WAY if 1 <= SCREENSHOT_WAY <= 3: process = subprocess.Popen(adb.adb_path + ' shell screencap -p', shell=True, stdout=subprocess.PIPE) binary_screenshot = process.stdout.read()
import random from PIL import Image from six.moves import input from skimage import io,transform import numpy as np import tensorflow as tf try: from common import debug, config, screenshot, UnicodeStreamFilter from common.auto_adb import auto_adb except Exception as ex: print(ex) print('请将脚本放在项目根目录中运行') print('请检查项目根目录中的 common 文件夹是否存在') exit(1) adb = auto_adb() VERSION = "1.1.4" # DEBUG 开关,需要调试的时候请改为 True,不需要调试的时候为 False DEBUG_SWITCH = False # Magic Number,不设置可能无法正常执行,请根据具体截图从上到下按需 # 设置,设置保存在 config 文件夹中 config = config.open_accordant_config() under_game_score_y = config['under_game_score_y'] # 长按的时间系数,请自己根据实际情况调节 press_coefficient = config['press_coefficient'] # 二分之一的棋子底座高度,可能要调节 piece_base_height_1_2 = config['piece_base_height_1_2'] # 棋子的宽度,比截图中量到的稍微大一点比较安全,可能要调节
class app(): adb = auto_adb() def __init__(self): # 检查设备是否连接 self.adb.test_device() # 打印设备信息 # debug.dump_device_info() pass # 打开应用 def _open_app(self, main_activity, delay): current = self._is_focused_activity_super(main_activity) if current: return True else: cmd = 'shell am start -n {activity}'.format(activity=main_activity) self.adb.run(cmd) time.sleep(delay) current = self._is_focused_activity_super(main_activity) if current: return True else: print('目前未能显示主页窗口可能原因:息屏状态、被弹窗覆盖等,重试中...') return False pass # 模拟点击菜单后判断是否打开了目标activity def _is_focused_activity_super(self, activity): # 获取当前的activity cmd = 'shell dumpsys window | findstr mFocusedWindow' output = self.adb.run(cmd) # print(output) index = output.find(activity, 0, len(output)) if index < 0: return False else: return True pass # 设置输入法 def set_ime(self, ime): config_data = config.open_accordant_config('common') name = config_data[ime]['name'] # 1.检测输入法是否安装 cmd = 'shell ime list -a' output = self.adb.run(cmd) index = output.find(name, 0, len(output)) if index < 0: print('未安装{}输入法,安装后使用!'.format(name)) exit(0) # 2.输入法设置 cmd = 'shell ime set {}'.format(name) output = self.adb.run(cmd) index = output.find('selected', 0, len(output)) if index < 0: print('设置{}输入法失败,手动设置后使用'.format(name)) exit(0) pass # 新定义的点击操作 def _click_operate(self, current, x, y, delay, expect='', retry=0): if not self._is_focused_activity_super(current): return False cmd = 'shell input tap {x} {y}'.format(x=x + self._random_bias(10), y=y + self._random_bias(10)) self.adb.run(cmd) time.sleep(delay) if expect == '': return True for i in range(retry): i += 1 if self._is_focused_activity_super(expect): return True return False pass # 点击操作 def click(self, x, y): cmd = 'shell input tap {x} {y}'.format(x=x + self._random_bias(10), y=y + self._random_bias(10)) self.adb.run(cmd) pass # 点击区域随机偏置 def _random_bias(self, num): return random.randint(-num, num) pass # 滑动屏幕 def swipe_operate(self, x1, y1, x2, y2, delay, duration=200): cmd = 'shell input swipe {x1} {y1} {x2} {y2} {duration}'.format( x1=x1, y1=y1, x2=x2, y2=y2, duration=duration) self.adb.run(cmd) time.sleep(delay) pass # 模拟返回按键 def back_expect_page(self, current_activity, expect_activity, delay, retry): cmd = 'shell input keyevent 4' is_current_activity = self._is_focused_activity_super(current_activity) if is_current_activity: for i in range(retry): self.adb.run(cmd) i += 1 time.sleep(delay) is_expect_activity = self._is_focused_activity_super( expect_activity) if is_expect_activity: return True print('已完成操作,尝试返回{}'.format(i)) return False pass # 截屏返回图像数据 def screen_to_img(self, name='', region=()): im = screenshot.pull_screenshot() if not name: name = './tmp/screen.png' if region: crop_img = im.crop(region) crop_img.save(name) with open(name, 'rb') as bin_data: image = bin_data.read() return image pass