2

iam using UDP connection ,i have an android application which send video to pc which runs a java application. the java application runs on pc recieve packets send by android application as ByteArrayInputStream(imageData) ,now i need to convert these images to a video clip how can i do that, following is my code java application pc code..

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.lang.Runnable;
import java.lang.Thread;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class cam_thread_UDP implements Runnable
{
    int nb = 0;
    CAR_GUI car_state;
    Thread t;

    public static int HEADER_SIZE = 5;
    public static int DATAGRAM_MAX_SIZE = 1450;
    public static int DATA_MAX_SIZE = DATAGRAM_MAX_SIZE - HEADER_SIZE;

    public cam_thread_UDP(CAR_GUI gui) 
    {
        car_state = gui;
        try 
        {
            t = new Thread(this);
            t.start();
        } 
        catch (Exception e){e.printStackTrace();}
    }

    public void run() 
    {
        System.out.println("Cam thread waiting...");

        handleConnection_UDP();
    }

    public void handleConnection_UDP() 
    {
        int current_frame = -1;
        int slicesStored = 0;
        byte[] imageData = null;
        DatagramSocket socket=null;

        try 
        {                   
            InetAddress serverAddr = InetAddress.getLocalHost();
            System.out.println("PC IP address: " + serverAddr.getHostAddress());
            socket = new DatagramSocket(9000, serverAddr);
            byte[] buffer = new byte[DATAGRAM_MAX_SIZE];
            DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

            while (true) 
            {               
                socket.receive(packet);             
                byte[] data = packet.getData();         
                int frame_nb = (int)data[0];
                int nb_packets = (int)data[1];
                int packet_nb = (int)data[2];
                int size_packet = (int) ((data[3] & 0xff) << 8 | (data[4] & 0xff)); 

                if((packet_nb==0) && (current_frame != frame_nb))
                {
                    current_frame = frame_nb;
                    slicesStored = 0;               
                    imageData = new byte[nb_packets * DATA_MAX_SIZE];
                }

                if(frame_nb == current_frame)
                {
                        System.arraycopy(data, HEADER_SIZE, imageData, packet_nb * DATA_MAX_SIZE, size_packet);
                        slicesStored++;             
                }

                /* If image is complete display it */
                if (slicesStored == nb_packets) 
                {                   
                    ByteArrayInputStream bis = new ByteArrayInputStream(imageData);
                    car_state.set_image(bis);
                }
            }
        } catch (IOException e) 
        {
            e.printStackTrace();
            socket.close();
        } 
    }
}
4

0 回答 0