6

1.我将android相机数据转换为bitmao

if (mBitmap == null)        //create Bitmap image first time
        {
            Camera.Parameters params = camera.getParameters();
            width_ima = params.getPreviewSize().width;
            height_ima = params.getPreviewSize().height;                      
            mBitmap = Bitmap.createBitmap(width_ima, height_ima, Bitmap.Config.RGB_565);
            mRGBData = new int[width_ima * height_ima];
        }

        decodeYUV420SP(mRGBData, data, width_ima, height_ima);
        mBitmap.setPixels(mRGBData, 0, width_ima, 0, 0, width_ima, height_ima);

2.使用udp发送数据

ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); 

        mBitmap.compress(Bitmap.CompressFormat.JPEG, 50, byteStream);   // !!!!!!!  change compression rate to change packets size

        byte data[] = byteStream.toByteArray();
        Log.e(TAG, "SIZE: " + data.length);

        int nb_packets = (int) Math.ceil(data.length / (float)DATAGRAM_MAX_SIZE);
        int size = DATAGRAM_MAX_SIZE;

        /* Loop through slices */
        for(i = 0; i < nb_packets; i++) 
        {           
            if(i >0 && i == nb_packets-1) size = data.length - i * DATAGRAM_MAX_SIZE;

            /* Set additional header */
            byte[] data2 = new byte[HEADER_SIZE + size];
            data2[0] = (byte)frame_nb;
            data2[1] = (byte)nb_packets;
            data2[2] = (byte)i;
            data2[3] = (byte)(size >> 8);
            data2[4] = (byte)size;

            /* Copy current slice to byte array */
            System.arraycopy(data, i * DATAGRAM_MAX_SIZE, data2, HEADER_SIZE, size);        

            try 
            {           
                size_p = data2.length;
                DatagramPacket packet = new DatagramPacket(data2, size_p, serverAddr, 9000);
                socket.send(packet);

3.我在安卓手机上接收数据

while (true) 
        {           

            try {
                socket.receive(packet);
                System.out.println("已经收到信息了!!!");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }               
            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);

            }

我的问题是:如何在 android 手机上显示 ByteArrayInputStream bis 数据...谢谢您的回复!

4

1 回答 1

7

为了转换ByteArrayInputStream为 jpeg 图像,您可以简单地使用以下代码:

    ByteArrayInputStream bis = new ByteArrayInputStream(imageData);
    Bitmap bp=BitmapFactory.decodeStream(bis); //decode stream to a bitmap image
    yourImageView.setImageBitmap(bp); //set the JPEG image in your image view

希望这有帮助..谢谢..

于 2012-10-16T06:28:33.833 回答