如何增加Image DPI?

时间:2012-11-21 10:23:26

标签: image dpi

我将图像的DPI从96 DPI增加到299.99 DPI。但它对图像没有任何影响。缩放时图像仍然模糊。如何增加DPI以使图像清晰度增加?

String dotsPerMeter = String.valueOf((int)(300 / 0.0254)); // 300是所需的dpi 迭代器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;
            }

1 个答案:

答案 0 :(得分:0)

将Dpi添加到模糊的图片不会使其更清晰,因为图像的像素只是分割,但图像中不再创建任何细节。 然而,有一些算法可以让图片看起来更清晰。