UIAutomator2+python大图识别小图

import uiautomator2 as u2
import cv2
import time


def cross_click(d, template_path, threshold=0.8, retry=3):
"""跨分辨率图像点击核心方法"""
for attempt in range(retry):
try:
# 获取设备参数
device_w = d.info['displayWidth']
device_h = d.info['displayHeight']
print(device_w)
print(device_h)
scale_factor = 1080 / device_w # 基准缩放系数‌:ml-citation{ref="1,8" data="citationList"}

# 屏幕截图处理
screen = d.screenshot(format='opencv')
# 强制修改为 600x400 分辨率
# resized = cv2.resize(screen, (720, 1600), interpolation=cv2.INTER_AREA)
# cv2.imwrite("D:/stretched.png", resized) # 保存为JPEG:ml-citation{ref="3,6" data="citationList"}
# screen = "D:/stretched.png"
screen_gray = cv2.cvtColor(screen, cv2.COLOR_BGR2GRAY)
scaled_screen = cv2.resize(screen_gray, (0,0), fx=scale_factor, fy=scale_factor)

# 模板处理
template = cv2.imread(template_path, cv2.IMREAD_GRAYSCALE)
scaled_template = cv2.resize(template, (0,0), fx=scale_factor, fy=scale_factor)
h, w = scaled_template.shape

# 多算法匹配
res = cv2.matchTemplate(scaled_screen, scaled_template, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
print(max_val)
print(threshold)
if max_val > threshold:
# 坐标逆向计算
raw_x = int((max_loc[0] + w/2) / scale_factor)
raw_y = int((max_loc[1] + h/2) / scale_factor)

# 安全边界处理
safe_x = max(10, min(raw_x, device_w-10))
safe_y = max(10, min(raw_y, device_h-10))

d.click(safe_x, safe_y)
return True

except Exception as e:
print(f"第{attempt+1}次尝试失败: {str(e)}")
time.sleep(1)

return False

# 使用示例
d = u2.connect("bd197cc7")
cross_click(d, r"D:\png\mt\sy\0.png", threshold=0.75)

posted @ 2025-04-05 08:43  粥店接济  阅读(115)  评论(0)    收藏  举报