Пример #1
0
        /// <summary>
        /// Gets the frame buffer from the specified end point.
        /// </summary>
        /// <param name="adbSockAddr">The adb sock addr.</param>
        /// <param name="device">The device.</param>
        /// <returns>Returns the RawImage.</returns>
        /// <exception cref="Managed.Adb.Exceptions.AdbException">
        /// failed asking for frame buffer
        /// or
        /// failed nudging
        /// </exception>
        public RawImage GetFrameBuffer(IPEndPoint adbSockAddr, IDevice device)
        {
            RawImage imageParams = new RawImage();
            byte[] request = FormAdbRequest("framebuffer:"); //$NON-NLS-1$
            byte[] nudge = {
                        0
                };
            byte[] reply;

            Socket adbChan = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try {
                adbChan.Connect(adbSockAddr);
                adbChan.Blocking = true;

                // if the device is not -1, then we first tell adb we're looking to talk
                // to a specific device
                SetDevice(adbChan, device);
                if(!Write(adbChan, request))
                    throw new AdbException("failed asking for frame buffer");

                AdbResponse resp = ReadAdbResponse(adbChan, false /* readDiagString */);
                if(!resp.IOSuccess || !resp.Okay) {
                    Log.w(TAG, "Got timeout or unhappy response from ADB fb req: " + resp.Message);
                    adbChan.Close();
                    return null;
                }

                // first the protocol version.
                reply = new byte[4];
                if(!Read(adbChan, reply)) {
                    Log.w(TAG, "got partial reply from ADB fb:");

                    adbChan.Close();
                    return null;
                }
                BinaryReader buf;
                int version = 0;
                using(MemoryStream ms = new MemoryStream(reply)) {
                    buf = new BinaryReader(ms);
                    version = buf.ReadInt16();
                }

                // get the header size (this is a count of int)
                int headerSize = RawImage.GetHeaderSize(version);
                // read the header
                reply = new byte[headerSize * 4];
                if(!Read(adbChan, reply)) {
                    Log.w(TAG, "got partial reply from ADB fb:");

                    adbChan.Close();
                    return null;
                }

                using(MemoryStream ms = new MemoryStream(reply)) {
                    buf = new BinaryReader(ms);

                    // fill the RawImage with the header
                    if(imageParams.ReadHeader(version, buf) == false) {
                        Log.w(TAG, "Unsupported protocol: " + version);
                        return null;
                    }
                }

                Log.d(TAG, "image params: bpp=" + imageParams.Bpp + ", size="
                                + imageParams.Size + ", width=" + imageParams.Width
                                + ", height=" + imageParams.Height);

                if(!Write(adbChan, nudge))
                    throw new AdbException("failed nudging");

                reply = new byte[imageParams.Size];
                if(!Read(adbChan, reply)) {
                    Log.w(TAG, "got truncated reply from ADB fb data");
                    adbChan.Close();
                    return null;
                }

                imageParams.Data = reply;
            } finally {
                if(adbChan != null) {
                    adbChan.Close();
                }
            }

            return imageParams;
        }
Пример #2
0
        /**
         * Returns a rotated version of the image
         * The image is rotated counter-clockwise.
         */
        public RawImage GetRotated()
        {
            RawImage rotated = new RawImage();
            rotated.Version = this.Version;
            rotated.Bpp = this.Bpp;
            rotated.Size = this.Size;
            rotated.Red.Offset = this.Red.Offset;
            rotated.Red.Length = this.Red.Length;
            rotated.Green.Offset = this.Green.Offset;
            rotated.Green.Length = this.Green.Length;
            rotated.Blue.Offset = this.Blue.Offset;
            rotated.Blue.Length = this.Blue.Length;
            rotated.Alpha.Offset = this.Alpha.Offset;
            rotated.Alpha.Length = this.Alpha.Length;

            rotated.Width = this.Height;
            rotated.Height = this.Width;

            int count = this.Data.Length;
            rotated.Data = new byte[count];

            int byteCount = this.Bpp >> 3; // bpp is in bits, we want bytes to match our array
            int w = this.Width;
            int h = this.Height;
            for (int y = 0; y < h; y++)
            {
                for (int x = 0; x < w; x++)
                {
                    Array.Copy(
                        this.Data, ((y * w) + x) * byteCount,
                        rotated.Data,
                        (((w - x - 1) * h) + y) * byteCount,
                        byteCount);
                }
            }

            return rotated;
        }
        /// <summary>
        /// Gets the frame buffer from the specified end point.
        /// </summary>
        /// <param name="adbSockAddr">The adb sock addr.</param>
        /// <param name="device">The device.</param>
        /// <returns>Returns the RawImage.</returns>
        /// <exception cref="Managed.Adb.Exceptions.AdbException">
        /// failed asking for frame buffer
        /// or
        /// failed nudging
        /// </exception>
        public RawImage GetFrameBuffer(IPEndPoint adbSockAddr, IDevice device)
        {
            RawImage imageParams = new RawImage();

            byte[] request = FormAdbRequest("framebuffer:");             //$NON-NLS-1$
            byte[] nudge   =
            {
                0
            };
            byte[] reply;

            Socket adbChan = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try {
                adbChan.Connect(adbSockAddr);
                adbChan.Blocking = true;

                // if the device is not -1, then we first tell adb we're looking to talk
                // to a specific device
                SetDevice(adbChan, device);
                if (!Write(adbChan, request))
                {
                    throw new AdbException("failed asking for frame buffer");
                }

                AdbResponse resp = ReadAdbResponse(adbChan, false /* readDiagString */);
                if (!resp.IOSuccess || !resp.Okay)
                {
                    Log.w(TAG, "Got timeout or unhappy response from ADB fb req: " + resp.Message);
                    adbChan.Close();
                    return(null);
                }

                // first the protocol version.
                reply = new byte[4];
                if (!Read(adbChan, reply))
                {
                    Log.w(TAG, "got partial reply from ADB fb:");

                    adbChan.Close();
                    return(null);
                }
                BinaryReader buf;
                int          version = 0;
                using (MemoryStream ms = new MemoryStream(reply)) {
                    buf     = new BinaryReader(ms);
                    version = buf.ReadInt16();
                }

                // get the header size (this is a count of int)
                int headerSize = RawImage.GetHeaderSize(version);
                // read the header
                reply = new byte[headerSize * 4];
                if (!Read(adbChan, reply))
                {
                    Log.w(TAG, "got partial reply from ADB fb:");

                    adbChan.Close();
                    return(null);
                }

                using (MemoryStream ms = new MemoryStream(reply)) {
                    buf = new BinaryReader(ms);

                    // fill the RawImage with the header
                    if (imageParams.ReadHeader(version, buf) == false)
                    {
                        Log.w(TAG, "Unsupported protocol: " + version);
                        return(null);
                    }
                }

                Log.d(TAG, "image params: bpp=" + imageParams.Bpp + ", size="
                      + imageParams.Size + ", width=" + imageParams.Width
                      + ", height=" + imageParams.Height);

                if (!Write(adbChan, nudge))
                {
                    throw new AdbException("failed nudging");
                }

                reply = new byte[imageParams.Size];
                if (!Read(adbChan, reply))
                {
                    Log.w(TAG, "got truncated reply from ADB fb data");
                    adbChan.Close();
                    return(null);
                }

                imageParams.Data = reply;
            } finally {
                if (adbChan != null)
                {
                    adbChan.Close();
                }
            }

            return(imageParams);
        }