I am a software engineer at Google on the Android project and the creator of the Java testing framework TestNG. When I'm not updating this weblog with various software-related posts or speaking at conferences, I am busy snowboarding, playing squash, tennis, golf or volleyball or scuba diving. Cedric is a DZone MVB and is not an employee of DZone and has posted 84 posts at DZone. You can read more from them at their website. View Full User Profile

Coding Challenge: Fast Width/Height of an Image

01.21.2014
| 3437 views |
  • submit to reddit

I am trying to find out the width and height of an image (a URL) transferring as few bytes as possible. This coding challenge has two separate parts:

  • Finding the cheapest way to retrieve width and height.
  • Verifying that the solution is indeed cheap.

The first part is not that interesting since it’s a fairly well documented problem. A bit too documented, actually, since I found several solutions:

This last solution looks pretty efficient from a network perspective but I’m worried it might leave out a few corner cases in image handling. The first two solutions are probably battle tested and very robust but I have no idea how effective they are from a network standpoint (their Javadoc doesn’t describe the specifics of how they operate).

So… Do you have a better solution? And whether you do or don’t, how would you assess how many bytes these implementations transfer?

Published at DZone with permission of Cedric Beust, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Dawid Solecki replied on Tue, 2014/01/21 - 4:35pm

I'm not sure if I got the problem right, but to get info on how much data is actually read just wrap the InputStreams with CountingInputStream from Apache commons (or Google). Checked it for the third solution with some images from various internet locations and the stream never got above 150 bytes.

Ronald Muller replied on Tue, 2014/01/21 - 12:55pm

What about http://th-schwarz.github.io/JII/? Also see the referenced ImageInfo library

Ronald

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.