内存不足处理图像时出现异常。在java中

时间:2012-12-15 11:38:37

标签: java imgscalr

==================嗨这里是mycode ======================== ==

这是

的例子
  

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)

我无法找到解决方案可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

代码难以阅读,因为它没有正确缩进,但似乎你在内存中一次处理一个图像,所以如果这会导致OutOfMemoryError,那么除了增加内存之外你别无选择您的VM,或者找到使用较少内存的库来执行相同的操作。

要增加堆内存,请使用-Xmx标志启动程序。例如,对于512 MB的堆,您将使用

java -Xmx512m my.MainClass