我无法找到类似于我的应用程序的内容,所以我想我会问一个新问题。我是开发android(和一般的java)的新手,但在C和Visual Basic中有一些先前的编程经验。我正在使用JPEG TTL相机(Link Sprite LS Y201)并拍照从TCP服务器发送到Android客户端设备。在客户端,我使用Async任务来连续从套接字获取数据。到目前为止,我已经能够获得一些字节并将它们存储在一个数组中。这是我的问题: 1.来自套接字的数据量未知。如何解释? 2.如何检查是否已读取所有数据? JPEG图像数据从十六进制值0xFFD8开始,结束值为0xFFD9。 3.如何将此数据更新为imageview?
感谢您抽出宝贵时间仔细研究这个问题。我真的很感激我能得到的任何帮助!
我目前的代码如下:
// ----------------------- THE NETWORK TASK - begin ----------------------------
public class NetworkTask extends AsyncTask<Void, byte[], Boolean> {
Socket nsocket; //Network Socket
InputStream nis; //Network Input Stream
OutputStream nos; //Network Output Stream
BufferedReader inFromServer;//Buffered reader to store the incoming bytes
@Override
protected void onPreExecute() {
//change the connection status to "connected" when the task is started
changeConnectionStatus(true);
}
@Override
protected Boolean doInBackground(Void... params) { //This runs on a different thread
boolean result = false;
try {
//create a new socket instance
SocketAddress sockaddr = new InetSocketAddress("192.168.1.115",5050);
nsocket = new Socket();
nsocket.connect(sockaddr, 5000);//connect and set a 10 second connection timeout
if (nsocket.isConnected()) {//when connected
nis = nsocket.getInputStream();//get input
nos = nsocket.getOutputStream();//and output stream from the socket
BufferedInputStream inFromServer = new BufferedInputStream(nis);//"attach the inputstreamreader"
while(true){//while connected
ByteArrayBuffer baf = new ByteArrayBuffer(256);
int msgFromServer = 0;
while((msgFromServer = inFromServer.read()) != -1){;//read the lines coming from the socket
baf.append((byte) msgFromServer);
byte[] ImageArray = baf.toByteArray();
publishProgress(ImageArray);//update the publishProgress
}
}
}
//catch exceptions
} catch (IOException e) {
e.printStackTrace();
result = true;
} catch (Exception e) {
e.printStackTrace();
result = true;
} finally {
closeSocket();
}
return result;
}
//Method closes the socket
public void closeSocket(){
try {
nis.close();
nos.close();
nsocket.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
//Method tries to send Strings over the socket connection
public void SendDataToNetwork(String cmd) { //You run this from the main thread.
try {
if (nsocket.isConnected()) {
nos.write(cmd.getBytes());
nos.flush();
} else {
outputText("SendDataToNetwork: Cannot send message. Socket is closed");
}
} catch (Exception e) {
outputText("SendDataToNetwork: Message send failed. Caught an exception");
}
}
//Methods is called every time a new byte is received from the socket connection
@Override
protected void onProgressUpdate(byte[]... values) {
if (values.length > 0) {//if the received data is at least one byte
if(values[85]== 255 ){//Start of image is at the 85th byte
///This is where I get lost. How to start updating imageview with JPEG bytes?
}
}
}
//Method is called when task is cancelled
@Override
protected void onCancelled() {
changeConnectionStatus(false);//change the connection to "disconnected"
}
//Method is called after taskexecution
@Override
protected void onPostExecute(Boolean result) {
if (result) {
outputText("onPostExecute: Completed with an Error.");
} else {
outputText("onPostExecute: Completed.");
}
changeConnectionStatus(false);//change connectionstaus to disconnected
}
}
// ----------------------- THE NETWORK TASK - end ----------------------------
答案 0 :(得分:0)
available()
函数。 inFromServer.available() == 0
将告诉您是否已读取所有数据。以下代码将执行此操作:
byte[] ImageArray = baf.toByteArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(ImageArray, 0, ImageArray.length);
imageView.setImageBitmap(bitmap);
希望你觉得这很有用!祝你好运!