0

==================嗨这里是我的代码=========================== ===

这是一个例子

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)

我无法找到解决方案,任何人都可以帮助我吗?

4

1 回答 1

2

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

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

java -Xmx512m my.MainClass
于 2012-12-15T11:44:47.927 回答