# -*- coding: utf-8 -*- """ 安装命令:pip install opencv-python vncdotool -i https://mirrors.aliyun.com/pypi/simple """ import ctypes import time from logging import shutdown import random import cv2, numpy as np from vncdotool import api from vncdotool.client import KEYMAP class VNC: button_left = 1 button_mid = 2 button_right = 3 def __init__(self, ip, prot,password=None): self.ip = ip + "::" + prot self.cmd = f"vncdo -s {self.ip} " self.client = api.connect(self.ip,password) self.key_map = KEYMAP self.image_size = 1024*768 # 1024*768分辨率大小的 self.image_buffer = (ctypes.c_ubyte * self.image_size)() def __del__(self): self.stop() def stop(self): self.client.disconnect() # 截图,可以保存到本地,也可以直接获取cv图像对象 def capture(self, path=None): if path: self.client.captureScreen(path) else: # 不写入图像,直接转cv图像bgr格式 self.flush_screen(1) return cv2.cvtColor(np.asarray(self.client.screen), cv2.COLOR_RGB2BGR) def capture_to_addr(self): self.flush_screen(1) image_bytes = np.asarray(self.client.screen).tobytes() ctypes.memmove(self.image_buffer, image_bytes, self.image_size) return ctypes.addressof(self.image_buffer), self.image_size # 移动鼠标 def move(self, x, y): self.client.mouseMove(x, y) # 点击鼠标按钮,123分别对应左中右键 def click(self, button=1, delay=0.05): self.client.mouseDown(button) time.sleep(delay) self.client.mouseUp(button) # self.flush_screen() # 移动并点击鼠标左键 def left_click(self, x, y): self.move(x, y) self.click() self.flush_screen() # 双击鼠标左键 def double_left_click(self, x, y): self.move(x, y) self.click() time.sleep(0.1) self.click() # 点击鼠标右键 def right_click(self, x, y): self.move(x, y) self.click(3) # 拖动 def drag(self, x, y, step=1): return self.client.mouseDrag(x, y, step) # 按键一次 def key_press(self, key_str): # key_str可以参考 KEYMAP self.client.keyPress(key_str) self.flush_screen() # 刷新屏幕 def flush_screen(self, incremental=1): return self.client.refreshScreen(incremental) # 屏幕更改时才刷新,节省宽带 def key_down(self, key_str): return self.client.keyDown(key_str) def key_up(self, key_str): return self.client.keyUp(key_str) # 组合键 def hot_key(self, key_list): for key_str in key_list: self.key_down(key_str) time.sleep(0.05) for key_str in key_list[::-1]: self.key_down(key_str) time.sleep(0.05) if __name__ == '__main__': v = VNC("176.232.102.57", "5900","Qwert12345") # # 键盘测试 # v.key_press("a") # # 鼠标测试 # v.move(200, 500) # v.click(1) # 截图测试 FPS = 0 while True: s = time.time() new_image = v.capture(path=None) # 获取新图像 print(time.time()-s) FPS = 1 / (time.time() - s) # 绘制帧率 cv2.putText(new_image, str(int(FPS)), (0, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) cv2.imshow("img", new_image) cv2.waitKey(1) v.move(random.randint(100,800),random.randint(100,800)) api.shutdown() # 关闭事件循环
标签:VNC,str,Python,self,client,key,image,连接,def From: https://www.cnblogs.com/zwnsyw/p/18542395