I need to change an image with java, so I am using BufferedImage
for this. After I change the image, I will need to save it in BMP format with 600 DPI; however, BufferedImage
defaults the DPI to 72.
I've tried to set the DPI directly on the image, but nothing changed. I referenced this Wikipedia article to change DPI data in the BMP format.
Here is my code used for changing the DPI value.
public static void main(String[] args) throws Exception {
File output = new File("/Users/alex/Desktop/out.bmp");
try (RandomAccessFile f = new RandomAccessFile(output, "rw")) {
f.seek(38);
f.write(1);
f.seek(42);
f.write(1);
}
}
BMP stands for Bitmap which suggests that each pixel value is stored in a grid. This grid is simply read in without any special decompression or interpolation.
BMP images do not change based on the DPI value indicated in the image's metadata but it is nice when the value is accurate. Rather, the DPI is there to help anyone looking at the image to see what the DPI is. Changing this in the image is fruitless because it won't actually change the resolution of your image.
The resolution is determined by the size of your image (how many pixels you have jammed into your image).
Here is a good article on Bitmaps.