We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › image loading progression
Page Index Toggle Pages: 1
image loading progression (Read 497 times)
image loading progression
Jan 30th, 2008, 7:27am
 
Hi all,

I'm using threads to load images in the background and it works well. Currently, I only get the status (loading / loaded), but I wish I could know in realtime how much (pixels?) of the image has been loaded.

Does anyone know if Java provides such information tools? (I know ActionScript does)
Re: image loading progression
Reply #1 - Jan 30th, 2008, 5:22pm
 
:-D seems like I found the answer searching in the sources : I think java.awt.Toolkit gonna save me!

I'll post some code here if it works.
Re: image loading progression
Reply #2 - Jan 30th, 2008, 7:41pm
 
Here is the code. There may be some bugs, but here it is :

Quote:
LoadingThread lt;
int progressBarValue = 0;

void setup() {
 size(300, 200);
 lt = new LoadingThread();
 lt.start();
}

void draw() {
 if (lt.isAlive()) {
   background(255);
   int progressBarValue = int(lt.completed*(width-40));
   noStroke(); fill(250, 150, 0); rect(20, height*0.5-10, progressBarValue, 20);
   stroke(0); noFill(); rect(20, height*0.5-10, width-40, 20);
 } else {
   image(lt.img, 0, 0, width, height);
 }
}

class LoadingThread extends Thread {
 float completed = 0.0;
 PImage img;
 void run() {
   String url = "http://n.clavaud.free.fr/blog/images/gdbassin_case_creole_1024.jpg";
   int fileLen = 0;
   try {
     java.net.URL u = new java.net.URL(url);
     java.net.URLConnection uc = u.openConnection();
     fileLen = uc.getContentLength();
   } catch (java.net.MalformedURLException e) {
     println(e.getMessage());
     stop();
   } catch (java.io.IOException e) {
     println(e.getMessage());
     stop();
   }
   InputStream is = openStream(url);
   BufferedInputStream bis = new BufferedInputStream(is);
   ByteArrayOutputStream out = new ByteArrayOutputStream();    
   try {
     int c = bis.read();
     int bytesRead = 0;
     while (c != -1) {
       out.write(c);
       c = bis.read();
       bytesRead++;
       completed = (float)bytesRead/(float)fileLen;
     }
   } catch (java.io.IOException e) {
     println(e.getMessage());
   }
   byte[] bytes = out.toByteArray();
   Image awtImage = Toolkit.getDefaultToolkit().createImage(bytes);
   img = loadImageSync(awtImage);
 }
}
Re: image loading progression
Reply #3 - Jan 31st, 2008, 8:53pm
 
I am so happy with this code that I made a library!
http://processing.org/discourse/yabb_beta/YaBB.cgi?board=os_libraries_tools;action=display;num=1201809099

feel like I'm talking to myself! :-)
Re: image loading progression
Reply #4 - Feb 1st, 2008, 12:07pm
 
Wow... hey thanks a lot this is going to be very helpful. I'm writing a lot of stuff that grabs images from the internet these days and this will be a great asset.
Page Index Toggle Pages: 1