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!
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.
// 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.
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..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With