使用com.sun.imageio.plugins.png.PNGMetadata讀取圖片的後設資料

i042416發表於2018-10-16

所謂圖片後設資料,就是除了我們肉眼看到的圖片內容外,隱藏在這些內容背後的一些技術資料。

本文介紹如何使用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方法的程式碼:

使用com.sun.imageio.plugins.png.PNGMetadata讀取圖片的後設資料

一張png圖片的後設資料,散佈在下面這些節點裡:

printNode(pngmeta.getStandardChromaNode());
printNode(pngmeta.getStandardCompressionNode());
printNode(pngmeta.getStandardDataNode());
printNode(pngmeta.getStandardDimensionNode());
printNode(pngmeta.getStandardDocumentNode());
printNode(pngmeta.getStandardTextNode());
printNode(pngmeta.getStandardTransparencyNode());

通過printNode列印出來:

使用com.sun.imageio.plugins.png.PNGMetadata讀取圖片的後設資料

printNode方法的原始碼:

使用com.sun.imageio.plugins.png.PNGMetadata讀取圖片的後設資料

列印出來的後設資料:

使用com.sun.imageio.plugins.png.PNGMetadata讀取圖片的後設資料

如果大家想要複製貼上,這是全部的原始碼:

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的原創文章,請關注公眾號"汪子熙":

使用com.sun.imageio.plugins.png.PNGMetadata讀取圖片的後設資料


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2216519/,如需轉載,請註明出處,否則將追究法律責任。

相關文章