使用 Java 2D 可以创建的图像的最大尺寸是多少?
我使用的是 Windows 7 Pro 64 位操作系统和 JDK 1.6.0_33,64 位版本。我可以创建一个最大为 5 MB 的 BufferedImage。除此之外,我得到了 OutOfMemoryError。
请指导我如何使用 Java 2D 或 JAI 创建更大尺寸的图像。
这是我的尝试。
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class CreateBiggerImage
{
private String fileName = "images/107.gif";
private String outputFileName = "images/107-Output.gif";
public CreateBiggerImage()
{
try
{
BufferedImage image = readImage(fileName);
ImageIO.write(createImage(image, 9050, 9050), "GIF", new File(System.getProperty("user.dir"), outputFileName));
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
private BufferedImage readImage(String fileName) throws Exception
{
BufferedImage image = ImageIO.read(new File(System.getProperty("user.dir"), fileName));
return image;
}
private BufferedImage createImage(BufferedImage image, int outputWidth, int outputHeight) throws Exception
{
int actualImageWidth = image.getWidth();
int actualImageHeight = image.getHeight();
BufferedImage imageOutput = new BufferedImage(outputWidth, outputHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = imageOutput.createGraphics();
for (int width = 0; width < outputWidth; width += actualImageWidth)
{
for (int height = 0; height < outputHeight; height += actualImageHeight)
{
g2d.drawImage(image, width, height, null);
}
}
g2d.dispose();
return imageOutput;
}
public static void main(String[] args)
{
new CreateBiggerImage();
}
}