1- import io
21import subprocess
32
43import numpy as np
87
98
109class Screen :
11- @staticmethod
12- def take_screenshot ():
13- """
14- Take a screenshot of the emulator
15- """
16- screenshot_bytes = subprocess .check_output (['adb' , 'exec-out' , 'screencap' , '-p' ])
17- if screenshot_bytes and len (screenshot_bytes ) > 5 and screenshot_bytes [5 ] == 0x0d :
18- screenshot_bytes = screenshot_bytes .replace (b'\r \n ' , b'\n ' )
19- screenshot = io .BytesIO (screenshot_bytes )
20- screenshot = Image .open (screenshot ).convert ('RGB' )
21- screenshot = screenshot .resize ((SCREENSHOT_WIDTH , SCREENSHOT_HEIGHT ), Image .BILINEAR )
22- return screenshot
10+ def __init__ (self ):
11+ # Physical size: 720x1280 -> self.width = 720, self.height = 1280
12+ window_size = subprocess .check_output (['adb' , 'shell' , 'wm' , 'size' ])
13+ window_size = window_size .decode ('ascii' ).replace ('Physical size: ' , '' )
14+ self .width , self .height = [int (i ) for i in window_size .split ('x' )]
2315
2416 @staticmethod
2517 def click (x , y ):
@@ -28,6 +20,15 @@ def click(x, y):
2820 """
2921 subprocess .run (['adb' , 'shell' , 'input' , 'tap' , str (x ), str (y )])
3022
23+ def take_screenshot (self ):
24+ """
25+ Take a screenshot of the emulator
26+ """
27+ screenshot_bytes = subprocess .run (['adb' , 'exec-out' , 'screencap' ], check = True , capture_output = True ).stdout
28+ screenshot = Image .frombuffer ('RGBA' , (self .width , self .height ), screenshot_bytes [12 :], 'raw' , 'RGBX' , 0 , 1 )
29+ screenshot = screenshot .convert ('RGB' ).resize ((SCREENSHOT_WIDTH , SCREENSHOT_HEIGHT ), Image .BILINEAR )
30+ return screenshot
31+
3132
3233def main ():
3334 cls = Screen ()
0 commit comments