0

私のアプリケーションに似たものを見つけることができなかったので、新しい質問をしようと思いました. 私はAndroid(およびJava全般)の開発は初めてですが、CおよびVisual Basicでのプログラミング経験があります。私は JPEG TTL カメラ (Link Sprite LS Y201) を使用しており、写真を撮って TCP サーバーから Android クライアント デバイスに送信しています。クライアント側では、非同期タスクを使用してソケットからデータを継続的に取得しています。これまでのところ、いくつかのバイトを取得して配列に格納することができました。ここに私の質問があります: 1. ソケットから入ってくるデータの量は不明です。それをどのように説明するのですか?2. すべてのデータが読み取られたかどうかを確認する方法は? JPEG 画像データは 16 進値 0xFFD8 で始まり、終了値は 0xFFD9 です。3. このデータをイメージビューに更新するには?

また、時間を割いてご覧いただきありがとうございます。私が得ることができるどんな助けにも本当に感謝しています!

私が現在持っているコードは以下の通りです:

  // ----------------------- 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