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 & HelpIntegration › read / write files from disc in applet
Page Index Toggle Pages: 1
read / write files from disc in applet (Read 683 times)
read / write files from disc in applet
Nov 17th, 2007, 4:20am
 
I wasn't sure where to post this, but it seemed like a systems integration issue, sooo...

I read here:

http://processing.org/reference/troubleshooting/index.html#applet

that:

"Applets cannot open or write to files on the local machine. So things like saveFrame() will cause an error when run from an applet. To read files, make sure that you use "Add File..." to add the file to your sketch (or place it in the data folder yourself) so that it will be included when exported. Then, only use functions like loadStrings(), loadImage(), openStream(), etc. to read data from your file."

I realize this is probably a security measure, but is there a work around for this that anyone is aware of? I want the applet I'm hosting on my server to be able to read & write image files that were not in the sketch's data directory at the time it was compiled.

ex. php is writing images to /path/to/sketch/data/ and I'd like my sketch to be able to load those images, manipulate them, then save the results.

The code I've worked up could accomplish this, but since images can't be dynamically read from the data dir, it won't.

Here's a snippet:

/**
* Assuming the HTML embed contains the parameter
* <param name="someimage" value="arch.jpg" />
* where arch.jpg is a file written to data/ sometime
* after compilation.
*/
String img = param("someimage");
PImage tmp_img = loadImage(img);
image(tmp_img, 0, 0);

Any thoughts?

Re: read / write files from disc in applet
Reply #1 - Nov 17th, 2007, 6:04am
 
According to http://processing.org/reference/loadImage_.html loadImage() can get stuff from the web. If the applet is online, it can only download images from the same domain.

I'm not sure, but I think the applet will hang untill the file is downloaded.

/**
* Assuming the HTML embed contains the parameter
* <param name="someimage" value="arch.jpg" />
* where arch.jpg is a file written to data/ sometime
* after compilation.
*/
String img = param("someimage");
PImage tmp_img = loadImage("http://your.server.com/"+ img);
image(tmp_img, 0, 0);
Re: read / write files from disc in applet
Reply #2 - Nov 19th, 2007, 12:26am
 
That worked for loading the image, but what about saving it after it's been modified?
Re: read / write files from disc in applet
Reply #3 - Nov 19th, 2007, 11:53am
 
ah, thought your php is gonna create/write the image files. Not sure if this is an easy task... try searching the board, things like this http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Integrate;action=display;num=1136283692 might help.
Another thing that could work is opening a net connection to a php socket... then pass the pixel array to a php script and let it recreate a jpg... err... just tinkering, don't think this will work though...
Re: read / write files from disc in applet
Reply #4 - Nov 19th, 2007, 5:23pm
 
PHP is writing the initial image file to disc, but processing needs to output the modified version to disc too.

I considered inserting the image into a mysql table, but I read that database connections in applets are a security risk. Anyone can decompile the .jar and see your password.

I could limit that db user's permissions & tell the db to only accept connections from the localhost, so a compromised l/p wouldn't matter as much, but that's not exactly ideal.

Also, I'll have a lot of images in a db that I don't really want in a db. I would have to write a cron script or a mysql function to dump the image to disc after its been added to the db.

I think this idea of processing > DB > disc will work, but it all starts to get too baroque for such a simple task. I'm still looking for an alternative...

Page Index Toggle Pages: 1