在Grails中调整图像大小

在Grails中调整图像大小,第1张

在Grails中调整图像大小
import java.awt.Image as AWTImage import java.awt.image.BufferedImage      import javax.swing.ImageIcon import javax.imageio.ImageIO as IIO  import java.awt.Graphics2Dstatic 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 )}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5601194.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存