# coding=utf-8 import asyncio import logging import os from datetime import datetime import uiautomator2 as u2 from Config.Config import TEMP_IMAGE_DIR # 配置日志输出 logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' ) logger = logging.getLogger("ScreenshotTool") async def take_screenshot(): """ 打开微信主界面并拍照(截图) """ # 连接设备 d = u2.connect() # 执行截图 timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") filename = f"Screenshot_{timestamp}.jpg" filepath = os.path.join(TEMP_IMAGE_DIR, filename) logger.info(f"正在拍照(截图)并保存至: {filepath}") d.screenshot(filepath) if os.path.exists(filepath): logger.info("拍照成功!") return filepath else: logger.error("拍照失败,未找到生成的文件。") return None async def main(): filepath = await take_screenshot() if filepath: logger.info(f"任务执行完成,截图文件: {filepath}") else: logger.error("任务执行失败。") if __name__ == "__main__": try: asyncio.run(main()) except KeyboardInterrupt: logger.info("程序被用户中断.") except Exception as e: logger.exception(f"运行过程中出现异常: {e}")