开发者

Stream camera feed of android to a java based server

开发者 https://www.devze.com 2023-04-07 09:35 出处:网络
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 st

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:

      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;

    }


  }
 }

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 jai [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.

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).

Thanks in advance

regards

usama

Edit:

C# 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 startVi开发者_开发问答deoConferencing()
    {
        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();
    }

}
}

Basically the problem is that what is the equivalent of Image.FromStream 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 ??


Video stream is not just a stream of images (e.g. Jpegs). Depending on codec, its a stream of encoded frames, some of them are only partial (intraframes) frames. So you can not just take a video stream and simply decode it with image codec. You need a video stream codec.

Take a look at this two open source projects to see how they implemented it:

http://code.google.com/p/spydroid-ipcamera/

http://code.google.com/p/ipcamera-for-android/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号