Processing – Asynchronous load web image

How to load an image from an URL asynchronously from the web?

When you use the regular loadImage() function, sometimes it ‘hangs’ while fetching an image from the web.
The following class has a timeout parameter, so it will give up after an x-amount of seconds.

Add to your sketch:

// ASYNC IMAGE LOADER
WebImageLoader webImageLoader;

void setup() {
(...)
webImageLoader = new WebImageLoader(this);
(...)
}

The loader class:

/**************************************************************************
 *
 *  ASYNC IMAGE LOADER
 *
 **************************************************************************/
boolean loaderDebug = false;
boolean done;

/**************************************************************************
 *
 *  LOADER CLASS
 *
 **************************************************************************/
class Loader extends Thread {
  PImage p;
  PApplet parent;
  String f;

  /************************************************************************
   *
   *  CONSTRUCTOR
   *
   ************************************************************************/
  Loader(PApplet parent) {
    this.parent = parent;
    done = true;
  } // Loader()


  /************************************************************************
   *
   *  LOAD
   *
   ************************************************************************/
  void load(String file) {
    if (done == true) {
      this.f = file;
      done   = false;
    } // if (this.done == true)
  } // load()


  /************************************************************************
   *
   *  RUN
   *
   ************************************************************************/
  public void run() {
    if (done == false) {
      if (loaderDebug) println("Loader loading: " + this.f);
      try {
        this.p = this.parent.loadImage(this.f);
      } 
      catch(Exception e) {
        e.printStackTrace();
      };

      if (this.p != null) {
        if (loaderDebug) println(this.p.width + " " + this.p.height);
      }
      done = true;
      if (loaderDebug) println("Loader done");
    } // if (this.done == false)
  } // run()
} // Loader


/**************************************************************************
 *
 *  ASYNC LOAD IMAGE CLASS
 *
 **************************************************************************/
public class WebImageLoader {
  PApplet parent;

  /************************************************************************
   *
   *  CONSTRUCTOR
   *
   ************************************************************************/
  WebImageLoader(PApplet _parent) {
    parent = _parent;
  } // WebImageLoader()


  /************************************************************************
   *
   *  LOAD THE IMAGE
   *
   ************************************************************************/
  PImage loadWebImage(String file, int timeout) {
    Loader ldr = new Loader(parent);
    ldr.start();
    ldr.load(file);
    long load = System.currentTimeMillis();

    while (true) {
      if (loaderDebug) println("Checking: "+ done);

      if (done == true) {
        if (loaderDebug) println("Returning image");
        return ldr.p;
      } // if (ldr.done == true)

      if ((System.currentTimeMillis() - load)  > timeout) {
        //if (loaderDebug) println("Timeout");
        return null;
      } // if ((System.currentTimeMillis() - load)  > timeout)

      try {
        parent.delay(50); // 500
      } 
      catch(Exception e) {
      } // try
    } // while (true)
  } // loadWebImage()
} // WebImageLoader

Load an image:

PImage img = webImageLoader.loadWebImage(url, 5000);
with url is the url of the image; 5000 is the timeout in millis

Adapted from:
https://forum.processing.org/one/topic/loadimage-timeout.html