From ca4b640b4cc4f00a9fec23e016006d0c7cb7dea9 Mon Sep 17 00:00:00 2001 From: Fleey Date: Thu, 10 Oct 2024 18:08:50 +0800 Subject: [PATCH] =?UTF-8?q?-=20Update=20number=5Fcommand.py:=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=AF=B9=E6=A0=B9=E6=8D=AE=E8=AE=BE=E5=A4=87=E5=88=86?= =?UTF-8?q?=E8=BE=A8=E7=8E=87=E7=9A=84=E8=87=AA=E5=8A=A8=E7=BC=A9=E6=94=BE?= =?UTF-8?q?=EF=BC=88=E4=BB=A5=E4=BD=9C=E8=80=85=E6=B5=8B=E8=AF=95=E5=B9=B3?= =?UTF-8?q?=E6=9D=BF=E4=B8=BA=E5=9F=BA=E5=87=86=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 已在小米 13(1080x2400)测试 --- number_command.py | 96 +++++++++++++++++++++++++++-------------------- 1 file changed, 56 insertions(+), 40 deletions(-) diff --git a/number_command.py b/number_command.py index 1278673..4f907b9 100644 --- a/number_command.py +++ b/number_command.py @@ -1,47 +1,63 @@ import subprocess +from functools import lru_cache -def run_adb_command(command): - # 打开一个持久的 adb shell 会话 - shell_process = subprocess.Popen(["adb", "shell"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) - shell_process.communicate(command) - shell_process.stdin.close() - -def swipe_screen(str): - xy = str_to_xy(str) - all_commands = "\n" - if xy: - for i in range(len(xy)): - for j in range(len(xy[i]) - 1): - command = f"input swipe {xy[i][j][0]} {xy[i][j][1]} {xy[i][j+1][0]} {xy[i][j+1][1]} 0" - all_commands += command + "\n" - all_commands += "exit\n" - run_adb_command(all_commands) - - -def str_to_xy(str): - match str: - case "1": - return [[1480, 1050], [1440, 1470]] - case "2": - return [[1255, 1100], [1700, 1100], [1255, 1470], [1700, 1470]] - case "3": - return [[1344, 1040], [1600, 1200], [1270, 1323], [1635, 1379], [1249, 1588]] - case "4": - return [[1716, 1274],[1245,1296],[1450,1030],[1450,1466]] - case "5": - return [[1558,1020],[1290,1211],[160,1348],[1300.1472]] - case "6": - return [[1533,1027],[1265,1428],[1663,1439]] - case ">": - return [[[1350, 1080], [1545, 1172], [1295, 1297]]] - case "<": - return [[[1578,1058],[1308,1231],[1560,1292]]] - case "=": - return [[[1284, 1122], [1700, 1122]],[[1280, 1300], [1700, 1300]]] +# 以作者的测试平板分辨率为基准(1800x2880) +# 已在小米 13 测试(1080x2400) +BASE_COORDINATES = { + "1": [[1480, 1050], [1440, 1470]], + "2": [[1255, 1100], [1700, 1100], [1255, 1470], [1700, 1470]], + "3": [[1344, 1040], [1600, 1200], [1270, 1323], [1635, 1379], [1249, 1588]], + "4": [[1716, 1274], [1245, 1296], [1450, 1030], [1450, 1466]], + "5": [[1558, 1020], [1290, 1211], [1600, 1348], [1300, 1472]], + "6": [[1533, 1027], [1265, 1428], [1663, 1439]], + ">": [[[1350, 1080], [1545, 1172], [1295, 1297]]], + "<": [[[1578, 1058], [1308, 1231], [1560, 1292]]], + "=": [[[1284, 1122], [1700, 1122], [1280, 1300], [1700, 1300]]] +} + +@lru_cache() +def get_device_resolution(): + result = subprocess.run(["adb", "shell", "wm", "size"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) + output = result.stdout + if "Physical size" in output: + resolution_str = output.split(":")[-1].strip() + width, height = map(int, resolution_str.split("x")) + return width, height + else: + raise Exception("无法获取设备分辨率") + +def run_adb_command(commands): + # 批量执行 ADB 命令 + for command in commands: + result = subprocess.run(["adb", "shell", command], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) + if result.returncode != 0: + print(f"命令执行失败: {result.stderr}") + +def swipe_screen(command_str, base_resolution=(1800, 2880)): + current_resolution = get_device_resolution() + scale_factor_x = current_resolution[0] / base_resolution[0] + scale_factor_y = current_resolution[1] / base_resolution[1] + + xy_paths = str_to_xy(command_str, scale_factor_x, scale_factor_y) + if xy_paths: + adb_commands = [] + for path in xy_paths: + for i in range(len(path) - 1): + adb_commands.append(f"input swipe {path[i][0]} {path[i][1]} {path[i+1][0]} {path[i+1][1]} 0") + # 批量执行命令 + run_adb_command(adb_commands) + +def scale_coordinates(base_coordinates, scale_x, scale_y): + # 缩放所有坐标 + return [[(int(x * scale_x), int(y * scale_y)) for (x, y) in path] for path in base_coordinates] + +def str_to_xy(command_str, scale_x, scale_y): + if command_str in BASE_COORDINATES: + return scale_coordinates(BASE_COORDINATES[command_str], scale_x, scale_y) + else: + return None if __name__ == "__main__": - # 执行滑动操作 swipe_screen("<") swipe_screen("=") -