【问题标题】:groovy better quality image resizinggroovy 更好的质量图像调整大小
【发布时间】:2013-12-10 06:34:51
【问题描述】:

我正在使用 java.awt.Graphics2D 进行一些图像大小调整的 grails 项目。 我正在调整大小以获得 5 个尺寸。最小的尺寸有宽度:77 和高度:58。 问题是,对于这个尺寸,调整后的图片质量真的很差。 我知道 ImageMagic 但我无法更改它,我被一些 java 库所困扰。 这是我的一段代码:

 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 应用程序,我们真的需要有清晰的图片。 有人知道如何提高图片质量吗?

【问题讨论】:

标签: image image-processing groovy image-resizing


【解决方案1】:

所以,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 中我能得到的最好的方法是什么

【讨论】:

  • 谢谢。尝试了同样的结果:图片质量很差
  • @CC。是否有您尝试调整大小的图片的公共 URL,以便我可以测试一些内容?
  • 非,抱歉,无法分享图片。
  • @CC。也许给imgscalr a go
猜你喜欢
  • 2014-02-17
  • 2021-02-15
  • 2011-01-24
  • 1970-01-01
  • 2010-12-10
  • 2013-02-15
  • 2020-08-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多