public MainWindow() { InitializeComponent(); this.timer = new Timer(); this.timer.Interval = 40; this.timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); this.d3dSource = new D3DImageSource(); this.wbSource = new WriteableBitmapSource(); this.frameIndex = 0; try { this.yuvData = FrameData.LoadData("yv12.dat"); if (this.wbSource.SetupSurface(this.yuvData.FrameWidth, this.yuvData.FrameHeight, FrameFormat.YV12)) { this.imageWB.Source = this.wbSource.ImageSource; } else { MessageBox.Show("WriteableBitmapSource不支持该种帧格式:" + FrameFormat.YV12); } if (this.d3dSource.SetupSurface(this.yuvData.FrameWidth, this.yuvData.FrameHeight, FrameFormat.YV12)) { this.imageD3D.Source = this.d3dSource.ImageSource; } else { MessageBox.Show("本机显卡不支持该种帧格式:" + FrameFormat.YV12); } } catch { MessageBox.Show("加载数据文件失败"); } }
/// <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); } } } } }
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; } }