Exemplo n.º 1
0
        /// <summary>
        /// 保存VideoData数据. 格式为Frame头+FrameData。
        /// 
        /// 其中Frame头固定为64个int。
        /// 格式为:[VideoType][Frames][FrameSize][FrameWidth][FrameHeight][VideoBufferSize][保留字节]
        /// </summary>
        /// <param name="path"></param>
        public static void SaveData(string filePath, FrameData videoData)
        {
            if (videoData != null)
            {
                using (FileStream fs = new FileStream(filePath, FileMode.Create))
                {
                    using (BinaryWriter writer = new BinaryWriter(fs))
                    {
                        writer.Write(videoData.Frames);         // number of frames
                        writer.Write(videoData.FrameWidth);     // frame width
                        writer.Write(videoData.FrameHeight);    // frame height
                        writer.Write((int)videoData.Type);      // video type
                        writer.Write(videoData.FrameSize);      // frame size
                        for (int i = 0; i < 59; i++)
                        {
                            writer.Write((int)0);
                        }

                        if (videoData.FrameBuffer != null)
                        {
                            // 由于video buffer的size是重用的,因此buffer的size可能会大于实际数据的大小
                            // 这里使用实际的数据大小来进行保存
                            int dataSize = videoData.FrameSize * videoData.Frames;
                            writer.Write(videoData.FrameBuffer, 0, dataSize);
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        public static FrameData LoadData(string filePath)
        {
            try
            {
                using (FileStream fs = new FileStream(filePath, FileMode.Open))
                {
                    using (BinaryReader reader = new BinaryReader(fs))
                    {
                        FrameData videoData = new FrameData();

                        videoData.Frames = reader.ReadInt32();
                        videoData.FrameWidth = reader.ReadInt32();
                        videoData.FrameHeight = reader.ReadInt32();
                        videoData.Type = (VideoType)reader.ReadInt32();
                        videoData.FrameSize = reader.ReadInt32();
                        for (int i = 0; i < 59; i++)
                        {
                            reader.ReadInt32();
                        }

                        int bufferLength = videoData.Frames * videoData.FrameSize;
                        if (bufferLength > 0)
                        {
                            videoData.FrameBuffer = reader.ReadBytes(bufferLength);
                        }

                        return videoData;
                    }
                }
            }
            catch
            {
                return null;
            }
        }