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


Processing – Validate a date

How to validate a date in Processing?

boolean isValidDate(String _date) {
  try {
    DateFormat df = new SimpleDateFormat("dd-M-yyyy");
    df.setLenient(false);
    df.parse(_date);
    return true;
  } 
  catch (Exception e) {
    return false;
  } // try
} // isValidDate()



Processing Java – “WARNING: Could not open/create prefs root node”

How to get rid of the “Could not open/create prefs root node…” warning?

  • Go into your Start Menu and type regedit into the search field
  • Navigate to path HKEY_LOCAL_MACHINE\Software\JavaSoft
    (Windows 10 seems to now have this here: HKEY_LOCAL_MACHINE\Software\WOW6432Node\JavaSoft)
  • Right click on the JavaSoft folder and click on New -> Key
  • Name the new Key Prefs and everything should work