I'd just read the file into a byte using a FileInputStream, and write a few routines to compute the location of the bit corresponding to given x, y coordinates. You'd have to compute which byte the bit would be in, then what position within that byte, and then use the bitwise "&" operator to test its value.
There is now a default BMP decoder in the Java Image I/O API. I nearly wrote one myself, but that was a long time ago now - before they added it to the standard API! Having said that, I seem to remember that the bitmap format is pretty self-explanatory anyway. If you were to write your own decoder, probably the biggest problem you'd face is dealing with the bit format because (if I remember correctly), Bitmap is little-endian whereas Java is big-endian. So you'd need to flip all the bytes around - but NIO will do that for you. Basically, once you've figured out how to skip the headers (which wouldn't be too hard), the rest of the data is just the bits which make up the image so you can extract them directly.
Anyway, you can make use of the ImageIO API (javax.imageio) which can work with JPEG, PNG, BMP and GIF as standard.
Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / AmazonAmazon UK )
Joined: Jul 12, 2006
Thanks, it's great to know the API has that - I hate to reinvent wheels!
Originally posted by Charles Lyons: ... Bitmap is little-endian whereas Java is big-endian. So you'd need to flip all the bytes around...
Java is big-endian?! I've never heard about that. Java is not endian at all. The endianness depends on the platform it's running on. You don't know (and you don't need to know) how the JVM stores an int, long or other data type in memory exactly.
What made you think that Java is big-endian and can you show a piece of code that demonstrates this?
In Windows BMP files, pixel values are usually stored in the order B, G, R (not R, G, B), but that doesn't really have anything to do with endianness. [ July 18, 2006: Message edited by: Jesper Young ]