.Net, XNA, Kinect中各种图的转换

来源:互联网 发布:网络语废鱼是什么意思 编辑:程序博客网 时间:2024/05/20 20:48

几个概念:


Texture2D ——XNA的2D贴图


Bitmap ——位图,不解释


Color类,顾名思义


RGBA

R == red, G == green, B == blue, A == α 控制图像透明度的,0代表透明


各种信息的转换


Color[]-Bitmap

本质就是下面两句话,我是用于Winform下的编程

                    Color imagColor = Color.FromArgb(alpha, red, green, blue); // alpha, red, green, blue 为int
                    bkImage.SetPixel(x, y, imagColor); // bkImage 为 Bitmap 


ColorFrame-Bitmap-byte[]


用于Kinect与Winform结合的编程

ColorFrame是Kinect彩色摄像头返回的一帧的图像格式,在使用的时候都要利用CopyPixelDataTo()方法,将信息存入byte[],也就是说这里其实是讲的如何做到byte[]与Bitmap的转换,我发现这种问题国内真心比不过国外的程序员,下面的程序也是我从国外的网上摘来改的,原来这个牛人是写成了Bitmap BytesToBitmap(byte[] bytes)的函数了,不过精髓没变,后来我觉得我还是太挫了,应该自己就可以写出来的,但是这个牛人用的变量太漂亮了,效率应该比较高

                    // 将像素信息赋值到位图,colorBitmap就是最后要得到的位图变量,colorPixels就是byte[]


                    BitmapData bmpData = new BitmapData();


                    bmpData = colorBitmap.LockBits(
                         new Rectangle(0, 0, colorBitmap.Width, colorBitmap.Height),
                         ImageLockMode.WriteOnly,
                         colorBitmap.PixelFormat);
                    
                    IntPtr ptr = bmpData.Scan0; // bmp内存的首地址


                    Marshal.Copy(colorPixels, 0, ptr,
                        colorPixels.Length);


                    this.colorBitmap.UnlockBits(bmpData);


至于Bitmap怎么到byte[]跟你的Format有关,也就是你要知道每个像素点的RBGA值的位宽


ushort[]-Texture2D

用于XNA

为何用ushort作为Texture2D的填充数据是有原因的,因为用什么样的数据类型填充是根据Texture2D初始化的SurfaceFormat而定,我试了试,排除了些我一看就觉得不靠谱的还有尝试没效果的,就发现Rgba4444这个可以用,后来发现Kinect的某个Demo也用的Rgba4444,也就是说RGBA每个信息占四位,一共16位,也就是Int16或者说是ushort,unsigned short,记住一定是unsigned,因为没有负数位,这时候你就发现编程的妙处与艺术了,细节的处理犹如走钢丝一般,Int16显然不具备非负特性,也就是short不具备,所以不能用Int16。

下面上代码:shadowTexture2D 是 被填充的Texture2D,shadowColor是ushort数组

                         this.shadowTexture2D = new Texture2D(Game.GraphicsDevice,
                            this.Width, this.Height,
                            true, SurfaceFormat.Bgra4444); // Initialize Texture2D


                        this.shadowTexture2D.SetData(shadowColor, 0, shadowColor.Length); 


其中ushort数组的赋值可以参考下面的方法

具体哪个是RGBA。。。你们自己试试吧,我也忘了= =|||因为最后我没用这个点作为项目的一部分

this.shadowColor[depthIndex] = (0 << 12) + (0 << 8) + (0 << 4) + 0;



Tips:
int16=short;int32=int;int64=long;


Color[]-Texture2D ——此思路不可行


http://msdn.microsoft.com/en-us/library/bb198834.aspx Setdata不可用 Error:The type you are using for T in this method is an invalid size for this resource.

原创粉丝点击