javathumbnailsresizescaletransformscalr

Image resize not reducing kiloBytes of the image - using imgscalr-lib-3.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.


Solution

  • 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();