开发者

How to get the image size (width and height)?

I am new to grails. I spent fare amount of time in search of finding th开发者_如何学运维e image size using grails but end up with nothing. I want to find the image size (width and height) using grails by just giving path of the image. Is it possible using grails? if yes, how? Please suggest me easy way to find the image size.

Thanks in advance, Venkatesh


Supposing you have an image in C:\tmp, this would do it:

import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
...

def img = ImageIO.read(new File("C:\\tmp\\cherry.jpg"));
println("Width:"+img.getWidth()+" Height:"+img.getHeight());

ImageIO also supports reading from a URL: see ImageIO javadoc


Thanks to the answer of @Nicolas Modrzyk I have been able to write a one liner for the bash.

java -jar groovy-all.jar -e 'javax.imageio.ImageIO.read(new File(args[0])).with { println "width: $width, height: $height" }' -- fancyImage.jpg

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜