13

Grailsを使用してWebアルバムを開発しており、画像処理にはgrails-image-toolsプラグインを使用しています。アップロードされた画像のサイズが大きすぎる場合(たとえば、600 * 840を超える場合)に画像のサイズを変更する機能が必要です。この場合、この画像のサイズを600*840に変更する必要があります。これを行うための最も効率的な方法は何ですか?どうもありがとう。

4

3 に答える 3

14

imgscarBuildConfig.groovyへの依存関係を追加します。

dependencies {
    compile 'org.imgscalr:imgscalr-lib:4.1'     
}

次に、画像のサイズ変更がワンライナーになります。

BufferedImage thumbnail = Scalr.resize(image, 150);
于 2012-08-30T20:07:45.467 に答える
8
import java.awt.Image as AWTImage 
import java.awt.image.BufferedImage      
import javax.swing.ImageIcon 
import javax.imageio.ImageIO as IIO  
import java.awt.Graphics2D


static resize = { bytes, out, maxW, maxH -> 
    AWTImage ai = new ImageIcon(bytes).image 
    int width = ai.getWidth( null ) 
    int height = ai.getHeight( null )

    def limits = 300..2000 
    assert limits.contains( width ) && limits.contains( height ) : 'Picture is either too small or too big!'

    float aspectRatio = width / height float requiredAspectRatio = maxW / maxH

    int dstW = 0 
    int dstH = 0 
    if (requiredAspectRatio < aspectRatio) { 
        dstW = maxW dstH = Math.round( maxW / aspectRatio) 
    } else { 
        dstH = maxH dstW = Math.round(maxH * aspectRatio) 
    }

    BufferedImage bi = new BufferedImage(dstW, dstH,   BufferedImage.TYPE_INT_RGB)            
    Graphics2D g2d = bi.createGraphics() g2d.drawImage(ai, 0, 0, dstW, dstH, null, null) 

    IIO.write( bi, 'JPEG', out )
} 
于 2008-12-12T10:28:49.793 に答える
2

ImageTool プラグインを使用します。 https://grails.org/plugin/image-tools PS grails v2 でのみ使用できます。

于 2009-01-06T08:13:27.167 に答える