Java 修改png图片dpi值
时间: 2023-08-05 07:06:25 浏览: 66
要修改 PNG 图片的 DPI 值,可以使用 Java 的 ImageIO 类库和 Apache 的 Sanselan 库。
下面是一个示例代码,它可以读取 PNG 图片、修改 DPI 值,并将修改后的图片保存到指定的文件路径:
```java
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.apache.commons.imaging.ImageFormat;
import org.apache.commons.imaging.ImageReadException;
import org.apache.commons.imaging.ImageWriteException;
import org.apache.commons.imaging.Imaging;
import org.apache.commons.imaging.common.IImageMetadata;
import org.apache.commons.imaging.common.RationalNumber;
import org.apache.commons.imaging.common.RationalNumberUtilities;
import org.apache.commons.imaging.formats.png.PngImageMetadata;
import org.apache.commons.imaging.formats.png.PngTextMetadata;
import org.apache.commons.imaging.formats.png.chunks.PngChunkType;
import org.apache.commons.imaging.formats.png.chunks.PngHeaderChunk;
import org.apache.commons.imaging.formats.png.chunks.PngTextChunk;
public class ModifyPNGDPI {
public static void main(String[] args) throws ImageReadException, IOException, ImageWriteException {
// 读取 PNG 图片
BufferedImage image = ImageIO.read(new File("input.png"));
// 获取 PNG 图片的元数据
IImageMetadata metadata = Imaging.getMetadata(image, null);
// 如果是 PNG 图片,就修改 DPI 值
if (metadata instanceof PngImageMetadata) {
PngImageMetadata pngMetadata = (PngImageMetadata) metadata;
// 获取 DPI 值
RationalNumber xPixelSize = pngMetadata.getPhysicalPixelWidth();
RationalNumber yPixelSize = pngMetadata.getPhysicalPixelHeight();
// 修改 DPI 值为 300
RationalNumber newPixelSize = RationalNumberUtilities.getRationalNumber(300);
// 更新元数据
pngMetadata.setPhysicalPixelWidth(newPixelSize);
pngMetadata.setPhysicalPixelHeight(newPixelSize);
// 更新文本元数据
PngHeaderChunk headerChunk = pngMetadata.getPngHeader();
PngTextChunk[] textChunks = headerChunk.getTextChunks();
for (PngTextChunk textChunk : textChunks) {
if (textChunk.getChunkType().equals(PngChunkType.tEXt)) {
PngTextMetadata textMetadata = new PngTextMetadata(textChunk.getKeyword(), textChunk.getText());
if (textMetadata.getKeyword().equals("pHYs")) {
textMetadata.setValue("x_pixels_per_unit", "300");
textMetadata.setValue("y_pixels_per_unit", "300");
textMetadata.setValue("unit_specifier", "1");
headerChunk.removeChunk(textChunk);
headerChunk.addChunk(new PngTextChunk(textMetadata.getKeyword(), textMetadata.getText()));
break;
}
}
}
}
// 保存修改后的 PNG 图片
ImageIO.write(image, "png", new File("output.png"));
}
}
```
上面的代码使用了 Apache 的 Sanselan 库来修改 PNG 图片的 DPI 值。如果你不想使用这个库,也可以使用 Java 自带的 ImageIO 类库来修改 DPI 值。具体的方法是创建一个新的 PNG 图片,将原图片绘制到新图片上,并设置新图片的 DPI 值。这种方法的代码如下:
```java
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ModifyPNGDPI {
public static void main(String[] args) throws IOException {
// 读取 PNG 图片
BufferedImage image = ImageIO.read(new File("input.png"));
// 创建新的 PNG 图片
BufferedImage newImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
// 绘制原图片到新图片上
Graphics2D g = newImage.createGraphics();
g.drawImage(image, 0, 0, null);
// 设置新图片的 DPI 值
int dpi = 300;
int dotsPerMeter = (int) Math.round(dpi / 0.0254);
newImage.setDPI(dotsPerMeter, dotsPerMeter);
// 保存修改后的 PNG 图片
ImageIO.write(newImage, "png", new File("output.png"));
}
}
```