def snapshot(self, filename=None, strType=False, quality=10, max_size=None, **kwargs): if self._is_ios() or self._is_android(): value = self.driver.get_screenshot_as_base64() data = base64.b64decode(value) if strType: if filename: with open(filename, 'wb') as f: f.write(data) return data # output cv2 object try: screen = aircv.utils.string_2_img(data) except: # may be black/locked screen or other reason, print exc for debugging traceback.print_exc() return None # save as file if needed if filename: aircv.imwrite(filename, screen, quality, max_size=max_size) return screen else: raise Exception('Unsupport this keyword')
def snapshot(self, filename=None, quality=10, max_size=None): """ Take a screenshot and save it in ST.LOG_DIR folder Args: filename: name of the file to give to the screenshot, {time}.jpg by default quality: The image quality, integer in range [1, 99] max_size: the maximum size of the picture, e.g 1200 Returns: display the screenshot """ if self.handle: screen = screenshot(filename, self.handle) else: screen = screenshot(filename) if self.app: rect = self.get_rect() screen = aircv.crop_image( screen, [rect.left, rect.top, rect.right, rect.bottom]) if not screen.any(): if self.app: rect = self.get_rect() screen = aircv.crop_image( screenshot(filename), [rect.left, rect.top, rect.right, rect.bottom]) if self._focus_rect != (0, 0, 0, 0): height, width = screen.shape[:2] rect = (self._focus_rect[0], self._focus_rect[1], width + self._focus_rect[2], height + self._focus_rect[3]) screen = aircv.crop_image(screen, rect) if filename: aircv.imwrite(filename, screen, quality, max_size=max_size) return screen
def snapshot(self, filename="tmp.png"): """ Take a screenshot and save it to `tmp.png` filename by default Args: filename: name of file where to store the screenshot Returns: display the screenshot """ if not filename: filename = "tmp.png" if self.handle: screen = screenshot(filename, self.handle) else: screen = screenshot(filename) if self.app: rect = self.get_rect() screen = aircv.crop_image(screen, [rect.left, rect.top, rect.right, rect.bottom]) if self._focus_rect != (0, 0, 0, 0): height, width = screen.shape[:2] rect = (self._focus_rect[0], self._focus_rect[1], width + self._focus_rect[2], height + self._focus_rect[3]) screen = aircv.crop_image(screen, rect) if filename: aircv.imwrite(filename, screen) return screen
def snapshot(self, filename=None): """ Take a screenshot and save it in ST.LOG_DIR folder Args: filename: name of the file to give to the screenshot, {time}.jpg by default Returns: display the screenshot """ if self.handle: screen = screenshot(filename, self.handle) else: screen = screenshot(filename) if self.app: rect = self.get_rect() screen = aircv.crop_image(screen, [rect.left, rect.top, rect.right, rect.bottom]) if not screen.any(): if self.app: rect = self.get_rect() screen = aircv.crop_image(screenshot(filename), [rect.left, rect.top, rect.right, rect.bottom]) if self._focus_rect != (0, 0, 0, 0): height, width = screen.shape[:2] rect = (self._focus_rect[0], self._focus_rect[1], width + self._focus_rect[2], height + self._focus_rect[3]) screen = aircv.crop_image(screen, rect) if filename: aircv.imwrite(filename, screen) return screen
def try_log_screen(screen=None, quality=None, max_size=None): """ Save screenshot to file Args: screen: screenshot to be saved quality: The image quality, default is ST.SNAPSHOT_QUALITY max_size: the maximum size of the picture, e.g 1200 Returns: None """ if not ST.LOG_DIR: return if not quality: quality = ST.SNAPSHOT_QUALITY if not max_size: max_size = ST.IMAGE_MAXSIZE if screen is None: screen = G.DEVICE.snapshot(quality=quality) filename = "%(time)d.jpg" % {'time': time.time() * 1000} filepath = os.path.join(ST.LOG_DIR, filename) aircv.imwrite(filepath, screen, quality, max_size=max_size) return {"screen": filename, "resolution": aircv.get_resolution(screen)}
def snapshot(self, filename=None, quality=10, max_size=None): """ take snapshot Args: filename: save screenshot to filename quality: The image quality, integer in range [1, 99] max_size: the maximum size of the picture, e.g 1200 Returns: """ data = self._neo_wda_screenshot() # output cv2 object try: screen = aircv.utils.string_2_img(data) except: # may be black/locked screen or other reason, print exc for debugging traceback.print_exc() return None # save as file if needed if filename: aircv.imwrite(filename, screen, quality, max_size=max_size) return screen
def snapshot(self, filename=None, strType=False, quality=10, max_size=None): """ take snapshot Args: filename: save screenshot to filename quality: The image quality, integer in range [1, 99] max_size: the maximum size of the picture, e.g 1200 Returns: display the screenshot """ data = None if self.cap_method == CAP_METHOD.MINICAP: raise NotImplementedError elif self.cap_method == CAP_METHOD.MINICAP_STREAM: raise NotImplementedError elif self.cap_method == CAP_METHOD.WDACAP: data = self._neo_wda_screenshot() # wda 截图不用考虑朝向 # 实时刷新手机画面,直接返回base64格式,旋转问题交给IDE处理 if strType: if filename: with open(filename, 'wb') as f: f.write(data) return data # output cv2 object try: screen = aircv.utils.string_2_img(data) except: # may be black/locked screen or other reason, print exc for debugging traceback.print_exc() return None h, w = screen.shape[:2] # save last res for portrait if self.orientation in [LANDSCAPE, LANDSCAPE_RIGHT]: self._size['height'] = w self._size['width'] = h else: self._size['height'] = h self._size['width'] = w winw, winh = self.window_size() self._touch_factor = float(winh) / float(h) # save as file if needed if filename: aircv.imwrite(filename, screen, quality, max_size=max_size) return screen
def snapshot(self, filename=None, ensure_orientation=True, quality=10, max_size=None): """ Take the screenshot of the display. The output is send to stdout by default. Args: filename: name of the file where to store the screenshot, default is None which is stdout ensure_orientation: True or False whether to keep the orientation same as display quality: The image quality, integer in range [1, 99] max_size: the maximum size of the picture, e.g 1200 Returns: screenshot output """ """default not write into file.""" if self.cap_method == CAP_METHOD.MINICAP_STREAM: self.rotation_watcher.get_ready() screen = self.minicap.get_frame_from_stream() elif self.cap_method == CAP_METHOD.MINICAP: screen = self.minicap.get_frame() elif self.cap_method == CAP_METHOD.JAVACAP: screen = self.javacap.get_frame_from_stream() else: screen = self.adb.snapshot() # output cv2 object try: screen = aircv.utils.string_2_img(screen) except Exception: # may be black/locked screen or other reason, print exc for debugging import traceback traceback.print_exc() return None # ensure the orientation is right if ensure_orientation and self.display_info["orientation"]: # minicap screenshots are different for various sdk_version if self.cap_method in ( CAP_METHOD.MINICAP, CAP_METHOD.MINICAP_STREAM) and self.sdk_version <= 16: h, w = screen.shape[:2] # cvshape是高度在前面!!!! if w < h: # 当前是横屏,但是图片是竖的,则旋转,针对sdk<=16的机器 screen = aircv.rotate(screen, self.display_info["orientation"] * 90, clockwise=False) # adb 截图总是要根据orientation旋转,但是SDK版本大于等于25(Android7.1以后)无需额外旋转 elif self.cap_method == CAP_METHOD.ADBCAP and self.sdk_version <= SDK_VERISON_ANDROID7: screen = aircv.rotate(screen, self.display_info["orientation"] * 90, clockwise=False) if filename: aircv.imwrite(filename, screen, quality, max_size=max_size) return screen
def try_log_screen(screen=None): """ Save screenshot to file Args: screen: screenshot to be saved Returns: None """ if not ST.LOG_DIR: return if screen is None: screen = G.DEVICE.snapshot(quality=ST.SNAPSHOT_QUALITY) filename = "%(time)d.jpg" % {'time': time.time() * 1000} filepath = os.path.join(ST.LOG_DIR, filename) aircv.imwrite(filepath, screen, ST.SNAPSHOT_QUALITY) return {"screen": filename, "resolution": aircv.get_resolution(screen)}
def try_log_screen(screen=None): """ Save screenshot to file Args: screen: screenshot to be saved Returns: None """ if not ST.LOG_DIR: return if screen is None: screen = G.DEVICE.snapshot() filename = "%(time)d.jpg" % {'time': time.time() * 1000} filepath = os.path.join(ST.LOG_DIR, filename) aircv.imwrite(filepath, screen) log_in_func({"screen": filename})
def snapshot(self, filename=None, strType=False, quality=10, max_size=None): """ take snapshot Args: filename: save screenshot to filename quality: The image quality, integer in range [1, 99] max_size: the maximum size of the picture, e.g 1200 Returns: display the screenshot """ data = None # 暂时只有一种截图方法, WDACAP if self.cap_method == CAP_METHOD.WDACAP: data = self._neo_wda_screenshot() # wda 截图不用考虑朝向 # 实时刷新手机画面,直接返回base64格式,旋转问题交给IDE处理 if strType: if filename: with open(filename, 'wb') as f: f.write(data) return data # output cv2 object try: screen = aircv.utils.string_2_img(data) except: # may be black/locked screen or other reason, print exc for debugging traceback.print_exc() return None # save as file if needed if filename: aircv.imwrite(filename, screen, quality, max_size=max_size) return screen
def snapshot(self, filename=None, ensure_orientation=True, quality=10, max_size=None): """ Take the screenshot of the display. The output is send to stdout by default. Args: filename: name of the file where to store the screenshot, default is None which is stdout ensure_orientation: True or False whether to keep the orientation same as display quality: The image quality, integer in range [1, 99] max_size: the maximum size of the picture, e.g 1200 Returns: screenshot output """ # default not write into file. screen = self.screen_proxy.snapshot( ensure_orientation=ensure_orientation) if filename: aircv.imwrite(filename, screen, quality, max_size=max_size) return screen
def loop_find3(total_pic, query, posi, tag=None, timeout=ST.FIND_TIMEOUT, threshold=None, interval=0.5, intervalfunc=None): # print("ST.FIND_TIMEOUT",ST.FIND_TIMEOUT) G.LOGGING.info("Try finding:\n%s", query.record_pos) G.LOGGING.info(query._imread().shape) # screen_resolution = aircv.get_resolution(screen) part_size_width = query._imread().shape[0] part_size_height = query._imread().shape[1] part_resolution_width = query.resolution[0] part_resolution_height = query.resolution[1] # pairt_origin_xmin_relative = posi[0]; # pairt_origin_xmax_relative = posi[2] ; pairt_origin_ymin_relative = posi[0] / part_resolution_height pairt_origin_ymax_relative = posi[1] / part_resolution_height # print("香菇:",(pairt_origin_ymin_relative,pairt_origin_ymax_relative)) posi_x = query.record_pos[0] posi_y = query.record_pos[1] # print("posi_x",posi_x) start_time = time.time() screen = aircv.imread(total_pic) G.LOGGING.debug("query path %s" % query.filepath) # predict_area = aircv.crop_image(screen, (xmin, ymin, xmax, ymax)) screen_resolution = aircv.get_resolution(screen) # print("总图片的分辨率",screen_resolution); total_resolution_width = screen_resolution[0] total_resolution_height = screen_resolution[1] width_scale = round(total_resolution_width / part_resolution_width, 2) height_scale = round(total_resolution_height / part_resolution_height, 2) # print("width_scale",width_scale) # print("width_scale",height_scale) # print("总图的分辨率",(total_resolution_width,total_resolution_height)) # print("缩放之后的相对坐标",(pairt_origin_xmin_relative * width_scale,pairt_origin_ymin_relative * height_scale,pairt_origin_xmax_relative * width_scale,pairt_origin_ymax_relative * height_scale)) # pairt_end_xmin_absolute = round( (pairt_origin_xmin_relative * width_scale) * total_resolution_width ); pairt_end_xmin_absolute = 0 # pairt_end_xmax_absolute = round( (pairt_origin_xmax_relative * width_scale) * total_resolution_width ); pairt_end_xmax_absolute = total_resolution_width pairt_end_ymin_absolute = round( (pairt_origin_ymin_relative * height_scale) * total_resolution_height) - 30 pairt_end_ymax_absolute = round( (pairt_origin_ymax_relative * height_scale) * total_resolution_height) + 30 # print("enddddd",(pairt_end_xmin_absolute,pairt_end_ymin_absolute,pairt_end_xmax_absolute,pairt_end_ymax_absolute)) # part_resolution_width_scaled = part_size_width * width_scale; # part_resolution_height_scaled = part_size_height * height_scale; # part_end_xmax = ((posi_x + 0.5) if (posi_x + 0.5)<=1 else 1)*total_resolution_width # part_end_ymax = ((posi_y + 0.5) if (posi_y + 0.5)<=1 else 1)*total_resolution_height # part_end_xmin = part_end_xmax - part_resolution_width_scaled # part_end_ymin = part_end_ymax - part_resolution_height_scaled predict_area = aircv.crop_image( screen, (pairt_end_xmin_absolute, pairt_end_ymin_absolute, pairt_end_xmax_absolute, pairt_end_ymax_absolute)) screen[0:pairt_end_ymin_absolute, 0:total_resolution_width] = 0 screen[pairt_end_ymax_absolute:total_resolution_height, 0:total_resolution_width] = 0 # print("黑素区域",(pairt_end_ymin_absolute,pairt_end_ymax_absolute)) # screen[200:250,0:50] = 0; aircv.imwrite("part_black.png", screen, 99) # predict_area = aircv.crop_image(screen, (0, 0, 1126, 2436)); # print("hahahahah") # print((part_end_xmin, part_end_ymin, part_end_xmax, part_end_ymax)) # print(predict_area.shape) aircv.imwrite("predict_area3.png", predict_area, 99) while True: if predict_area is None: G.LOGGING.warning("预测区域有问题!") else: if threshold: query.threshold = threshold match_pos = query.match_in(screen) if match_pos: try_log_screen(screen) return match_pos if intervalfunc is not None: intervalfunc() # 超时则raise,未超时则进行下次循环: if (time.time() - start_time) > timeout: try_log_screen(screen) # raise TargetNotFoundError('Picture %s not found in screen' % query) G.LOGGING.warning("图片查找超时!") break else: time.sleep(interval)
def loop_find2(total_pic, query, timeout=ST.FIND_TIMEOUT, threshold=None, interval=0.5, intervalfunc=None): G.LOGGING.info("Try finding:\n%s", query.record_pos) G.LOGGING.info(query._imread().shape) part_size_width = query._imread().shape[0] part_size_height = query._imread().shape[1] part_resolution_width = query.resolution[0] part_resolution_height = query.resolution[1] # print("part_resolution_width:" + str(part_resolution_width)) # print("part_resolution_height:" + str(part_resolution_height)) posi_x = query.record_pos[0] posi_y = query.record_pos[1] print("posi_x", posi_x) start_time = time.time() screen = aircv.imread(total_pic) G.LOGGING.debug("query path %s" % query.filepath) # predict_area = aircv.crop_image(screen, (xmin, ymin, xmax, ymax)) screen_resolution = aircv.get_resolution(screen) total_resolution_width = query.resolution[0] total_resolution_height = query.resolution[1] width_scale = total_resolution_width / part_resolution_width height_scale = total_resolution_height / part_resolution_height print("test_scale:" + str(1 / 2)) print("width_scale:" + str(width_scale)) print("height_scase:" + str(height_scale)) part_resolution_width_scaled = part_size_width * width_scale part_resolution_height_scaled = part_size_height * height_scale print("part_resolution_width_scaled:" + str(part_resolution_width_scaled)) print("part_resolution_height_scaled:" + str(part_resolution_height_scaled)) G.LOGGING.debug("图片的分辨率") G.LOGGING.debug(screen_resolution) part_end_xmax = ((posi_x + 0.5) if (posi_x + 0.5) <= 1 else 1) * total_resolution_width part_end_ymax = ((posi_y + 0.5) if (posi_y + 0.5) <= 1 else 1) * total_resolution_height part_end_xmin = part_end_xmax - part_resolution_width_scaled part_end_ymin = part_end_ymax - part_resolution_height_scaled predict_area = aircv.crop_image( screen, (part_end_xmin, part_end_ymin, part_end_xmax, part_end_ymax)) print("hahahahah") print((part_end_xmin, part_end_ymin, part_end_xmax, part_end_ymax)) print(predict_area.shape) aircv.imwrite("predict_area.png", screen, 99) while True: if predict_area is None: G.LOGGING.warning("预测区域有问题!") else: if threshold: query.threshold = threshold match_pos = query.match_in(screen) if match_pos: try_log_screen(screen) return match_pos if intervalfunc is not None: intervalfunc() # 超时则raise,未超时则进行下次循环: if (time.time() - start_time) > timeout: try_log_screen(predict_area) # raise TargetNotFoundError('Picture %s not found in screen' % query) G.LOGGING.warning("图片查找超时!") break else: time.sleep(interval)
def snapshot(self, strType=False, filename=None, ensure_orientation=True): """ take snapshot filename: save screenshot to filename """ data = None if self.cap_method == CAP_METHOD.MINICAP: raise NotImplementedError elif self.cap_method == CAP_METHOD.MINICAP_STREAM: raise NotImplementedError elif self.cap_method == CAP_METHOD.WDACAP: data = self._neo_wda_screenshot() # wda 截图不用考虑朝向 if strType: if filename: with open(filename, 'wb') as f: f.write(data) return data # output cv2 object try: screen = aircv.utils.string_2_img(data) except: # may be black/locked screen or other reason, print exc for debugging import traceback traceback.print_exc() return None now_orientation = self.orientation # ensure the orientation is right if ensure_orientation and now_orientation in [LANDSCAPE, LANDSCAPE_RIGHT]: # minicap screenshots are different for various sdk_version if self.cap_method in (CAP_METHOD.MINICAP, CAP_METHOD.MINICAP_STREAM) and self.sdk_version <= 16: h, w = screen.shape[:2] # cvshape是高度在前面!!!! if w < h: # 当前是横屏,但是图片是竖的,则旋转,针对sdk<=16的机器 screen = aircv.rotate(screen, self.display_info["orientation"] * 90, clockwise=False) # wda 截图是要根据orientation旋转 elif self.cap_method == CAP_METHOD.WDACAP: # seems no need to rotate now pass #screen = aircv.rotate(screen, 90, clockwise= (now_orientation == LANDSCAPE_RIGHT) ) # readed screen size h, w = screen.shape[:2] # save last res for portrait if now_orientation in [LANDSCAPE, LANDSCAPE_RIGHT]: self._size['height'] = w self._size['width'] = h else: self._size['height'] = h self._size['width'] = w winw, winh = self.window_size() self._touch_factor = float(winh) / float(h) # save as file if needed if filename: aircv.imwrite(filename, screen) return screen