I want to read the header of a BMP image so that when a person writes the path of an image bmp, it displays:
- the File size in bytes
- the Width in pixels
- the Height in pixels
I'm working on Java and the teacher suggested to use FileInputStream and FileOutputStream.
Here is the Wikipedia article on the Bitmap image format, specifically the file header section.
Wikipedia – BMP file format – Bitmap file header.
The byte offsets for width and height are 18 and 22.
You can use the InputStream#readNBytes method to obtain the first n bytes of data. In this case, n + 4 since the last field is 4 bytes.
You could utilize the ByteBuffer class to convert the numeric values.
I am using the following NASA bitmap image.
NASA – SVS – Index of Frames Files for ID 13326 – frame_0001.bmp (3840×3840).
Output
Another approach is to use bit-wise and shift operations.