Python实现:对安卓模拟器进行屏幕滑动、调用截图功能

使用

打开下载的雷电模拟器路径

找到adb.exe的可执行文件文件,替换代码中的“E:\leidian\LDPlayer9\adb.exe”部分

运行代码可实现模拟由下置上滑动

运行本程序前请:打开模拟器并启用adb.exe;否则会报错

import subprocess
import threading
import time
import tkinter as tk
import keyboard  # 使用keyboard库检测按键事件
import tkinter.font as tkfont  # 导入tkinter的font模块

adb_path = r"E:\leidian\LDPlayer9\adb.exe"  # 请根据实际路径修改
is_swiping = False
swipe_thread = None  # 用来存储滑动操作的线程
swipe_speed = 1000  # 初始滑动速度


def check_adb_connection():
    result = subprocess.run([adb_path, "devices"], capture_output=True, text=True)
    devices = result.stdout.strip().splitlines()
    if len(devices) > 1:
        print("设备连接成功")
    else:
        print("未找到设备,请确认模拟器是否已启动并连接。")


def swipe_screen():
    global is_swiping, swipe_speed
    while is_swiping:
        # 模拟滑动操作,速度根据swipe_speed变量来调整
        subprocess.run([adb_path, "shell", "input", "touchscreen", "swipe", "800", "600", "800", "300", str(swipe_speed)])
        time.sleep(1)  # 每次滑动间隔1秒


def stop_swipe():
    global is_swiping, swipe_thread
    is_swiping = False
    # 等待滑动线程结束
    if swipe_thread and swipe_thread.is_alive():
        swipe_thread.join()
    print("停止滑动并松开屏幕...")
    # 模拟松开按下的屏幕
    subprocess.run([adb_path, "shell", "input", "touchscreen", "tap", "20", "600"])


def take_screenshot():
    # 获取模拟器当前时间戳
    timestamp = subprocess.run([adb_path, "shell", "date", "+%Y%m%d%H%M%S"], capture_output=True,
                               text=True).stdout.strip()
    # 构建截图文件名,包含模拟器时间戳
    screenshot_filename = f"screenshot_{timestamp}.png"
    screenshot_path = f"/sdcard/Pictures/Screenshots/{screenshot_filename}"

    # 执行截图操作
    subprocess.run([adb_path, "shell", "screencap", "-p", screenshot_path])
    print(f"截图已保存到模拟器相册:{screenshot_path}")

    # 通知系统刷新相册,让模拟器识别并显示截图
    subprocess.run([adb_path, "shell", "am", "broadcast", "-a", "android.intent.action.MEDIA_SCANNER_SCAN_FILE",
                    "--es", "path", screenshot_path])


def start_swiping():
    global is_swiping, swipe_thread
    if not is_swiping:
        is_swiping = True
        print("开始滑动屏幕...")
        # 在新线程中执行滑动操作
        swipe_thread = threading.Thread(target=swipe_screen, daemon=True)
        swipe_thread.start()
    else:
        print("滑动操作已在进行中。")


def stop_swiping():
    if is_swiping:
        stop_swipe()
    else:
        print("没有滑动操作在进行中。")


def speed_up():
    global swipe_speed
    if swipe_speed > 200:
        swipe_speed -= 200  # 加速滑动,减少滑动速度
        print(f"加速滑动,当前速度:{swipe_speed}")
    else:
        print("已达到最小滑动速度。")


def slow_down():
    global swipe_speed
    if swipe_speed < 5000:
        swipe_speed += 400  # 减慢滑动,增加滑动速度
        print(f"减慢滑动,当前速度:{swipe_speed}")
    else:
        print(f"已达到最大滑动速度:{swipe_speed}")


def reset_speed():
    global swipe_speed
    swipe_speed = 1000  # 恢复到初始滑动速度
    print("恢复到初始滑动速度:1000")


def handle_screenshot():
    print("开始截屏...")
    take_screenshot()


def on_key_event(keyboard_event):
    if keyboard_event.name == "esc":
        root.quit()  # 按下Esc键退出


# 创建GUI
root = tk.Tk()
root.title("设备控制窗口")
root.geometry("300x210")  # 设置窗口大小     500x350

# 设置窗口透明度为0%
root.attributes('-alpha', 1.0)

# 加载自定义字体 (EMXS.ttf)
font_path = "EMXS.ttf"  # 假设字体文件与脚本在同一目录下
custom_font = tkfont.Font(family=font_path, size=16, weight="bold")  # 加载字体,设置大小和加粗

# 创建按钮并绑定功能
button_width = 15  # 按钮宽度
button_height = 2  # 按钮高度

start_button = tk.Button(root, text="开始滑动", command=start_swiping, font=custom_font, width=button_width, height=button_height)
start_button.grid(row=0, column=0, padx=10, pady=10)

stop_button = tk.Button(root, text="停止滑动", command=stop_swiping, font=custom_font, width=button_width, height=button_height)
stop_button.grid(row=0, column=1, padx=10, pady=10)

slow_down_button = tk.Button(root, text="减速滑动", command=slow_down, font=custom_font, width=button_width, height=button_height)
slow_down_button.grid(row=1, column=0, padx=10, pady=10)

speed_up_button = tk.Button(root, text="加速滑动", command=speed_up, font=custom_font, width=button_width, height=button_height)
speed_up_button.grid(row=1, column=1, padx=10, pady=10)

reset_button = tk.Button(root, text="重置速度", command=reset_speed, font=custom_font, width=button_width, height=button_height)
reset_button.grid(row=2, column=0, padx=10, pady=10)

screenshot_button = tk.Button(root, text="截取屏幕", command=handle_screenshot, font=custom_font, width=button_width, height=button_height)
screenshot_button.grid(row=2, column=1, padx=10, pady=10)

# 设置居中布局
for row in range(3):
    root.grid_rowconfigure(row, weight=1)
for col in range(2):
    root.grid_columnconfigure(col, weight=1)

# 监听Esc键退出
keyboard.hook(on_key_event)

# 启动ADB连接检查
check_adb_connection()

# 运行GUI
root.mainloop()

说明

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值