下面主要介绍这几种方法的实现:
 
一、存二进制流
复制代码
 1 using Microsoft.Kinect;
 2 using System.IO;
 3 class ColorStreamBinaryRecorder:ColorStreamRecorder
 4     {
 5         Stream recordstream;
 6         DateTime referenceTime;
 7         readonly BinaryWriter writer;
 8         private string _path;
 9 
10         internal ColorStreamBinaryRecorder(string path)
11         {
12             _path = Path.Combine(path, "Color.replay");
13             Stream recordStream = File.Create(_path);
14             writer = new BinaryWriter(recordStream);
15             referenceTime = DateTime.Now;
16         }
17 
18         public override  void Record(ColorImageFrame frame)
19         {
20             /*
21             // Data
22             TimeSpan timeSpan = DateTime.Now.Subtract(referenceTime);
23             referenceTime = DateTime.Now;
24             writer.Write((long)timeSpan.TotalMilliseconds);
25             writer.Write(frame.BytesPerPixel);
26             writer.Write((int)frame.Format);
27              * */
28             writer.Write(frame.Width);
29             writer.Write(frame.Height);
30             /*
31             writer.Write(frame.FrameNumber);
32              */
33             // Bytes
34             writer.Write(frame.PixelDataLength);
35             byte[] bytes = new byte[frame.PixelDataLength];
36             frame.CopyPixelDataTo(bytes);
37             writer.Write(bytes);
38         }
39 
40         public override void Stop()
41         {
42             if (writer != null)
43             {
44                 writer.Close();
45                 writer.Dispose();
46             }
47             if (recordstream != null)
48             {
49                 recordstream.Dispose();
50                 recordstream = null;
51             }
52         }
53     }
复制代码

其实这个参考Kinect.toolbox即可,但是这里有一个问题就是存的数据会很大,所以不是很推荐。

二、存图片:

复制代码
 1 class ColorStreamPJPGRecorder : ColorStreamRecorder
 2     {
 3         private string _path;
 4 
 5         internal ColorStreamPJPGRecorder(string path)
 6         {
 7             _path = Path.Combine(path, "Color");
 8             if (!Directory.Exists(_path))
 9             {
10                 Directory.CreateDirectory(_path);
11             }
12         }
13 
14         public override void Record(ColorImageFrame frame)
15         {
16             if (frame == null)
17             {
18                 return;
19             }
20 
21             byte[] pixelBuffer = new byte[frame.PixelDataLength];
22             frame.CopyPixelDataTo(pixelBuffer);
23 
24             Bitmap bitmapFrame = new Bitmap(frame.Width, frame.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
25             BitmapData bitmapData = bitmapFrame.LockBits(new Rectangle(0, 0, frame.Width, frame.Height), ImageLockMode.WriteOnly, bitmapFrame.PixelFormat);
26 
27             IntPtr intPointer = bitmapData.Scan0;
28             Marshal.Copy(pixelBuffer, 0, intPointer, frame.PixelDataLength);
29 
30             bitmapFrame.UnlockBits(bitmapData);
31 
32             string pathtmp = Path.Combine(_path, GlobalVar.RecordFrameID + ".jpg");
33             bitmapFrame.Save(pathtmp, ImageFormat.Jpeg);
34         }
35         public override void Stop()
36         {
37 
38         }
复制代码

当时项目中发现一个问题就是在深度图和颜色图均存为png的情况下会出现丢失帧很严重的情况,所以这块颜色图要存为jpg格式。这个可以参考KInect depveloper tooklit

三、存放视频格式:

 1 class ColorStreamAVIRecorder:ColorStreamRecorder
 2 {
 3 private string _path;
 4 VideoWriter _writer;
 5 
 6 internal ColorStreamAVIRecorder(string path)
 7 {
 8 _path = Path.Combine(path, "Color.avi");
 9 /*CvInvoke.CV_FOURCC('P', 'I', 'M', '1'); //= MPEG-1 codec
10 CvInvoke.CV_FOURCC('M', 'J', 'P', 'G'); //= motion-jpeg codec (does not work well)
11 CvInvoke.CV_FOURCC('M', 'P', '4', '2');//= MPEG-4.2 codec
12 CvInvoke.CV_FOURCC('D', 'I', 'V', '3'); //= MPEG-4.3 codec
13 CvInvoke.CV_FOURCC('D', 'I', 'V', 'X'); //= MPEG-4 codec
14 CvInvoke.CV_FOURCC('U', '2', '6', '3'); //= H263 codec
15 CvInvoke.CV_FOURCC('I', '2', '6', '3'); //= H263I codec
16 CvInvoke.CV_FOURCC('F', 'L', 'V', '1'); //= FLV1 codec*/
17 _writer = new VideoWriter(_path, CvInvoke.CV_FOURCC('U', '2', '6', '3'),30, 640, 480, true);
18 }
19 
20 public override void Record(ColorImageFrame frame)
21 {
22 _writer.WriteFrame<Rgb, Byte>(frame.ToOpenCVImage<Rgb, Byte>());
23 }
24 
25 public override void Stop()
26 {
27 if(_writer != null)
28 {
29 _writer.Dispose();
30 }
31 }

需要用到emgucv,网上资料很多。但是注意版本问题。我的是64位,原理差不多,运行的时候记得复制opencv的一些dll到debug目录或者release目录下。要指定编码方式,不然数据没有压缩会很大

PS:

还有其他方法比如用aforge.net进行录制等等。还可以参考ispy开源工程。