我有 13255 张图像,每张 240 x 240 像素宽,最大的 15,412 字节,最小的 839 字节。
我正在尝试遍历将它们中的每一个添加到文件 [] 的文件夹。一旦我有了每个图像的数组,我就将它们放在一个 BufferedImage[] 中,准备循环并绘制到由每个单独的图像组成的更大的单个图像上。
每个图像都以以下形式命名
图像 xy.png
但是,我一直以 java.lang.OutOfMemoryError: Java heap space 错误告终。我不知道为什么。我尝试通过在 Eclipse 目标的末尾添加参数来更改 JVM 可用内存的大小。以下是我使用过的:
IDE's\eclipse-jee-juno-SR2-win32-x86_64\eclipse\eclipse.exe -vmargs -Xms64m -Xmx1024m
和
IDE's\eclipse-jee-juno-SR2-win32-x86_64\eclipse\eclipse.exe -vmargs -Xms64m -Xmx4096m
两者都没有效果。我还进入了控制面板 -> 程序 -> Java 并更改了那里的可用内存量。
这是我写的方法:
public static void merge_images() throws IOException {
int rows = 115;
int cols = 115;
int chunks = rows * cols;
System.out.println(chunks);
int chunkWidth, chunkHeight;
int type;
// fetching image files
File[] imgFiles = new File[chunks];
int count = 0;
for (int j = 1; j <= 115; j++) {
for (int k = 1; k <= 115; k++) {
imgFiles[count] = new File("G:\\Images\\Image " + j
+ "-" + k + ".png");
count++;
}
}
System.out.println(imgFiles.length);
// creating a buffered image array from image files
BufferedImage[] buffImages = new BufferedImage[chunks];
for (int i = 0; i < chunks; i++) {
buffImages[i] = ImageIO.read(imgFiles[i]);
System.out.println(i);
}
type = buffImages[0].getType();
chunkWidth = buffImages[0].getWidth();
chunkHeight = buffImages[0].getHeight();
// Initializing the final image
BufferedImage finalImg = new BufferedImage(chunkWidth * cols,
chunkHeight * rows, type);
int num = 0;
for (int i = 0; i < rows; i++) {
for (int k = 0; k < cols; k++) {
finalImg.createGraphics().drawImage(buffImages[num], null,
chunkWidth * k, chunkHeight * i);
num++;
}
}
System.out.println("Image concatenated.....");
ImageIO.write(finalImg, "png", new File("fusions.png"));
System.out.println("Image Saved, Exiting");
}
在此处的打印行
for (int i = 0; i < chunks; i++) {
buffImages[i] = ImageIO.read(imgFiles[i]);
System.out.println(i);
}
它总是停在 7320 点附近。
这是确切的控制台打印输出
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.awt.image.DataBufferByte.<init>(Unknown Source)
at java.awt.image.ComponentSampleModel.createDataBuffer(Unknown Source)
at java.awt.image.Raster.createWritableRaster(Unknown Source)
at javax.imageio.ImageTypeSpecifier.createBufferedImage(Unknown Source)
at javax.imageio.ImageReader.getDestination(Unknown Source)
at com.sun.imageio.plugins.png.PNGImageReader.readImage(Unknown Source)
at com.sun.imageio.plugins.png.PNGImageReader.read(Unknown Source)
at javax.imageio.ImageIO.read(Unknown Source)
at javax.imageio.ImageIO.read(Unknown Source)
at main.merge_images(main.java:48)
at main.main(main.java:19)
任何我出错的想法将不胜感激。
问候,
杰米