def make_canvas(self): # screen canvas, bind mouse input & keyboard input self.canvas = tk.Canvas(self.root, bg='black', bd=0, highlightthickness=0) self.canvas.pack() def _screen2touch(w, h, x, y): '''convert touch position''' ori = adb.orientation() if ori == 0: return x, y elif ori == 1: # landscape-right return w - y, x elif ori == 2: # upsidedown return w - x, h - y elif ori == 3: # landscape-left return h - x, y return x, y w, h = adb.display() screen2touch = partial(_screen2touch, w, h) def on_mouse_down(event): self.canvas.focus_set() x, y = int(event.x / scale), int(event.y / scale) x, y = screen2touch(x, y) adb._mini.touchqueue.put('d 0 %d %d 30\nc\n' % (x, y)) def on_mouse_up(event): adb._mini.touchqueue.put('u 0\nc\n') def on_mouse_drag(event): x, y = int(event.x / scale), int(event.y / scale) x, y = screen2touch(x, y) adb._mini.touchqueue.put('m 0 %d %d 30\nc\n' % (x, y)) self.canvas.bind('<ButtonPress-1>', on_mouse_down) self.canvas.bind('<ButtonRelease-1>', on_mouse_up) self.canvas.bind('<B1-Motion>', on_mouse_drag) keymap = { '\r': 'KEYCODE_ENTER', ' ': 'KEYCODE_SPACE', '\x08': 'KEYCODE_DEL', } def on_key(event): c = event.char # print 'key pressed', repr(c), type(c) if c in 'adbcdefghijklmnopqrstuvwxyz0123456789': adb.input(c) return 'break' if c in keymap: adb.keyevent(keymap[c]) return 'break' self.canvas.bind('<Key>', on_key)
def make_canvas(self): # screen canvas, bind mouse input & keyboard input self.canvas = tk.Canvas(self.root, bg='black', bd=0, highlightthickness=0) self.canvas.pack() def _screen2touch(w, h, x, y): '''convert touch position''' ori = adb.orientation() if ori == 0: return x, y elif ori == 1: # landscape-right return w-y, x elif ori == 2: # upsidedown return w-x, h-y elif ori == 3: # landscape-left return h-x, y return x, y w, h = adb.display() screen2touch = partial(_screen2touch, w, h) def on_mouse_down(event): self.canvas.focus_set() x, y = int(event.x/scale), int(event.y/scale) x, y = screen2touch(x, y) adb._mini.touchqueue.put('d 0 %d %d 30\nc\n' % (x, y)) def on_mouse_up(event): adb._mini.touchqueue.put('u 0\nc\n') def on_mouse_drag(event): x, y = int(event.x/scale), int(event.y/scale) x, y = screen2touch(x, y) adb._mini.touchqueue.put('m 0 %d %d 30\nc\n' % (x, y)) self.canvas.bind('<ButtonPress-1>', on_mouse_down) self.canvas.bind('<ButtonRelease-1>', on_mouse_up) self.canvas.bind('<B1-Motion>', on_mouse_drag) keymap = {'\r':'KEYCODE_ENTER', ' ':'KEYCODE_SPACE', '\x08':'KEYCODE_DEL', } def on_key(event): c = event.char # print 'key pressed', repr(c), type(c) if c in 'adbcdefghijklmnopqrstuvwxyz0123456789': adb.input(c) return 'break' if c in keymap: adb.keyevent(keymap[c]) return 'break' self.canvas.bind('<Key>', on_key)
def main(output='out.avi', scale=0.5, portrait=False, overwrite=True, verbose=True): if os.path.exists(output): print 'output file exists!' if overwrite: print 'overwriting', output os.remove(output) else: return adb.use_openstf() img = adb.screenshot() while img is None: time.sleep(1) img = adb.screenshot() if verbose: cv2.imshow('screen', img) w, h = adb.display() w, h = int(w * scale), int(h * scale) framesize = (w, h) if portrait else (h, w) fps = 24.0 # refs http://www.fourcc.org/codecs.php # avaiable fourccs: XVID, MJPG fourcc = cv2.cv.FOURCC(*'MJPG') writer = cv2.VideoWriter(output, fourcc, fps, framesize) # video (width, height), images should be resized to fit in video frame. vw, vh = framesize tic = time.clock() toc = time.clock() while True: try: time.sleep(1.0 / fps - max(toc - tic, 0)) tic = time.clock() img = adb.screenshot() h, w = img.shape[:2] if h * vw == w * vh: h, w = vh, vw frame = cv2.resize(img, dsize=(w, h)) else: frame = np.zeros((vh, vw, 3), dtype=np.uint8) sh = vh * 1.0 / h sw = vw * 1.0 / w if sh < sw: h, w = vh, int(sh * w) else: h, w = int(sw * h), vw left, top = (vw - w) / 2, (vh - h) / 2 frame[top:top + h, left:left + w, :] = cv2.resize(img, dsize=(w, h)) writer.write(frame) toc = time.clock() if verbose: cv2.imshow('screen', frame) cv2.waitKey(1) except KeyboardInterrupt: print 'Done' break except: traceback.print_exc() break writer.release()
def main(output="out.avi", scale=0.5, portrait=False, overwrite=True, verbose=True): if os.path.exists(output): print "output file exists!" if overwrite: print "overwriting", output os.remove(output) else: return adb.use_openstf() img = adb.screenshot(format="cv2") while img is None: time.sleep(1) img = adb.screenshot(format="cv2") if verbose: cv2.imshow("screen", img) w, h = adb.display() w, h = int(w * scale), int(h * scale) framesize = (w, h) if portrait else (h, w) fps = 24.0 # refs http://www.fourcc.org/codecs.php # avaiable fourccs: XVID, MJPG fourcc = cv2.cv.FOURCC(*"MJPG") writer = cv2.VideoWriter(output, fourcc, fps, framesize) # video (width, height), images should be resized to fit in video frame. vw, vh = framesize tic = time.clock() toc = time.clock() while True: try: time.sleep(1.0 / fps - max(toc - tic, 0)) tic = time.clock() img = adb.screenshot(format="cv2") h, w = img.shape[:2] if h * vw == w * vh: h, w = vh, vw frame = cv2.resize(img, dsize=(w, h)) else: frame = np.zeros((vh, vw, 3), dtype=np.uint8) sh = vh * 1.0 / h sw = vw * 1.0 / w if sh < sw: h, w = vh, int(sh * w) else: h, w = int(sw * h), vw left, top = (vw - w) / 2, (vh - h) / 2 frame[top : top + h, left : left + w, :] = cv2.resize(img, dsize=(w, h)) writer.write(frame) toc = time.clock() if verbose: cv2.imshow("screen", frame) cv2.waitKey(1) except KeyboardInterrupt: print "Done" break except: traceback.print_exc() break writer.release()