0

I have to resize images in Java from approx 1000px to 200px , then they are copied to web folder to be displayed in Html report at 200px resolution. (Note I have to create these files because the original images will not be available to the webserver, and just copying the original images will require too much space.)

Although the original images are usdually high quality the 200px images can be quite grainy, can I tweak the code below to produce better quality images

public static BufferedImage resizeUsingImageIO(Image srcImage, int size)
    {
        int w = srcImage.getWidth(null);
        int h = srcImage.getHeight(null);

        // Determine the scaling required to get desired result.
        float scaleW = (float) size / (float) w;
        float scaleH = (float) size / (float) h;

        MainWindow.logger.finest("Image Resizing to size:" + size + " w:" + w + ":h:" + h + ":scaleW:" + scaleW + ":scaleH" + scaleH);

        //Create an image buffer in which to paint on, create as an opaque Rgb type image, it doesn't matter what type
        //the original image is we want to convert to the best type for displaying on screen regardless
        BufferedImage bi = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);

        // Set the scale.
        AffineTransform tx = new AffineTransform();
        tx.scale(scaleW, scaleH);

        // Paint image.
        Graphics2D g2d = bi.createGraphics();
        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, size, size);
        g2d.setComposite(AlphaComposite.SrcOver);
        g2d.drawImage(srcImage, tx, null);
        g2d.dispose();
        return bi;
Paul Taylor
  • 13,411
  • 42
  • 184
  • 351
  • Any objections to close this as a duplicate of https://stackoverflow.com/questions/24745147/java-resize-image-without-losing-quality ? – Marco13 Oct 01 '19 at 15:23
  • The other question gives plenty of theoretical answers but doesnt give a simple answer of what to actually do, this one seems to – Paul Taylor Oct 03 '19 at 06:52
  • My answer to the linked question, https://stackoverflow.com/a/24746194/3182664 , shows an [MCVE], with methods for resizing an image and storing it as JPG with arbitrary quality. It can be considered as a pure utility function. I also compared the performance of different scaling approaches in https://stackoverflow.com/a/32278737/3182664 , with different `scaleWith...` methods that could easily be re-used. – Marco13 Oct 03 '19 at 11:37

2 Answers2

0

You can increase quality by turning on antialiasing. In your case that would be:

Graphics2D g2d = bi.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);

g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);

g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Yoda
  • 17,363
  • 67
  • 204
  • 344
0

For better quality than Java's 'antialiased' bilinear resamping, you can use a library like imageScalr which is optimized for thumbnails, or my own ImageUtilities which may have better/different quality*

*) Mine produces scientifically accurate resamplings, while thumbnails may subjectively look better when slightly oversharpened like imageScalr results. Mine is much faster.

Mark Jeronimus
  • 9,278
  • 3
  • 37
  • 50