==================嗨这里是我的代码=========================== ===
这是一个例子
imgscalr
package anil1.main;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageFilter;
import java.io.File;
import javax.imageio.ImageIO;
import org.imgscalr.Scalr;
import static org.imgscalr.Scalr.*;
public class ImageScalar {
public static void thum(String img) throws Exception
{
String path1 = ".";
String files;
File folder = new File("C:\\Program Files\\Apache Software Foundation\\Tomcat 6.0\\webapps\\Myapp\\images\\candidates");
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++)
{
if (listOfFiles[i].isFile())
{
files = listOfFiles[i].getName();
if (files.endsWith(".jpg") || files.endsWith(".png") || files.endsWith(".jpeg")|| files.endsWith(".JPEG") )
{
boolean token =false;
String path=listOfFiles[i].getPath();
BufferedImage originalImage = ImageIO.read(new File(path));
// BufferedImage thumbnail =Scalr.crop(originalImage, 140, 140, 140, 140);
BufferedImage thumbnail=Scalr.resize(originalImage, Scalr.Method.ULTRA_QUALITY,Scalr.Mode.FIT_EXACT, 180, 250);
String parent = "C:\\Program Files\\Apache Software Foundation\\Tomcat 6.0\\webapps\\Myapp";
String child = "\\Thumbs\\images\\candidates";
String total=parent+child+path.substring(path.lastIndexOf("\\"));
String total1=parent+child;
System.out.println(total);
File f=new File(total1);
File f1=new File(path);
File f2=new File(total);
try{
if(f1.exists()){
if(f.exists())
{
System.out.println("from if");
ImageIO.write(thumbnail, "jpeg", f2);
}
else
{
System.out.println("from else");
f.mkdirs();
ImageIO.write(thumbnail, "jpeg", f2);
}}
if (new File(total1).exists())
token=true;
System.out.println(token);
}finally{
originalImage.flush();
thumbnail.flush();
f=null;
f1=null;
thumbnail=null;
originalImage=null;
}
}//if
}//for
}//main
}//class
public static void main(String[] args)throws Exception {
thum("");
}
}
处理一些图像后,我得到了以下异常
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.awt.image.DataBufferInt.<init>(Unknown Source)
at java.awt.image.Raster.createPackedRaster(Unknown Source)
at java.awt.image.DirectColorModel.createCompatibleWritableRaster(Unknown Source)
at java.awt.image.BufferedImage.<init>(Unknown Source)
at org.imgscalr.Scalr.createOptimalImage(Scalr.java:2006)
at org.imgscalr.Scalr.scaleImage(Scalr.java:2133)
at org.imgscalr.Scalr.scaleImageIncrementally(Scalr.java:2275)
at org.imgscalr.Scalr.resize(Scalr.java:1711)
at anil1.main.ImageScalar.thum(ImageScalar.java:40)
at anil1.main.ImageScalar.main(ImageScalar.java:114)
我无法找到解决方案,任何人都可以帮助我吗?