How can i convert ByteArrayInputStream to jpeg and display on android mobile?
Asked Answered
A

1

6

1.i get the android camera data convert to 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.send the data with 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.i receive the data on android mobile

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

            }

And my question is : how i can display the ByteArrayInputStream bis data on android mobile... Thank you for reply!

Appendectomy answered 16/10, 2012 at 5:50 Comment(3)
Well what type of object are you looking for on the client side? I'd expect it to have a constructor or factory method taking InputStream, or possibly taking something else which has a constructor taking an InputStream.Atomicity
Do you mean to say that you want to convert the ByteArrayInputStream to a Bitmap image??Unfolded
yes,i mean convert the ByteArrayInputStream to a Bitmap imageAppendectomy
U
7

In order to convert ByteArrayInputStream to a jpeg image you can simply use the following code:

    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

Hope this helps.. Thanks..

Unfolded answered 16/10, 2012 at 6:28 Comment(3)
hello, i mean display bitmap one by one . it looks like a video. what can i do?Appendectomy
@纯洁的坏蛋 i know its very late, but did you get to know how to show it as a video?Synapsis
#62432902 please see my questionUnderskirt

© 2022 - 2024 — McMap. All rights reserved.