Generate thumbnail in Java

8.1k views Asked by At

I would like to know if I can generate thumbnail with proper aspect ratio for an image in Java without using any third party libraries. If not then kindly suggest a fast and efficient open source library to generate thumbnails with proper aspect ratio.

With proper aspect ratio I mean if the image is very large lets say 1900 X 1080, then the thumbnail should not just be a resized version of the original image (hence resulting a compressed look or improper image), rather it should be properly cropped and resized resulting in a clean view.

Note: The size of the thumbnail would be constant for image of any size.

3

There are 3 answers

0
Luca Rasconi On

I like im4java, a wrapper for Image Magick.

im4java just generates the command line for the ImageMagick commands and passes the generated line to the selected IM-command (using the java.lang.ProcessBuilder.start()-method), this means that elaboration and the memory consumption is delegated to another preocess within the server, and I like this.

I'm using and I used it in many projects and I found it very reliable, fast and complete.

2
tucuxi On

No third-party libraries required (but probably slower than ImageMagick):

import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.nio.file.*;

public class J {

    public static BufferedImage createThumb(BufferedImage in, int w, int h) {
        // scale w, h to keep aspect constant
        double outputAspect = 1.0*w/h;
        double inputAspect = 1.0*in.getWidth()/in.getHeight();
        if (outputAspect < inputAspect) {
            // width is limiting factor; adjust height to keep aspect
            h = (int)(w/inputAspect);
        } else {
            // height is limiting factor; adjust width to keep aspect
            w = (int)(h*inputAspect);
        }
        BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = bi.createGraphics();
        g2.setRenderingHint(
            RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(in, 0, 0, w, h, null);
        g2.dispose();
        return bi;
    }

    public static void main(String args[]) throws Exception {
        for (String in : args) {
            BufferedImage bi = createThumb(
                ImageIO.read(Files.newInputStream(Paths.get(in))),
                128, 128);
            String ext = in.substring(in.lastIndexOf(".")+1);
            String out = in.replaceFirst(".([a-z]+)$", "_thumb." + ext);
            System.err.println(in + " --> " + out);
            ImageIO.write(bi, ext, Files.newOutputStream(Paths.get(out)));
        }
    }
}

The main method expects, as arguments, an array of paths to images. It will attempt to create thumbnails in the same format as it finds the originals; you can change this by replacing the line that assigns ext with ext = "png" (for example).

0
ceklock On

Try imgscalr:

image = Scalr.resize(image, size);

https://github.com/rkalla/imgscalr