Das erste Quellcode-Beispiel zeigt, wie man den Color Data Stream auf der Oberfläche einer WPF-Anwendung anzeigen kann.
Die Beispielanwendung besteht aus einem einfachen MainWindow mit einem Image-Objekt, in dem der Color Data Stream angezeigt wird.
Hier ist der Quellcode von MainWindow.xaml:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Kinect for Windows Test App 01" Height="600" Width="800" WindowState="Maximized">
<Grid ShowGridLines="True">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Image x:Name="imVideoStream"
Width="640"
Height="480"
Grid.Row="0" />
<Image x:Name="imDepthStream"
Width="640"
Height="480"
Grid.Row="1" />
</Grid>
</Grid>
</Window>
Imports Microsoft.Kinect
Class MainWindow
Private kinectSensor As KinectSensor
Private pixelData As Byte()
Private outputImage As WriteableBitmap
Private Shared ReadOnly Bgr32BytesPerPixel As Integer = CInt(Math.Floor((PixelFormats.Bgr32.BitsPerPixel + 7) / 8))
Public Sub New()
InitializeComponent()
kinectSensor = (From sensorToCheck In kinectSensor.KinectSensors
Where sensorToCheck.Status = KinectStatus.Connected
Select sensorToCheck).FirstOrDefault()
If kinectSensor IsNot Nothing Then
kinectSensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30)
kinectSensor.Start()
AddHandler kinectSensor.ColorFrameReady, AddressOf kisColorFrameReady
End If
End Sub
Private Sub kisColorFrameReady(sender As Object, e As ColorImageFrameReadyEventArgs)
Using imageFrame As ColorImageFrame = e.OpenColorImageFrame()
If imageFrame IsNot Nothing Then
Me.pixelData = New Byte(imageFrame.PixelDataLength - 1) {}
imageFrame.CopyPixelDataTo(Me.pixelData)
imVideoStream.Visibility = Visibility.Visible
Me.outputImage = New WriteableBitmap(imageFrame.Width, imageFrame.Height, 96, 96, PixelFormats.Bgr32, Nothing)
Me.imVideoStream.Source = Me.outputImage
Me.outputImage.WritePixels(New Int32Rect(0, 0, imageFrame.Width, imageFrame.Height), Me.pixelData, imageFrame.Width * Bgr32BytesPerPixel, 0)
End If
End Using
End Sub
Private Sub MainWindow_Closing(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles Me.Closing
kinectSensor.Stop()
End Sub
End Class
Have fun with Kinect for Windows!
Keine Kommentare:
Kommentar veröffentlichen