原始出處: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聯系
