所谓图片元数据,就是除了我们肉眼看到的图片内容外,隐藏在这些内容背后的一些技术数据。

本文介绍如何使用Java代码将一张图片的隐藏信息读取出来。

首先不需要下载任何额外的Java库,用JDK自带的库就能工作。

importjava.io.ByteArrayInputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.IOException;importjavax.imageio.ImageIO;importjavax.imageio.ImageReader;importjavax.imageio.metadata.IIOMetadata;importjavax.imageio.metadata.IIOMetadataNode;importorg.w3c.dom.NamedNodeMap;importorg.w3c.dom.Node;importorg.w3c.dom.NodeList;importcom.sun.imageio.plugins.png.PNGMetadata;新建一个Java类,这个类的main方法也是非常直接的:staticpublicvoidmain(String[]arg)throwsIOException{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方法的源代码:

打印出来的元数据:

如果大家想要复制粘贴,这是全部的源代码:

packageimage;importjava.io.ByteArrayInputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.IOException;importjavax.imageio.ImageIO;importjavax.imageio.ImageReader;importjavax.imageio.metadata.IIOMetadata;importjavax.imageio.metadata.IIOMetadataNode;importorg.w3c.dom.NamedNodeMap;importorg.w3c.dom.Node;importorg.w3c.dom.NodeList;importcom.sun.imageio.plugins.png.PNGMetadata;publicclasspngTest{staticprivatebyte[]getContent(StringfilePath)throwsIOException{Filefile=newFile(filePath);longfileSize=file.length();if(fileSize>Integer.MAX_VALUE){System.out.println("filetoobig...");returnnull;}FileInputStreamfi=newFileInputStream(file);byte[]buffer=newbyte[(int)fileSize];intoffset=0;intnumRead=0;while(offset<buffer.length&&(numRead=fi.read(buffer,offset,buffer.length-offset))>=0){offset+=numRead;}if(offset!=buffer.length){fi.close();thrownewIOException("Couldnotcompletelyreadfile"+file.getName());}fi.close();returnbuffer;}staticprivatevoidreadCustomData(byte[]imageData)throwsIOException{ImageReaderimageReader=ImageIO.getImageReadersByFormatName("png").next();imageReader.setInput(ImageIO.createImageInputStream(newByteArrayInputStream(imageData)),true);IIOMetadatametadata=imageReader.getImageMetadata(0);PNGMetadatapngmeta=(PNGMetadata)metadata;printNode(pngmeta.getStandardChromaNode());printNode(pngmeta.getStandardCompressionNode());printNode(pngmeta.getStandardDataNode());printNode(pngmeta.getStandardDimensionNode());printNode(pngmeta.getStandardDocumentNode());printNode(pngmeta.getStandardTextNode());printNode(pngmeta.getStandardTransparencyNode());}staticprivatevoidprintNode(IIOMetadataNodemetanode){if(metanode==null)return;NodeListchildNodes=metanode.getChildNodes();if(childNodes==null)return;for(inti=0;i<childNodes.getLength();i++){Nodenode=childNodes.item(i);NamedNodeMapattribute=node.getAttributes();if(attribute==null)continue;intlength=attribute.getLength();for(intj=0;j<length;j++){Nodeeach=attribute.item(j);Stringvalue=each.getNodeValue();Stringname=each.getNodeName();System.out.println("Name:"+name+"value:"+value);}}}staticpublicvoidmain(String[]arg)throwsIOException{byte[]content=getContent("C:\Users\i042416\Desktop\test\clipboard1.png");readCustomData(content);}}

要获取更多Jerry的原创文章,请关注公众号"汪子熙":