2016-03-23 53 views

答えて

6

あなたはエンコードに画像をBase64でを使用することができます。

public void sendImage(String path) 
    { 
     JSONObject sendData = new JSONObject(); 
     try{ 
      sendData.put("image", encodeImage(path)); 
      socket.emit("message",sendData); 
     }catch(JSONException e){ 
     } 
    } 

    private String encodeImage(String path) 
    { 
     File imagefile = new File(path); 
     FileInputStream fis = null; 
     try{ 
      fis = new FileInputStream(imagefile); 
     }catch(FileNotFoundException e){ 
      e.printStackTrace(); 
     } 
     Bitmap bm = BitmapFactory.decodeStream(fis); 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     bm.compress(Bitmap.CompressFormat.JPEG,100,baos); 
     byte[] b = baos.toByteArray(); 
     String encImage = Base64.encodeToString(b, Base64.DEFAULT); 
     //Base64.de 
     return encImage; 

    } 

だから基本的に、あなたがBase64での画像だけデコードを受信したい場合は

をNode.jsのための文字列を送信しています:

private Bitmap decodeImage(String data) 
{ 
    byte[] b = Base64.decode(data,Base64.DEFAULT); 
    Bitmap bmp = BitmapFactory.decodeByteArray(b,0,b.length); 
    return bmp; 
}  
+0

助けてくれてありがとう –

+0

喜んで助けてください:) –

+1

Thコードのためのanks。それは私の多くを助ける。 –

関連する問題