1

I want to send frames, captured from a webcam, to my android application over UDP Sockets, and display them so that it appears as live-stream. I am able to receive images at the Android App, but I can only display single image. Not a stream. What I have done is following.

public class getImage extends AsyncTask<String, Bitmap, Bitmap> {
protected void onProgressUpdate(Bitmap... values) {
    Log.i("onProgressUpdate", "done");
    imageDisplay.setImageBitmap(values[0]); //imageDisplay is an ImageView on UI
    imageDisplay.invalidate();
}//end of onProgressUpdate


protected Bitmap doInBackground(String... params) {
//receive an image over socket
publishProgress(bitmapimage); //update on UI
return bitmapimage;

}//end of getImage


public void ConnectionButton(View v) {
Connect = (Button) findViewById(R.id.Connect);
//while(true) {
     try {
          clientsocket = new DatagramSocket(9999);

        } catch (IOException e) {
          Log.i("Connection", "exception while creating socket");
        }

     try {
          getImage obj = new getImage();
          receivedImage = obj.execute("9999").get(); //receive image and over stream
          //obj.onProgressUpdate(receivedImage);

       }
   catch(Exception e){
   e.printStackTrace();
}
    //}//endof while
}//end of ConnectionButton

If I press, the Connect button repeatedly, I get images as stream. I have tried onProgress update, but it updates images infrequently with quite large delay. How can I make these received images display on application as real-time stream.

LW001
  • 2,452
  • 6
  • 27
  • 36
mibrahimy
  • 722
  • 4
  • 18
  • I am not sure but I think the packets received in datagram sockets are not serialized. Did you consider that case? – Ankit Shukla Oct 05 '17 at 08:06
  • Even if they aren't serialized, they are supposed to be received more than once. I get update on UI very rarely. – mibrahimy Oct 05 '17 at 09:06
  • I overrode `onPostExecute` function to return a single image received from socket. Removed my call to `publishProgress()` in `doInBackground()`. And, the last step was to re-initialize the class object which extended AsyncTask and execute it in the `onPostExecute()`. Hope it helps, anyone else facing the same issue. – mibrahimy Oct 06 '17 at 07:01

1 Answers1

0

An AsyncTaskobject can be executed only once, so I was unable to receive a stream. But I achieved it through following mehtod.

I overrode onPostExecute() to return a single image received from socket. Removed my call to publishProgress() in doInBackground(). And, the last step was to re-initialize the class object which extended AsyncTask and execute it in the onPostExecute() i.e. create a new object and asyncobject.execute().get() it in the onPostExecute(). Hope it helps, anyone else facing the same issue.

mibrahimy
  • 722
  • 4
  • 18