开发者

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

开发者 https://www.devze.com 2023-03-22 16:49 出处:网络
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 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

精彩评论

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