-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from MultiWolf/main
- Update number_command.py: 增加对根据设备分辨率的自动缩放(以作者测试平板为基准)
- Loading branch information
Showing
1 changed file
with
56 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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("=") | ||
|