2011-07-23 50 views
3

我试过下面的代码来生成缩略图。高品质缩略图在java

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

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; 
+0

当你说质量,你是什么意思?分辨率是否与预期不同?或者图像是模糊的还是什么? – Jon7

+0

我得到的图像模糊。它不清楚。 – Antony

回答

0
+0

我得到的图像模糊不清。 – Antony

+0

请不要发布仅链接的答案,但也可以在答案中添加一些相关部分。链接可能会死亡等 – RobAu

+0

@RobAu这些是官方文档,如果您觉得*强烈地关注它,请随时更新它们。我不再处于这个问题的顶部 – prusswan

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

+0

如果任何人需要更多的信息然后评论 –