2

I have been struggling with an issue involving a Processing sketch which I want to use to create an image file (to a server-side directory). In the Java, I use a PImage of the canvas, converted to a BufferedImage, which is then created as a file via ImageIO.write.

It creates the file successfully when run as a sketch on my system, file.png appears in the same folder as the program. The problem is that when the program is exported as an applet, it no longer creates the file. It would be huge if anyone had any input as to why the sketch of the program can create the image but the applet cannot.

Here's a couple snippets of what I'm using if it's of any help. Thanks all.

PImage pimg = get();
BufferedImage canvas = convertToBufferedImage(pimg);
File file = new File("./sketch/file.png");
try
{
  ImageIO.write(canvas, "PNG", file);
}
catch (Exception e) { println(e); }

And here's the method that's converting the PImage to a BufferedImage.

BufferedImage convertToBufferedImage(PImage pimg){
    PGraphics pg = createGraphics(width, height, JAVA2D);
    pg.image(pimg, 0, 0);
    BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB_PRE);
    Graphics2D g2d = img.createGraphics();
    g2d.drawImage((java.awt.Image)pg.image, 0, 0, width, height, this);
    g2d.finalize();
    g2d.dispose();
    return img;
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Scott
  • 103
  • 1
  • 1
  • 10
  • No exception caught when I change that. The applet runs normally, but no image file is produced in the directory. Another strange thing is that when it's a sketch, if the path is just "file.png" it actually creates the file in the parent directory, thus I have to state the path as "./sketch/file.png" to keep it in the same directory as the sketch. When I export it as an applet, if I change it to "./applet/file.png" or simply "file.png" it's still nowhere to be seen. – Scott Jan 10 '11 at 23:26
  • Tried that. Returns true when run as sketch, .png file successfully created. I still get no errors when run it as an applet. – Scott Jan 10 '11 at 23:33
  • 1
    Applets cannot access server-side files. You have to write a servlet if you want to do that. – OrangeDog Jan 10 '11 at 23:41

1 Answers1

1

I think it's just applet security restrictions preventing you from modifying the filesystem on the client machine.

Take a look at FileSaveService in JNLP. This can be used from an unsigned applet to save a file to the filesystem on the client machine.

If you are trying to write to a directory on the server from an applet, you cannot do this directly. You would need to create an upload form on the server and maybe use HttpURLConnection to send a POST request to the server.

finnw
  • 47,861
  • 24
  • 143
  • 221
  • So is ImageIO.write the wrong approach altogether to accomplish this? I've seen solutions such as encoding the BufferedImage to Base64String which is then received and decoded by PHP. Is that the proper way to POST the BufferedImage to PHP to be uploaded by the server? – Scott Jan 10 '11 at 23:43
  • @Scott, nothing wrong with using `ImageIO.write` to encode the image, but you will need to send it to a `HttpURLConnection` or a byte array instead of a file. Have a look at the answers to [this question](http://stackoverflow.com/questions/1314249/java-upload-and-post-file-to-php-page) and [this one](http://stackoverflow.com/questions/1599018/java-applet-to-upload-a-file). – finnw Jan 10 '11 at 23:48
  • Thanks everyone so much for the gift of knowledge. Probably the most promising solution to my problem was this one. http://stackoverflow.com/questions/1889881/using-processing-on-a-server-to-create-images-behind-the-scenes/2006597#2006597 – Scott Jan 11 '11 at 00:01