Changing DPI of BMP image in Java

I need to change the image using java, so I am using BufferedImage

. After I change the image, I will need to save it in BMP format with 600 DPI; however BufferedImage

, the default DPI is 72.

I tried to set DPI directly on the image, but nothing changed. I referenced this Wikipedia article for changing DPI data in BMP format.

Here is my code used to change 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);
    }
}

      

+3


source to share


1 answer


BMP stands for Bitmap, which assumes that each pixel value is stored in a grid. This grid is simply read without any special decompression or interpolation.

BMP images do not change based on the DPI value specified in the image metadata, but it's nice when the value is accurate. Rather, DPI is there to help anyone looking at an image see what DPI is. Changing this in an image is fruitless because it won't actually change the resolution of your image.



Resolution is determined by the size of your image (the number of pixels you jammed in your image).

Here is a good article on bitmaps.

+2


source







All Articles