java.lang.OutOfMemoryError:将13k .png图像拼接在一起时的Java堆空间 [英] java.lang.OutOfMemoryError: Java heap space when stitching 13k .png images together
问题描述
我有13255张图片,每张240 x 240像素宽,最大的15,412字节大小和最小的839字节。
I have 13255 images, each 240 x 240 pixels wide, the biggest 15,412 bytes in size and the smallest 839 bytes.
我试图循环浏览文件夹将它们中的每一个添加到File []。一旦我有一个每个图像的数组,然后我将它们放在BufferedImage []中,准备好循环并绘制到由每个图像组成的更大的单个图像。
I am trying to loop through the folder adding each of them to a File[]. Once I have an array of each image I am then placing them inside a BufferedImage[] ready to be looped through and drawn onto a larger single image made up of each individual one.
每张图片的格式为
图片xy.png
Image x-y.png
但是,我最后还是遇到了java.lang.OutOfMemoryError:Java堆空间错误。我不知道为什么。我已经尝试通过向Eclipse的目标末尾添加参数来更改JVM可用内存的大小。以下是我使用的:
However, I keep ending up with a java.lang.OutOfMemoryError: Java heap space error. I have no idea why. I have tried altering the size of the memory available to the JVM by adding parameters to the end of the target for Eclipse. Below is what I have used:
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并改变了那里可用的内存量。
Both have no effect. I have also gone into Control Panel -> Programs -> Java and changed the amount of memory available there.
这是我写的方法:
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点左右。
it always stops at around the 7320 point.
这是确切的控制台打印输出
Here is the exact console print out
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)
任何想法w非常感谢我在这里出错。
Any ideas where I am going wrong would be greatly appreciated.
问候,
Jamie
推荐答案
您无需将所有块图像保留在内存中。您可以逐个阅读它们并在最终循环中绘制到最终图像,如下所示:
You don't need to keep all your chunk images in memory. You can read them one by one and draw to your final image in final loop like this:
for (int i = 0; i < rows; i++) {
for (int k = 0; k < cols; k++) {
BufferedImage buffImage = ImageIO.read(imgFiles[num]);
finalImg.createGraphics().drawImage(buffImage, null,
chunkWidth * k, chunkHeight * i);
num++;
}
}
这将节省至少一半的内存。
This will save at least half of the memory.
这篇关于java.lang.OutOfMemoryError:将13k .png图像拼接在一起时的Java堆空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!