开发者

Applet not creating image file via ImageIO.write

开发者 https://www.devze.com 2023-02-04 13:46 出处:网络
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 Bu

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(w开发者_C百科idth, 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;
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消