0

画面をキャプチャし、その画像をバイト配列に変換してソケット接続に送信しています。サーバーにはデータが表示されますが、画像は表示されません。

` public class ImageToByteActivity extends Activity {

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();
}

}

`

注:- byte[] を表示している場合、単一の値のみが含まれます。

4

1 に答える 1

0

バイトバッファを出力ストリームに直接書き込む必要があります。これで、最初に文字列、さらには utf-8 に変換しています。画像は文字列で表現できないため、これは意味がありません。したがって、すべての文字列を削除し、buffer=bos.toByteArray() アウトの後にバッファーを書き込みます。tempStr は使用できません。

于 2013-02-06T01:01:01.737 に答える