2020年11月3日 星期二

Kinect VideoCAM

1. Ref add Microsoft.Kinect 1.8

2.  xaml

<Window x:Class="WPFCam1.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:local="clr-namespace:WPFCam1"

        mc:Ignorable="d"

        Title="MainWindow" Height="450" Width="800">

    <Grid>

        <Rectangle Width="320" Height="240">

            <Rectangle.Fill>

                <ImageBrush x:Name="image1">

                   

                </ImageBrush>

            </Rectangle.Fill>

        </Rectangle>

    </Grid>

</Window>

3. C#

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;


namespace WPFCam1

{

    using Microsoft.Kinect;

    /// <summary>

    /// MainWindow.xaml 的互動邏輯

    /// </summary>

    public partial class MainWindow : Window

    {

        private KinectSensor sensor;

        private WriteableBitmap colorBitmap;

        private byte[] colorPixels;

        public MainWindow()

        {

            InitializeComponent();

            sensor = KinectSensor.KinectSensors[0];

            this.sensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);

            // Allocate space to put the pixels we'll receive

            this.colorPixels = new byte[this.sensor.ColorStream.FramePixelDataLength];

            // This is the bitmap we'll display on-screen

            this.colorBitmap = new WriteableBitmap(this.sensor.ColorStream.FrameWidth, this.sensor.ColorStream.FrameHeight, 96.0, 96.0, PixelFormats.Bgr32, null);

            // Set the image we display to point to the bitmap where we'll put the image data

            this.image1.ImageSource = this.colorBitmap;

            this.sensor.ColorFrameReady += this.SensorColorFrameReady;

            // Start the sensor!

            this.sensor.Start();

        }

        private void SensorColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)

        {

            using (ColorImageFrame colorFrame = e.OpenColorImageFrame())

            {

                if (colorFrame != null)

                {

                    // Copy the pixel data from the image to a temporary array

                    colorFrame.CopyPixelDataTo(this.colorPixels);


                    // Write the pixel data into our bitmap

                    this.colorBitmap.WritePixels(

                        new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight),

                        this.colorPixels,

                        this.colorBitmap.PixelWidth * sizeof(int),

                        0);

                }


            }


        }

    }

}