[英]Read Image Byte Array of Unknown Size to display in Imageview
我找不到類似於我的應用程序的內容,所以我想問一個新問題。 我是開發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 ----------------------------
available()
函數可用於此目的。 inFromServer.available() == 0
將告訴您是否已讀取所有數據。 下面的代碼可以做到這一點:
byte[] ImageArray = baf.toByteArray(); Bitmap bitmap = BitmapFactory.decodeByteArray(ImageArray, 0, ImageArray.length); imageView.setImageBitmap(bitmap);
希望你覺得這個有用! 祝好運!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.