Java中的高质量缩略图

3
我尝试了下面的代码来生成缩略图。
我能够得到缩略图,但质量不够好。请问有人能帮助我生成高质量的缩略图吗?原始图像是高质量的。
BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2D = thumbImage.createGraphics();
graphics2D.setBackground(Color.WHITE);
graphics2D.setPaint(Color.WHITE); 
graphics2D.fillRect(0, 0, thumbWidth, thumbHeight);
graphics2D.setComposite(AlphaComposite.Src);

graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
graphics2D.dispose();      
File file = new File(thumbnailFile);
if (javax.imageio.ImageIO.write(thumbImage, "JPG", file))
    return file;

当你说质量时,你指的是什么?分辨率是否与预期不同,还是图像模糊不清,或者其他什么问题? - Jon7
我得到的图片很模糊,不清晰。 - Antony
3个回答

0

检查这个我在这里找到了最好的jar文件

public static javaxt.io.Image resizeThumbnailImage(javaxt.io.Image image, int width, int height) {

    Integer imgWidth = image.getWidth();
    Integer imgHeight = image.getHeight();
    Double imgRatio = (imgWidth.doubleValue() / imgHeight.doubleValue());
    logger.info("\n======= imgRatio " + imgRatio);
    if (imgRatio >= 2) {
        image.setWidth(width - 1);

    } else if (imgRatio < 1) {
        image.setHeight(300);

    } else {
        Double expectedHeight = (imgRatio * (height / ProjectConstant.THUMBNAIL_IMG_ASPECT_RATIO));
        image.setHeight(expectedHeight.intValue());

        if (image.getWidth() > width) {
            image.setWidth(width - 20);
        }
    }
    logger.info("=======after Processing  image Width  " + image.getWidth()+" Hight "+image.getHeight());

    return image;
}

我的常量

public static final double THUMBNAIL_IMG_ASPECT_RATIO = 1.4;

enter image description here enter image description here enter image description here


如果有人需要更多信息,则评论。 - Sameer Kazi

0

请不要只发布链接答案,而是尝试在答案中添加一些相关部分。链接可能会失效等问题。 - Rob Audenaerde
@RobAu 那些是官方文档,如果你对此感到非常强烈,请随意更新它们。我已经不再掌握这个问题了。 - prusswan

0

请不要仅发布链接答案,而是尝试在答案中添加一些相关部分。链接可能会失效等。 - Rob Audenaerde

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接