public static bool PollColor(IntPtr colorStreamHandle, ref byte[] videoBuffer)
    {
        IntPtr imageFramePtr = IntPtr.Zero;
        bool   newColor      = false;

        int hr = KinectV1Wrapper.NuiImageStreamGetNextFrame(colorStreamHandle, 0, ref imageFramePtr);

        if (hr == 0)
        {
            newColor = true;

            NuiImageFrame    imageFrame   = (NuiImageFrame)Marshal.PtrToStructure(imageFramePtr, typeof(NuiImageFrame));
            INuiFrameTexture frameTexture = (INuiFrameTexture)Marshal.GetObjectForIUnknown(imageFrame.pFrameTexture);

            NuiLockedRect lockedRectPtr = new NuiLockedRect();
            IntPtr        r             = IntPtr.Zero;

            frameTexture.LockRect(0, ref lockedRectPtr, r, 0);
            Marshal.Copy(lockedRectPtr.pBits, videoBuffer, 0, videoBuffer.Length);
            frameTexture.UnlockRect(0);
            hr = KinectV1Wrapper.NuiImageStreamReleaseFrame(colorStreamHandle, imageFramePtr);
        }
        else
        {
            Debug.Log("color frame not ready " + hr.ToString());
        }

        return(newColor);
    }
Example #2
0
    /// <summary>
    ///The first time in each frame that it is called, poll the kinect for updated color data and return
    ///true if there is new data. Subsequent calls do nothing and return the same value.
    /// </summary>
    /// <returns>
    /// A <see cref="System.Boolean"/> : is there new data this frame
    /// </returns>
    bool KinectInterface.pollColor()
    {
        if (!updatedColor)
        {
            updatedColor = true;
            IntPtr imageFramePtr = IntPtr.Zero;

            int hr = NativeMethods.NuiImageStreamGetNextFrame(colorStreamHandle, 100, ref imageFramePtr);
            if (hr == 0)
            {
                newColor = true;
                NuiImageFrame imageFrame = (NuiImageFrame)Marshal.PtrToStructure(imageFramePtr, typeof(NuiImageFrame));

                INuiFrameTexture frameTexture = (INuiFrameTexture)Marshal.GetObjectForIUnknown(imageFrame.pFrameTexture);

                NuiLockedRect lockedRectPtr = new NuiLockedRect();
                IntPtr        r             = IntPtr.Zero;

                frameTexture.LockRect(0, ref lockedRectPtr, r, 0);
                colorImage = extractColorImage(lockedRectPtr);

                hr = NativeMethods.NuiImageStreamReleaseFrame(colorStreamHandle, imageFramePtr);
            }
        }
        return(newColor);
    }
Example #3
0
    /// <summary>
    ///The first time in each frame that it is called, poll the kinect for updated depth (and player) data and return
    ///true if there is new data. Subsequent calls do nothing and return the same value.
    /// </summary>
    /// <returns>
    /// A <see cref="System.Boolean"/> : is there new data this frame
    /// </returns>
    bool KinectInterface.pollDepth()
    {
        if (!updatedDepth)
        {
            updatedDepth = true;
            IntPtr imageFramePtr = IntPtr.Zero;

            /* KK Addition*/
            /// <summary>
            /// Sets near mode - move this into the Awake () function
            /// if you do not need to constantly change between near and far mode
            /// current organization is this way to allow for rapid changes IF they're required
            /// and to allow for experimentation with the 2 modes
            /// </summary>
            if (enableNearMode)
            {
                NativeMethods.NuiImageStreamSetImageFrameFlags
                    (depthStreamHandle, NuiImageStreamFlags.EnableNearMode);
                //test = NativeMethods.NuiImageStreamSetImageFrameFlags
                //						(depthStreamHandle, NuiImageStreamFlags.TooFarIsNonZero);
            }

            else
            {
                NativeMethods.NuiImageStreamSetImageFrameFlags
                    (depthStreamHandle, NuiImageStreamFlags.None);
            }

            int hr = NativeMethods.NuiImageStreamGetNextFrame(depthStreamHandle, 100, ref imageFramePtr);
            if (hr == 0)
            {
                newDepth = true;
                NuiImageFrame imageFrame;
                imageFrame = (NuiImageFrame)Marshal.PtrToStructure(imageFramePtr, typeof(NuiImageFrame));

                INuiFrameTexture frameTexture = (INuiFrameTexture)Marshal.GetObjectForIUnknown(imageFrame.pFrameTexture);

                NuiLockedRect lockedRectPtr = new NuiLockedRect();
                IntPtr        r             = IntPtr.Zero;

                frameTexture.LockRect(0, ref lockedRectPtr, r, 0);
                depthPlayerData = extractDepthImage(lockedRectPtr);

                frameTexture.UnlockRect(0);
                hr = NativeMethods.NuiImageStreamReleaseFrame(depthStreamHandle, imageFramePtr);
            }
        }
        return(newDepth);
    }
Example #4
0
    private Color32[] extractColorImage(NuiLockedRect buf)
    {
        int totalPixels = 640 * 480;

        Color32[]   colorBuf = colorImage;
        ColorBuffer cb       = (ColorBuffer)Marshal.PtrToStructure(buf.pBits, typeof(ColorBuffer));

        for (int pix = 0; pix < totalPixels; pix++)
        {
            colorBuf[pix].r = cb.pixels[pix].r;
            colorBuf[pix].g = cb.pixels[pix].g;
            colorBuf[pix].b = cb.pixels[pix].b;
        }
        return(colorBuf);
    }
Example #5
0
    private static void ExtractColorImage(NuiLockedRect buf, ref Color32[] colorImage)
    {
        ColorBuffer cb          = (ColorBuffer)Marshal.PtrToStructure(buf.pBits, typeof(ColorBuffer));
        int         totalPixels = Constants.ImageWidth * Constants.ImageHeight;

        for (int pix = 0; pix < totalPixels; pix++)
        {
            int ind = totalPixels - pix - 1;

            colorImage[ind].r = cb.pixels[pix].r;
            colorImage[ind].g = cb.pixels[pix].g;
            colorImage[ind].b = cb.pixels[pix].b;
            colorImage[ind].a = 255;
        }
    }
Example #6
0
    //public static bool PollColor(IntPtr colorStreamHandle, ref byte[] videoBuffer, ref Color32[] colorImage)
    public static bool PollColor(IntPtr colorStreamHandle, ref Color32[] colorImage)
    {
        IntPtr imageFramePtr = IntPtr.Zero;
        bool   newColor      = false;

        int hr = KinectWrapper.NuiImageStreamGetNextFrame(colorStreamHandle, 0, ref imageFramePtr);

        if (hr == 0)
        {
            newColor = true;

            NuiImageFrame    imageFrame   = (NuiImageFrame)Marshal.PtrToStructure(imageFramePtr, typeof(NuiImageFrame));
            INuiFrameTexture frameTexture = (INuiFrameTexture)Marshal.GetObjectForIUnknown(imageFrame.pFrameTexture);

            NuiLockedRect lockedRectPtr = new NuiLockedRect();
            IntPtr        r             = IntPtr.Zero;

            frameTexture.LockRect(0, ref lockedRectPtr, r, 0);

            ColorBuffer cb          = (ColorBuffer)Marshal.PtrToStructure(lockedRectPtr.pBits, typeof(ColorBuffer));
            int         totalPixels = Constants.ColorImageWidth * Constants.ColorImageHeight;

            for (int pix = 0; pix < totalPixels; pix++)
            {
                int ind = pix;                 // totalPixels - pix - 1;
                colorImage[ind].r = cb.pixels[pix].r;
                colorImage[ind].g = cb.pixels[pix].g;
                colorImage[ind].b = cb.pixels[pix].b;
                colorImage[ind].a = 255;
//				videoBuffer[4 * ind] = cb.pixels[pix].b;
//				videoBuffer[4 * ind + 1] = cb.pixels[pix].g;
//				videoBuffer[4 * ind + 2] = cb.pixels[pix].r;
//				videoBuffer[4 * ind + 3] = 255;
            }

            frameTexture.UnlockRect(0);
            hr = KinectWrapper.NuiImageStreamReleaseFrame(colorStreamHandle, imageFramePtr);
        }

        return(newColor);
    }
    private Color32[] extractColorImage(NuiLockedRect buf)
    {
        int totalPixels = 640 * 480;

        Color32[]   colorBuf = colorImage;
        ColorBuffer cb       = (ColorBuffer)Marshal.PtrToStructure(buf.pBits, typeof(ColorBuffer));

        for (int pix = 0; pix < totalPixels; pix++)
        {
            int ind = totalPixels - pix - 1; // Waterstrong Add
            //colorBuf[pix].r = cb.pixels[pix].r;
            //colorBuf[pix].g = cb.pixels[pix].g;
            //colorBuf[pix].b = cb.pixels[pix].b;

            // Waterstrong Alter
            colorBuf[ind].r = cb.pixels[pix].r;
            colorBuf[ind].g = cb.pixels[pix].g;
            colorBuf[ind].b = cb.pixels[pix].b;
            colorBuf[ind].a = (byte)255; // Waterstrong Add
        }
        return(colorBuf);
    }
Example #8
0
    public static bool PollDepth(IntPtr depthStreamHandle, bool isNearMode, ref ushort[] depthPlayerData)
    {
        IntPtr imageFramePtr = IntPtr.Zero;
        bool   newDepth      = false;

        if (isNearMode)
        {
            KinectWrapper.NuiImageStreamSetImageFrameFlags(depthStreamHandle, NuiImageStreamFlags.EnableNearMode);
        }
        else
        {
            KinectWrapper.NuiImageStreamSetImageFrameFlags(depthStreamHandle, NuiImageStreamFlags.None);
        }

        int hr = KinectWrapper.NuiImageStreamGetNextFrame(depthStreamHandle, 0, ref imageFramePtr);

        if (hr == 0)
        {
            newDepth = true;

            NuiImageFrame    imageFrame   = (NuiImageFrame)Marshal.PtrToStructure(imageFramePtr, typeof(NuiImageFrame));
            INuiFrameTexture frameTexture = (INuiFrameTexture)Marshal.GetObjectForIUnknown(imageFrame.pFrameTexture);

            NuiLockedRect lockedRectPtr = new NuiLockedRect();
            IntPtr        r             = IntPtr.Zero;

            frameTexture.LockRect(0, ref lockedRectPtr, r, 0);

            DepthBuffer db = (DepthBuffer)Marshal.PtrToStructure(lockedRectPtr.pBits, typeof(DepthBuffer));
            depthPlayerData = db.pixels;

            frameTexture.UnlockRect(0);
            hr = KinectWrapper.NuiImageStreamReleaseFrame(depthStreamHandle, imageFramePtr);
        }

        return(newDepth);
    }
	public static bool PollDepth(IntPtr depthStreamHandle, bool isNearMode, ref ushort[] depthPlayerData)
	{
#if USE_KINECT_INTERACTION_OR_FACETRACKING
		uint depthBufLen = (uint)(depthPlayerData.Length << 1);

		var pDepthData = GCHandle.Alloc(depthPlayerData, GCHandleType.Pinned);
		bool newDepth = GetDepthFrameData(pDepthData.AddrOfPinnedObject(), ref depthBufLen, true);
		pDepthData.Free();
#else
		IntPtr imageFramePtr = IntPtr.Zero;
		bool newDepth = false;

		if (isNearMode)
		{
			KinectWrapper.NuiImageStreamSetImageFrameFlags(depthStreamHandle, NuiImageStreamFlags.EnableNearMode);
		}
		else
		{
			KinectWrapper.NuiImageStreamSetImageFrameFlags(depthStreamHandle, NuiImageStreamFlags.None);
		}
		
		int hr = KinectWrapper.NuiImageStreamGetNextFrame(depthStreamHandle, 0, ref imageFramePtr);
		if (hr == 0)
		{
			newDepth = true;
			
			NuiImageFrame imageFrame = (NuiImageFrame)Marshal.PtrToStructure(imageFramePtr, typeof(NuiImageFrame));
			INuiFrameTexture frameTexture = (INuiFrameTexture)Marshal.GetObjectForIUnknown(imageFrame.pFrameTexture);
			
			NuiLockedRect lockedRectPtr = new NuiLockedRect();
			IntPtr r = IntPtr.Zero;
			
			frameTexture.LockRect(0, ref lockedRectPtr,r,0);
			//depthPlayerData = ExtractDepthImage(lockedRectPtr);
			
			DepthBuffer db = (DepthBuffer)Marshal.PtrToStructure(lockedRectPtr.pBits, typeof(DepthBuffer));
			depthPlayerData = db.pixels;

			frameTexture.UnlockRect(0);
			hr = KinectWrapper.NuiImageStreamReleaseFrame(depthStreamHandle, imageFramePtr);
		}
#endif
		
		return newDepth;
	}
	public static bool PollColor(IntPtr colorStreamHandle, ref byte[] videoBuffer, ref Color32[] colorImage)
	{
#if USE_KINECT_INTERACTION_OR_FACETRACKING
		uint videoBufLen = (uint)videoBuffer.Length;

		var pColorData = GCHandle.Alloc(videoBuffer, GCHandleType.Pinned);
		bool newColor = GetColorFrameData(pColorData.AddrOfPinnedObject(), ref videoBufLen, true);
		pColorData.Free();

		if (newColor)
		{
			int totalPixels = colorImage.Length;
			
			for (int pix = 0; pix < totalPixels; pix++)
			{
				int ind = pix; // totalPixels - pix - 1;
				int src = pix << 2;
				
				colorImage[ind].r = videoBuffer[src + 2]; // pixels[pix].r;
				colorImage[ind].g = videoBuffer[src + 1]; // pixels[pix].g;
				colorImage[ind].b = videoBuffer[src]; // pixels[pix].b;
				colorImage[ind].a = 255;
			}
		}
#else
		IntPtr imageFramePtr = IntPtr.Zero;
		bool newColor = false;
	
		int hr = KinectWrapper.NuiImageStreamGetNextFrame(colorStreamHandle, 0, ref imageFramePtr);
		if (hr == 0)
		{
			newColor = true;
			
			NuiImageFrame imageFrame = (NuiImageFrame)Marshal.PtrToStructure(imageFramePtr, typeof(NuiImageFrame));
			INuiFrameTexture frameTexture = (INuiFrameTexture)Marshal.GetObjectForIUnknown(imageFrame.pFrameTexture);
			
			NuiLockedRect lockedRectPtr = new NuiLockedRect();
			IntPtr r = IntPtr.Zero;
			
			frameTexture.LockRect(0, ref lockedRectPtr, r, 0);
			//ExtractColorImage(lockedRectPtr, ref colorImage);
			
			ColorBuffer cb = (ColorBuffer)Marshal.PtrToStructure(lockedRectPtr.pBits, typeof(ColorBuffer));
			int totalPixels = Constants.ColorImageWidth * Constants.ColorImageHeight;
			
			for (int pix = 0; pix < totalPixels; pix++)
			{
				int ind = pix; // totalPixels - pix - 1;
				
				colorImage[ind].r = cb.pixels[pix].r;
				colorImage[ind].g = cb.pixels[pix].g;
				colorImage[ind].b = cb.pixels[pix].b;
				colorImage[ind].a = 255;
			}
			
			frameTexture.UnlockRect(0);
			hr = KinectWrapper.NuiImageStreamReleaseFrame(colorStreamHandle, imageFramePtr);
		}
#endif
		
		return newColor;
	}
Example #11
0
 private static short[] ExtractDepthImage(NuiLockedRect lockedRect)
 {
     DepthBuffer db = (DepthBuffer)Marshal.PtrToStructure(lockedRect.pBits, typeof(DepthBuffer));
     return db.pixels;
 }
Example #12
0
    private static void ExtractColorImage(NuiLockedRect buf, ref Color32[] colorImage)
    {
        ColorBuffer cb = (ColorBuffer)Marshal.PtrToStructure(buf.pBits, typeof(ColorBuffer));
        int totalPixels = Constants.ImageWidth * Constants.ImageHeight;

        for (int pix = 0; pix < totalPixels; pix++)
        {
            int ind = totalPixels - pix - 1;

            colorImage[ind].r = cb.pixels[pix].r;
            colorImage[ind].g = cb.pixels[pix].g;
            colorImage[ind].b = cb.pixels[pix].b;
            colorImage[ind].a = 255;
        }
    }
Example #13
0
    public static bool PollDepth(IntPtr depthStreamHandle, bool isNearMode, ref short[] depthPlayerData)
    {
        IntPtr imageFramePtr = IntPtr.Zero;
        bool newDepth = false;

        if (isNearMode)
        {
            KinectWrapper.NuiImageStreamSetImageFrameFlags(depthStreamHandle, NuiImageStreamFlags.EnableNearMode);
        }
        else
        {
            KinectWrapper.NuiImageStreamSetImageFrameFlags(depthStreamHandle, NuiImageStreamFlags.None);
        }

        int hr = KinectWrapper.NuiImageStreamGetNextFrame(depthStreamHandle, 0, ref imageFramePtr);
        if (hr == 0)
        {
            newDepth = true;

            NuiImageFrame imageFrame = (NuiImageFrame)Marshal.PtrToStructure(imageFramePtr, typeof(NuiImageFrame));
            INuiFrameTexture frameTexture = (INuiFrameTexture)Marshal.GetObjectForIUnknown(imageFrame.pFrameTexture);

            NuiLockedRect lockedRectPtr = new NuiLockedRect();
            IntPtr r = IntPtr.Zero;

            frameTexture.LockRect(0,ref lockedRectPtr,r,0);
            depthPlayerData = ExtractDepthImage(lockedRectPtr);

            frameTexture.UnlockRect(0);
            hr = KinectWrapper.NuiImageStreamReleaseFrame(depthStreamHandle, imageFramePtr);
        }

        return newDepth;
    }
Example #14
0
    public static bool PollColor(IntPtr colorStreamHandle, ref Color32[] colorImage)
    {
        IntPtr imageFramePtr = IntPtr.Zero;
        bool newColor = false;

        int hr = KinectWrapper.NuiImageStreamGetNextFrame(colorStreamHandle, 0, ref imageFramePtr);
        if (hr == 0)
        {
            newColor = true;

            NuiImageFrame imageFrame = (NuiImageFrame)Marshal.PtrToStructure(imageFramePtr, typeof(NuiImageFrame));
            INuiFrameTexture frameTexture = (INuiFrameTexture)Marshal.GetObjectForIUnknown(imageFrame.pFrameTexture);

            NuiLockedRect lockedRectPtr = new NuiLockedRect();
            IntPtr r = IntPtr.Zero;

            frameTexture.LockRect(0, ref lockedRectPtr, r, 0);
            ExtractColorImage(lockedRectPtr, ref colorImage);

            frameTexture.UnlockRect(0);
            hr = KinectWrapper.NuiImageStreamReleaseFrame(colorStreamHandle, imageFramePtr);
        }

        return newColor;
    }
Example #15
0
    private short[] extractDepthImage(NuiLockedRect lockedRect)
    {
        DepthBuffer db = (DepthBuffer)Marshal.PtrToStructure(lockedRect.pBits, typeof(DepthBuffer));

        return(db.pixels);
    }