I already asked a Question how to save large images and I think I’m on the right track but I still need some advice.
I have an Image 12000 x 12000 and I need to save it as .png
BufferedImage can’t be used.
I was already advised to use the RenderedImage interface but somehow I can’t get the desired result. ( I haven’t worked with rasters yet so probably I got something wrong )
Code for the saving image method:
public static void SavePanel() {
PanelImage IMAGE = new PanelImage(panel);
try {
ImageIO.write(IMAGE, "png", new File(ProjectNameTxt.getText() + ".png"));
} catch (IOException e) {
}
}
And code for the PanelImage class:
public static class PanelImage implements RenderedImage {
// some variables here
public PanelImage(JImagePanel panel) {
this.panel = panel;
}
public Raster getData(Rectangle rect) {
sizex = (int) rect.getWidth();
sizey += (int) rect.getHeight();
image = null;
image = new BufferedImage(
(int) sizex,
(int) sizey,
BufferedImage.TYPE_INT_RGB);
g2 = image.createGraphics();
panel.paintComponent(g2);
return image.getData();
}
// rest of the implemented methods - no problems here
}
I noticed that the ImageIO requests one line of pixels at a time ( 12000 x 1 ).
This method is working but I still need the whole image in the BufferedImage.
I have to increase the size of the BImage each time ImageIO calls the method, otherwise I get ” Coordinate out of bounds! ” exeption
Thanks
This PNGJ library can be useful to read/write huge images, because it does it sequentially, it only keeps a line in memory at a time. (I wrote it myself a while ago, because I had a similar need)