0

我找不到与我的申请类似的东西,所以我想我会问一个新问题。我是开发 android(和一般的 java)的新手,但在 C 和 Visual Basic 方面有一些编程经验。我正在使用 JPEG TTL 相机(Link Sprite LS Y201)并拍照以从 TCP 服务器发送到 android 客户端设备。在客户端,我使用异步任务不断地从套接字获取数据。到目前为止,我已经能够获取一些字节并将它们存储在一个数组中。以下是我的问题: 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 ----------------------------
4

1 回答 1

0
  1. 在服务器端,在发送每个图像之前,您可以发送一个整数,其中包含您要传输的图像的大小。
  2. available()功能可以用于此。inFromServer.available() == 0将告诉您是否已读取所有数据。
  3. 以下代码将执行此操作:

    byte[] ImageArray = baf.toByteArray();
    Bitmap bitmap = BitmapFactory.decodeByteArray(ImageArray, 0, ImageArray.length);
    imageView.setImageBitmap(bitmap);
    

希望你觉得这个有用!祝你好运!

于 2013-07-15T00:19:25.463 回答