This discussion is archived
5 Replies Latest reply: Nov 28, 2012 12:10 AM by 975635 RSS

How to increase Image DPI?

975635 Newbie
Currently Being Moderated
Hi All,

I increased the DPI of a image to 299.99 DPI from 96 DPI. It displays 299.99 DPI in the image property but it doesnt have any effect on the image clarity. The image is still blur on zoom. How to increase the DPI so that the image clarity increases?

I have used the below logic -

String dotsPerMeter = String.valueOf((int) (300 / 0.0254));//300 is the dpi required
Iterator imageWriters = ImageIO.getImageWritersByFormatName("png");
while (imageWriters.hasNext()) {
ImageWriter iw = (ImageWriter)imageWriters.next();

ImageWriteParam iwp = iw.getDefaultWriteParam();
IIOMetadata metadata = iw.getDefaultImageMetadata(new ImageTypeSpecifier(image), iwp);

String pngFormatName = metadata.getNativeMetadataFormatName();
IIOMetadataNode pngNode =
(IIOMetadataNode) metadata.getAsTree(pngFormatName);

IIOMetadataNode physNode = null;
NodeList childNodes = pngNode.getElementsByTagName("pHYs");
if (childNodes.getLength() == 0) {
physNode = new IIOMetadataNode("pHYs");
pngNode.appendChild(physNode);
} else if (childNodes.getLength() == 1) {
physNode = (IIOMetadataNode) childNodes.item(0);
} else {
throw new IllegalStateException("Don't know what to do with multiple pHYs nodes");
}
physNode.setAttribute("pixelsPerUnitXAxis", dotsPerMeter);
physNode.setAttribute("pixelsPerUnitYAxis", dotsPerMeter);
physNode.setAttribute("unitSpecifier", "meter");
try {
metadata.setFromTree(pngFormatName, pngNode);
IIOImage iioImage = new IIOImage(image, null, metadata);
File file = new File(KIT_ID + "_" + KIT_VER + "_newDPI.png");
ImageOutputStream ios = ImageIO.createImageOutputStream(file);

iw.setOutput(ios);
iw.write(iioImage);
ios.flush();
ios.close();
} catch (Exception e) {
e.printStackTrace();
continue;
}

break;
}

Please someone, help me solve this.

Thanks a ton.
  • 1. Re: How to increase Image DPI?
    rp0428 Guru
    Currently Being Moderated
    >
    I increased the DPI of a image to 299.99 DPI from 96 DPI. It displays 299.99 DPI in the image property but it doesnt have any effect on the image clarity. The image is still blur on zoom. How to increase the DPI so that the image clarity increases?
    >
    Prove that your image actually has more than 96 DIP. You can't get a higher resolution than the image has to begin with so without knowing what the resolution of the original image is there is no way to know if you can change the DPI without blurring things.
  • 2. Re: How to increase Image DPI?
    Kayaman Guru
    Currently Being Moderated
    Are you working for the CSI?
  • 3. Re: How to increase Image DPI?
    975635 Newbie
    Currently Being Moderated
    No i don't work for the CSI.

    Ok let me be more clear of my requirement.

    I create an image using java code. It basically text, lines, color and borders. When it gets created, its of 96 DPI. My requirement is to retain the size of the image and increase the DPIs to 300 or more so that the clarity of the image is not lost when zoomed.

    please suggest me a way to achieve it.
  • 4. Re: How to increase Image DPI?
    EJP Guru
    Currently Being Moderated
    When it gets created, its of 96 DPI.
    So change that so when it gets created it is 300 DPI. Afterwards it is too late.
  • 5. Re: How to increase Image DPI?
    975635 Newbie
    Currently Being Moderated
    Yes, i have tried that using the below method -

    private void setDPI(IIOMetadata metadata) throws IIOInvalidTreeException {

    // for PNG, it's dots per millimeter
    double dotsPerMilli = 1.0 * 300 / 10 / 2.54;

    IIOMetadataNode horiz = new IIOMetadataNode("HorizontalPixelSize");
    horiz.setAttribute("value", Double.toString(dotsPerMilli));

    IIOMetadataNode vert = new IIOMetadataNode("VerticalPixelSize");
    vert.setAttribute("value", Double.toString(dotsPerMilli));

    IIOMetadataNode dim = new IIOMetadataNode("Dimension");
    dim.appendChild(horiz);
    dim.appendChild(vert);

    IIOMetadataNode root = new IIOMetadataNode("javax_imageio_1.0");
    root.appendChild(dim);

    metadata.mergeTree("javax_imageio_1.0", root);
    }


    This method shows the DPI of the image as 299.99, but there is no real impact to the image as it looks. It loses clarity when zoomed. There is no difference between the original one and the modified one.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points