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

原始出处:www.cnblogs.com/Charltsing/p/adbpngfix.html

QQ:564955427

 

adb由于兼容性问题,会把0a替换成0d0a输出到控制台,这会造成png图片解析失败。所以,对adb shell screencap -p命令直接返回的数据要进行修复。需要注意的是,不同的手机系统返回的可能是0d0d0a,也可能是0d0a,替换的时候需要注意检查。

C#代码如下:

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;
        }

  注意:如果是锤子手机,需要去掉前面的字符串。

有问题欢迎qq联系

 

posted @ 2018-01-03 23:18  Charltsing  阅读(1439)  评论(0编辑  收藏  举报