2013-11-25 43 views
1

我在使用java.awt.Graphics2D调整一些图像大小的grails项目上工作。 我正在做一个调整大小,以有5个尺寸。最小尺寸的宽度为:77,高度为58. 问题是,对于这个尺寸,调整大小的图片的质量非常糟糕。 我知道ImageMagic,但我不能改变它,我坚持一些Java库。 这里是我的一段代码:groovy更好的质量图像调整大小

def img = sourceImage.getScaledInstance(77, 58, Image.SCALE_SMOOTH) 
BufferedImage bimage = new BufferedImage(77, 58, BufferedImage.TYPE_INT_RGB) 
Graphics2D bGr = bimage.createGraphics() 
bGr.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY) 
bGr.drawImage(img, 0, 0, null) 
bGr.dispose() 

我试过型动物的提示,但不改变质量。 我们有一个iOS应用程序,我们真的需要有清晰的图片。 有没有人有任何想法如何提高图片质量?

+1

得到[这可能是感兴趣(https://today.java。 net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html) –

回答

2

所以,munging the code half way down that link到Groovy中,我们得到:

import java.awt.image.* 
import java.awt.* 
import static java.awt.RenderingHints.* 
import javax.imageio.* 

BufferedImage getScaledInstance(image, int nw, int nh, hint) { 
    int type = (image.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB 
    int w = image.width 
    int h = image.height 

    while(true) { 
     if(w > nw) { 
      w /= 2 
      if(w < nw) { 
       w = nw 
      } 
     } 
     if(h > nh) { 
      h /= 2 
      if(h < nh) { 
       h = nh 
      } 
     } 
     image = new BufferedImage(w, h, type).with { ni -> 
      ni.createGraphics().with { g -> 
       g.setRenderingHint(KEY_INTERPOLATION, hint) 
       g.drawImage(image, 0, 0, w, h, null) 
       g.dispose() 
       ni 
      } 
     } 
     if(w == nw || h == nh) { 
      return image 
     } 
    } 
} 

def img = ImageIO.read('https://raw.github.com/grails/grails-core/master/media/logos/grails-logo-highres.jpg'.toURL()) 
int newWidth = img.width/20 
int newHeight = img.height/20 
BufferedImage newImage = getScaledInstance(img, newWidth, newHeight, VALUE_INTERPOLATION_BILINEAR) 

这是最好的,我可以与Java/Groovy的

+0

谢谢。尝试和相同的结果:质量差的图片 –

+0

@CC。是否有您想要调整大小的图片的公开网址,以便我可以测试一些内容? –

+0

非对不起,无法分享图像。 –

相关问题