-
-
Save hiruthicShaSS/e2ce710032199729b16160eb040e5c8d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import cv2 | |
| import subprocess | |
| import time | |
| SCREENSHOT_DEVICE_PATH = "/sdcard/screen.png" | |
| SCREENSHOT_LOCAL_PATH = "screen.png" | |
| def take_screenshot(): | |
| subprocess.run(["adb", "shell", "screencap", "-p", SCREENSHOT_DEVICE_PATH], stdout=subprocess.DEVNULL) | |
| subprocess.run(["adb", "pull", SCREENSHOT_DEVICE_PATH, SCREENSHOT_LOCAL_PATH], stdout=subprocess.DEVNULL) | |
| def send_adb_tap(x, y): | |
| subprocess.run(["adb", "shell", "input", "tap", str(x), str(y)]) | |
| def send_adb_swipe(x1, y1, x2, y2, duration_ms=300): | |
| subprocess.run(["adb", "shell", "input", "swipe", str(x1), str(y1), str(x2), str(y2), str(duration_ms)]) | |
| def send_adb_hold(x, y, duration_ms=1000): | |
| print(f"Holding at ({x}, {y}) for {duration_ms}ms") | |
| subprocess.run(["adb", "shell", "input", "swipe", str(x), str(y), str(x), str(y), str(duration_ms)]) | |
| def send_adb_text_input(): | |
| text = input("Enter text to send: ") | |
| escaped_text = text.replace(" ", "%s") | |
| subprocess.run(["adb", "shell", "input", "text", escaped_text]) | |
| def load_and_resize_image(max_width=800, max_height=800): | |
| img = cv2.imread(SCREENSHOT_LOCAL_PATH) | |
| if img is None: | |
| return None, None, None | |
| orig_height, orig_width = img.shape[:2] | |
| scale = min(max_width / orig_width, max_height / orig_height, 1.0) | |
| resized_img = cv2.resize(img, (int(orig_width * scale), int(orig_height * scale))) | |
| return resized_img, scale, (orig_width, orig_height) | |
| def main(): | |
| window_name = "ADB View + Tap/Type/Swipe/Hold" | |
| cv2.namedWindow(window_name) | |
| swipe_start = [None] | |
| scale = [1.0] # wrap in list to modify in inner scope | |
| def on_mouse_event(event, x, y, flags, param): | |
| real_x = int(x / scale[0]) | |
| real_y = int(y / scale[0]) | |
| if event == cv2.EVENT_LBUTTONDOWN: # Left Click (Tap) | |
| swipe_start[0] = (real_x, real_y) | |
| elif event == cv2.EVENT_LBUTTONUP and swipe_start[0]: # Left Release (Swipe) | |
| x1, y1 = swipe_start[0] | |
| x2, y2 = real_x, real_y | |
| if (x1, y1) == (x2, y2): # Tap (No movement) | |
| print(f"Tap at ({x1}, {y1})") | |
| send_adb_tap(x1, y1) | |
| else: # Swipe | |
| print(f"Swipe from ({x1}, {y1}) to ({x2}, {y2})") | |
| send_adb_swipe(x1, y1, x2, y2) | |
| swipe_start[0] = None | |
| elif event == cv2.EVENT_RBUTTONDOWN: # Right Click (Hold) | |
| print(f"Hold at ({real_x}, {real_y})") | |
| send_adb_hold(real_x, real_y, duration_ms=1000) # Hold for 1 second | |
| cv2.setMouseCallback(window_name, on_mouse_event) | |
| print("ESC: Exit | T: Type | Left Click: Tap | Left Drag: Swipe | Right Click: Hold") | |
| while True: | |
| take_screenshot() | |
| img, scale_val, _ = load_and_resize_image() | |
| if img is not None: | |
| scale[0] = scale_val | |
| cv2.imshow(window_name, img) | |
| key = cv2.waitKey(1) & 0xFF | |
| if key == 27: # ESC | |
| break | |
| elif key == ord('t'): | |
| send_adb_text_input() | |
| time.sleep(0.2) # refresh interval | |
| cv2.destroyAllWindows() | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment