java中的高质量缩略图

时间:2011-07-23 08:06:15

标签: java image-processing thumbnails

我尝试使用以下代码生成缩略图。

我能够获得缩略图,但质量不存在。请任何人帮助我在这一个生成高质量的缩略图?原始图像质量很高。

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;

3 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

我遇到了同样的问题,并在最后找到了包含示例代码和示例图像的精彩文章:

http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html

答案 2 :(得分:0)

检查此I found best jar file here

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