2

I am using JSOUP to extract some information from given URL. My target is to get all image URL that are having minimum width W and min height H.

The problem is many site just use src but no height and width attribute. SO the only solution that i found was to use BufferedImage to get the image height and width of image but I think its not good choice if you have huge number of image

Is there some other way to get image height and width without creating the image first.

Manish Kumar
  • 10,214
  • 25
  • 77
  • 147

2 Answers2

5

Try this:

String location = "http://www.sxc.hu/pic/l/f/fa/fangol/1301370_97011830.jpg";
InputStream stream = new URL(location).openStream();
Object obj = ImageIO.createImageInputStream(stream);
ImageReader reader = ImageIO.getImageReaders(obj).next();
System.out.println(next.getWidth(0));
stream.close();

This is high-productive in reading width and height in every jdk.

Grim
  • 1,938
  • 10
  • 56
  • 123
0

(I am late to game, but anyway)If you have used JSOUP then you can measure it with JSOUP. So let say have fetched it with following way:

This is your JSOUP fetcher (you decide how to create it):

Document document=Jsoup.connect(your_url).get();
Elements elements=document.select("img[src]");
int length=elements.size();

And then just use this to get heights:

for(int i=0;i<length;i++){
 String height=elements.get(i).attr("height");
 System.out.println(height);
}
Ankur
  • 50,282
  • 110
  • 242
  • 312
Farid
  • 2,317
  • 1
  • 20
  • 34