Last active
January 15, 2018 06:31
-
-
Save bitristan/b7a2ed6454999d6fbb8ae43c398eca63 to your computer and use it in GitHub Desktop.
使用adb进行手机屏幕截图
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
# -*- coding: utf-8 -*- | |
""" | |
手机屏幕截图,参考 https://github.com/wangshub/wechat_jump_game | |
""" | |
import subprocess | |
import os | |
import sys | |
from PIL import Image | |
SCREENSHOT_WAY = 3 | |
def pull_screenshot(): | |
""" | |
获取屏幕截图,目前有 0 1 2 3 四种方法,未来添加新的平台监测方法时, | |
可根据效率及适用性由高到低排序 | |
""" | |
global SCREENSHOT_WAY | |
if 1 <= SCREENSHOT_WAY <= 3: | |
process = subprocess.Popen( | |
'adb shell screencap -p', | |
shell=True, stdout=subprocess.PIPE) | |
binary_screenshot = process.stdout.read() | |
if SCREENSHOT_WAY == 2: | |
binary_screenshot = binary_screenshot.replace(b'\r\n', b'\n') | |
elif SCREENSHOT_WAY == 1: | |
binary_screenshot = binary_screenshot.replace(b'\r\r\n', b'\n') | |
f = open('autojump.png', 'wb') | |
f.write(binary_screenshot) | |
f.close() | |
elif SCREENSHOT_WAY == 0: | |
os.system('adb shell screencap -p /sdcard/autojump.png') | |
os.system('adb pull /sdcard/autojump.png .') | |
def check_screenshot(): | |
""" | |
检查获取截图的方式 | |
""" | |
global SCREENSHOT_WAY | |
if os.path.isfile('autojump.png'): | |
try: | |
os.remove('autojump.png') | |
except Exception: | |
pass | |
if SCREENSHOT_WAY < 0: | |
print('暂不支持当前设备') | |
sys.exit() | |
pull_screenshot() | |
try: | |
Image.open('./autojump.png').load() | |
print('采用方式 {} 获取截图'.format(SCREENSHOT_WAY)) | |
except Exception: | |
SCREENSHOT_WAY -= 1 | |
check_screenshot() | |
def main(): | |
# 检测手机截图方式 | |
check_screenshot() | |
# 循环截图 | |
while True: | |
pull_screenshot() | |
# 等待1s | |
time.sleep(1) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment