使用com.sun.imageio.plugins.png.PNGMetadata读取图片的元数据

  

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

  

  本文介绍如何使用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类,这个类的主方法也是非常直接的:static  public  void 主要(String[],参数),throws  IOException {byte [], content =, getContent(用户“C: \ \ i042416 \桌面\ \ clipboard1.png"测试;);   readCustomData(内容);   }   之前   

  首先把桌面上名叫clipboard1.png的图片文件的内容读到字节数组内容中。   

  

  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 形象;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 文件(filePath); long  fileSize =, file.length (); if  (fileSize 祝辞,Integer.MAX_VALUE), {   System.out.println (“file  too 大…“);return 零;   }   FileInputStream  fi =, new  FileInputStream(文件);byte [], buffer =, new 字节((int),文件大小];int  offset =, 0; int  numRead =, 0; while  (offset  & lt; buffer.length   ,,,(numRead =, fi.read(缓冲区,,抵消,,buffer.length 作用;抵消)),在=,0),{   +=offset  numRead;   }if  (offset  !=, buffer.length), {   fi.close (); throw  new  IOException (“Could  not  completely  read  file “+, file.getName ());   }   fi.close (); return 缓冲;   }static  private  void  readCustomData (byte [], imageData), throws  IOException {   ImageReader  ImageReader =, ImageIO.getImageReadersByFormatName (“png") . next ();   imageReader.setInput (ImageIO.createImageInputStream (new  ByteArrayInputStream (imageData)),,真的);   IIOMetadata  metadata =, imageReader.getImageMetadata (0);   PNGMetadata  pngmeta =, (PNGMetadata),元数据;   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)返回;   NodeList  childNodes =, metanode.getChildNodes();如果(==,,childNodes  null)返回;for  (int 小姐:=,0;,小姐:& lt;, childNodes.getLength();,我+ +),{   Node  Node =, childNodes.item(我);   NamedNodeMap  attribute =, node.getAttributes();如果(==,,attribute  null)继续;int  length =, attribute.getLength();为(,int  j =, 0;, j  & lt;,长度;,j + +) {   Node  each =, attribute.item (j);   String  value =, each.getNodeValue ();   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

使用com.sun.imageio.plugins.png.PNGMetadata读取图片的元数据