Question

==================Hi here is the mycode ==============================

this is example to

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("");
}
}

after processing some images i got following exception

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)

i am unable find out solution can any one help to me?

Was it helpful?

Solution

The code is hard to read, because it's not indented properly, but it seems you're processing one image at a time in memory, so if that causes an OutOfMemoryError, then you don't have much choice except increase the memory of your VM, or find a library that uses less memory to do the same thing.

To increase the heap memory, use the -Xmx flag to launch your program. For example, for a heap of 512 MBs, you would use

java -Xmx512m my.MainClass
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top