1

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

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

1 回答 1

0

Adding Dpi to a blurry picture does not make it sharper, because the pixels of your image just split, but there wont be any more detail created in the image. there are however some algorithms that help make pictures look more sharp.

于 2012-11-21T10:43:52.257 回答