调整大小后图像质量很差 -- Java

27
在脚本中,它从大约300x300的标记下降到60x60。需要提高整体图像质量,因为目前输出效果非常差。
public static Boolean resizeImage(String sourceImg, String destImg, Integer Width, Integer Height, Integer whiteSpaceAmount) 
{
    BufferedImage origImage;

    try 
    {
        origImage = ImageIO.read(new File(sourceImg));
        int type = origImage.getType() == 0? BufferedImage.TYPE_INT_ARGB : origImage.getType();
        int fHeight = Height;
        int fWidth = Width;
        int whiteSpace = Height + whiteSpaceAmount; //Formatting all to squares so don't need two whiteSpace calcs..
        double aspectRatio;

        //Work out the resized dimensions
        if (origImage.getHeight() > origImage.getWidth()) //If the pictures height is greater than the width then scale appropriately.
        {
            fHeight = Height; //Set the height to 60 as it is the biggest side.

            aspectRatio = (double)origImage.getWidth() / (double)origImage.getHeight(); //Get the aspect ratio of the picture.
            fWidth = (int)Math.round(Width * aspectRatio); //Sets the width as created via the aspect ratio.
        }
        else if (origImage.getHeight() < origImage.getWidth()) //If the pictures width is greater than the height scale appropriately.
        {
            fWidth = Width; //Set the height to 60 as it is the biggest side.

            aspectRatio = (double)origImage.getHeight() / (double)origImage.getWidth(); //Get the aspect ratio of the picture.
            fHeight = (int)Math.round(Height * aspectRatio); //Sets the height as created via the aspect ratio.
        }

        int extraHeight = whiteSpace - fHeight;
        int extraWidth = whiteSpace - fWidth;

        BufferedImage resizedImage = new BufferedImage(whiteSpace, whiteSpace, type);
        Graphics2D g = resizedImage.createGraphics();
        g.setColor(Color.white);
        g.fillRect(0, 0, whiteSpace, whiteSpace);

        g.setComposite(AlphaComposite.Src);
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        g.drawImage(origImage, extraWidth/2, extraHeight/2, fWidth, fHeight, null);
        g.dispose();

        ImageIO.write(resizedImage, "jpg", new File(destImg));
    } 
    catch (IOException ex) 
    {
        return false;
    }

    return true;
}

我只想知道是否有可以提高质量的插件,或者我需要完全看其他东西。

编辑:图片对比。

来源,随机从谷歌选了一台洗衣机。 http://www.essexappliances.co.uk/images/categories/washing-machine.jpg

Washing Machine

同一张图片在Photoshop中被转换成我需要的样子。 http://imgur.com/78B1p

Good resize in Paint shop

这样转换后是什么样子。 http://imgur.com/8WlXD

Bad resize


质量低的方面是什么?你能发布一些示例图片吗? - André Stannek
你确定问题是在调整大小上,而不是你将其保存为JPG格式,而且默认的JPG压缩太高了吗?你尝试过先将其保存为无压缩格式吗? - NYCdotNet
  1. 为了更快得到更好的帮助,请发布一个SSCCE
  2. JPG本质上是有损压缩的。可以在创建时设置压缩/质量,如此答案所示。
  3. 对我来说,上传的图像看起来完全相同。鉴于“白色家电”与白色背景之间的淡色对比度,这也不是我认为适合调整大小的图像。
- Andrew Thompson
对我来说,这两张图片看起来非常不同,第一张图片中的拇指更加光滑。我需要它的质量更接近那个水平。现在正在调整压缩质量。 - dutchman191
你尝试过使用VALUE_INTERPOLATION_BICUBIC而不是VALUE_INTERPOLATION_BILINEAR吗?根据文档,双线性插值只考虑输入的4x4样本,而对于双三次插值则没有这么具体。适当的调整大小需要更大的采样窗口,因为输出与输入的比例越小。 - Mark Ransom
1
Java 调整大小后的图像存在严重的锯齿。声称大小版本看起来相同是某种否认的形式。我通过使用 ImageMagick 集成解决了这个问题。 - le3th4x0rbot
4个回答

21

在很大的范围内缩小图像是具有固有危险性(从质量的角度看),尤其是使用单步骤。

推荐的方法是使用分而治之的方法。基本上,您将图像按50%的步长缩小,直到达到所需大小。

因此,我将原始图像650x748缩小以适合60x60区域(52x60)。

enter image description here

与一步相比,分而治之...

enter image description hereenter image description here

public class TestImageResize {

    public static void main(String[] args) {
        new TestImageResize();
    }

    public TestImageResize() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new ScalePane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class ScalePane extends JPanel {

        private BufferedImage original;
        private BufferedImage scaled;

        public ScalePane() {
            try {
                original = ImageIO.read(new File("path/to/master.jpg"));
                scaled = getScaledInstanceToFit(original, new Dimension(60, 60));
                ImageIO.write(scaled, "jpg", new File("scaled.jpg"));

                BufferedImage image = new BufferedImage(52, 60, BufferedImage.TYPE_INT_RGB);
                Graphics2D g2d = image.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);
                g2d.drawImage(original, 0, 0, 52, 60, this);
                g2d.dispose();

                ImageIO.write(image, "jpg", new File("test.jpg"));

            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        @Override
        public Dimension getPreferredSize() {

            Dimension size = super.getPreferredSize();
            if (original != null) {
                if (scaled != null) {
                    size.width = original.getWidth() + scaled.getWidth();
                    size.height = original.getHeight();
                } else {
                    size.width = original.getWidth();
                    size.height = original.getHeight();
                }
            }

            return size;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            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);

            if (original != null) {
                int x = 0;
                int y = (getHeight() - original.getHeight()) / 2;;
                if (scaled != null) {
                    x = (getWidth() - (original.getWidth() + scaled.getWidth())) / 2;
                } else {
                    x = (getWidth() - original.getWidth()) / 2;
                }
                g2d.drawImage(original, x, y, this);

                if (scaled != null) {
                    x += original.getWidth();
                    y = (getHeight() - scaled.getHeight()) / 2;
                    g2d.drawImage(scaled, x, y, this);
                }
            }
            g2d.dispose();
        }

        public BufferedImage getScaledInstanceToFit(BufferedImage img, Dimension size) {
            float scaleFactor = getScaleFactorToFit(img, size);
            return getScaledInstance(img, scaleFactor);
        }

        public float getScaleFactorToFit(BufferedImage img, Dimension size) {
            float scale = 1f;
            if (img != null) {
                int imageWidth = img.getWidth();
                int imageHeight = img.getHeight();
                scale = getScaleFactorToFit(new Dimension(imageWidth, imageHeight), size);
            }
            return scale;
        }

        public float getScaleFactorToFit(Dimension original, Dimension toFit) {
            float scale = 1f;
            if (original != null && toFit != null) {
                float dScaleWidth = getScaleFactor(original.width, toFit.width);
                float dScaleHeight = getScaleFactor(original.height, toFit.height);
                scale = Math.min(dScaleHeight, dScaleWidth);
            }
            return scale;
        }

        public float getScaleFactor(int iMasterSize, int iTargetSize) {
            float scale = 1;
            if (iMasterSize > iTargetSize) {
                scale = (float) iTargetSize / (float) iMasterSize;
            } else {
                scale = (float) iTargetSize / (float) iMasterSize;
            }
            return scale;
        }

        public BufferedImage getScaledInstance(BufferedImage img, double dScaleFactor) {
            BufferedImage imgBuffer = null;
            imgBuffer = getScaledInstance(img, dScaleFactor, RenderingHints.VALUE_INTERPOLATION_BILINEAR, true);
            return imgBuffer;
        }

        protected BufferedImage getScaledInstance(BufferedImage img, double dScaleFactor, Object hint, boolean higherQuality) {

            int targetWidth = (int) Math.round(img.getWidth() * dScaleFactor);
            int targetHeight = (int) Math.round(img.getHeight() * dScaleFactor);

            int type = (img.getTransparency() == Transparency.OPAQUE)
                            ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;

            BufferedImage ret = (BufferedImage) img;

            if (targetHeight > 0 || targetWidth > 0) {
                int w, h;
                if (higherQuality) {
                    w = img.getWidth();
                    h = img.getHeight();
                } else {
                    w = targetWidth;
                    h = targetHeight;
                }

                do {
                    if (higherQuality && w > targetWidth) {
                        w /= 2;
                        if (w < targetWidth) {
                            w = targetWidth;
                        }
                    }

                    if (higherQuality && h > targetHeight) {
                        h /= 2;
                        if (h < targetHeight) {
                            h = targetHeight;
                        }
                    }

                    BufferedImage tmp = new BufferedImage(Math.max(w, 1), Math.max(h, 1), type);
                    Graphics2D g2 = tmp.createGraphics();
                    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
                    g2.drawImage(ret, 0, 0, w, h, null);
                    g2.dispose();

                    ret = tmp;
                } while (w != targetWidth || h != targetHeight);
            } else {
                ret = new BufferedImage(1, 1, type);
            }
            return ret;
        }
    }
}

您可能会发现Image.getScaledInstance()的危险性也很有意思。


可以尝试一步一步地降低它,而不是一步到位。 - dutchman191
已决定使用java-image-scaling或Scalr来快速缩小图像。谢谢您的建议,这确实是一个好点子。 - dutchman191
3
+1 很好的建议,我认为这篇文章对于OP来说也是一篇很棒的阅读材料。文章名为《Image.getScaledInstance()的危险性》,介绍了缩放图像的解决方案,非常值得一读。 - David Kroukamp

17
您看到的问题实际上与下采样时使用的重采样滤波器有关。显然,您的库使用的一个对于这种情况来说是不好的滤波器。最近邻、双线性和双三次插值是典型的不适合用于下采样的示例。我不知道Photoshop使用的确切重采样滤波器是什么,但我使用了3-lobed lanczos并得到了以下结果:

enter image description here

因此,要解决您的问题,您需要使用更智能的重采样滤波器。

1
正如您在http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6500894中所看到的,这些例程不支持lanczos。但是,正如您可以在http://code.google.com/p/java-image-scaling/(http://code.google.com/p/java-image-scaling/source/browse/trunk/src/main/java/com/mortennobel/imagescaling/Lanczos3Filter.java)中找到的那样,实现Lanczos特别容易。 - mmgp
4
我刚刚让Scalr库使用ULTRA_QUALITY,效果相当不错,图像大小仍然很小。现在需要比较一下两者之间的差异。 - dutchman191
啊,糟糕——我刚才回复了关于imgscalr的评论,没看到这条留言。希望它能很好地为你工作,无论哪个库都是一个很好的选择! - Riyad Kalla
+1和所有这些不好的提到的东西都是Java所拥有的 :( (至少是本地的) - David Kroukamp

13
荷兰人,这就是为什么我维护“imgscalr库”的原因 - 为了使这种事情变得非常容易。
在你的例子中,只需在第一行ImageIO.read之后调用一个方法就可以解决问题。
origImage = ImageIO.read(new File(sourceImg));

你可以按照以下步骤来获取你想要的内容(此方法的javadoc):
origImage = Scalr.resize(origImage, Method.ULTRA_QUALITY, 60);

如果仍然看起来有点锯齿(因为你从图像中删除了很多信息),你可以在命令中添加以下OP来对图像应用轻度抗锯齿滤镜,使其看起来更加平滑。
origImage = Scalr.resize(origImage, Method.ULTRA_QUALITY, 60, Scalr.OP_ANTIALIAS);

那将替换掉你所拥有的所有代码逻辑的其余部分。我唯一推荐的另一件事是将你的非常小的样本保存为PNG格式,这样图像就不会再进行压缩/有损转换,或者如果你真的想要使用JPG格式,确保在JPG上使用尽可能少的压缩。(这里有一篇关于如何做到这一点的文章,它使用了ImageWriteParam类)
imgscalr在Apache 2许可下授权,并托管在GitHub上,所以你可以随心所欲地使用它;如果你在服务器端应用程序中使用该库并且需要排队执行大量的缩放操作,而又不想使服务器负载过重,它还包括异步缩放支持

1
希望关于重采样的研究永远停止,这样你就不必创建一个名为SUPERMEGABLASTER_QUALITY的常量了。说真的,为什么要这样命名? - mmgp
1
@dutchman191,目前还没有接受4个参数的pad方法,我已经将此作为功能请求提交了。 - Riyad Kalla
1
@RiyadKalla 根据所使用的算法命名常量。但现在您将不得不处理向后兼容性,因此在一段时间内(可能是几年),新的常量名称将与已弃用的ULTRA_QUALITY常量并存。 - mmgp
3
有趣的是,昨晚我正在研究其他算法(考虑添加什么内容到5.0版本),并意识到我早期的名称选择因为这个原因而存在问题。我一直倾向于让库隐藏细节,使用起来“非常简单”,但像“双线性”、“双三次”这样的命名以及“卷积核”等概念对新用户来说是可怕的。我不认为我犯了错误,但你的观点很有道理 :) - Riyad Kalla
1
如果事实如此,我建议在常量之间创建别名。例如,文档应该说明ULTRA_QUALITY始终是库中最好的重采样方法(根据某些标准),其实现可能会在将来的版本中更改,但在这个版本中它是METHOD_X的别名。但这只是我使用库时的个人偏好。 - mmgp
显示剩余5条评论

13
正如先前所述,Java的Graphics2D没有提供很好的缩小算法。如果您不想自己实现复杂的算法,可以尝试当前面向此领域的开源库:Thumbnailatorimgscalr以及适用于ImageMagick的Java接口。

在为私人项目进行研究时,我尝试了它们(除了ImageMagick),这是与Photoshop作为参考的可视化结果:

comparison

A. Thumbnailator 0.4.8使用默认设置(无额外内部调整大小)
B. imgscalr 4.2使用ULTRA_QUALTY设置
C. Photoshop CS5双三次滤波器(保存为Web)
D. Graphics2d 使用所有HQ渲染提示

这是使用的代码

Thumbnailator和PS创建了类似的结果,而imgscalr则似乎更加柔和。哪个库创建的结果更好是主观的。还要考虑的另一个因素是性能。虽然Thumbnailator和Graphics2d具有类似的运行时间,但在我的基准测试中,imgscalr较慢(使用ULTRA_QUALITY)

点击此处查看更多详细信息。


感谢提供这个基准测试,非常有帮助。 - affhendrawan

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