将ByteArrayInputStream图像序列转换为视频剪辑

时间:2013-04-20 01:22:05

标签: java android udp

我使用UDP连接,我有一个Android应用程序,它发送视频到运行Java应用程序的PC。 java应用程序运行在pc接收由android应用程序发送的数据包作为ByteArrayInputStream(imageData),现在我需要将这些图像转换为视频剪辑我该怎么做,以下是我的代码 java应用程序pc代码..

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();
        } 
    }
}

0 个答案:

没有答案