【发布时间】:2014-10-21 14:26:52
【问题描述】:
我正在尝试将屏幕截图从桌面发送到 android,并在 android 上显示。据我所知,返回 null 可能是因为我没有 inputStream.read() 读取所有输入流,因此我创建了一个 while 循环来读取所有输入流然后显示。桌面还发送了字节数组的大小,为了让android设置字节数组的大小,代码如下:
public class connection extends AsyncTask {
private byte[] data;
public void setByteSize(int size) {
data = new byte[size];
}
public byte[] getByteSize() {
return data;
}
@Override
protected Object doInBackground(Object... arg0) {
try {
clientSocket = new Socket("134.129.125.126", 8080);
System.out.println("client connect to server");
input = clientSocket.getInputStream();
System.out.println("getinputstream");
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while (true) {
int totalBytesRead = 0;
int chunkSize = 0;
int tempRead = 0;
String msg = null;
// byte[] data = null;
byte[] tempByte = new byte[1024 * 1024 * 4];
try {
// read from the inputstream
tempRead = input.read(tempByte);
// tempbyte has x+4 byte
System.out.println("Fist time read:" + tempRead);
// convert x+4 byte into String
String message = new String(tempByte, 0, tempRead);
msg = message.substring(0, 4);
// cut the header into imageMsg
String imageMsg = new String(tempByte, 4, tempRead - 4);
// convert string into byte
System.out.println("message head:" + msg);
byteSize = Integer.parseInt(msg);
System.out.println("ByteSize:" + byteSize);
data = imageMsg.getBytes();
setByteSize(byteSize);
totalBytesRead = tempRead - 4;
System.out.println("total Byte Read=" + totalBytesRead);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// get string for the size of image
try {
int tmp = 0;
while (chunkSize > -1) {
System.out.println("data length:"
+ getByteSize().length);
chunkSize = input.read(getByteSize(), totalBytesRead,
getByteSize().length - totalBytesRead);
System.out.println("chunkSize is " + chunkSize);
System.out.println("iteration id = " + tmp);
tmp++;
totalBytesRead += chunkSize;
// System.out.println("Total byte read "
// + totalBytesRead);
if (totalBytesRead == getByteSize().length) {
break;
}
}
System.out.println("Complete reading - total read = "
+ totalBytesRead);
} catch (Exception e) {
}
bitmap = BitmapFactory.decodeByteArray(getByteSize(), 0,
getByteSize().length);
System.out.println("deco");
runOnUiThread(new Runnable() {
public void run() {
image.setImageBitmap(bitmap);
System.out.println("setImage at less than 500");
}
});
}
}
}
这是我的解码结果,我想我已经阅读了所有的,但它仍然不允许我返回图像
10-21 23:25:15.203: I/System.out(7222): Fist time read:1452
10-21 23:25:15.203: I/System.out(7222): message head:3359
10-21 23:25:15.203: I/System.out(7222): ByteSize:3359
10-21 23:25:15.203: I/System.out(7222): total Byte Read=1448
10-21 23:25:15.203: I/System.out(7222): data length:3359
10-21 23:25:15.203: I/System.out(7222): chunkSize is 1911
10-21 23:25:15.203: I/System.out(7222): iteration id = 0
10-21 23:25:15.213: I/System.out(7222): Complete reading - total read = 3359
10-21 23:25:15.213: D/skia(7222): --- SkImageDecoder::Factory returned null
【问题讨论】:
标签: java android sockets image-processing network-programming