コード例 #1
0
    def takeSnapshot(self, reconnect=False):
        '''
        Takes a snapshot of the device and return it as a PIL Image.
        '''

        if PROFILE:
            profileStart()

        global PIL_AVAILABLE
        if not PIL_AVAILABLE:
            try:
                global Image
                from PIL import Image
                PIL_AVAILABLE = True
            except:
                raise Exception(
                    "You have to install PIL to use takeSnapshot()")

        USE_ADB_FRAMEBUFFER_METHOD = False
        if USE_ADB_FRAMEBUFFER_METHOD:
            self.__checkTransport()

            self.__send('framebuffer:', checkok=True, reconnect=False)
            # case 1: // version
            #           return 12; // bpp, size, width, height, 4*(length, offset)
            received = self.__receive(1 * 4 + 12 * 4)
            (version, bpp, size, width, height, roffset, rlen, boffset, blen,
             goffset, glen, aoffset,
             alen) = struct.unpack('<' + 'L' * 13, received)
            if DEBUG:
                print >> sys.stderr, "    takeSnapshot:", (version, bpp, size,
                                                           width, height,
                                                           roffset, rlen,
                                                           boffset, blen,
                                                           goffset, glen,
                                                           aoffset, alen)
            offsets = {roffset: 'R', goffset: 'G', boffset: 'B'}
            if bpp == 32:
                if alen != 0:
                    offsets[aoffset] = 'A'
                else:
                    warnings.warn(
                        '''framebuffer is specified as 32bpp but alpha length is 0'''
                    )
            argMode = ''.join([offsets[o] for o in sorted(offsets)])
            if DEBUG:
                print >> sys.stderr, "    takeSnapshot:", (version, bpp, size,
                                                           width, height,
                                                           roffset, rlen,
                                                           boffset, blen,
                                                           goffset, blen,
                                                           aoffset, alen,
                                                           argMode)
            if argMode == 'BGRA':
                argMode = 'RGBA'
            if bpp == 16:
                mode = 'RGB'
                argMode += ';16'
            else:
                mode = argMode
            self.__send('\0', checkok=False, reconnect=False)
            if DEBUG:
                print >> sys.stderr, "    takeSnapshot: reading %d bytes" % (
                    size)
            received = self.__receive(size)
            if reconnect:
                self.socket = AdbClient.connect(self.hostname, self.port,
                                                self.timeout)
                self.__setTransport()
            if DEBUG:
                print >> sys.stderr, "    takeSnapshot: Image.frombuffer(%s, %s, %s, %s, %s, %s, %s)" % (
                    mode, (width, height), 'data', 'raw', argMode, 0, 1)
            image = Image.frombuffer(mode, (width, height), received, 'raw',
                                     argMode, 0, 1)
        else:
            # ALTERNATIVE_METHOD: screencap
            received = self.shell('/system/bin/screencap -p').replace(
                "\r\n", "\n")
            stream = StringIO.StringIO(received)
            image = Image.open(stream)

        # Just in case let's get the real image size
        (w, h) = image.size
        if w == self.display['height'] and h == self.display['width']:
            # FIXME: We are not catching the 180 degrees rotation here
            if 'orientation' in self.display:
                r = (0, 90, 180, -90)[self.display['orientation']]
            else:
                r = 90
            image = image.rotate(r)

        if PROFILE:
            profileEnd()
        return image
コード例 #2
0
    def takeSnapshot(self, reconnect=False):
        '''
        Takes a snapshot of the device and return it as a PIL Image.
        '''

        if PROFILE:
            profileStart()

        global PIL_AVAILABLE
        if not PIL_AVAILABLE:
            try:
                global Image
                from PIL import Image
                PIL_AVAILABLE = True
            except:
                raise Exception("You have to install PIL to use takeSnapshot()")

        USE_ADB_FRAMEBUFFER_METHOD = False
        if USE_ADB_FRAMEBUFFER_METHOD:
            self.__checkTransport()

            self.__send('framebuffer:', checkok=True, reconnect=False)
            # case 1: // version
            #           return 12; // bpp, size, width, height, 4*(length, offset)
            received = self.__receive(1 * 4 + 12 * 4)
            (version, bpp, size, width, height, roffset, rlen, boffset, blen, goffset, glen, aoffset,
             alen) = struct.unpack(
                '<' + 'L' * 13, received)
            if DEBUG:
                print >> sys.stderr, "    takeSnapshot:", (
                    version, bpp, size, width, height, roffset, rlen, boffset, blen, goffset, glen, aoffset, alen)
            offsets = {roffset: 'R', goffset: 'G', boffset: 'B'}
            if bpp == 32:
                if alen != 0:
                    offsets[aoffset] = 'A'
                else:
                    warnings.warn('''framebuffer is specified as 32bpp but alpha length is 0''')
            argMode = ''.join([offsets[o] for o in sorted(offsets)])
            if DEBUG:
                print >> sys.stderr, "    takeSnapshot:", (
                    version, bpp, size, width, height, roffset, rlen, boffset, blen, goffset, blen, aoffset, alen,
                    argMode)
            if argMode == 'BGRA':
                argMode = 'RGBA'
            if bpp == 16:
                mode = 'RGB'
                argMode += ';16'
            else:
                mode = argMode
            self.__send('\0', checkok=False, reconnect=False)
            if DEBUG:
                print >> sys.stderr, "    takeSnapshot: reading %d bytes" % (size)
            received = self.__receive(size)
            if reconnect:
                self.socket = AdbClient.connect(self.hostname, self.port, self.timeout)
                self.__setTransport()
            if DEBUG:
                print >> sys.stderr, "    takeSnapshot: Image.frombuffer(%s, %s, %s, %s, %s, %s, %s)" % (
                    mode, (width, height), 'data', 'raw', argMode, 0, 1)
            image = Image.frombuffer(mode, (width, height), received, 'raw', argMode, 0, 1)
        else:
            # ALTERNATIVE_METHOD: screencap
            received = self.shell('/system/bin/screencap -p').replace("\r\n", "\n")
            stream = StringIO.StringIO(received)
            image = Image.open(stream)

        # Just in case let's get the real image size
        (w, h) = image.size
        if w == self.display['height'] and h == self.display['width']:
            # FIXME: We are not catching the 180 degrees rotation here
            if 'orientation' in self.display:
                r = (0, 90, 180, -90)[self.display['orientation']]
            else:
                r = 90
            image = image.rotate(r)

        if PROFILE:
            profileEnd()
        return image
コード例 #3
0
                print >> sys.stderr, ex
                print repr(stream)
                sys.exit(1)

        # Just in case let's get the real image size
        (w, h) = image.size
        if w == self.display['height'] and h == self.display['width']:
            # FIXME: We are not catching the 180 degrees rotation here
            if 'orientation' in self.display:
                r = (0, 90, 180, -90)[self.display['orientation']]
            else:
                r = 90
            image = image.rotate(r)

        if PROFILE:
            profileEnd()
        return image

    def __transformPointByOrientation(self, (x, y), orientationOrig, orientationDest):
        if orientationOrig != orientationDest:
            if orientationDest == 1:
                _x = x
                x = self.display['width'] - y
                y = _x
            elif orientationDest == 3:
                _x = x
                x = y
                y = self.display['height'] - _x
        return (x, y)

    def touch(self, x, y, orientation=-1, eventType=DOWN_AND_UP):
コード例 #4
0
                print >> sys.stderr, repr(stream)
                print >> sys.stderr, repr(received)
                raise RuntimeError('Cannot convert stream to image: ' + ex)

        # Just in case let's get the real image size
        (w, h) = image.size
        if w == self.display['height'] and h == self.display['width']:
            # FIXME: We are not catching the 180 degrees rotation here
            if 'orientation' in self.display:
                r = (0, 90, 180, -90)[self.display['orientation']]
            else:
                r = 90
            image = image.rotate(r, expand=1).resize((h, w))

        if PROFILE:
            profileEnd()
        return image

    def __transformPointByOrientation(self, (x, y), orientationOrig,
                                      orientationDest):
        if orientationOrig != orientationDest:
            if orientationDest == 1:
                _x = x
                x = self.display['width'] - y
                y = _x
            elif orientationDest == 3:
                _x = x
                x = y
                y = self.display['height'] - _x
        return (x, y)
コード例 #5
0
ファイル: adbclient.py プロジェクト: ll2088/AndroidViewClient
    def takeSnapshot(self, reconnect=False):
        """
        Takes a snapshot of the device and return it as a PIL Image.
        """

        if PROFILE:
            profileStart()

        global PIL_AVAILABLE
        if not PIL_AVAILABLE:
            try:
                global Image
                from PIL import Image

                PIL_AVAILABLE = True
            except:
                raise Exception("You have to install PIL to use takeSnapshot()")

        USE_ADB_FRAMEBUFFER_METHOD = False
        if USE_ADB_FRAMEBUFFER_METHOD:
            self.__checkTransport()

            self.__send("framebuffer:", checkok=True, reconnect=False)
            # case 1: // version
            #           return 12; // bpp, size, width, height, 4*(length, offset)
            received = self.__receive(1 * 4 + 12 * 4)
            (
                version,
                bpp,
                size,
                width,
                height,
                roffset,
                rlen,
                boffset,
                blen,
                goffset,
                glen,
                aoffset,
                alen,
            ) = struct.unpack("<" + "L" * 13, received)
            if DEBUG:
                print >> sys.stderr, "    takeSnapshot:", (
                    version,
                    bpp,
                    size,
                    width,
                    height,
                    roffset,
                    rlen,
                    boffset,
                    blen,
                    goffset,
                    glen,
                    aoffset,
                    alen,
                )
            offsets = {roffset: "R", goffset: "G", boffset: "B"}
            if bpp == 32:
                if alen != 0:
                    offsets[aoffset] = "A"
                else:
                    warnings.warn("""framebuffer is specified as 32bpp but alpha length is 0""")
            argMode = "".join([offsets[o] for o in sorted(offsets)])
            if DEBUG:
                print >> sys.stderr, "    takeSnapshot:", (
                    version,
                    bpp,
                    size,
                    width,
                    height,
                    roffset,
                    rlen,
                    boffset,
                    blen,
                    goffset,
                    blen,
                    aoffset,
                    alen,
                    argMode,
                )
            if argMode == "BGRA":
                argMode = "RGBA"
            if bpp == 16:
                mode = "RGB"
                argMode += ";16"
            else:
                mode = argMode
            self.__send("\0", checkok=False, reconnect=False)
            if DEBUG:
                print >> sys.stderr, "    takeSnapshot: reading %d bytes" % (size)
            received = self.__receive(size)
            if reconnect:
                self.socket = AdbClient.connect(self.hostname, self.port, self.timeout)
                self.__setTransport()
            if DEBUG:
                print >> sys.stderr, "    takeSnapshot: Image.frombuffer(%s, %s, %s, %s, %s, %s, %s)" % (
                    mode,
                    (width, height),
                    "data",
                    "raw",
                    argMode,
                    0,
                    1,
                )
            image = Image.frombuffer(mode, (width, height), received, "raw", argMode, 0, 1)
        else:
            # ALTERNATIVE_METHOD: screencap
            received = self.shell("/system/bin/screencap -p").replace("\r\n", "\n")
            stream = StringIO.StringIO(received)
            image = Image.open(stream)

        # Just in case let's get the real image size
        (w, h) = image.size
        if w == self.display["height"] and h == self.display["width"]:
            # FIXME: We are not catching the 180 degrees rotation here
            if "orientation" in self.display:
                r = (0, 90, 180, -90)[self.display["orientation"]]
            else:
                r = 90
            image = image.rotate(r)

        if PROFILE:
            profileEnd()
        return image