Re-sizing an image without losing quality

前端 未结 9 1432
温柔的废话
温柔的废话 2020-11-29 01:45

I made this code to resize images with two factors. It works, but the quality of image is very bad after it is resized! Can you help me?

This is the code

<         


        
9条回答
  •  星月不相逢
    2020-11-29 02:20

    Know question is old... I've tried different solutions surfing then web, I got the best result using getScaledInstance(), supplying Image.SCALE_SMOOTH as argument. In fact the resulting image quality was really better. My code below:

    final int THUMB_SIDE = 140;
    try {
        BufferedImage masterImage = ImageIO.read(startingImage);
        BufferedImage thumbImage = new BufferedImage(THUMB_SIDE, THUMB_SIDE, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = thumbImage.createGraphics();
        g2d.drawImage(masterImage.getScaledInstance(THUMB_SIDE, THUMB_SIDE, Image.SCALE_SMOOTH), 0, 0, THUMB_SIDE, THUMB_SIDE, null);
        g2d.dispose();
        String thumb_path = path.substring(0, path.indexOf(".png")) + "_thumb.png";
        ImageIO.write(thumbImage, "png", new File(thumb_path));
    } catch (IOException e) {
        e.printStackTrace();
    }
    

提交回复
热议问题