【发布时间】:2016-02-13 14:56:55
【问题描述】:
实际上,我正在使用 JAVA 拆分一个 600x700 .jpg 文件。这里方法的参数宽度和高度与图像的宽度和高度完全相同。但是在执行过程中,我遇到了一个与堆内存相关的异常。
/*
* this method will split the secureImage file
*/
private BufferedImage[] getsecureFragments(int width, int height,
File secureFile)throws IOException {
FileInputStream secureFileStream = new FileInputStream(secureFile);
BufferedImage secureimage = ImageIO.read(secureFileStream);
int rows = width;
int columns = height;
int chunks = rows *columns;
int chunkWidth = width/width;
int chunkHeight=height/height;
int count = 0;
BufferedImage fragmentImgs[] = new BufferedImage[chunks];
System.out.println(fragmentImgs.length);
for (int x = 0; x < rows; x++) {
for (int y = 0; y < columns; y++) {
//Initialize the image array with image chunks
fragmentImgs[count] = new BufferedImage(chunkWidth, chunkHeight, secureimage.getType());
// draws the image chunk
Graphics2D grSecure = fragmentImgs[count++].createGraphics();
grSecure.drawImage(secureimage, 0, 0, chunkWidth, chunkHeight, chunkWidth * y, chunkHeight * x, chunkWidth * y + chunkWidth, chunkHeight * x + chunkHeight, null);
grSecure.dispose();
}
}
System.out.println("Splitting done");
//writing mini images into image files for test purpose
for (int i = 0; i < fragmentImgs.length; i++) {
ImageIO.write(fragmentImgs[i], "jpg", new File("F://uploads//fragmentImgs" + i + ".jpg"));
}
System.out.println("Mini images created");
return fragmentImgs;
}//end of method getsecureFragments
线程“http-bio-8080-exec-9”java.lang.OutOfMemoryError 中的异常:Java 堆空间
这是因为我的逻辑缺乏?或者我是否需要了解更多关于 JAVA 堆内存的信息。
【问题讨论】:
-
你能发布错误的堆栈跟踪吗?
-
是的,我弄错了。谢谢
-
您在内存中同时打开了 420 000 张图像......我并不惊讶它会耗尽内存。另外,看看
BufferedImage.getSubimage——也许有用。
标签: java