安卓调试工具 adb 返回的 png 截图,直接输出到控制台的修复问题

2018-01-04 17:04:01 +08:00
 Charltsing

adb 由于兼容性问题,会把 0a 替换成 0d0a 输出到控制台,这会造成 png 图片解析失败。

所以,对 adb shell screencap -p 命令直接返回的数据要进行修复。

需要注意的是,不同的手机系统返回的可能是 0d0d0a,也可能是 0d0a,替换的时候需要注意检查。

    private byte[] Fix0d0d0a(byte[] bytes)
    {
        long length = bytes.Length;
        byte[] bytesfix = new byte[length];

        int idx = 0;
        int count = 0;
        int idxFirst0D = 0;
        int idxFirst0A = 0;
        bool is0D = false;
        for (int i = 0; i < length; i++)
        {
            byte b = bytes[i];
            if (b == 0x0d && idxFirst0D == 0)
            {
                idxFirst0D = i;
                is0D = true;
            }
            if (b == 0x0a && idxFirst0A == 0)
            {
                idxFirst0A = i;
            }
            if (i > 2 && b == 0x0a && is0D)
            {
                count++;
                idx = idx - (idxFirst0A - idxFirst0D - 1);
                bytesfix[idx] = b;
                idx++;
            }
            else
            {
                bytesfix[idx] = b;
                idx++;
            }
            if (b == 0x0d)
                is0D = true;
            else
                is0D = false;
        }
        byte[] bytesfinal = new byte[length-count* (idxFirst0A - idxFirst0D-1)];
        Buffer.BlockCopy(bytesfix, 0, bytesfinal, 0, bytesfinal.Length);           
        return bytesfinal;
    }
4110 次点击
所在节点    Android
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/420093

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX