Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Grails中调整图像大小_Java_Grails_Image Processing_Frameworks - Fatal编程技术网

Java 在Grails中调整图像大小

Java 在Grails中调整图像大小,java,grails,image-processing,frameworks,Java,Grails,Image Processing,Frameworks,我正在使用Grails开发一个Web相册,对于图像处理,我正在使用Grails图像工具插件。我需要一个功能来调整图像大小,如果上传的图像太大(例如:超过600*840)。在这种情况下,我需要将此图像的大小调整为600*840)。最有效的方法是什么?非常感谢。将java.awt.Image导入为AWTImage import java.awt.Image as AWTImage import java.awt.image.BufferedImage import javax.swin

我正在使用Grails开发一个Web相册,对于图像处理,我正在使用Grails图像工具插件。我需要一个功能来调整图像大小,如果上传的图像太大(例如:超过600*840)。在这种情况下,我需要将此图像的大小调整为600*840)。最有效的方法是什么?非常感谢。

将java.awt.Image导入为AWTImage
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 )
} 
导入java.awt.image.buffereImage 导入javax.swing.ImageIcon 将javax.imageio.imageio作为IIO导入 导入java.awt.Graphics2D 静态调整大小={bytes,out,maxW,maxH-> AWTImage ai=新图像图标(字节)。图像 int width=ai.getWidth(null) int height=ai.getHeight(null) def限制=300..2000 断言limits.contains(宽度)和limits.contains(高度):“图片太小或太大!” float aspectRatio=所需的宽度/高度浮动aspectRatio=最大宽度/最大高度 int-dstW=0 int dstH=0 如果(requiredAspectRatio
使用ImageTool插件。
P.S.仅适用于grails v2。

BuildConfig.groovy
中向

然后,调整图像大小将变成一行:

BufferedImage thumbnail = Scalr.resize(image, 150);

知道它是否与Grails2兼容吗?是否需要从git构建我自己,或者最新的0.1版本是否也可以工作?Don,它将保持图像的质量。因为我正在使用GrailsImageTools插件,但它创建的图像质量非常高poor@Charsee我对图像质量非常满意。我试图使用scalr,但我得到了“没有这样的属性:类的scalr…”错误。我必须进口吗?或者我需要做更多的事情来让它工作是的,你需要导入Scalr类。将以下内容添加到类中的导入列表中,您可以在该类中执行调整大小的
import org.imgscalr.Scalr
@hvgotcodes旧问题,但发生这种情况的原因是,图像一开始并没有真正旋转,它只有EXIF数据指示照片查看器(如浏览器)显示旋转后的图像。一些相机(尤其是手机)这样做,而不是实际旋转图像。由于图像没有真正旋转,而且JavaAPI没有查看EXIF旋转数据,因此图像最终看起来是旋转的,即使实际上不是。下面是一组检查EXIF数据并相应旋转的代码。
BufferedImage thumbnail = Scalr.resize(image, 150);