使用Java 2D可以創建圖像的最大尺寸是多少?如何在Java2D中創建更大尺寸的圖像
我使用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();
}
}
你給Java程序多少內存? – Kayaman
您好Kayaman,我曾嘗試以下,Java的罐子MaxMemoryTest.jar以及Java的罐子-Xms512m -Xmx1024m MaxMemoryTest.jar – Sivagururaja
你的大小9050 9050 X的圖像是利用每像素4個字節(TYPE_INT_RGB)。這導致327610000字節,或在內存中> 312 MB。你確定5MB的限制嗎?它應該可以用-Xmx1024m來實現,除非輸入圖像很大。還記得JVM需要的*連續可用空間*塊來爲你在堆上圖像的int數組。所以有1024 MB的堆不能保證(但它*應該*工作)。 – haraldK