How to write two images into one file.
Hello all,
I have a very big image and don't have enought ram to make a copy of the image in the buffer. So, I want to convert this image partly and on each iteration append an image part to a file.
BufferedImage bigImage = ImageIO.read(new File("bigImage.jpg"));
for (int i = 0; i < 100; i++){
BufferedImage imgPart = img.getSubimage(i*10, 0, 100, 100);
imgPart = convert(imgPart);
//Now I need to write this part into a file, then, get a next Subimage,
//convert it and append a second image.
}
Doing this, I don't need the double size of my RAM. My problem, I don't know how can I append a second image to the first one.
I want to write first 0,0,100,100 image and then append to this 100,100,100,100 and the image should be
0,0,200,200 now.
Thanks in advance.
[1087 byte] By [
flexeda] at [2007-11-27 4:13:13]

# 1
Something like this, doesn't work for JPEG and BMP, I haven't tested other formats:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
public class Test {
public static void main(String[] args) {
Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("BMP");
ImageWriter writer = (ImageWriter) iter.next();
ImageOutputStream stream;
try {
stream = ImageIO.createImageOutputStream(new FileOutputStream("test.BMP"));
writer.setOutput(stream);
System.out.println(writer);
BufferedImage image = new BufferedImage(1000, 1000, BufferedImage.TYPE_INT_RGB);
writer.write(image);
ImageWriteParam param = writer.getDefaultWriteParam();
System.out.println(param.canOffsetTiles());
image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.setColor(Color.RED);
g.fillRect(0, 0, 100, 100);
param.setDestinationOffset(new Point(100, 100));
writer.prepareReplacePixels(0, new Rectangle(100, 100, 100, 100));
writer.replacePixels(image, param);
writer.dispose();
stream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
//
e.printStackTrace();
}
}
}
I get:
Exception in thread "main" java.lang.UnsupportedOperationException: Unsupported write variant!
at javax.imageio.ImageWriter.unsupported(Unknown Source)
at javax.imageio.ImageWriter.prepareReplacePixels(Unknown Source)
at org.freeais.gui.components.plotter.Test.main(Test.java:38)