Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 754
纯洁的坏蛋 Avatar asked Oct 16 '12 05:10

纯洁的坏蛋


People also ask

How do I create a byte array image?

Create a ByteArrayInputStream object by passing the byte array (that is to be converted) to its constructor. Read the image using the read() method of the ImageIO class (by passing the ByteArrayInputStream objects to it as a parameter). Finally, Write the image to using the write() method of the ImageIo class.

How do you create a object of ByteArrayInputStream?

// Creates a ByteArrayInputStream that reads a portion of array ByteArrayInputStream input = new ByteArrayInputStream(byte[] arr, int start, int length); Here the input stream reads the number of bytes equal to length from the array starting from the start position.


1 Answers

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

like image 123
Shekhar Chikara Avatar answered Oct 18 '22 04:10

Shekhar Chikara