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:
- A naïve version using ImageIO.
- A more advanced solution using ImageReaders.
- A hand rolled solution written by Jaimon Matthew.
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?
#1 by Frank De prins on January 21, 2014 - 10:28 pm
http://th-schwarz.github.io/JII/
#2 by PJ on February 7, 2014 - 5:58 am
Off the cuff fast approach that (sadly) may not be supported by many web servers (though if you have control of the web server you can fix that, of course): use file extension to detect the image type then use the HTTP ‘Content-Range’ request to pull just the bytes that encode the image size.