操作PNG图片的Chunks

本文介绍了如何利用commons-imaging库来读取和插入PNG图片的metadata。通过一个简单的练习类,展示了如何操作图片的Chunks,实现元数据的读取和保存到新图片中。
摘要由CSDN通过智能技术生成

在学习如何向png图片中插入metaData 时候,发现了一个工具包,很好用,commons-imaging-1.0, 可以到网上搜索下载jar包。

这里写了一个简短的练习类,来学习, 实现的功能是读取图片的metadata,并插入到新图片中。

 

public static byte[] imageWriteExample(final File file)
			throws ImageReadException, ImageWriteException, IOException {
		// read image
		final BufferedImage image = Imaging.getBufferedImage(file);

		final ImageFormat format = ImageFormats.PNG;
		ImageInfo imageInfo = Imaging.getImageInfo(file);
		List<String> allComments = imageInfo.getComments();
		List newComments = new ArrayList();
		for (String items : allComments) {
			System.out.println(items);
			String[] item = items.split(":");
			if (item[0].equals("view_nodes")) {
				newComments.add(new PngText.Itxt(item[0], item[1], "utf-8", ""));
			} else {
				newComments.add(new PngText.Text(item[0], item[1]));
			}
		}

		final Map<Stri
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要修改 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")); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值