0

I'm building a crawler and i need to get the height and the width of a remote image (or multiple images). I'm already doing that with threads - but still this is the task consuming most of the time:

//Start threadppol
            ExecutorService imageExecutorService = null;
            List<Future<ImageModel>> futureImages = null;
            if (imageSize > 0) {
                imageExecutorService = Executors.newFixedThreadPool(4);
                futureImages = new ArrayList<>();
                int counter=0;
                for (Image imgUrl : imgUrls) {
                    Callable<ImageModel> callable = new ExtractImages(imgUrl,hostUrl);
                    Future<ImageModel> future = imageExecutorService.submit(callable);
                    futureImages.add(future);
                    if(counter>=4){
                        break;
                    }
                    counter++;
                }
            }

ExtractImages class:

 // Get the image
 BufferedImage image = ImageIO.read(new URL(hostUrl, extractedImage.getSrc()));

So i'm using kind of the standard way to do it in Java (ImageIO.read(url)). The question is, is there a better way to get the height and with of an image from an imageurl?

Fabian Lurz
  • 2,029
  • 6
  • 26
  • 52
  • Has to be. If the image file didn't encode that somehow somewhere, how could it ever be re-created from data? That being said, I don't know anything on the specifics – ControlAltDel Sep 16 '15 at 18:52
  • Made an edit - hope i made it clearer. What i want is to retrieve the height and width of an image from an image url. ImageIO.read(url) is too slow - and this is probably because of the response time needed (from my application to the image host). But still - i would like to know if there is a better way. – Fabian Lurz Sep 16 '15 at 18:55
  • So you want to get height and width of a remote image (remote resource)? – afrin216 Sep 16 '15 at 18:57
  • Exactly yes - but without reading inline styles (img src "" width=365). – Fabian Lurz Sep 16 '15 at 18:59
  • 2
    The information you need is in the image header, so it's unnecessary to read the image data. Check out `ImageReader.getImageMetadata()` or `ImageReader.getWidth/Height()`. – martinez314 Sep 16 '15 at 19:03
  • Ahh - sounds promising :) Will try that! Maybe write it as an answer so you get credit for it! – Fabian Lurz Sep 16 '15 at 19:04

0 Answers0