溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用com.sun.imageio.plugins.png.PNGMetadata讀取圖片的元數(shù)據(jù)

發(fā)布時(shí)間:2020-08-18 00:29:02 來源:ITPUB博客 閱讀:159 作者:i042416 欄目:編程語言

所謂圖片元數(shù)據(jù),就是除了我們?nèi)庋劭吹降膱D片內(nèi)容外,隱藏在這些內(nèi)容背后的一些技術(shù)數(shù)據(jù)。

本文介紹如何使用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;
新建一個(gè)Java類,這個(gè)類的main方法也是非常直接的:static public void main(String[] arg) throws IOException{byte[] content = getContent("C:\Users\i042416\Desktop\test\clipboard1.png");
readCustomData(content);
}

首先把桌面上名叫clipboard1.png的圖片文件的內(nèi)容讀到字節(jié)數(shù)組content中。

getContent方法的代碼:

使用com.sun.imageio.plugins.png.PNGMetadata讀取圖片的元數(shù)據(jù)

一張png圖片的元數(shù)據(jù),散布在下面這些節(jié)點(diǎn)里:

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讀取圖片的元數(shù)據(jù)

printNode方法的源代碼:

使用com.sun.imageio.plugins.png.PNGMetadata讀取圖片的元數(shù)據(jù)

打印出來的元數(shù)據(jù):

使用com.sun.imageio.plugins.png.PNGMetadata讀取圖片的元數(shù)據(jù)

如果大家想要復(fù)制粘貼,這是全部的源代碼:

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的原創(chuàng)文章,請關(guān)注公眾號"汪子熙":

使用com.sun.imageio.plugins.png.PNGMetadata讀取圖片的元數(shù)據(jù)


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI