为什么在Java中缩小后图像看起来很糟糕? [英] Why does this image look so bad after being scaled down in Java?

查看:124
本文介绍了为什么在Java中缩小后图像看起来很糟糕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是原始图片: http://rank.my/public/ images / uploaded / orig-4193395691714613396.png



此处缩小至300x225:

http://rank.my/public/images/uploaded/norm-4193395691714613396 .png



此处缩小至150x112:

http://rank.my/public/images/uploaded/small-4193395691714613396.png



正如您所看到的,300x225看起来非常糟糕,而150x112看起来很糟糕。这里是我用来缩小图像的代码:

  private static BufferedImage createResizedCopy(final BufferedImage source,final int destWidth ,
final int destHeight){
final BufferedImage resized = new BufferedImage(destWidth,destHeight,source.getType());
final Graphics2D bg = resized.createGraphics();
bg.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
bg.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
bg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
final float sx =(float)destWidth / source.getWidth();
final float sy =(float)destHeight / source.getHeight();
bg.scale(sx,sy);
bg.drawImage(source,0,0,null);
bg.dispose();
返回调整大小;
}

我在这里做错了什么?图像缩放不一定要特别快,质量绝对是速度优先。我是否使用了错误的技术?

解决方案

有三种方法可以解决缩小问题。首先是分多步进行,每步减少不超过75%。第二种是在调整大小之前模糊图像;它越缩小,你越需要模糊。第三种方法是使用一种方法,该方法使用多于2×2到4×4像素块进行过滤,这些像素块是由朴素双线性或双三次插值方法使用的。随着收缩因子变大,过滤器使用的像素块也应该变大,否则会出现锯齿伪像,如您在此处看到的那样。


Here is the original image: http://rank.my/public/images/uploaded/orig-4193395691714613396.png

And here it is scaled down to 300x225:

http://rank.my/public/images/uploaded/norm-4193395691714613396.png

And here it is scaled down to 150x112:

http://rank.my/public/images/uploaded/small-4193395691714613396.png

As you can see, 300x225 looks pretty bad, and 150x112 looks awful. Here is the code I'm using to scale down the image:

private static BufferedImage createResizedCopy(final BufferedImage source, final int destWidth,
        final int destHeight) {
    final BufferedImage resized = new BufferedImage(destWidth, destHeight, source.getType());
    final Graphics2D bg = resized.createGraphics();
    bg.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    bg.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    bg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    final float sx = (float) destWidth / source.getWidth();
    final float sy = (float) destHeight / source.getHeight();
    bg.scale(sx, sy);
    bg.drawImage(source, 0, 0, null);
    bg.dispose();
    return resized;
}

What am I doing wrong here? The image scaling doesn't have to be especially fast, quality is definitely a priority over speed. Am I using the wrong technique?

解决方案

There are three ways to fix the downscaling problem. First is to do it in multiple steps, with no more than a 75% reduction in each step. The second is to blur the image before resizing; the more it shrinks, the more you'll have to blur. The third way is to use a method that filters using more than the 2x2 to 4x4 pixel blocks used by a naive bilinear or bicubic interpolation method. As the shrink factor grows larger, so should the pixel block used by the filter, otherwise you get aliasing artifacts as you've seen here.

这篇关于为什么在Java中缩小后图像看起来很糟糕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆