title: Stream camera feed of android to a java based server
body: <p>i am making a simple android app which streams video feed of camera to a custom java based server, i am sending the frames (basically byte[]) using tcp, [i know tcp is not a good approach for video streaming but it is ok for me], android is the client and a java application is the server, i have implemented the client successfully but i am having problem in implementing the java application, the server has to receive the byte[], convert them to image and display in some image container, this is the source code of server:</p>
  
  <pre><code>      package dummyserver;
  
         import java.awt.Image;
         import java.awt.image.BufferedImage;
         import java.io.ByteArrayInputStream;
         import java.io.DataInputStream;
         import java.io.IOException;
         import java.io.InputStream;
         import java.net.ServerSocket;
         import java.net.Socket;
         import javax.imageio.ImageIO;
         import javax.media.jai.PlanarImage;
  
  
       /**
           *
         * @author usama
       */
    public class ServerListen implements Runnable{
  
  
      CameraFeed camera_feed_ui;
      ServerSocket server_socket=null;
      Socket client_socket=null;
      InputStream in=null;
      int port=9000;
      DataInputStream dis=null;
  
  public ServerListen() {}
  
  public ServerListen(CameraFeed camera_feed_ui)
  {
      this.camera_feed_ui = camera_feed_ui;
  }
  
  
  
  public void run() {
  
          int len = 0;
          byte[] data;
  
              while(true)
      {
      try {
          System.out.println("Waiting");
          server_socket = new ServerSocket(port);
          client_socket=server_socket.accept();
          System.out.println("Client arrived");
          System.out.println("Reading Image");
         in=client_socket.getInputStream();
  
         data=new byte[client_socket.getReceiveBufferSize()];
  
         in.read(data, 0, client_socket.getReceiveBufferSize());
  
          Image image = getImageFromByteArray(data);
  
          BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
  
          PlanarImage planar_image=PlanarImage.wrapRenderedImage(bufferedImage);
          System.out.println("Converting byte[] to Image completed");
          camera_feed_ui.displayImage(planar_image);
  
  
  //        client_socket.close();
          server_socket.close();
  
  
  
          }
   catch(Exception ex)
      {
          System.out.println("error: "  + ex.toString());
      }
  
      }
  }
  
  
  
  
  public static Image getImageFromByteArray(byte[] byteArray) {
      InputStream is = new ByteArrayInputStream(byteArray);
      try {
          return ImageIO.read(is);
      } catch (IOException ex) {
          System.out.println("Unable to converet byte[] into image.");
          return null;
  
      }
  
  
    }
   }
  </code></pre>
  
  <p>Explanation of code:
  CameraFeed is the object of my JFrame and it basically contains the image container on which the video is to be displayed (for displaying video i am using <a href="http://java.sun.com/javase/technologies/desktop/media/jai/" rel="nofollow">jai</a> [java advance imaging]). The method displayImage(PlanarImage) simply takes the image to be displayed in the container. I think the problem is in converting byte[] to image or i am not correctly extracting byte[] from the sockets, now in the output i am getting a black image.</p>
  
  <p>One more thing, in the client side, i am establishing tcp connection for every frame, it's also clear from this code, i am closing the connection (server_socket.close()) after the frame is received, is this a good approach? How can i make this streaming efficient? if u can please describe the appropriate approach for video streaming from android phone to server (i am asking about algorithm).</p>
  
  <p>Thanks in advance</p>
  
  <p>regards</p>
  
  <p>usama</p>
  
  <p><strong>Edit:</strong></p>
  
  <p><em>C# code:</em></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  using System.Net.Sockets;
  using System.Threading;
  
  namespace VideoServer
  {
      public partial class Form1 : Form
     { 
  
      TcpListener server_socket;
      Socket client_socket;
      Thread video_thread;
      NetworkStream ns;
  
      public Form1()
      {
          InitializeComponent();
          server_socket = null;
          client_socket = null;
      }
  
      private void startVideoConferencing()
      {
          try
          {
              server_socket = new TcpListener(System.Net.IPAddress.Parse("192.168.15.153"),9000);
              server_socket.Start();
              client_socket = server_socket.AcceptSocket();
              ns = new NetworkStream(client_socket);
              pictureBoxVideo.Image = Image.FromStream(ns);
              server_socket.Stop();
  
              if (client_socket.Connected == true)
              {
                  while (true)
                  {
                      startVideoConferencing();
                  }
                  ns.Flush();
              }
          }
          catch (Exception ex)
          {
              button1.Enabled = true;
              video_thread.Abort();
          }
      }
  
      private void button1_Click(object sender, EventArgs e)
      {
          button1.Enabled = false;
          video_thread = new Thread(new ThreadStart(startVideoConferencing));
          video_thread.Start();
      }
  
  }
  }
  </code></pre>
  
  <p>Basically the problem is that what is the equivalent of <a href="http://msdn.microsoft.com/en-us/library/93z9ee4x.aspx" rel="nofollow">Image.FromStream</a> in java, if there is any such method in java which just takes a stream and convert into image while abstracting the low level details of conversion, then this task would also be done for Java. So any idea ??</p>