【问题标题】:Better quality image when resizing 1000px image to 200px using Java使用 Java 将 1000 像素图像大小调整为 200 像素时,图像质量更好
【发布时间】:2019-10-01 14:37:58
【问题描述】:

我必须将 Java 中的图像大小从大约 1000px 调整为 200px ,然后将它们复制到 web 文件夹中,以便以 200px 的分辨率显示在 Html 报告中。 (注意我必须创建这些文件,因为原始图像将无法用于网络服务器,并且仅复制原始图像将需要太多空间。)

虽然原始图像通常质量很高,但 200 像素的图像可能会很粗糙,我可以调整下面的代码以生成更高质量的图像

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;

【问题讨论】:

标签: java bufferedimage java-2d


【解决方案1】:

要获得比 Java 的“抗锯齿”双线性重采样更好的质量,您可以使用像 imageScalr 这样针对缩略图进行了优化的库,或者我自己的 ImageUtilities,它可能具有更好/不同的质量*

*) 我的图像会产生科学准确的重采样,而像 imageScalr 结果稍微过度锐化时,缩略图可能主观上看起来更好。我的速度要快得多。

【讨论】:

  • 好吧,我可以试试,但与标准 Java 方法相比性能如何?
【解决方案2】:

您可以通过打开抗锯齿来提高质量。你的情况是:

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);

【讨论】:

  • 我看不出有什么不同,应该可以
  • 我更新了答案,请尝试一下,如果它提高了质量,请告诉我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-10
  • 2013-12-17
  • 2010-12-10
  • 2020-02-26
  • 2014-02-17
  • 2021-02-15
  • 1970-01-01
相关资源
最近更新 更多