2012-10-16 63 views
5

1.I獲得Android相機數據轉換成bitmao如何將ByteArrayInputStream轉換爲jpeg並在Android移動設備上顯示?

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.我收到Android手機

數據
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雙數據...謝謝你的回覆!

+0

那麼你在找什麼類型的對象爲客戶端?我期望它有一個構造函數或工廠方法採用InputStream,或者可能採取其他構造函數採用InputStream的* else *。 –

+0

你的意思是說你想將ByteArrayInputStream轉換成位圖圖像? –

+0

是的,我的意思是將ByteArrayInputStream轉換爲位圖圖像 –

回答

5

爲了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 

希望這有助於..謝謝..

+0

你好,我的意思是逐個顯示位圖。它看起來像一個視頻。我能做什麼? –

+0

謝謝你的回覆,我明白了! –

+0

@純潔的壞蛋我知道它已經很晚了,但是您是否知道如何將它作爲視頻展示? –

相關問題