手机按键精灵找图点击封装代码


以下是一个基于Python的按键精灵找图点击功能的封装代码示例,使用`pyautogui`和`opencv`库实现: ```python import pyautogui import cv2 import time from typing import Tuple, Optional class ImageClicker: """ 屏幕找图并点击的封装类 功能:在屏幕上查找目标图片,找到后执行点击操作 """ def __init__(self, confidence: float = 0.8, wait_time: float = 1.0): """ 初始化参数 :param confidence: 识别置信度(0-1),默认0.8 :param wait_time: 每次操作后的等待时间(秒),默认1秒 """ self.confidence = confidence self.wait_time = wait_time def find_image(self, target_image: str) -> Optional[Tuple[int, int]]: """ 在屏幕上查找目标图片 :param target_image: 目标图片路径 :return: 图片中心坐标(x,y)或None """ try: # 使用pyautogui定位图片 location = pyautogui.locateOnScreen(target_image, confidence=self.confidence) if location: center_x = location.left + location.width // 2 center_y = location.top + location.height // 2 return (center_x, center_y) except Exception as e: print(f"找图失败: {e}") return None def click_image(self, target_image: str, click_count: int = 1) -> bool: """ 查找并点击图片 :param target_image: 目标图片路径 :param click_count: 点击次数,默认1次 :return: 是否点击成功 """ position = self.find_image(target_image) if position: pyautogui.moveTo(position[0], position[1]) time.sleep(self.wait_time/2) pyautogui.click(clicks=click_count) time.sleep(self.wait_time) return True return False def wait_and_click(self, target_image: str, timeout: float = 10.0, interval: float = 1.0) -> bool: """ 等待图片出现并点击(轮询方式) :param target_image: 目标图片路径 :param timeout: 超时时间(秒) :param interval: 检查间隔(秒) :return: 是否点击成功 """ start_time = time.time() while time.time() - start_time < timeout: if self.click_image(target_image): return True time.sleep(interval) return False # 使用示例 if __name__ == "__main__": clicker = ImageClicker(confidence=0.9) # 单次查找点击 clicker.click_image("button.png") # 等待点击(最多等10秒,每秒检查一次) clicker.wait_and_click("popup.png", timeout=10) ``` 代码说明: 1. 使用`pyautogui`实现屏幕操作和图像识别 2. 封装了三种常用操作:查找图片、直接点击、等待点击 3. 添加了置信度参数应对图像识别误差 4. 每次操作后自动等待防止操作过快 三条额外需要注意的逻辑点: 1. 屏幕分辨率变化会导致识别失败,建议使用相对坐标或分辨率适配方案 2. 目标图片需要保持原样(不能有遮挡或变形) 3. 高频率找图操作会消耗大量CPU资源,建议适当添加延迟 [2025-08-05 05:08:51 | AI写代码神器 | 787点数解答]
- .method public 按钮1$被弹起(ii)v .locals 4 .param p1, "\u6a2a\u5750\u6807" # i .param p2, "\u7eb5\u5750\u6807" # i .prologue .line 20 new-instance v0, ljava/lang/stringbuilder; invoke-direct {v0}, ljava/lang/stringbuilder;-><init>()v iget-object v1, p0, lcom/chuchenqi/窗口29;->手机1:lcom/e4a/runtime/components/impl/android/n9/手机; invoke-interface {v1}, lcom/e4a/runtime/components/impl/android/n9/手机;->取imei码()ljava/lang/string; move-result-object v1(208点数解答 | 2024-09-12 16:36:31)266
- 易语言利用大漠插件实现多窗口多线程单控脚本,界面有信息获取,启动任务,停止任务,全部停止任务4个按钮,1个列表框。1、点击“获取信息”按钮就遍历所有游戏窗口,将得到的窗口句柄,窗口标题,线程ID 传递到列表框里。2、先选中列表框里的某一行,再点击“启动任务”按钮,则绑定这一行的对应窗口句柄,然后后台模拟按键操作。(1541点数解答 | 2025-08-01 07:14:07)102
- 易语言利用大漠插件实现多窗口多线程单控脚本,界面有信息获取,启动任务,停止任务,全部停止任务4个按钮,1个列表框。1、点击“获取信息”按钮就遍历所有游戏窗口,将得到的窗口句柄,窗口标题,线程ID 传递到列表框里。2、先选中列表框里的某一行,再点击“启动任务”按钮,则绑定这一行的对应窗口句柄,然后后台模拟按键操作。 (949点数解答 | 2025-08-01 07:15:59)88
- 易语言利用大漠插件实现多窗口多线程单控脚本,界面有信息获取,启动任务,停止任务,全部停止任务4个按钮,1个列表框。1、点击“获取信息”按钮就遍历所有游戏窗口,将得到的窗口句柄,窗口标题,线程ID 传递到列表框里。2、先选中列表框里的某一行,再点击“启动任务”按钮,则绑定这一行的对应窗口句柄,然后后台模拟按键操作。(1131点数解答 | 2025-08-01 07:16:58)106
- 3 时序图 3.1 题目:设计并绘制 uml 时序图 任务描述: 在车载系统中,不同组件之间存在交互和消息传递。请设计一个 uml 时序图,展示以下核心实体类之间的交互: car(汽车) engine(发动机) entertainmentsystem(娱乐系统) navigationsystem(导航系统) phone(手机) 请根据以下要求设计这个系统的时序图: car 启动过程中与 engine 的交互。 car 中的 entertainmentsystem 与 phone 的蓝牙连接过程。 navigationsystem 提供路线规划给 car 的过程。 要求: 标识不同类之间的消息传递顺序和交互过程。 明确展示消息的类型和内容。 描述关键交互步骤和时间顺序。 使用环境为vsode下的plantuml(216点数解答 | 2024-08-13 13:58:22)315
- 题目:设计并绘制 uml 类图 任务描述: 一个车载系统涉及以下核心实体类: 1. car(汽车) 2. vehicle(车辆) 3. engine(发动机) 4. entertainmentsystem(娱乐系统) 5. navigationsystem(导航系统) 6. bluetoothdevice(蓝牙设备) 7. phone(手机) 要求: 1. 为每个类定义其属性和方法。 2. 描述类之间的继承、实现、组合、聚合、关联和依赖关系。 提示: - car 类继承自 vehicle 类。 - entertainmentsystem 类实现 mediaplayer 接口。 - car 类与 engine 类之间存在组合关系。 - car 类与 vehicle 类之间存在聚合关系。 - phone 类与 bluetoothdevice 类之间存在关联关系。 - entertainmentsystem 类依赖于 navigationsystem 类。(760点数解答 | 2024-08-15 22:51:04)220
- 3 时序图 3.1 题目:设计并绘制 uml 时序图 任务描述: 在车载系统中,不同组件之间存在交互和消息传递。请设计一个 uml 时序图,展示以下核心实体类之间的交互: car(汽车) engine(发动机) entertainmentsystem(娱乐系统) navigationsystem(导航系统) phone(手机) 请根据以下要求设计这个系统的时序图: car 启动过程中与 engine 的交互。 car 中的 entertainmentsystem 与 phone 的蓝牙连接过程。 navigationsystem 提供路线规划给 car 的过程。 要求: 标识不同类之间的消息传递顺序和交互过程。 明确展示消息的类型和内容。 描述关键交互步骤和时间顺序。 使用环境为vsode下的plantuml,用英文(115点数解答 | 2024-08-16 10:39:43)225
- 3 时序图 3.1 题目:设计并绘制 uml 时序图 任务描述: 在车载系统中,不同组件之间存在交互和消息传递。请设计一个 uml 时序图,展示以下核心实体类之间的交互: car(汽车) engine(发动机) entertainmentsystem(娱乐系统) navigationsystem(导航系统) phone(手机) 请根据以下要求设计这个系统的时序图: car 启动过程中与 engine 的交互。 car 中的 entertainmentsystem 与 phone 的蓝牙连接过程。 navigationsystem 提供路线规划给 car 的过程。 要求: 标识不同类之间的消息传递顺序和交互过程。 明确展示消息的类型和内容。 描述关键交互步骤和时间顺序。 使用环境为vsode下的plantuml。方法用英文表示(263点数解答 | 2024-08-16 10:42:50)183
- <!doctype html> <html lang="zh"> <head> <meta charset="utf-8"> <link rel="stylesheet" href="css/mian.css"> <link rel="stylesheet" href="css/index.css"> <script src="js/jquery-3.3.1.min.js"></script> <script src="js/jquery-ui.min.js"></script> <script type="text/javascript"> // (1)创建js函数,使用jquery动画,实现ready事件 $(document). ____(1)____ (function(){ //ready事件 $('li').each(function(index){ //遍历对象 $(this).hover(function(){ //单击触发事件 $('li').animate({ width:100 },500); //删除当前元素的样式 $('li').eq(index).animate({ width:200 },500); }); }); }); </script> <title>首页</title> </head> <body> <header> <div class="logo"> <img src="img/logo.jpg" width="250" height="60"> </div> <form class="searchform"> <input type="text" name="search" id="search"/> <input type="submit" value="搜索" id="button"/> </form> <div class="navigation"> <a href="#">手机</a> | <a(475点数解答 | 2024-12-19 14:58:05)267
- <!doctype html> <html lang="zh"> <head> <meta charset="utf-8"> <link rel="stylesheet" href="css/mian.css"> <link rel="stylesheet" href="css/index.css"> <script src="js/jquery-3.3.1.min.js"></script> <script src="js/jquery-ui.min.js"></script> <script type="text/javascript"> // (1)创建js函数,使用jquery动画,实现ready事件 $(document). ____(1)____ (function(){ //ready事件 $('li').each(function(index){ //遍历对象 $(this).hover(function(){ //单击触发事件 $('li').animate({ width:100 },500); //删除当前元素的样式 $('li').eq(index).animate({ width:200 },500); }); }); }); </script> <title>首页</title> </head> <body> <header> <div class="logo"> <img src="img/logo.jpg" width="250" height="60"> </div> <form class="searchform"> <input type="text" name="search" id="search"/> <input type="submit" value="搜索" id="button"/> </form> <div class="navigation"> <a href="#">手机</a> | <a(495点数解答 | 2024-12-19 14:58:06)211
- <!doctype html> <html lang="zh"> <head> <meta charset="utf-8"> <link rel="stylesheet" href="css/mian.css"> <link rel="stylesheet" href="css/index.css"> <script src="js/jquery-3.3.1.min.js"></script> <script src="js/jquery-ui.min.js"></script> <script type="text/javascript"> // (1)创建js函数,使用jquery动画,实现ready事件 $(document). ____(1)____ (function(){ //ready事件 $('li').each(function(index){ //遍历对象 $(this).hover(function(){ //单击触发事件 $('li').animate({ width:100 },500); //删除当前元素的样式 $('li').eq(index).animate({ width:200 },500); }); }); }); </script> <title>首页</title> </head> <body> <header> <div class="logo"> <img src="img/logo.jpg" width="250" height="60"> </div> <form class="searchform"> <input type="text" name="search" id="search"/> <input type="submit" value="搜索" id="button"/> </form> <div class="navigation"> <a href="#">手机</a> | <a(497点数解答 | 2024-12-19 14:58:18)221
- <!doctype html> <html lang="zh"> <head> <meta charset="utf-8"> <link rel="stylesheet" href="css/mian.css"> <link rel="stylesheet" href="css/index.css"> <script src="js/jquery-3.3.1.min.js"></script> <script src="js/jquery-ui.min.js"></script> <script type="text/javascript"> // (1)创建js函数,使用jquery动画,实现ready事件 $(document). ____(1)____ (function(){ //ready事件 $('li').each(function(index){ //遍历对象 $(this).hover(function(){ //单击触发事件 $('li').animate({ width:100 },500); //删除当前元素的样式 $('li').eq(index).animate({ width:200 },500); }); }); }); </script> <title>首页</title> </head> <body> <header> <div class="logo"> <img src="img/logo.jpg" width="250" height="60"> </div> <form class="searchform"> <input type="text" name="search" id="search"/> <input type="submit" value="搜索" id="button"/> </form> <div class="navigation"> <a href="#">手机</a> | <a(497点数解答 | 2024-12-19 14:58:20)234