diff --git a/clashroyalebuildabot/screen.py b/clashroyalebuildabot/screen.py index 63dd4a4..6461a97 100644 --- a/clashroyalebuildabot/screen.py +++ b/clashroyalebuildabot/screen.py @@ -1,4 +1,3 @@ -import io import subprocess import numpy as np @@ -8,18 +7,11 @@ class Screen: - @staticmethod - def take_screenshot(): - """ - Take a screenshot of the emulator - """ - screenshot_bytes = subprocess.check_output(['adb', 'exec-out', 'screencap', '-p']) - if screenshot_bytes and len(screenshot_bytes) > 5 and screenshot_bytes[5] == 0x0d: - screenshot_bytes = screenshot_bytes.replace(b'\r\n', b'\n') - screenshot = io.BytesIO(screenshot_bytes) - screenshot = Image.open(screenshot).convert('RGB') - screenshot = screenshot.resize((SCREENSHOT_WIDTH, SCREENSHOT_HEIGHT), Image.BILINEAR) - return screenshot + def __init__(self): + # Physical size: 720x1280 -> self.width = 720, self.height = 1280 + window_size = subprocess.check_output(['adb', 'shell', 'wm', 'size']) + window_size = window_size.decode('ascii').replace('Physical size: ', '') + self.width, self.height = [int(i) for i in window_size.split('x')] @staticmethod def click(x, y): @@ -28,6 +20,15 @@ def click(x, y): """ subprocess.run(['adb', 'shell', 'input', 'tap', str(x), str(y)]) + def take_screenshot(self): + """ + Take a screenshot of the emulator + """ + screenshot_bytes = subprocess.run(['adb', 'exec-out', 'screencap'], check=True, capture_output=True).stdout + screenshot = Image.frombuffer('RGBA', (self.width, self.height), screenshot_bytes[12:], 'raw', 'RGBX', 0, 1) + screenshot = screenshot.convert('RGB').resize((SCREENSHOT_WIDTH, SCREENSHOT_HEIGHT), Image.BILINEAR) + return screenshot + def main(): cls = Screen()