V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Charltsing
V2EX  ›  Android

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

  •  
  •   Charltsing · 2018-01-04 17:04:01 +08:00 · 3503 次点击
    这是一个创建于 2276 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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;
        }
    
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5293 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 09:13 · PVG 17:13 · LAX 02:13 · JFK 05:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.