尝试将kinect byte []转换为网络流

时间:2014-04-13 12:14:52

标签: c# networking tcp kinect networkstream

嘿,我想知道是否有人可以帮我找出这里出了什么问题。

我正在尝试从kinect传感器获取一个字节数组,打开一个tcp客户端和监听器,然后通过网络流发送字节数组。

我继续得到一个null对象异常。 "对象引用未设置为对象的实例。"

服务器 -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
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 AthenaServer
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{

    ConsoleOutput outputter;
    private WriteableBitmap colorBitmap;
    private static byte[] colorPixels;
    Thread t_recieve;

    public MainWindow()
    {
        InitializeComponent();
        setupConsole();
        colorPixels = new byte[1228800];
        this.CameraViewer.Source = this.colorBitmap;
        t_recieve = new Thread(ReceiveVideo);
        t_recieve.Start();

    }
    void setupConsole()
    {
        outputter = new ConsoleOutput(ConsoleText);
        Console.SetOut(outputter);
        Console.WriteLine("Console Started");
    }

    TcpListener mytcpl;
    Socket mysocket;
    NetworkStream ns;
    NetworkStream myns;

    private void ReceiveVideo()
    {
        try
        {

            // Open The Port
            mytcpl = new TcpListener(IPAddress.Any, 8020);
            mytcpl.Start();                      // Start Listening on That Port
            mysocket = mytcpl.AcceptSocket();        // Accept Any Request From Client and Start a Session
            Console.WriteLine("Connected");
            ns = new NetworkStream(mysocket);    // Receives The Binary Data From Port

            Console.WriteLine("Reading...");
            ns.Read(colorPixels, 0, colorPixels.Length);
                             // Write the pixel data into our bitmap
                this.colorBitmap.WritePixels(
                    new Int32Rect(0, 0, 640, 480),
                    colorPixels,
                    460 * sizeof(int),
                    0);
            Console.WriteLine("Read Complete");
            mytcpl.Stop();                           // Close TCP Session

            if (mysocket.Connected == true)          // Looping While Connected to Receive Another Message 
            {
                while (true)
                {
                    ReceiveVideo();              // Back to First Method
                }
            }
            myns.Flush();

        }
        catch (Exception) { 

        }
    }





}
}

客户 -

using Microsoft.Kinect;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
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 AthenaClient
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
public partial class MainWindow : Window
{

    ConsoleOutput outputter;
    private KinectSensor sensor;
    private WriteableBitmap colorBitmap;
    private static byte[] colorPixels;
    Thread t_sender;
    MemoryStream ms;
    TcpClient myclient;
    IPAddress ipAd = IPAddress.Parse("192.168.0.15");
    NetworkStream myns;
    BinaryWriter mysw;
    private bool Sending = false;

    public MainWindow()
    {
        InitializeComponent();
        setupConsole();
        setupKinect();
        Thread.Sleep(100);
        t_sender = new Thread(SendVideo);
        t_sender.Start();
    }
    void setupConsole()
    {
        outputter = new ConsoleOutput(ConsoleText);
        Console.SetOut(outputter);
        Console.WriteLine("Console Started");
    }

    void setupKinect()
    {

        foreach (var potentialSensor in KinectSensor.KinectSensors)
        {
            if (potentialSensor.Status == KinectStatus.Connected)
            {
                this.sensor = potentialSensor;
                break;
            }
        }

        if (null != this.sensor)
        {
            this.sensor.ColorStream.Enable(ColorImageFormat.YuvResolution640x480Fps15);
            colorPixels = new byte[this.sensor.ColorStream.FramePixelDataLength];
            this.colorBitmap = new WriteableBitmap(this.sensor.ColorStream.FrameWidth,      this.sensor.ColorStream.FrameHeight, 96.0, 96.0, PixelFormats.Bgr32, null);
            this.CameraViewer.Source = this.colorBitmap;
            this.sensor.ColorFrameReady += this.SensorColorFrameReady;



            // Start the sensor!
            try
            {
                this.sensor.Start();
            }
            catch (IOException)
            {
                this.sensor = null;
            }
        }

        if (null == this.sensor)
        {

        }

    }

    void SendVideo()
    {
        while (true) {
            if (Sending)
            {
                try
                {

                    myclient = new TcpClient();//Connecting with server
                    myclient.Connect(ipAd, 8020);
                    Console.WriteLine("Connected");
                    myns = myclient.GetStream();
                    myns.Write(colorPixels, 0, colorPixels.Length);//send the stream to above address
                    ms.Flush();
                    myns.Flush();
                    ms.Close();
                    myns.Close();
                    myclient.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.WriteLine("Problem with SendVideo");
                }
                Sending = false;
            }
            }
    }

    private void SensorColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
    {
        using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
        {
            if (colorFrame != null)
            {
                if (!Sending)
                { // Copy the pixel data from the image to a temporary array
                    colorFrame.CopyPixelDataTo(colorPixels);

                    Console.WriteLine(colorPixels.Length);
                    // Write the pixel data into our bitmap
                    this.colorBitmap.WritePixels(
                        new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight),
                        colorPixels,
                        this.colorBitmap.PixelWidth * sizeof(int),
                        0);
                    Sending = true;
                }
           }
        }
    }

}
}

有谁能让我知道我做错了什么?

0 个答案:

没有答案