下载地址:http://m.pan38.com/download.php?code=RQEBET 提取码:6666
基于OCR的屏幕自动点击器开发指南
一、技术原理
本方案采用OCR文字识别+图像匹配双模式识别技术:
OCR引擎识别屏幕文字内容
OpenCV模板匹配识别图形元素
PyAutoGUI实现精准鼠标控
多线程处理保证实时性
二、核心代码实现
import cv2
import numpy as np
import pyautogui
import pytesseract
import time
import threading
from PIL import ImageGrab
class AutoClicker:
def init(self):
self.running = False
self.ocr_config = r'--oem 3 --psm 6'
self.template_threshold = 0.8
self.refresh_rate = 0.5
def start_clicker(self):
self.running = True
self.thread = threading.Thread(target=self._monitor)
self.thread.start()
def stop_clicker(self):
self.running = False
self.thread.join()
def _monitor(self):
while self.running:
screenshot = ImageGrab.grab()
screen_np = np.array(screenshot)
# OCR模式处理
text_data = pytesseract.image_to_string(screen_np, config=self.ocr_config)
if "目标按钮" in text_data:
self._click_text_target("目标按钮", screen_np)
# 图像模板匹配模式
templates = ["confirm.png", "accept.png", "next.png"]
for template in templates:
self._match_template(template, screen_np)
time.sleep(self.refresh_rate)
def _click_text_target(self, target_text, screen_np):
data = pytesseract.image_to_data(screen_np, config=self.ocr_config, output_type=pytesseract.Output.DICT)
for i, text in enumerate(data['text']):
if target_text in text:
x = data['left'][i]
y = data['top'][i]
w = data['width'][i]
h = data['height'][i]
center_x = x + w//2
center_y = y + h//2
pyautogui.click(center_x, center_y)
print(f"Clicked text target at ({center_x}, {center_y})")
return True
return False
def _match_template(self, template_path, screen_np):
try:
template = cv2.imread(template_path, 0)
if template is None:
return False
screen_gray = cv2.cvtColor(screen_np, cv2.COLOR_BGR2GRAY)
res = cv2.matchTemplate(screen_gray, template, cv2.TM_CCOEFF_NORMED)
loc = np.where(res >= self.template_threshold)
for pt in zip(*loc[::-1]):
center_x = pt[0] + template.shape[1]//2
center_y = pt[1] + template.shape[0]//2
pyautogui.click(center_x, center_y)
print(f"Clicked template {template_path} at ({center_x}, {center_y})")
return True
except Exception as e:
print(f"Template matching error: {e}")
return False
使用示例
if name == "main":
clicker = AutoClicker()
try:
clicker.start_clicker()
while True:
time.sleep(1)
except KeyboardInterrupt:
clicker.stop_clicker()
三、功能扩展
配置文件支持:读取JSON配置定义点击规则
多显示器适配:通过screeninfo库获取多屏信息
性能优化:使用CUDA加速OpenCV处理
日志系统:记录操作历史便于调试
图形界面:PyQt5构建管理界面
四、注意事项
管理员权限运行(Windows系统)
屏幕缩放设置为100%
不同语言需调整Tesseract训练数据
需要安装VC++运行库