使用com.sun.imageio.plugins.png.PNGMetadata读取图片的元数据
2018-10-20 14:30
591 查看
所谓图片元数据,就是除了我们肉眼看到的图片内容外,隐藏在这些内容背后的一些技术数据。
本文介绍如何使用Java代码将一张图片的隐藏信息读取出来。
首先不需要下载任何额外的Java库,用JDK自带的库就能工作。
import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import javax.imageio.ImageIO; import javax.imageio.ImageReader; import javax.imageio.metadata.IIOMetadata; import javax.imageio.metadata.IIOMetadataNode; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import com.sun.imageio.plugins.png.PNGMetadata; 新建一个Java类,这个类的main方法也是非常直接的: static public void main(String[] arg) throws IOException{ byte[] content = getContent("C:\Users\i042416\Desktop\test\clipboard1.png"); readCustomData(content); }
首先把桌面上名叫clipboard1.png的图片文件的内容读到字节数组content中。
getContent方法的代码:
一张png图片的元数据,散布在下面这些节点里:
printNode(pngmeta.getStandardChromaNode()); printNode(pngmeta.getStandardCompressionNode()); printNode(pngmeta.getStandardDataNode()); printNode(pngmeta.getStandardDimensionNode()); printNode(pngmeta.getStandardDocumentNode()); printNode(pngmeta.getStandardTextNode()); printNode(pngmeta.getStandardTransparencyNode());
通过printNode打印出来:
printNode方法的源代码:
打印出来的元数据:
如果大家想要复制粘贴,这是全部的源代码:
package image; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import javax.imageio.ImageIO; import javax.imageio.ImageReader; import javax.imageio.metadata.IIOMetadata; import javax.imageio.metadata.IIOMetadataNode; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import com.sun.imageio.plugins.png.PNGMetadata; public class pngTest { static private byte[] getContent(String filePath) throws IOException { File file = new File(filePath); long fileSize = file.length(); if (fileSize > Integer.MAX_VALUE) { System.out.println("file too big..."); return null; } FileInputStream fi = new FileInputStream(file); byte[] buffer = new byte[(int) fileSize]; int offset = 0; int numRead = 0; while (offset < buffer.length && (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) { offset += numRead; } if (offset != buffer.length) { fi.close(); throw new IOException("Could not completely read file " + file.getName()); } fi.close(); return buffer; } static private void readCustomData(byte[] imageData) throws IOException{ ImageReader imageReader = ImageIO.getImageReadersByFormatName("png").next(); imageReader.setInput(ImageIO.createImageInputStream(new ByteArrayInputStream(imageData)), true); IIOMetadata metadata = imageReader.getImageMetadata(0); PNGMetadata pngmeta = (PNGMetadata) metadata; printNode(pngmeta.getStandardChromaNode()); printNode(pngmeta.getStandardCompressionNode()); printNode(pngmeta.getStandardDataNode()); printNode(pngmeta.getStandardDimensionNode()); printNode(pngmeta.getStandardDocumentNode()); printNode(pngmeta.getStandardTextNode()); printNode(pngmeta.getStandardTransparencyNode()); } static private void printNode(IIOMetadataNode metanode){ if (metanode == null) return; NodeList childNodes = metanode.getChildNodes(); if( childNodes == null) return; for (int i = 0; i < childNodes.getLength(); i++) { Node node = childNodes.item(i); NamedNodeMap attribute = node.getAttributes(); if( attribute == null) continue; int length = attribute.getLength(); for( int j = 0; j < length; j++){ Node each = attribute.item(j); String value = each.getNodeValue(); String name = each.getNodeName(); System.out.println("Name: " + name + " value: " + value); } } } static public void main(String[] arg) throws IOException{ byte[] content = getContent("C:\Users\i042416\Desktop\test\clipboard1.png"); readCustomData(content); } }
要获取更多Jerry的原创文章,请关注公众号"汪子熙":
相关文章推荐
- 读取图片信息(exif),使用com.drew.metadata.Metadata
- 读取图片信息(exif),使用com.drew.metadata.Metadata
- 使用Python读取SQLServer数据库的image格式数据并导出为png图片
- c++使用std_image.c来加载jpg,png,gif等格式图片
- png图片的简单介绍, 使用libpng读取内容,用于纹理贴图
- WPF中使用InPlaceBitmapMetadataWriter在图片中插入元数据(metadata)
- 使用 metadata-extractor 解析图片 exif 元数据信息
- ImageView使用和从内存读取图片显示
- Java读取图片Metadata元数据Exif
- 图像处理3_使用 metadata-extractor 解析图片 exif 元数据信息
- 从 png图片读取raw image data
- OpenGL使用libPng读取png图片
- PictureFilter——图像过滤类RGBImageFilter及图片文件读写类ImageIO的使用
- 使用libpng和GDI读取显示png图片
- 使用ANT编译项目报错 com.sun.image.codec.jpeg does not exist 解决方法
- 使用ANT编译项目报错 com.sun.image.codec.jpeg does not exist 解决方法
- ImageIO.read()方法读取图片后重写,图片蒙上一层红色的解决办法(上)
- 使用 metadata-extractor 解析图片 exif 元数据信息
- Android中读取图片EXIF元数据之metadata-extractor的使用
- ImageIO.read()方法读取,截取图片后重写,图片蒙上一层红色的解决 后感