1

i am using imgscalr-lib-3.1 to create thumbnails to use in my galleria-classic photo album. it is reducing the size (pixels i mean like 100*100) but not the kilobytes, (like 10K, 20K). here is my code :

    public static void createThumbNails(String path) throws ImageFormatException, IOException{

    File image ;
    FileInputStream in = null ;
    JPEGImageDecoder decoder;
    BufferedImage bufferedImage ;
    BufferedImage outBufferedImage ;
    Scalr scalr = new Scalr();
    File folder = new File(path+"/images/album/award/");
    File[] files = folder.listFiles();  
    File outputfile ;



    for(File file : files){
        image = file;
        in = new FileInputStream(file);
        decoder = JPEGCodec.createJPEGDecoder(in);
        bufferedImage = decoder.decodeAsBufferedImage();
        outBufferedImage = scalr.resize(bufferedImage, Scalr.Method.SPEED,100);


        ImageIO.write(outBufferedImage, "jpg", file);
    }

    in.close();

}

do you guys know what to do? thank you.

MoienGK
  • 4,544
  • 11
  • 58
  • 92
  • Just a quick tip Dave, imgscalr uses all static methods, so no need to instantiate an instance of it -- I should have made the constructor private. sorry about the confusion. Just calling Scalr.resize(...) is the intended use. – Riyad Kalla Aug 02 '11 at 21:44

1 Answers1

3

You write the thumbnails to the same files you read the original images from. This might not be what you want.

If you want that, however you'll need to either delete or truncate the input file after reading from it to ensure that you just don't overwrite the first few bytes with the thumbnail, leaving the rest of it to contain the old image data.

One easy way to truncate a file is using RandomAccessFile:

RandomAccessFile raf = new RandomAccessFile(file, "rw");
raf.setLength(0);
raf.close();
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • ImageIO.write(RenderedImage, String, File) will replace any existing file, so this is obviously not the problem. – jarnbjo Apr 20 '11 at 10:55
  • @jarnbjo : joachim is right!! i changed the destination folder and that did the trick, problem solved, rate him up :D ;) – MoienGK Apr 20 '11 at 11:00