替换PNG文件中的CRLF

时间:2015-02-12 20:13:13

标签: c# binary adb screenshot

我正在尝试用LF取代CRLF(参见原因@ Read binary stdout data from adb shell?

上面提到的线程的简短摘要是,基本上当通过Android调试桥从Android设备管道屏幕截图时,看起来数据流中的换行行结尾被替换为回车换行,因此我在管道的另一端收到一个损坏的文件。对其他人有用的是通过代码撤消替换,如下所示,但它似乎并不适用于我。

我的代码仍在吐出一个损坏的文件...任何想法为什么?

++我知道代码不是那么干净和高效,之后会修好,请保留与我的编码技能相关的评论,或者缺少这些评论。

由于

        static void Main(string[] args)
        {
            // adb shell screencap -p > <path>
            string path = @"<filepath>\screen.png";

            var fs = new FileStream(path, FileMode.Open);
            var fsw = new FileStream(path.Replace(".png", "_fixed.png"), FileMode.Create);

            var buffer = new byte[fs.Length];
            fs.Read(buffer, 0, buffer.Length);
            fs.Flush();
            fs.Close();

            var switched = Repair(buffer);
            fsw.Write(switched, 0, switched.Length);
            fsw.Flush();
            fsw.Close();

            Console.WriteLine(buffer.Length);
            Console.WriteLine(switched.Length);

            Console.Read();
        }

        static byte[] Repair(byte[] enc)
        {
            var bstr = new MemoryStream();
            for (int i = 0; i < enc.Length; i++)
            {
                if (enc.Length > i + 1 && enc[i] == 0x0d && enc[i + 1] == 0x0a)
                {
                    bstr.WriteByte(0x0a);
                    i++;
                }
                else bstr.WriteByte(enc[i]);
            }

            bstr.Flush();
            bstr.Close();

            return bstr.ToArray();
        }

0 个答案:

没有答案