2013-02-05 2 views
0

화면을 캡처하여 해당 이미지를 바이트 배열로 변환하고 소켓 연결로 보냅니다. 서버는 데이터를 표시하지만 이미지는 표시하지 않습니다.서버는 데이터를 표시하지만 이미지는 표시하지 않습니다.

` 공용 클래스 ImageToByteActivity가 활동을 확장 {

boolean connected = false; 
public ByteArrayOutputStream bos; 

public char[] tempChar1 = new char[15]; 
//public char[] finalValue; 
public byte[] buffer; 


    byte[] b9; 
    String tempStr,s; 
    public static final String mCommand1 = "xxxxxxxxxxx"; 
    public static final String mCommand2 = "yyyyyyyyyyyyyyyyyyyyy"; 
    /** Called when the activity is first created. */ 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

      Button btn = (Button) findViewById(R.id.btn); 
      btn.setOnClickListener(new OnClickListener(){ 

       //@Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 

        View view = v.getRootView();; 

        view.setDrawingCacheEnabled(true); 
        Bitmap largeIcon = view.getDrawingCache(); 

      bos = new ByteArrayOutputStream(); 
      largeIcon.compress(Bitmap.CompressFormat.JPEG, 100 , bos); 

      buffer = bos.toByteArray(); 

         try { 
         s = new String(buffer,"UTF8"); 
        } catch (UnsupportedEncodingException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
         //Log.w("Image Conversion",bos.toString()); 

         // Log.w("Image Conversion",Arrays.toString(bos.toByteArray())); 
         Log.w("Image Conversion", String.valueOf(buffer.length)); 
         Log.w("Image Conversion1", s); 
         stringToBytesASCII(); 
        connection(); 

       } 
      }); 

     } 
public byte[] stringToBytesASCII() { 
    String pq = String.valueOf(buffer.length); 
    tempChar1[0] = 'K'; 
    tempChar1[1] = 'U'; 
    tempChar1[2] = 'M'; 
    tempChar1[3] = 'R'; 
    tempChar1[4] = 'H'; 
    tempChar1[5] = 'I'; 
    tempChar1[6] = 'M'; 
    tempChar1[7] = 'A'; 
    tempChar1[8] = 'N'; 
    tempChar1[9] = 'S'; 
    tempChar1[10] = 'H'; 
    tempChar1[11] = 'U'; 

    b9 = new byte[tempChar1.length]; 
    Log.w("abcdefgh",String.valueOf(tempChar1.length)); 
    for (int i = 0; i < b9.length; i++) { 
     b9[i] = (byte) tempChar1[i]; 
     try { 
      tempStr = new String(b9,"UTF8"); 
      Log.w("abcd",tempStr); 
      Log.w("abcd",String.valueOf(tempStr.length())); 


     } catch (UnsupportedEncodingException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     }  
    } 


    return b9; 
    } 

//------------------------------------------------------------- 

public void connection(){ 
try {  
     InetAddress serverAddr = InetAddress.getByName("ip"); 
    Log.d("ClientActivity", "C: Connecting..."); 
    Socket socket = new Socket(serverAddr, port); 
    connected = true; 

     try { 
      Log.d("ClientActivity", "C: Sending command."); 
      PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket 
         .getOutputStream())), true); 
       // where you issue the commands 
      Log.w("gdfgdf",tempStr); 
       out.println(mCommand1); 
       out.flush(); 
       Log.d("ClientActivity", "C: Sent."); 

       BufferedInputStream inX1 = new BufferedInputStream(socket.getInputStream()); 
       System.out.println("Server says " + inX1.available()); 
       Log.d("ClientActivity", "C: Received."); 

       while (connected) { 

       BufferedOutputStream out3 = (BufferedOutputStream) socket.getOutputStream(); 
       //out.write("Hello World\n".getBytes(charSet)); 
       out.write(tempStr); 
       out.write(s); 
       out.flush(); 
       Log.d("ClientActivity", "C: Sent Second."); 
       //Log.d("value",s); 
       } 
     } catch (Exception e) { 
      Log.e("ClientActivity", "S: Error", e); 
     } 
    //} 
    socket.close(); 
    Log.d("ClientActivity", "C: Closed."); 
} catch (Exception e) { 
    Log.e("ClientActivity", "C: Error", e); 
    connected = false; 
} 

}

String toBinary(byte[] bytes) 
{ 
    StringBuilder sb = new StringBuilder(bytes.length * Byte.SIZE); 
    for(int i = 0; i < Byte.SIZE * bytes.length; i++) 
     sb.append((bytes[i/Byte.SIZE] << i % Byte.SIZE & 0x80) == 0 ? '0' : '1'); 
    return sb.toString(); 
} 

} 

`

참고 : - 나는 바이트를 표시하고있는 경우 [] 그것은 단지 하나의 값을 포함합니다.

+0

출력 스트림에 직접 바이트를 쓰고 있기 때문입니다. 서버 측 코드는이 바이트를 이미지 파일로 변환해야합니다. –

+0

내 서버 측 코드는 모두 준비가되었습니다.이 바이트를 변환하십시오 ...... –

답변

0

직접 출력 스트림에 바이트 버퍼를 써야합니다. 이제는 문자열로 변환하고 심지어는 utf-8로 변환합니다. 이미지가 문자열로 표현 될 수 없으므로 아무 의미가 없습니다. 따라서 모든 문자열을 제거하고 buffer = bos.toByteArray()를 출력 한 다음 버퍼를 작성하십시오. tempStr을 사용할 수 없습니다.

+0

나는 또한 그것을 시도했지만 단일 값만을 보여줍니다. –

+0

단일 가치 란 무엇을 의미합니까? – greenapps

관련 문제